blob: 79a73645346e0e2cb59462f29acc980b1bbe31df [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 Masonb888db2b2007-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 Masonb888db2b2007-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 Masonb888db2b2007-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
Josef Bacik66657b32012-08-01 15:36:24 -04001099 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001100 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001101 waitqueue_active(&root->fs_info->async_submit_wait))
1102 wake_up(&root->fs_info->async_submit_wait);
1103
Chris Masond3977122009-01-05 21:25:51 -05001104 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001105 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001106}
Chris Masonc8b97812008-10-29 14:49:59 -04001107
Chris Mason771ed682008-11-06 22:02:51 -05001108static noinline void async_cow_free(struct btrfs_work *work)
1109{
1110 struct async_cow *async_cow;
1111 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001112 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001113 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001114 kfree(async_cow);
1115}
1116
1117static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1118 u64 start, u64 end, int *page_started,
1119 unsigned long *nr_written)
1120{
1121 struct async_cow *async_cow;
1122 struct btrfs_root *root = BTRFS_I(inode)->root;
1123 unsigned long nr_pages;
1124 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001125 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001126
Chris Masona3429ab2009-10-08 12:30:20 -04001127 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1128 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001129 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001130 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001131 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001132 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001133 async_cow->root = root;
1134 async_cow->locked_page = locked_page;
1135 async_cow->start = start;
1136
Wang Shilongf79707b2014-07-17 11:44:09 +08001137 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1138 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001139 cur_end = end;
1140 else
1141 cur_end = min(end, start + 512 * 1024 - 1);
1142
1143 async_cow->end = cur_end;
1144 INIT_LIST_HEAD(&async_cow->extents);
1145
Liu Bo9e0af232014-08-15 23:36:53 +08001146 btrfs_init_work(&async_cow->work,
1147 btrfs_delalloc_helper,
1148 async_cow_start, async_cow_submit,
1149 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001150
Chris Mason771ed682008-11-06 22:02:51 -05001151 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1152 PAGE_CACHE_SHIFT;
1153 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1154
Qu Wenruoafe3d242014-02-28 10:46:07 +08001155 btrfs_queue_work(root->fs_info->delalloc_workers,
1156 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001157
1158 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1159 wait_event(root->fs_info->async_submit_wait,
1160 (atomic_read(&root->fs_info->async_delalloc_pages) <
1161 limit));
1162 }
1163
Chris Masond3977122009-01-05 21:25:51 -05001164 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001165 atomic_read(&root->fs_info->async_delalloc_pages)) {
1166 wait_event(root->fs_info->async_submit_wait,
1167 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1168 0));
1169 }
1170
1171 *nr_written += nr_pages;
1172 start = cur_end + 1;
1173 }
1174 *page_started = 1;
1175 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001176}
1177
Chris Masond3977122009-01-05 21:25:51 -05001178static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001179 u64 bytenr, u64 num_bytes)
1180{
1181 int ret;
1182 struct btrfs_ordered_sum *sums;
1183 LIST_HEAD(list);
1184
Yan Zheng07d400a2009-01-06 11:42:00 -05001185 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001186 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001187 if (ret == 0 && list_empty(&list))
1188 return 0;
1189
1190 while (!list_empty(&list)) {
1191 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1192 list_del(&sums->list);
1193 kfree(sums);
1194 }
1195 return 1;
1196}
1197
Chris Masond352ac62008-09-29 15:18:18 -04001198/*
1199 * when nowcow writeback call back. This checks for snapshots or COW copies
1200 * of the extents that exist in the file, and COWs the file as required.
1201 *
1202 * If no cow copies or snapshots exist, we write directly to the existing
1203 * blocks on disk
1204 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001205static noinline int run_delalloc_nocow(struct inode *inode,
1206 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001207 u64 start, u64 end, int *page_started, int force,
1208 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001209{
Chris Masonbe20aa92007-12-17 20:14:01 -05001210 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001211 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001212 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001213 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001214 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001215 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001216 u64 cow_start;
1217 u64 cur_offset;
1218 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001219 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001220 u64 disk_bytenr;
1221 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001222 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001223 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001224 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001225 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001226 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001227 int nocow;
1228 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001229 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001230 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001231
1232 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001233 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001234 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1235 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001236 EXTENT_DO_ACCOUNTING |
1237 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001238 PAGE_CLEAR_DIRTY |
1239 PAGE_SET_WRITEBACK |
1240 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001241 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001242 }
Li Zefan82d59022011-04-20 10:33:24 +08001243
Liu Bo83eea1f2012-07-10 05:28:39 -06001244 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001245
1246 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001247 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001248 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001249 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001250
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001251 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001252 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1253 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001254 EXTENT_DO_ACCOUNTING |
1255 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001256 PAGE_CLEAR_DIRTY |
1257 PAGE_SET_WRITEBACK |
1258 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001259 btrfs_free_path(path);
1260 return PTR_ERR(trans);
1261 }
1262
Josef Bacik74b21072011-04-13 12:02:53 -04001263 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001264
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 cow_start = (u64)-1;
1266 cur_offset = start;
1267 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001268 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001270 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001271 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1273 leaf = path->nodes[0];
1274 btrfs_item_key_to_cpu(leaf, &found_key,
1275 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001276 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001277 found_key.type == BTRFS_EXTENT_DATA_KEY)
1278 path->slots[0]--;
1279 }
1280 check_prev = 0;
1281next_slot:
1282 leaf = path->nodes[0];
1283 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1284 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001285 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001286 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 if (ret > 0)
1288 break;
1289 leaf = path->nodes[0];
1290 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001291
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 nocow = 0;
1293 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001294 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001296
Li Zefan33345d012011-04-20 10:31:50 +08001297 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001298 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1299 found_key.offset > end)
1300 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001301
Yan Zheng80ff3852008-10-30 14:20:02 -04001302 if (found_key.offset > cur_offset) {
1303 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001304 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 goto out_check;
1306 }
Chris Masonc31f8832008-01-08 15:46:31 -05001307
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 fi = btrfs_item_ptr(leaf, path->slots[0],
1309 struct btrfs_file_extent_item);
1310 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001311
Josef Bacikcc95bef2013-04-04 14:31:27 -04001312 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001313 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1314 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001315 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001316 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 extent_end = found_key.offset +
1318 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001319 disk_num_bytes =
1320 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 if (extent_end <= start) {
1322 path->slots[0]++;
1323 goto next_slot;
1324 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001325 if (disk_bytenr == 0)
1326 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001327 if (btrfs_file_extent_compression(leaf, fi) ||
1328 btrfs_file_extent_encryption(leaf, fi) ||
1329 btrfs_file_extent_other_encoding(leaf, fi))
1330 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001331 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1332 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001333 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001334 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001335 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001336 found_key.offset -
1337 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001338 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001339 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001340 disk_bytenr += cur_offset - found_key.offset;
1341 num_bytes = min(end + 1, extent_end) - cur_offset;
1342 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001343 * if there are pending snapshots for this root,
1344 * we fall into common COW way.
1345 */
1346 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001347 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001348 if (!err)
1349 goto out_check;
1350 }
1351 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001352 * force cow if csum exists in the range.
1353 * this ensure that csum for a given extent are
1354 * either valid or do not exist.
1355 */
1356 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1357 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 nocow = 1;
1359 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1360 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001361 btrfs_file_extent_inline_len(leaf,
1362 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 extent_end = ALIGN(extent_end, root->sectorsize);
1364 } else {
1365 BUG_ON(1);
1366 }
1367out_check:
1368 if (extent_end <= start) {
1369 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001370 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001371 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001372 goto next_slot;
1373 }
1374 if (!nocow) {
1375 if (cow_start == (u64)-1)
1376 cow_start = cur_offset;
1377 cur_offset = extent_end;
1378 if (cur_offset > end)
1379 break;
1380 path->slots[0]++;
1381 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001382 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001383
David Sterbab3b4aa72011-04-21 01:20:15 +02001384 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001385 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001386 ret = cow_file_range(inode, locked_page,
1387 cow_start, found_key.offset - 1,
1388 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001389 if (ret) {
1390 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001391 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001392 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001393 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001394 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001395 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001396
Yan Zhengd899e052008-10-30 14:25:28 -04001397 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1398 struct extent_map *em;
1399 struct extent_map_tree *em_tree;
1400 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001401 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001402 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001403 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001404 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001405 em->len = num_bytes;
1406 em->block_len = num_bytes;
1407 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001408 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001409 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001410 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001411 em->mod_start = em->start;
1412 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001413 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001414 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001415 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001416 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001417 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001418 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001419 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001420 if (ret != -EEXIST) {
1421 free_extent_map(em);
1422 break;
1423 }
1424 btrfs_drop_extent_cache(inode, em->start,
1425 em->start + em->len - 1, 0);
1426 }
1427 type = BTRFS_ORDERED_PREALLOC;
1428 } else {
1429 type = BTRFS_ORDERED_NOCOW;
1430 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001431
1432 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001433 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001434 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001435
Yan, Zhengefa56462010-05-16 10:49:59 -04001436 if (root->root_key.objectid ==
1437 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1438 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1439 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001440 if (ret) {
1441 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001442 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001443 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001444 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001445 }
1446
Josef Bacikc2790a22013-07-29 11:20:47 -04001447 extent_clear_unlock_delalloc(inode, cur_offset,
1448 cur_offset + num_bytes - 1,
1449 locked_page, EXTENT_LOCKED |
1450 EXTENT_DELALLOC, PAGE_UNLOCK |
1451 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001452 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001453 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001454 cur_offset = extent_end;
1455 if (cur_offset > end)
1456 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001457 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001458 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001459
Josef Bacik17ca04a2012-05-31 15:58:55 -04001460 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001461 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001462 cur_offset = end;
1463 }
1464
Yan Zheng80ff3852008-10-30 14:20:02 -04001465 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001466 ret = cow_file_range(inode, locked_page, cow_start, end,
1467 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001468 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001469 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001470 }
1471
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001472error:
Miao Xiea698d0752012-09-20 01:51:59 -06001473 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001474 if (!ret)
1475 ret = err;
1476
Josef Bacik17ca04a2012-05-31 15:58:55 -04001477 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001478 extent_clear_unlock_delalloc(inode, cur_offset, end,
1479 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001480 EXTENT_DELALLOC | EXTENT_DEFRAG |
1481 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1482 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001483 PAGE_SET_WRITEBACK |
1484 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001485 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001486 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001487}
1488
Wang Shilong47059d92014-07-03 18:22:07 +08001489static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1490{
1491
1492 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1493 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1494 return 0;
1495
1496 /*
1497 * @defrag_bytes is a hint value, no spinlock held here,
1498 * if is not zero, it means the file is defragging.
1499 * Force cow if given extent needs to be defragged.
1500 */
1501 if (BTRFS_I(inode)->defrag_bytes &&
1502 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1503 EXTENT_DEFRAG, 0, NULL))
1504 return 1;
1505
1506 return 0;
1507}
1508
Chris Masond352ac62008-09-29 15:18:18 -04001509/*
1510 * extent_io.c call back to do delayed allocation processing
1511 */
Chris Masonc8b97812008-10-29 14:49:59 -04001512static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001513 u64 start, u64 end, int *page_started,
1514 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001515{
Chris Masonbe20aa92007-12-17 20:14:01 -05001516 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001517 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001518
Wang Shilong47059d92014-07-03 18:22:07 +08001519 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001520 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001521 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001522 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001523 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001524 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001525 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001526 ret = cow_file_range(inode, locked_page, start, end,
1527 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001528 } else {
1529 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1530 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001531 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001532 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001533 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001534 return ret;
1535}
1536
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001537static void btrfs_split_extent_hook(struct inode *inode,
1538 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001539{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001540 u64 size;
1541
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001542 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001543 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001544 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001545
Josef Bacikdcab6a32015-02-11 15:08:59 -05001546 size = orig->end - orig->start + 1;
1547 if (size > BTRFS_MAX_EXTENT_SIZE) {
1548 u64 num_extents;
1549 u64 new_size;
1550
1551 /*
Josef Bacikba117212015-03-13 15:01:24 -04001552 * See the explanation in btrfs_merge_extent_hook, the same
1553 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001554 */
1555 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001556 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001557 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001558 new_size = split - orig->start;
1559 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1560 BTRFS_MAX_EXTENT_SIZE);
1561 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1562 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001563 return;
1564 }
1565
Josef Bacik9e0baf62011-07-15 15:16:44 +00001566 spin_lock(&BTRFS_I(inode)->lock);
1567 BTRFS_I(inode)->outstanding_extents++;
1568 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001569}
1570
1571/*
1572 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1573 * extents so we can keep track of new extents that are just merged onto old
1574 * extents, such as when we are doing sequential writes, so we can properly
1575 * account for the metadata space we'll need.
1576 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001577static void btrfs_merge_extent_hook(struct inode *inode,
1578 struct extent_state *new,
1579 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001580{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001581 u64 new_size, old_size;
1582 u64 num_extents;
1583
Josef Bacik9ed74f22009-09-11 16:12:44 -04001584 /* not delalloc, ignore it */
1585 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001586 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001587
Josef Bacik8461a3d2015-03-13 15:12:08 -04001588 if (new->start > other->start)
1589 new_size = new->end - other->start + 1;
1590 else
1591 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001592
1593 /* we're not bigger than the max, unreserve the space and go */
1594 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1595 spin_lock(&BTRFS_I(inode)->lock);
1596 BTRFS_I(inode)->outstanding_extents--;
1597 spin_unlock(&BTRFS_I(inode)->lock);
1598 return;
1599 }
1600
1601 /*
Josef Bacikba117212015-03-13 15:01:24 -04001602 * We have to add up either side to figure out how many extents were
1603 * accounted for before we merged into one big extent. If the number of
1604 * extents we accounted for is <= the amount we need for the new range
1605 * then we can return, otherwise drop. Think of it like this
1606 *
1607 * [ 4k][MAX_SIZE]
1608 *
1609 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1610 * need 2 outstanding extents, on one side we have 1 and the other side
1611 * we have 1 so they are == and we can return. But in this case
1612 *
1613 * [MAX_SIZE+4k][MAX_SIZE+4k]
1614 *
1615 * Each range on their own accounts for 2 extents, but merged together
1616 * they are only 3 extents worth of accounting, so we need to drop in
1617 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001618 */
Josef Bacikba117212015-03-13 15:01:24 -04001619 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001620 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1621 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001622 old_size = new->end - new->start + 1;
1623 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1624 BTRFS_MAX_EXTENT_SIZE);
1625
Josef Bacikdcab6a32015-02-11 15:08:59 -05001626 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001627 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001628 return;
1629
Josef Bacik9e0baf62011-07-15 15:16:44 +00001630 spin_lock(&BTRFS_I(inode)->lock);
1631 BTRFS_I(inode)->outstanding_extents--;
1632 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001633}
1634
Miao Xieeb73c1b2013-05-15 07:48:22 +00001635static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1636 struct inode *inode)
1637{
1638 spin_lock(&root->delalloc_lock);
1639 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1640 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1641 &root->delalloc_inodes);
1642 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1643 &BTRFS_I(inode)->runtime_flags);
1644 root->nr_delalloc_inodes++;
1645 if (root->nr_delalloc_inodes == 1) {
1646 spin_lock(&root->fs_info->delalloc_root_lock);
1647 BUG_ON(!list_empty(&root->delalloc_root));
1648 list_add_tail(&root->delalloc_root,
1649 &root->fs_info->delalloc_roots);
1650 spin_unlock(&root->fs_info->delalloc_root_lock);
1651 }
1652 }
1653 spin_unlock(&root->delalloc_lock);
1654}
1655
1656static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1657 struct inode *inode)
1658{
1659 spin_lock(&root->delalloc_lock);
1660 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1661 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1662 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1663 &BTRFS_I(inode)->runtime_flags);
1664 root->nr_delalloc_inodes--;
1665 if (!root->nr_delalloc_inodes) {
1666 spin_lock(&root->fs_info->delalloc_root_lock);
1667 BUG_ON(list_empty(&root->delalloc_root));
1668 list_del_init(&root->delalloc_root);
1669 spin_unlock(&root->fs_info->delalloc_root_lock);
1670 }
1671 }
1672 spin_unlock(&root->delalloc_lock);
1673}
1674
Chris Masond352ac62008-09-29 15:18:18 -04001675/*
1676 * extent_io.c set_bit_hook, used to track delayed allocation
1677 * bytes in this file, and to maintain the list of inodes that
1678 * have pending delalloc work to be done.
1679 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001680static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001681 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001682{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001683
Wang Shilong47059d92014-07-03 18:22:07 +08001684 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1685 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001686 /*
1687 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001688 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001689 * bit, which is only set or cleared with irqs on
1690 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001691 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001692 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001693 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001694 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001695
Josef Bacik9e0baf62011-07-15 15:16:44 +00001696 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001697 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001698 } else {
1699 spin_lock(&BTRFS_I(inode)->lock);
1700 BTRFS_I(inode)->outstanding_extents++;
1701 spin_unlock(&BTRFS_I(inode)->lock);
1702 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001703
Josef Bacik6a3891c2015-03-16 17:38:52 -04001704 /* For sanity tests */
1705 if (btrfs_test_is_dummy_root(root))
1706 return;
1707
Miao Xie963d6782013-01-29 10:10:51 +00001708 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1709 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001710 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001711 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001712 if (*bits & EXTENT_DEFRAG)
1713 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001714 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001715 &BTRFS_I(inode)->runtime_flags))
1716 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001717 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001718 }
Chris Mason291d6732008-01-29 15:55:23 -05001719}
1720
Chris Masond352ac62008-09-29 15:18:18 -04001721/*
1722 * extent_io.c clear_bit_hook, see set_bit_hook for why
1723 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001724static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001725 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001726 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001727{
Wang Shilong47059d92014-07-03 18:22:07 +08001728 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001729 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1730 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001731
1732 spin_lock(&BTRFS_I(inode)->lock);
1733 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1734 BTRFS_I(inode)->defrag_bytes -= len;
1735 spin_unlock(&BTRFS_I(inode)->lock);
1736
Chris Mason75eff682008-12-15 15:54:40 -05001737 /*
1738 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001739 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001740 * bit, which is only set or cleared with irqs on
1741 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001742 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001743 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001744 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001745
Josef Bacik9e0baf62011-07-15 15:16:44 +00001746 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001747 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001748 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1749 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001750 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001751 spin_unlock(&BTRFS_I(inode)->lock);
1752 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001753
Josef Bacikb6d08f02013-09-27 14:57:43 -04001754 /*
1755 * We don't reserve metadata space for space cache inodes so we
1756 * don't need to call dellalloc_release_metadata if there is an
1757 * error.
1758 */
1759 if (*bits & EXTENT_DO_ACCOUNTING &&
1760 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001761 btrfs_delalloc_release_metadata(inode, len);
1762
Josef Bacik6a3891c2015-03-16 17:38:52 -04001763 /* For sanity tests. */
1764 if (btrfs_test_is_dummy_root(root))
1765 return;
1766
Josef Bacik0cb59c92010-07-02 12:14:14 -04001767 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001768 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001769 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001770
Miao Xie963d6782013-01-29 10:10:51 +00001771 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1772 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001773 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001774 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001775 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001776 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001777 &BTRFS_I(inode)->runtime_flags))
1778 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001779 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001780 }
Chris Mason291d6732008-01-29 15:55:23 -05001781}
1782
Chris Masond352ac62008-09-29 15:18:18 -04001783/*
1784 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1785 * we don't create bios that span stripes or chunks
1786 */
David Woodhouse64a16702009-07-15 23:29:37 +01001787int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001788 size_t size, struct bio *bio,
1789 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001790{
1791 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001792 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001793 u64 length = 0;
1794 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001795 int ret;
1796
Chris Mason771ed682008-11-06 22:02:51 -05001797 if (bio_flags & EXTENT_BIO_COMPRESSED)
1798 return 0;
1799
Kent Overstreet4f024f32013-10-11 15:44:27 -07001800 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001801 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001802 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001803 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001804 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001805 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001806 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001807 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001808 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001809}
1810
Chris Masond352ac62008-09-29 15:18:18 -04001811/*
1812 * in order to insert checksums into the metadata in large chunks,
1813 * we wait until bio submission time. All the pages in the bio are
1814 * checksummed and sums are attached onto the ordered extent record.
1815 *
1816 * At IO completion time the cums attached on the ordered extent record
1817 * are inserted into the btree
1818 */
Chris Masond3977122009-01-05 21:25:51 -05001819static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1820 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001821 unsigned long bio_flags,
1822 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001823{
Chris Mason065631f2008-02-20 12:07:25 -05001824 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001825 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001826
Chris Masond20f7042008-12-08 16:58:54 -05001827 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001828 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001829 return 0;
1830}
Chris Masone0156402008-04-16 11:15:20 -04001831
Chris Mason4a69a412008-11-06 22:03:00 -05001832/*
1833 * in order to insert checksums into the metadata in large chunks,
1834 * we wait until bio submission time. All the pages in the bio are
1835 * checksummed and sums are attached onto the ordered extent record.
1836 *
1837 * At IO completion time the cums attached on the ordered extent record
1838 * are inserted into the btree
1839 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001840static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001841 int mirror_num, unsigned long bio_flags,
1842 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001843{
1844 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001845 int ret;
1846
1847 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1848 if (ret)
1849 bio_endio(bio, ret);
1850 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001851}
1852
Chris Masond352ac62008-09-29 15:18:18 -04001853/*
Chris Masoncad321a2008-12-17 14:51:42 -05001854 * extent_io.c submission hook. This does the right thing for csum calculation
1855 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001856 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001857static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001858 int mirror_num, unsigned long bio_flags,
1859 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001860{
1861 struct btrfs_root *root = BTRFS_I(inode)->root;
1862 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001863 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001864 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001865 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001866
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001867 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001868
Liu Bo83eea1f2012-07-10 05:28:39 -06001869 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001870 metadata = 2;
1871
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001872 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001873 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1874 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001875 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001876
Chris Masond20f7042008-12-08 16:58:54 -05001877 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001878 ret = btrfs_submit_compressed_read(inode, bio,
1879 mirror_num,
1880 bio_flags);
1881 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001882 } else if (!skip_sum) {
1883 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1884 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001885 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001886 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001887 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001888 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001889 /* csum items have already been cloned */
1890 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1891 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001892 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001893 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001894 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001895 bio_flags, bio_offset,
1896 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001897 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001898 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001899 } else if (!skip_sum) {
1900 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1901 if (ret)
1902 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001903 }
1904
Chris Mason0b86a832008-03-24 15:01:56 -04001905mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001906 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1907
1908out:
1909 if (ret < 0)
1910 bio_endio(bio, ret);
1911 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001912}
Chris Mason6885f302008-02-20 16:11:05 -05001913
Chris Masond352ac62008-09-29 15:18:18 -04001914/*
1915 * given a list of ordered sums record them in the inode. This happens
1916 * at IO completion time based on sums calculated at bio submission time.
1917 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001918static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001919 struct inode *inode, u64 file_offset,
1920 struct list_head *list)
1921{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001922 struct btrfs_ordered_sum *sum;
1923
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001924 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001925 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001926 btrfs_csum_file_blocks(trans,
1927 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001928 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001929 }
1930 return 0;
1931}
1932
Josef Bacik2ac55d42010-02-03 19:33:23 +00001933int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1934 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001935{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001936 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001937 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001938 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001939}
1940
Chris Masond352ac62008-09-29 15:18:18 -04001941/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001942struct btrfs_writepage_fixup {
1943 struct page *page;
1944 struct btrfs_work work;
1945};
1946
Christoph Hellwigb2950862008-12-02 09:54:17 -05001947static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001948{
1949 struct btrfs_writepage_fixup *fixup;
1950 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001951 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001952 struct page *page;
1953 struct inode *inode;
1954 u64 page_start;
1955 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001956 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001957
1958 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1959 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001960again:
Chris Mason247e7432008-07-17 12:53:51 -04001961 lock_page(page);
1962 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1963 ClearPageChecked(page);
1964 goto out_page;
1965 }
1966
1967 inode = page->mapping->host;
1968 page_start = page_offset(page);
1969 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1970
Josef Bacik2ac55d42010-02-03 19:33:23 +00001971 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001972 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001973
1974 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001975 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001976 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001977
1978 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1979 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001980 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1981 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001982 unlock_page(page);
1983 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001984 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001985 goto again;
1986 }
Chris Mason247e7432008-07-17 12:53:51 -04001987
Jeff Mahoney87826df2012-02-15 16:23:57 +01001988 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1989 if (ret) {
1990 mapping_set_error(page->mapping, ret);
1991 end_extent_writepage(page, ret, page_start, page_end);
1992 ClearPageChecked(page);
1993 goto out;
1994 }
1995
Josef Bacik2ac55d42010-02-03 19:33:23 +00001996 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001997 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001998 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001999out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002000 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2001 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002002out_page:
2003 unlock_page(page);
2004 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002005 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002006}
2007
2008/*
2009 * There are a few paths in the higher layers of the kernel that directly
2010 * set the page dirty bit without asking the filesystem if it is a
2011 * good idea. This causes problems because we want to make sure COW
2012 * properly happens and the data=ordered rules are followed.
2013 *
Chris Masonc8b97812008-10-29 14:49:59 -04002014 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002015 * hasn't been properly setup for IO. We kick off an async process
2016 * to fix it up. The async helper will wait for ordered extents, set
2017 * the delalloc bit and make it safe to write the page.
2018 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002019static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002020{
2021 struct inode *inode = page->mapping->host;
2022 struct btrfs_writepage_fixup *fixup;
2023 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002024
Chris Mason8b62b722009-09-02 16:53:46 -04002025 /* this page is properly in the ordered list */
2026 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002027 return 0;
2028
2029 if (PageChecked(page))
2030 return -EAGAIN;
2031
2032 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2033 if (!fixup)
2034 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002035
Chris Mason247e7432008-07-17 12:53:51 -04002036 SetPageChecked(page);
2037 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002038 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2039 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002040 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002041 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002042 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002043}
2044
Yan Zhengd899e052008-10-30 14:25:28 -04002045static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2046 struct inode *inode, u64 file_pos,
2047 u64 disk_bytenr, u64 disk_num_bytes,
2048 u64 num_bytes, u64 ram_bytes,
2049 u8 compression, u8 encryption,
2050 u16 other_encoding, int extent_type)
2051{
2052 struct btrfs_root *root = BTRFS_I(inode)->root;
2053 struct btrfs_file_extent_item *fi;
2054 struct btrfs_path *path;
2055 struct extent_buffer *leaf;
2056 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002057 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002058 int ret;
2059
2060 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002061 if (!path)
2062 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002063
Chris Masona1ed8352009-09-11 12:27:37 -04002064 /*
2065 * we may be replacing one extent in the tree with another.
2066 * The new extent is pinned in the extent map, and we don't want
2067 * to drop it from the cache until it is completely in the btree.
2068 *
2069 * So, tell btrfs_drop_extents to leave this extent in the cache.
2070 * the caller is expected to unpin it and allow it to be merged
2071 * with the others.
2072 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002073 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2074 file_pos + num_bytes, NULL, 0,
2075 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002076 if (ret)
2077 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002078
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002079 if (!extent_inserted) {
2080 ins.objectid = btrfs_ino(inode);
2081 ins.offset = file_pos;
2082 ins.type = BTRFS_EXTENT_DATA_KEY;
2083
2084 path->leave_spinning = 1;
2085 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2086 sizeof(*fi));
2087 if (ret)
2088 goto out;
2089 }
Yan Zhengd899e052008-10-30 14:25:28 -04002090 leaf = path->nodes[0];
2091 fi = btrfs_item_ptr(leaf, path->slots[0],
2092 struct btrfs_file_extent_item);
2093 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2094 btrfs_set_file_extent_type(leaf, fi, extent_type);
2095 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2096 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2097 btrfs_set_file_extent_offset(leaf, fi, 0);
2098 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2099 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2100 btrfs_set_file_extent_compression(leaf, fi, compression);
2101 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2102 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002103
Yan Zhengd899e052008-10-30 14:25:28 -04002104 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002105 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002106
2107 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002108
2109 ins.objectid = disk_bytenr;
2110 ins.offset = disk_num_bytes;
2111 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002112 ret = btrfs_alloc_reserved_file_extent(trans, root,
2113 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002114 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002115out:
Yan Zhengd899e052008-10-30 14:25:28 -04002116 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002117
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002118 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002119}
2120
Liu Bo38c227d2013-01-29 03:18:40 +00002121/* snapshot-aware defrag */
2122struct sa_defrag_extent_backref {
2123 struct rb_node node;
2124 struct old_sa_defrag_extent *old;
2125 u64 root_id;
2126 u64 inum;
2127 u64 file_pos;
2128 u64 extent_offset;
2129 u64 num_bytes;
2130 u64 generation;
2131};
2132
2133struct old_sa_defrag_extent {
2134 struct list_head list;
2135 struct new_sa_defrag_extent *new;
2136
2137 u64 extent_offset;
2138 u64 bytenr;
2139 u64 offset;
2140 u64 len;
2141 int count;
2142};
2143
2144struct new_sa_defrag_extent {
2145 struct rb_root root;
2146 struct list_head head;
2147 struct btrfs_path *path;
2148 struct inode *inode;
2149 u64 file_pos;
2150 u64 len;
2151 u64 bytenr;
2152 u64 disk_len;
2153 u8 compress_type;
2154};
2155
2156static int backref_comp(struct sa_defrag_extent_backref *b1,
2157 struct sa_defrag_extent_backref *b2)
2158{
2159 if (b1->root_id < b2->root_id)
2160 return -1;
2161 else if (b1->root_id > b2->root_id)
2162 return 1;
2163
2164 if (b1->inum < b2->inum)
2165 return -1;
2166 else if (b1->inum > b2->inum)
2167 return 1;
2168
2169 if (b1->file_pos < b2->file_pos)
2170 return -1;
2171 else if (b1->file_pos > b2->file_pos)
2172 return 1;
2173
2174 /*
2175 * [------------------------------] ===> (a range of space)
2176 * |<--->| |<---->| =============> (fs/file tree A)
2177 * |<---------------------------->| ===> (fs/file tree B)
2178 *
2179 * A range of space can refer to two file extents in one tree while
2180 * refer to only one file extent in another tree.
2181 *
2182 * So we may process a disk offset more than one time(two extents in A)
2183 * and locate at the same extent(one extent in B), then insert two same
2184 * backrefs(both refer to the extent in B).
2185 */
2186 return 0;
2187}
2188
2189static void backref_insert(struct rb_root *root,
2190 struct sa_defrag_extent_backref *backref)
2191{
2192 struct rb_node **p = &root->rb_node;
2193 struct rb_node *parent = NULL;
2194 struct sa_defrag_extent_backref *entry;
2195 int ret;
2196
2197 while (*p) {
2198 parent = *p;
2199 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2200
2201 ret = backref_comp(backref, entry);
2202 if (ret < 0)
2203 p = &(*p)->rb_left;
2204 else
2205 p = &(*p)->rb_right;
2206 }
2207
2208 rb_link_node(&backref->node, parent, p);
2209 rb_insert_color(&backref->node, root);
2210}
2211
2212/*
2213 * Note the backref might has changed, and in this case we just return 0.
2214 */
2215static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2216 void *ctx)
2217{
2218 struct btrfs_file_extent_item *extent;
2219 struct btrfs_fs_info *fs_info;
2220 struct old_sa_defrag_extent *old = ctx;
2221 struct new_sa_defrag_extent *new = old->new;
2222 struct btrfs_path *path = new->path;
2223 struct btrfs_key key;
2224 struct btrfs_root *root;
2225 struct sa_defrag_extent_backref *backref;
2226 struct extent_buffer *leaf;
2227 struct inode *inode = new->inode;
2228 int slot;
2229 int ret;
2230 u64 extent_offset;
2231 u64 num_bytes;
2232
2233 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2234 inum == btrfs_ino(inode))
2235 return 0;
2236
2237 key.objectid = root_id;
2238 key.type = BTRFS_ROOT_ITEM_KEY;
2239 key.offset = (u64)-1;
2240
2241 fs_info = BTRFS_I(inode)->root->fs_info;
2242 root = btrfs_read_fs_root_no_name(fs_info, &key);
2243 if (IS_ERR(root)) {
2244 if (PTR_ERR(root) == -ENOENT)
2245 return 0;
2246 WARN_ON(1);
2247 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2248 inum, offset, root_id);
2249 return PTR_ERR(root);
2250 }
2251
2252 key.objectid = inum;
2253 key.type = BTRFS_EXTENT_DATA_KEY;
2254 if (offset > (u64)-1 << 32)
2255 key.offset = 0;
2256 else
2257 key.offset = offset;
2258
2259 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302260 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002261 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002262 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002263
2264 while (1) {
2265 cond_resched();
2266
2267 leaf = path->nodes[0];
2268 slot = path->slots[0];
2269
2270 if (slot >= btrfs_header_nritems(leaf)) {
2271 ret = btrfs_next_leaf(root, path);
2272 if (ret < 0) {
2273 goto out;
2274 } else if (ret > 0) {
2275 ret = 0;
2276 goto out;
2277 }
2278 continue;
2279 }
2280
2281 path->slots[0]++;
2282
2283 btrfs_item_key_to_cpu(leaf, &key, slot);
2284
2285 if (key.objectid > inum)
2286 goto out;
2287
2288 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2289 continue;
2290
2291 extent = btrfs_item_ptr(leaf, slot,
2292 struct btrfs_file_extent_item);
2293
2294 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2295 continue;
2296
Liu Boe68afa42013-07-01 22:13:26 +08002297 /*
2298 * 'offset' refers to the exact key.offset,
2299 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2300 * (key.offset - extent_offset).
2301 */
2302 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002303 continue;
2304
Liu Boe68afa42013-07-01 22:13:26 +08002305 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002306 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002307
Liu Bo38c227d2013-01-29 03:18:40 +00002308 if (extent_offset >= old->extent_offset + old->offset +
2309 old->len || extent_offset + num_bytes <=
2310 old->extent_offset + old->offset)
2311 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002312 break;
2313 }
2314
2315 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2316 if (!backref) {
2317 ret = -ENOENT;
2318 goto out;
2319 }
2320
2321 backref->root_id = root_id;
2322 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002323 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002324 backref->num_bytes = num_bytes;
2325 backref->extent_offset = extent_offset;
2326 backref->generation = btrfs_file_extent_generation(leaf, extent);
2327 backref->old = old;
2328 backref_insert(&new->root, backref);
2329 old->count++;
2330out:
2331 btrfs_release_path(path);
2332 WARN_ON(ret);
2333 return ret;
2334}
2335
2336static noinline bool record_extent_backrefs(struct btrfs_path *path,
2337 struct new_sa_defrag_extent *new)
2338{
2339 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2340 struct old_sa_defrag_extent *old, *tmp;
2341 int ret;
2342
2343 new->path = path;
2344
2345 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002346 ret = iterate_inodes_from_logical(old->bytenr +
2347 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002348 path, record_one_backref,
2349 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002350 if (ret < 0 && ret != -ENOENT)
2351 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002352
2353 /* no backref to be processed for this extent */
2354 if (!old->count) {
2355 list_del(&old->list);
2356 kfree(old);
2357 }
2358 }
2359
2360 if (list_empty(&new->head))
2361 return false;
2362
2363 return true;
2364}
2365
2366static int relink_is_mergable(struct extent_buffer *leaf,
2367 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002368 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002369{
Liu Bo116e0022013-08-02 16:30:40 +08002370 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002371 return 0;
2372
2373 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2374 return 0;
2375
Liu Bo116e0022013-08-02 16:30:40 +08002376 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2377 return 0;
2378
2379 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002380 btrfs_file_extent_other_encoding(leaf, fi))
2381 return 0;
2382
2383 return 1;
2384}
2385
2386/*
2387 * Note the backref might has changed, and in this case we just return 0.
2388 */
2389static noinline int relink_extent_backref(struct btrfs_path *path,
2390 struct sa_defrag_extent_backref *prev,
2391 struct sa_defrag_extent_backref *backref)
2392{
2393 struct btrfs_file_extent_item *extent;
2394 struct btrfs_file_extent_item *item;
2395 struct btrfs_ordered_extent *ordered;
2396 struct btrfs_trans_handle *trans;
2397 struct btrfs_fs_info *fs_info;
2398 struct btrfs_root *root;
2399 struct btrfs_key key;
2400 struct extent_buffer *leaf;
2401 struct old_sa_defrag_extent *old = backref->old;
2402 struct new_sa_defrag_extent *new = old->new;
2403 struct inode *src_inode = new->inode;
2404 struct inode *inode;
2405 struct extent_state *cached = NULL;
2406 int ret = 0;
2407 u64 start;
2408 u64 len;
2409 u64 lock_start;
2410 u64 lock_end;
2411 bool merge = false;
2412 int index;
2413
2414 if (prev && prev->root_id == backref->root_id &&
2415 prev->inum == backref->inum &&
2416 prev->file_pos + prev->num_bytes == backref->file_pos)
2417 merge = true;
2418
2419 /* step 1: get root */
2420 key.objectid = backref->root_id;
2421 key.type = BTRFS_ROOT_ITEM_KEY;
2422 key.offset = (u64)-1;
2423
2424 fs_info = BTRFS_I(src_inode)->root->fs_info;
2425 index = srcu_read_lock(&fs_info->subvol_srcu);
2426
2427 root = btrfs_read_fs_root_no_name(fs_info, &key);
2428 if (IS_ERR(root)) {
2429 srcu_read_unlock(&fs_info->subvol_srcu, index);
2430 if (PTR_ERR(root) == -ENOENT)
2431 return 0;
2432 return PTR_ERR(root);
2433 }
Liu Bo38c227d2013-01-29 03:18:40 +00002434
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002435 if (btrfs_root_readonly(root)) {
2436 srcu_read_unlock(&fs_info->subvol_srcu, index);
2437 return 0;
2438 }
2439
Liu Bo38c227d2013-01-29 03:18:40 +00002440 /* step 2: get inode */
2441 key.objectid = backref->inum;
2442 key.type = BTRFS_INODE_ITEM_KEY;
2443 key.offset = 0;
2444
2445 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2446 if (IS_ERR(inode)) {
2447 srcu_read_unlock(&fs_info->subvol_srcu, index);
2448 return 0;
2449 }
2450
2451 srcu_read_unlock(&fs_info->subvol_srcu, index);
2452
2453 /* step 3: relink backref */
2454 lock_start = backref->file_pos;
2455 lock_end = backref->file_pos + backref->num_bytes - 1;
2456 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2457 0, &cached);
2458
2459 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2460 if (ordered) {
2461 btrfs_put_ordered_extent(ordered);
2462 goto out_unlock;
2463 }
2464
2465 trans = btrfs_join_transaction(root);
2466 if (IS_ERR(trans)) {
2467 ret = PTR_ERR(trans);
2468 goto out_unlock;
2469 }
2470
2471 key.objectid = backref->inum;
2472 key.type = BTRFS_EXTENT_DATA_KEY;
2473 key.offset = backref->file_pos;
2474
2475 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2476 if (ret < 0) {
2477 goto out_free_path;
2478 } else if (ret > 0) {
2479 ret = 0;
2480 goto out_free_path;
2481 }
2482
2483 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2484 struct btrfs_file_extent_item);
2485
2486 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2487 backref->generation)
2488 goto out_free_path;
2489
2490 btrfs_release_path(path);
2491
2492 start = backref->file_pos;
2493 if (backref->extent_offset < old->extent_offset + old->offset)
2494 start += old->extent_offset + old->offset -
2495 backref->extent_offset;
2496
2497 len = min(backref->extent_offset + backref->num_bytes,
2498 old->extent_offset + old->offset + old->len);
2499 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2500
2501 ret = btrfs_drop_extents(trans, root, inode, start,
2502 start + len, 1);
2503 if (ret)
2504 goto out_free_path;
2505again:
2506 key.objectid = btrfs_ino(inode);
2507 key.type = BTRFS_EXTENT_DATA_KEY;
2508 key.offset = start;
2509
Liu Boa09a0a72013-03-11 09:20:58 +00002510 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002511 if (merge) {
2512 struct btrfs_file_extent_item *fi;
2513 u64 extent_len;
2514 struct btrfs_key found_key;
2515
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002516 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002517 if (ret < 0)
2518 goto out_free_path;
2519
2520 path->slots[0]--;
2521 leaf = path->nodes[0];
2522 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2523
2524 fi = btrfs_item_ptr(leaf, path->slots[0],
2525 struct btrfs_file_extent_item);
2526 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2527
Liu Bo116e0022013-08-02 16:30:40 +08002528 if (extent_len + found_key.offset == start &&
2529 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002530 btrfs_set_file_extent_num_bytes(leaf, fi,
2531 extent_len + len);
2532 btrfs_mark_buffer_dirty(leaf);
2533 inode_add_bytes(inode, len);
2534
2535 ret = 1;
2536 goto out_free_path;
2537 } else {
2538 merge = false;
2539 btrfs_release_path(path);
2540 goto again;
2541 }
2542 }
2543
2544 ret = btrfs_insert_empty_item(trans, root, path, &key,
2545 sizeof(*extent));
2546 if (ret) {
2547 btrfs_abort_transaction(trans, root, ret);
2548 goto out_free_path;
2549 }
2550
2551 leaf = path->nodes[0];
2552 item = btrfs_item_ptr(leaf, path->slots[0],
2553 struct btrfs_file_extent_item);
2554 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2555 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2556 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2557 btrfs_set_file_extent_num_bytes(leaf, item, len);
2558 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2559 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2560 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2561 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2562 btrfs_set_file_extent_encryption(leaf, item, 0);
2563 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2564
2565 btrfs_mark_buffer_dirty(leaf);
2566 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002567 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002568
2569 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2570 new->disk_len, 0,
2571 backref->root_id, backref->inum,
2572 new->file_pos, 0); /* start - extent_offset */
2573 if (ret) {
2574 btrfs_abort_transaction(trans, root, ret);
2575 goto out_free_path;
2576 }
2577
2578 ret = 1;
2579out_free_path:
2580 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002581 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002582 btrfs_end_transaction(trans, root);
2583out_unlock:
2584 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2585 &cached, GFP_NOFS);
2586 iput(inode);
2587 return ret;
2588}
2589
Liu Bo6f519562013-10-29 10:45:05 +08002590static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2591{
2592 struct old_sa_defrag_extent *old, *tmp;
2593
2594 if (!new)
2595 return;
2596
2597 list_for_each_entry_safe(old, tmp, &new->head, list) {
2598 list_del(&old->list);
2599 kfree(old);
2600 }
2601 kfree(new);
2602}
2603
Liu Bo38c227d2013-01-29 03:18:40 +00002604static void relink_file_extents(struct new_sa_defrag_extent *new)
2605{
2606 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002607 struct sa_defrag_extent_backref *backref;
2608 struct sa_defrag_extent_backref *prev = NULL;
2609 struct inode *inode;
2610 struct btrfs_root *root;
2611 struct rb_node *node;
2612 int ret;
2613
2614 inode = new->inode;
2615 root = BTRFS_I(inode)->root;
2616
2617 path = btrfs_alloc_path();
2618 if (!path)
2619 return;
2620
2621 if (!record_extent_backrefs(path, new)) {
2622 btrfs_free_path(path);
2623 goto out;
2624 }
2625 btrfs_release_path(path);
2626
2627 while (1) {
2628 node = rb_first(&new->root);
2629 if (!node)
2630 break;
2631 rb_erase(node, &new->root);
2632
2633 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2634
2635 ret = relink_extent_backref(path, prev, backref);
2636 WARN_ON(ret < 0);
2637
2638 kfree(prev);
2639
2640 if (ret == 1)
2641 prev = backref;
2642 else
2643 prev = NULL;
2644 cond_resched();
2645 }
2646 kfree(prev);
2647
2648 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002649out:
Liu Bo6f519562013-10-29 10:45:05 +08002650 free_sa_defrag_extent(new);
2651
Liu Bo38c227d2013-01-29 03:18:40 +00002652 atomic_dec(&root->fs_info->defrag_running);
2653 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002654}
2655
2656static struct new_sa_defrag_extent *
2657record_old_file_extents(struct inode *inode,
2658 struct btrfs_ordered_extent *ordered)
2659{
2660 struct btrfs_root *root = BTRFS_I(inode)->root;
2661 struct btrfs_path *path;
2662 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002663 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002664 struct new_sa_defrag_extent *new;
2665 int ret;
2666
2667 new = kmalloc(sizeof(*new), GFP_NOFS);
2668 if (!new)
2669 return NULL;
2670
2671 new->inode = inode;
2672 new->file_pos = ordered->file_offset;
2673 new->len = ordered->len;
2674 new->bytenr = ordered->start;
2675 new->disk_len = ordered->disk_len;
2676 new->compress_type = ordered->compress_type;
2677 new->root = RB_ROOT;
2678 INIT_LIST_HEAD(&new->head);
2679
2680 path = btrfs_alloc_path();
2681 if (!path)
2682 goto out_kfree;
2683
2684 key.objectid = btrfs_ino(inode);
2685 key.type = BTRFS_EXTENT_DATA_KEY;
2686 key.offset = new->file_pos;
2687
2688 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2689 if (ret < 0)
2690 goto out_free_path;
2691 if (ret > 0 && path->slots[0] > 0)
2692 path->slots[0]--;
2693
2694 /* find out all the old extents for the file range */
2695 while (1) {
2696 struct btrfs_file_extent_item *extent;
2697 struct extent_buffer *l;
2698 int slot;
2699 u64 num_bytes;
2700 u64 offset;
2701 u64 end;
2702 u64 disk_bytenr;
2703 u64 extent_offset;
2704
2705 l = path->nodes[0];
2706 slot = path->slots[0];
2707
2708 if (slot >= btrfs_header_nritems(l)) {
2709 ret = btrfs_next_leaf(root, path);
2710 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002711 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002712 else if (ret > 0)
2713 break;
2714 continue;
2715 }
2716
2717 btrfs_item_key_to_cpu(l, &key, slot);
2718
2719 if (key.objectid != btrfs_ino(inode))
2720 break;
2721 if (key.type != BTRFS_EXTENT_DATA_KEY)
2722 break;
2723 if (key.offset >= new->file_pos + new->len)
2724 break;
2725
2726 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2727
2728 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2729 if (key.offset + num_bytes < new->file_pos)
2730 goto next;
2731
2732 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2733 if (!disk_bytenr)
2734 goto next;
2735
2736 extent_offset = btrfs_file_extent_offset(l, extent);
2737
2738 old = kmalloc(sizeof(*old), GFP_NOFS);
2739 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002740 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002741
2742 offset = max(new->file_pos, key.offset);
2743 end = min(new->file_pos + new->len, key.offset + num_bytes);
2744
2745 old->bytenr = disk_bytenr;
2746 old->extent_offset = extent_offset;
2747 old->offset = offset - key.offset;
2748 old->len = end - offset;
2749 old->new = new;
2750 old->count = 0;
2751 list_add_tail(&old->list, &new->head);
2752next:
2753 path->slots[0]++;
2754 cond_resched();
2755 }
2756
2757 btrfs_free_path(path);
2758 atomic_inc(&root->fs_info->defrag_running);
2759
2760 return new;
2761
Liu Bo38c227d2013-01-29 03:18:40 +00002762out_free_path:
2763 btrfs_free_path(path);
2764out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002765 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002766 return NULL;
2767}
2768
Miao Xiee570fd22014-06-19 10:42:50 +08002769static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2770 u64 start, u64 len)
2771{
2772 struct btrfs_block_group_cache *cache;
2773
2774 cache = btrfs_lookup_block_group(root->fs_info, start);
2775 ASSERT(cache);
2776
2777 spin_lock(&cache->lock);
2778 cache->delalloc_bytes -= len;
2779 spin_unlock(&cache->lock);
2780
2781 btrfs_put_block_group(cache);
2782}
2783
Chris Masond352ac62008-09-29 15:18:18 -04002784/* as ordered data IO finishes, this gets called so we can finish
2785 * an ordered extent if the range of bytes in the file it covers are
2786 * fully written.
2787 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002788static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002789{
Josef Bacik5fd02042012-05-02 14:00:54 -04002790 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002791 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002792 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002793 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002794 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002795 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002796 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002797 int ret = 0;
2798 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002799 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002800 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002801
Liu Bo83eea1f2012-07-10 05:28:39 -06002802 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002803
Josef Bacik5fd02042012-05-02 14:00:54 -04002804 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2805 ret = -EIO;
2806 goto out;
2807 }
2808
Miao Xief6124962014-09-12 18:44:04 +08002809 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2810 ordered_extent->file_offset +
2811 ordered_extent->len - 1);
2812
Josef Bacik77cef2e2013-08-29 13:57:21 -04002813 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2814 truncated = true;
2815 logical_len = ordered_extent->truncated_len;
2816 /* Truncated the entire extent, don't bother adding */
2817 if (!logical_len)
2818 goto out;
2819 }
2820
Yan, Zhengc2167752009-11-12 09:34:21 +00002821 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002822 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002823 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2824 if (nolock)
2825 trans = btrfs_join_transaction_nolock(root);
2826 else
2827 trans = btrfs_join_transaction(root);
2828 if (IS_ERR(trans)) {
2829 ret = PTR_ERR(trans);
2830 trans = NULL;
2831 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002832 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002833 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2834 ret = btrfs_update_inode_fallback(trans, root, inode);
2835 if (ret) /* -ENOMEM or corruption */
2836 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002837 goto out;
2838 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002839
Josef Bacik2ac55d42010-02-03 19:33:23 +00002840 lock_extent_bits(io_tree, ordered_extent->file_offset,
2841 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002842 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002843
Liu Bo38c227d2013-01-29 03:18:40 +00002844 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2845 ordered_extent->file_offset + ordered_extent->len - 1,
2846 EXTENT_DEFRAG, 1, cached_state);
2847 if (ret) {
2848 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002849 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002850 /* the inode is shared */
2851 new = record_old_file_extents(inode, ordered_extent);
2852
2853 clear_extent_bit(io_tree, ordered_extent->file_offset,
2854 ordered_extent->file_offset + ordered_extent->len - 1,
2855 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2856 }
2857
Josef Bacik0cb59c92010-07-02 12:14:14 -04002858 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002859 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002860 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002861 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002862 if (IS_ERR(trans)) {
2863 ret = PTR_ERR(trans);
2864 trans = NULL;
2865 goto out_unlock;
2866 }
Chris Masona79b7d42014-05-22 16:18:52 -07002867
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002868 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002869
Chris Masonc8b97812008-10-29 14:49:59 -04002870 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002871 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002872 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002873 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002874 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002875 ordered_extent->file_offset,
2876 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002877 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002878 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002879 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002880 ret = insert_reserved_file_extent(trans, inode,
2881 ordered_extent->file_offset,
2882 ordered_extent->start,
2883 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002884 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002885 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002886 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002887 if (!ret)
2888 btrfs_release_delalloc_bytes(root,
2889 ordered_extent->start,
2890 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002891 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002892 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2893 ordered_extent->file_offset, ordered_extent->len,
2894 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002895 if (ret < 0) {
2896 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002897 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002898 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002899
Chris Masone6dcd2d2008-07-17 12:53:50 -04002900 add_pending_csums(trans, inode, ordered_extent->file_offset,
2901 &ordered_extent->list);
2902
Josef Bacik6c760c02012-11-09 10:53:21 -05002903 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2904 ret = btrfs_update_inode_fallback(trans, root, inode);
2905 if (ret) { /* -ENOMEM or corruption */
2906 btrfs_abort_transaction(trans, root, ret);
2907 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002908 }
2909 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002910out_unlock:
2911 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2912 ordered_extent->file_offset +
2913 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002914out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002915 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002916 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002917 if (trans)
2918 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002919
Josef Bacik77cef2e2013-08-29 13:57:21 -04002920 if (ret || truncated) {
2921 u64 start, end;
2922
2923 if (truncated)
2924 start = ordered_extent->file_offset + logical_len;
2925 else
2926 start = ordered_extent->file_offset;
2927 end = ordered_extent->file_offset + ordered_extent->len - 1;
2928 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2929
2930 /* Drop the cache for the part of the extent we didn't write. */
2931 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002932
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002933 /*
2934 * If the ordered extent had an IOERR or something else went
2935 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002936 * back to the allocator. We only free the extent in the
2937 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002938 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002939 if ((ret || !logical_len) &&
2940 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002941 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2942 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002943 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002944 }
2945
2946
Josef Bacik5fd02042012-05-02 14:00:54 -04002947 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002948 * This needs to be done to make sure anybody waiting knows we are done
2949 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002950 */
2951 btrfs_remove_ordered_extent(inode, ordered_extent);
2952
Liu Bo38c227d2013-01-29 03:18:40 +00002953 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002954 if (new) {
2955 if (ret) {
2956 free_sa_defrag_extent(new);
2957 atomic_dec(&root->fs_info->defrag_running);
2958 } else {
2959 relink_file_extents(new);
2960 }
2961 }
Liu Bo38c227d2013-01-29 03:18:40 +00002962
Chris Masone6dcd2d2008-07-17 12:53:50 -04002963 /* once for us */
2964 btrfs_put_ordered_extent(ordered_extent);
2965 /* once for the tree */
2966 btrfs_put_ordered_extent(ordered_extent);
2967
Josef Bacik5fd02042012-05-02 14:00:54 -04002968 return ret;
2969}
2970
2971static void finish_ordered_fn(struct btrfs_work *work)
2972{
2973 struct btrfs_ordered_extent *ordered_extent;
2974 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2975 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002976}
2977
Christoph Hellwigb2950862008-12-02 09:54:17 -05002978static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002979 struct extent_state *state, int uptodate)
2980{
Josef Bacik5fd02042012-05-02 14:00:54 -04002981 struct inode *inode = page->mapping->host;
2982 struct btrfs_root *root = BTRFS_I(inode)->root;
2983 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002984 struct btrfs_workqueue *wq;
2985 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002986
liubo1abe9b82011-03-24 11:18:59 +00002987 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2988
Chris Mason8b62b722009-09-02 16:53:46 -04002989 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002990 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2991 end - start + 1, uptodate))
2992 return 0;
2993
Liu Bo9e0af232014-08-15 23:36:53 +08002994 if (btrfs_is_free_space_inode(inode)) {
2995 wq = root->fs_info->endio_freespace_worker;
2996 func = btrfs_freespace_write_helper;
2997 } else {
2998 wq = root->fs_info->endio_write_workers;
2999 func = btrfs_endio_write_helper;
3000 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003001
Liu Bo9e0af232014-08-15 23:36:53 +08003002 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3003 NULL);
3004 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003005
3006 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003007}
3008
Miao Xiedc380ae2014-09-12 18:43:55 +08003009static int __readpage_endio_check(struct inode *inode,
3010 struct btrfs_io_bio *io_bio,
3011 int icsum, struct page *page,
3012 int pgoff, u64 start, size_t len)
3013{
3014 char *kaddr;
3015 u32 csum_expected;
3016 u32 csum = ~(u32)0;
3017 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
3018 DEFAULT_RATELIMIT_BURST);
3019
3020 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3021
3022 kaddr = kmap_atomic(page);
3023 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3024 btrfs_csum_final(csum, (char *)&csum);
3025 if (csum != csum_expected)
3026 goto zeroit;
3027
3028 kunmap_atomic(kaddr);
3029 return 0;
3030zeroit:
3031 if (__ratelimit(&_rs))
David Sterbaf0954c62014-12-19 18:38:44 +01003032 btrfs_warn(BTRFS_I(inode)->root->fs_info,
Miao Xiedc380ae2014-09-12 18:43:55 +08003033 "csum failed ino %llu off %llu csum %u expected csum %u",
3034 btrfs_ino(inode), start, csum, csum_expected);
3035 memset(kaddr + pgoff, 1, len);
3036 flush_dcache_page(page);
3037 kunmap_atomic(kaddr);
3038 if (csum_expected == 0)
3039 return 0;
3040 return -EIO;
3041}
3042
Chris Masond352ac62008-09-29 15:18:18 -04003043/*
Chris Masond352ac62008-09-29 15:18:18 -04003044 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003045 * if there's a match, we allow the bio to finish. If not, the code in
3046 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003047 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003048static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3049 u64 phy_offset, struct page *page,
3050 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003051{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003052 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003053 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003054 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003055 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003056
Chris Masond20f7042008-12-08 16:58:54 -05003057 if (PageChecked(page)) {
3058 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003059 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003060 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003061
3062 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003063 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003064
Yan Zheng17d217f2008-12-12 10:03:38 -05003065 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003066 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05003067 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
3068 GFP_NOFS);
3069 return 0;
3070 }
3071
Miao Xiefacc8a222013-07-25 19:22:34 +08003072 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003073 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3074 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003075}
Chris Masonb888db2b2007-08-27 16:49:44 -04003076
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003077struct delayed_iput {
3078 struct list_head list;
3079 struct inode *inode;
3080};
3081
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003082/* JDM: If this is fs-wide, why can't we add a pointer to
3083 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003084void btrfs_add_delayed_iput(struct inode *inode)
3085{
3086 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3087 struct delayed_iput *delayed;
3088
3089 if (atomic_add_unless(&inode->i_count, -1, 1))
3090 return;
3091
3092 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3093 delayed->inode = inode;
3094
3095 spin_lock(&fs_info->delayed_iput_lock);
3096 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3097 spin_unlock(&fs_info->delayed_iput_lock);
3098}
3099
3100void btrfs_run_delayed_iputs(struct btrfs_root *root)
3101{
3102 LIST_HEAD(list);
3103 struct btrfs_fs_info *fs_info = root->fs_info;
3104 struct delayed_iput *delayed;
3105 int empty;
3106
3107 spin_lock(&fs_info->delayed_iput_lock);
3108 empty = list_empty(&fs_info->delayed_iputs);
3109 spin_unlock(&fs_info->delayed_iput_lock);
3110 if (empty)
3111 return;
3112
Zhao Leid7c15172015-02-26 10:49:20 +08003113 down_read(&fs_info->delayed_iput_sem);
3114
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003115 spin_lock(&fs_info->delayed_iput_lock);
3116 list_splice_init(&fs_info->delayed_iputs, &list);
3117 spin_unlock(&fs_info->delayed_iput_lock);
3118
3119 while (!list_empty(&list)) {
3120 delayed = list_entry(list.next, struct delayed_iput, list);
3121 list_del(&delayed->list);
3122 iput(delayed->inode);
3123 kfree(delayed);
3124 }
Zhao Leid7c15172015-02-26 10:49:20 +08003125
3126 up_read(&root->fs_info->delayed_iput_sem);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003127}
3128
Yan, Zhengd68fc572010-05-16 10:49:58 -04003129/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003130 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003131 * files in the subvolume, it removes orphan item and frees block_rsv
3132 * structure.
3133 */
3134void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3135 struct btrfs_root *root)
3136{
Josef Bacik90290e12011-12-02 15:44:12 -05003137 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003138 int ret;
3139
Josef Bacik8a35d952012-05-23 14:26:42 -04003140 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003141 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3142 return;
3143
Josef Bacik90290e12011-12-02 15:44:12 -05003144 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003145 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003146 spin_unlock(&root->orphan_lock);
3147 return;
3148 }
3149
3150 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3151 spin_unlock(&root->orphan_lock);
3152 return;
3153 }
3154
3155 block_rsv = root->orphan_block_rsv;
3156 root->orphan_block_rsv = NULL;
3157 spin_unlock(&root->orphan_lock);
3158
Miao Xie27cdeb72014-04-02 19:51:05 +08003159 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003160 btrfs_root_refs(&root->root_item) > 0) {
3161 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3162 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003163 if (ret)
3164 btrfs_abort_transaction(trans, root, ret);
3165 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003166 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3167 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003168 }
3169
Josef Bacik90290e12011-12-02 15:44:12 -05003170 if (block_rsv) {
3171 WARN_ON(block_rsv->size > 0);
3172 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003173 }
3174}
3175
3176/*
Josef Bacik7b128762008-07-24 12:17:14 -04003177 * This creates an orphan entry for the given inode in case something goes
3178 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003179 *
3180 * NOTE: caller of this function should reserve 5 units of metadata for
3181 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003182 */
3183int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3184{
3185 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003186 struct btrfs_block_rsv *block_rsv = NULL;
3187 int reserve = 0;
3188 int insert = 0;
3189 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003190
Yan, Zhengd68fc572010-05-16 10:49:58 -04003191 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003192 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003193 if (!block_rsv)
3194 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003195 }
3196
Yan, Zhengd68fc572010-05-16 10:49:58 -04003197 spin_lock(&root->orphan_lock);
3198 if (!root->orphan_block_rsv) {
3199 root->orphan_block_rsv = block_rsv;
3200 } else if (block_rsv) {
3201 btrfs_free_block_rsv(root, block_rsv);
3202 block_rsv = NULL;
3203 }
Josef Bacik7b128762008-07-24 12:17:14 -04003204
Josef Bacik8a35d952012-05-23 14:26:42 -04003205 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3206 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003207#if 0
3208 /*
3209 * For proper ENOSPC handling, we should do orphan
3210 * cleanup when mounting. But this introduces backward
3211 * compatibility issue.
3212 */
3213 if (!xchg(&root->orphan_item_inserted, 1))
3214 insert = 2;
3215 else
3216 insert = 1;
3217#endif
3218 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003219 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003220 }
Josef Bacik7b128762008-07-24 12:17:14 -04003221
Josef Bacik72ac3c02012-05-23 14:13:11 -04003222 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3223 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003224 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003225 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003226
Yan, Zhengd68fc572010-05-16 10:49:58 -04003227 /* grab metadata reservation from transaction handle */
3228 if (reserve) {
3229 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003230 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003231 }
3232
3233 /* insert an orphan item to track this unlinked/truncated file */
3234 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003235 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003236 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003237 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003238 if (reserve) {
3239 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3240 &BTRFS_I(inode)->runtime_flags);
3241 btrfs_orphan_release_metadata(inode);
3242 }
3243 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003244 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3245 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003246 btrfs_abort_transaction(trans, root, ret);
3247 return ret;
3248 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003249 }
3250 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003251 }
3252
3253 /* insert an orphan item to track subvolume contains orphan files */
3254 if (insert >= 2) {
3255 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3256 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003257 if (ret && ret != -EEXIST) {
3258 btrfs_abort_transaction(trans, root, ret);
3259 return ret;
3260 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003261 }
3262 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003263}
3264
3265/*
3266 * We have done the truncate/delete so we can go ahead and remove the orphan
3267 * item for this particular inode.
3268 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003269static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3270 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003271{
3272 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003273 int delete_item = 0;
3274 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003275 int ret = 0;
3276
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003278 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3279 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003280 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003281
Josef Bacik72ac3c02012-05-23 14:13:11 -04003282 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3283 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003284 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003285 spin_unlock(&root->orphan_lock);
3286
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003287 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003288 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003289 if (trans)
3290 ret = btrfs_del_orphan_item(trans, root,
3291 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003292 }
Josef Bacik7b128762008-07-24 12:17:14 -04003293
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003294 if (release_rsv)
3295 btrfs_orphan_release_metadata(inode);
3296
Josef Bacik4ef31a42013-08-13 14:10:08 -04003297 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003298}
3299
3300/*
3301 * this cleans up any orphans that may be left on the list from the last use
3302 * of this root.
3303 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003304int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003305{
3306 struct btrfs_path *path;
3307 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003308 struct btrfs_key key, found_key;
3309 struct btrfs_trans_handle *trans;
3310 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003311 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003312 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3313
Yan, Zhengd68fc572010-05-16 10:49:58 -04003314 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003315 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003316
3317 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003318 if (!path) {
3319 ret = -ENOMEM;
3320 goto out;
3321 }
Josef Bacik7b128762008-07-24 12:17:14 -04003322 path->reada = -1;
3323
3324 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003325 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003326 key.offset = (u64)-1;
3327
Josef Bacik7b128762008-07-24 12:17:14 -04003328 while (1) {
3329 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003330 if (ret < 0)
3331 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003332
3333 /*
3334 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003335 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003336 * find the key and see if we have stuff that matches
3337 */
3338 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003339 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003340 if (path->slots[0] == 0)
3341 break;
3342 path->slots[0]--;
3343 }
3344
3345 /* pull out the item */
3346 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003347 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3348
3349 /* make sure the item matches what we want */
3350 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3351 break;
David Sterba962a2982014-06-04 18:41:45 +02003352 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003353 break;
3354
3355 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003356 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003357
3358 /*
3359 * this is where we are basically btrfs_lookup, without the
3360 * crossing root thing. we store the inode number in the
3361 * offset of the orphan item.
3362 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003363
3364 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003365 btrfs_err(root->fs_info,
3366 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003367 ret = -EINVAL;
3368 goto out;
3369 }
3370
3371 last_objectid = found_key.offset;
3372
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003373 found_key.objectid = found_key.offset;
3374 found_key.type = BTRFS_INODE_ITEM_KEY;
3375 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003376 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303377 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003378 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003379 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003380
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003381 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3382 struct btrfs_root *dead_root;
3383 struct btrfs_fs_info *fs_info = root->fs_info;
3384 int is_dead_root = 0;
3385
3386 /*
3387 * this is an orphan in the tree root. Currently these
3388 * could come from 2 sources:
3389 * a) a snapshot deletion in progress
3390 * b) a free space cache inode
3391 * We need to distinguish those two, as the snapshot
3392 * orphan must not get deleted.
3393 * find_dead_roots already ran before us, so if this
3394 * is a snapshot deletion, we should find the root
3395 * in the dead_roots list
3396 */
3397 spin_lock(&fs_info->trans_lock);
3398 list_for_each_entry(dead_root, &fs_info->dead_roots,
3399 root_list) {
3400 if (dead_root->root_key.objectid ==
3401 found_key.objectid) {
3402 is_dead_root = 1;
3403 break;
3404 }
3405 }
3406 spin_unlock(&fs_info->trans_lock);
3407 if (is_dead_root) {
3408 /* prevent this orphan from being found again */
3409 key.offset = found_key.objectid - 1;
3410 continue;
3411 }
3412 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003413 /*
3414 * Inode is already gone but the orphan item is still there,
3415 * kill the orphan item.
3416 */
3417 if (ret == -ESTALE) {
3418 trans = btrfs_start_transaction(root, 1);
3419 if (IS_ERR(trans)) {
3420 ret = PTR_ERR(trans);
3421 goto out;
3422 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003423 btrfs_debug(root->fs_info, "auto deleting %Lu",
3424 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003425 ret = btrfs_del_orphan_item(trans, root,
3426 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003427 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003428 if (ret)
3429 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003430 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003431 }
Josef Bacik7b128762008-07-24 12:17:14 -04003432
Josef Bacik7b128762008-07-24 12:17:14 -04003433 /*
3434 * add this inode to the orphan list so btrfs_orphan_del does
3435 * the proper thing when we hit it
3436 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003437 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3438 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003439 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003440
Josef Bacik7b128762008-07-24 12:17:14 -04003441 /* if we have links, this was a truncate, lets do that */
3442 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303443 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003444 iput(inode);
3445 continue;
3446 }
Josef Bacik7b128762008-07-24 12:17:14 -04003447 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003448
3449 /* 1 for the orphan item deletion. */
3450 trans = btrfs_start_transaction(root, 1);
3451 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003452 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003453 ret = PTR_ERR(trans);
3454 goto out;
3455 }
3456 ret = btrfs_orphan_add(trans, inode);
3457 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003458 if (ret) {
3459 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003460 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003461 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003462
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003463 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003464 if (ret)
3465 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003466 } else {
3467 nr_unlink++;
3468 }
3469
3470 /* this will do delete_inode and everything for us */
3471 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003472 if (ret)
3473 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003474 }
Miao Xie3254c872011-11-10 20:45:05 -05003475 /* release the path since we're done with it */
3476 btrfs_release_path(path);
3477
Yan, Zhengd68fc572010-05-16 10:49:58 -04003478 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3479
3480 if (root->orphan_block_rsv)
3481 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3482 (u64)-1);
3483
Miao Xie27cdeb72014-04-02 19:51:05 +08003484 if (root->orphan_block_rsv ||
3485 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003486 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003487 if (!IS_ERR(trans))
3488 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003489 }
Josef Bacik7b128762008-07-24 12:17:14 -04003490
3491 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003492 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003493 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003494 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003495
3496out:
3497 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003498 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003499 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003500 btrfs_free_path(path);
3501 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003502}
3503
Chris Masond352ac62008-09-29 15:18:18 -04003504/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003505 * very simple check to peek ahead in the leaf looking for xattrs. If we
3506 * don't find any xattrs, we know there can't be any acls.
3507 *
3508 * slot is the slot the inode is in, objectid is the objectid of the inode
3509 */
3510static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003511 int slot, u64 objectid,
3512 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003513{
3514 u32 nritems = btrfs_header_nritems(leaf);
3515 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003516 static u64 xattr_access = 0;
3517 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003518 int scanned = 0;
3519
Josef Bacikf23b5a52013-06-19 10:16:26 -04003520 if (!xattr_access) {
3521 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3522 strlen(POSIX_ACL_XATTR_ACCESS));
3523 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3524 strlen(POSIX_ACL_XATTR_DEFAULT));
3525 }
3526
Chris Mason46a53cc2009-04-27 11:47:50 -04003527 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003528 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003529 while (slot < nritems) {
3530 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3531
3532 /* we found a different objectid, there must not be acls */
3533 if (found_key.objectid != objectid)
3534 return 0;
3535
3536 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003537 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003538 if (*first_xattr_slot == -1)
3539 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003540 if (found_key.offset == xattr_access ||
3541 found_key.offset == xattr_default)
3542 return 1;
3543 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003544
3545 /*
3546 * we found a key greater than an xattr key, there can't
3547 * be any acls later on
3548 */
3549 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3550 return 0;
3551
3552 slot++;
3553 scanned++;
3554
3555 /*
3556 * it goes inode, inode backrefs, xattrs, extents,
3557 * so if there are a ton of hard links to an inode there can
3558 * be a lot of backrefs. Don't waste time searching too hard,
3559 * this is just an optimization
3560 */
3561 if (scanned >= 8)
3562 break;
3563 }
3564 /* we hit the end of the leaf before we found an xattr or
3565 * something larger than an xattr. We have to assume the inode
3566 * has acls
3567 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003568 if (*first_xattr_slot == -1)
3569 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003570 return 1;
3571}
3572
3573/*
Chris Masond352ac62008-09-29 15:18:18 -04003574 * read an inode from the btree into the in-memory inode
3575 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003576static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003577{
3578 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003579 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003580 struct btrfs_inode_item *inode_item;
3581 struct btrfs_root *root = BTRFS_I(inode)->root;
3582 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003583 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003584 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003585 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003586 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003587 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003588 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003589
3590 ret = btrfs_fill_inode(inode, &rdev);
3591 if (!ret)
3592 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003593
3594 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003595 if (!path)
3596 goto make_bad;
3597
Chris Mason39279cc2007-06-12 06:35:45 -04003598 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003599
Chris Mason39279cc2007-06-12 06:35:45 -04003600 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003601 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003602 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003603
Chris Mason5f39d392007-10-15 16:14:19 -04003604 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003605
3606 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003607 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003608
Chris Mason5f39d392007-10-15 16:14:19 -04003609 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3610 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003611 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003612 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003613 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3614 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003615 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003616
David Sterbaa937b972014-12-12 17:39:12 +01003617 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3618 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003619
David Sterbaa937b972014-12-12 17:39:12 +01003620 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3621 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003622
David Sterbaa937b972014-12-12 17:39:12 +01003623 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3624 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003625
chandan r9cc97d62012-07-04 12:48:07 +05303626 BTRFS_I(inode)->i_otime.tv_sec =
3627 btrfs_timespec_sec(leaf, &inode_item->otime);
3628 BTRFS_I(inode)->i_otime.tv_nsec =
3629 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003630
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003631 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003632 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003633 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3634
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003635 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003636 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003637 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003638 rdev = btrfs_inode_rdev(leaf, inode_item);
3639
Josef Bacikaec74772008-07-24 12:12:38 -04003640 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003641 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003642
3643cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003644 /*
3645 * If we were modified in the current generation and evicted from memory
3646 * and then re-read we need to do a full sync since we don't have any
3647 * idea about which extents were modified before we were evicted from
3648 * cache.
3649 *
3650 * This is required for both inode re-read from disk and delayed inode
3651 * in delayed_nodes_tree.
3652 */
3653 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3654 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3655 &BTRFS_I(inode)->runtime_flags);
3656
Filipe Mananabde6c242015-07-24 00:00:19 +01003657 /*
3658 * We don't persist the id of the transaction where an unlink operation
3659 * against the inode was last made. So here we assume the inode might
3660 * have been evicted, and therefore the exact value of last_unlink_trans
3661 * lost, and set it to last_trans to avoid metadata inconsistencies
3662 * between the inode and its parent if the inode is fsync'ed and the log
3663 * replayed. For example, in the scenario:
3664 *
3665 * touch mydir/foo
3666 * ln mydir/foo mydir/bar
3667 * sync
3668 * unlink mydir/bar
3669 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3670 * xfs_io -c fsync mydir/foo
3671 * <power failure>
3672 * mount fs, triggers fsync log replay
3673 *
3674 * We must make sure that when we fsync our inode foo we also log its
3675 * parent inode, otherwise after log replay the parent still has the
3676 * dentry with the "bar" name but our inode foo has a link count of 1
3677 * and doesn't have an inode ref with the name "bar" anymore.
3678 *
3679 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3680 * but it guarantees correctness at the expense of ocassional full
3681 * transaction commits on fsync if our inode is a directory, or if our
3682 * inode is not a directory, logging its parent unnecessarily.
3683 */
3684 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3685
Miao Xie67de1172013-12-26 13:07:06 +08003686 path->slots[0]++;
3687 if (inode->i_nlink != 1 ||
3688 path->slots[0] >= btrfs_header_nritems(leaf))
3689 goto cache_acl;
3690
3691 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3692 if (location.objectid != btrfs_ino(inode))
3693 goto cache_acl;
3694
3695 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3696 if (location.type == BTRFS_INODE_REF_KEY) {
3697 struct btrfs_inode_ref *ref;
3698
3699 ref = (struct btrfs_inode_ref *)ptr;
3700 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3701 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3702 struct btrfs_inode_extref *extref;
3703
3704 extref = (struct btrfs_inode_extref *)ptr;
3705 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3706 extref);
3707 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003708cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003709 /*
3710 * try to precache a NULL acl entry for files that don't have
3711 * any xattrs or acls
3712 */
Li Zefan33345d012011-04-20 10:31:50 +08003713 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003714 btrfs_ino(inode), &first_xattr_slot);
3715 if (first_xattr_slot != -1) {
3716 path->slots[0] = first_xattr_slot;
3717 ret = btrfs_load_inode_props(inode, path);
3718 if (ret)
3719 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003720 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003721 btrfs_ino(inode),
3722 root->root_key.objectid, ret);
3723 }
3724 btrfs_free_path(path);
3725
Al Viro72c04902009-06-24 16:58:48 -04003726 if (!maybe_acls)
3727 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003728
Chris Mason39279cc2007-06-12 06:35:45 -04003729 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003730 case S_IFREG:
3731 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003732 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003733 inode->i_fop = &btrfs_file_operations;
3734 inode->i_op = &btrfs_file_inode_operations;
3735 break;
3736 case S_IFDIR:
3737 inode->i_fop = &btrfs_dir_file_operations;
3738 if (root == root->fs_info->tree_root)
3739 inode->i_op = &btrfs_dir_ro_inode_operations;
3740 else
3741 inode->i_op = &btrfs_dir_inode_operations;
3742 break;
3743 case S_IFLNK:
3744 inode->i_op = &btrfs_symlink_inode_operations;
3745 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3746 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003747 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003748 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003749 init_special_inode(inode, inode->i_mode, rdev);
3750 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003751 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003752
3753 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003754 return;
3755
3756make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003757 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003758 make_bad_inode(inode);
3759}
3760
Chris Masond352ac62008-09-29 15:18:18 -04003761/*
3762 * given a leaf and an inode, copy the inode fields into the leaf
3763 */
Chris Masone02119d2008-09-05 16:13:11 -04003764static void fill_inode_item(struct btrfs_trans_handle *trans,
3765 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003766 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003767 struct inode *inode)
3768{
Liu Bo51fab692012-12-27 09:01:21 +00003769 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003770
Liu Bo51fab692012-12-27 09:01:21 +00003771 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003772
Liu Bo51fab692012-12-27 09:01:21 +00003773 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3774 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3775 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3776 &token);
3777 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3778 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003779
David Sterbaa937b972014-12-12 17:39:12 +01003780 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003781 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003782 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003783 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003784
David Sterbaa937b972014-12-12 17:39:12 +01003785 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003786 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003787 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003788 inode->i_mtime.tv_nsec, &token);
3789
David Sterbaa937b972014-12-12 17:39:12 +01003790 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003791 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003792 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003793 inode->i_ctime.tv_nsec, &token);
3794
chandan r9cc97d62012-07-04 12:48:07 +05303795 btrfs_set_token_timespec_sec(leaf, &item->otime,
3796 BTRFS_I(inode)->i_otime.tv_sec, &token);
3797 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3798 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3799
Liu Bo51fab692012-12-27 09:01:21 +00003800 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3801 &token);
3802 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3803 &token);
3804 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3805 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3806 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3807 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3808 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003809}
3810
Chris Masond352ac62008-09-29 15:18:18 -04003811/*
3812 * copy everything in the in-memory inode into the btree.
3813 */
Chris Mason21151332011-11-10 20:39:08 -05003814static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003815 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003816{
3817 struct btrfs_inode_item *inode_item;
3818 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003819 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003820 int ret;
3821
3822 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003823 if (!path)
3824 return -ENOMEM;
3825
Chris Masonb9473432009-03-13 11:00:37 -04003826 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003827 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3828 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003829 if (ret) {
3830 if (ret > 0)
3831 ret = -ENOENT;
3832 goto failed;
3833 }
3834
Chris Mason5f39d392007-10-15 16:14:19 -04003835 leaf = path->nodes[0];
3836 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003837 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003838
Chris Masone02119d2008-09-05 16:13:11 -04003839 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003840 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003841 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003842 ret = 0;
3843failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003844 btrfs_free_path(path);
3845 return ret;
3846}
3847
Chris Masond352ac62008-09-29 15:18:18 -04003848/*
Chris Mason21151332011-11-10 20:39:08 -05003849 * copy everything in the in-memory inode into the btree.
3850 */
3851noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3852 struct btrfs_root *root, struct inode *inode)
3853{
3854 int ret;
3855
3856 /*
3857 * If the inode is a free space inode, we can deadlock during commit
3858 * if we put it into the delayed code.
3859 *
3860 * The data relocation inode should also be directly updated
3861 * without delay
3862 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003863 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003864 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3865 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003866 btrfs_update_root_times(trans, root);
3867
Chris Mason21151332011-11-10 20:39:08 -05003868 ret = btrfs_delayed_update_inode(trans, root, inode);
3869 if (!ret)
3870 btrfs_set_inode_last_trans(trans, inode);
3871 return ret;
3872 }
3873
3874 return btrfs_update_inode_item(trans, root, inode);
3875}
3876
Josef Bacikbe6aef62012-10-22 15:43:12 -04003877noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3878 struct btrfs_root *root,
3879 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003880{
3881 int ret;
3882
3883 ret = btrfs_update_inode(trans, root, inode);
3884 if (ret == -ENOSPC)
3885 return btrfs_update_inode_item(trans, root, inode);
3886 return ret;
3887}
3888
3889/*
Chris Masond352ac62008-09-29 15:18:18 -04003890 * unlink helper that gets used here in inode.c and in the tree logging
3891 * recovery code. It remove a link in a directory with a given name, and
3892 * also drops the back refs in the inode to the directory
3893 */
Al Viro92986792011-03-04 17:14:37 +00003894static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3895 struct btrfs_root *root,
3896 struct inode *dir, struct inode *inode,
3897 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003898{
3899 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003900 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003901 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003902 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003903 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003904 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003905 u64 ino = btrfs_ino(inode);
3906 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003907
3908 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003909 if (!path) {
3910 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003911 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003912 }
3913
Chris Masonb9473432009-03-13 11:00:37 -04003914 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003915 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003916 name, name_len, -1);
3917 if (IS_ERR(di)) {
3918 ret = PTR_ERR(di);
3919 goto err;
3920 }
3921 if (!di) {
3922 ret = -ENOENT;
3923 goto err;
3924 }
Chris Mason5f39d392007-10-15 16:14:19 -04003925 leaf = path->nodes[0];
3926 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003927 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003928 if (ret)
3929 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003930 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003931
Miao Xie67de1172013-12-26 13:07:06 +08003932 /*
3933 * If we don't have dir index, we have to get it by looking up
3934 * the inode ref, since we get the inode ref, remove it directly,
3935 * it is unnecessary to do delayed deletion.
3936 *
3937 * But if we have dir index, needn't search inode ref to get it.
3938 * Since the inode ref is close to the inode item, it is better
3939 * that we delay to delete it, and just do this deletion when
3940 * we update the inode item.
3941 */
3942 if (BTRFS_I(inode)->dir_index) {
3943 ret = btrfs_delayed_delete_inode_ref(inode);
3944 if (!ret) {
3945 index = BTRFS_I(inode)->dir_index;
3946 goto skip_backref;
3947 }
3948 }
3949
Li Zefan33345d012011-04-20 10:31:50 +08003950 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3951 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003952 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003953 btrfs_info(root->fs_info,
3954 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003955 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003956 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003957 goto err;
3958 }
Miao Xie67de1172013-12-26 13:07:06 +08003959skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003960 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003961 if (ret) {
3962 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003963 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003964 }
Chris Mason39279cc2007-06-12 06:35:45 -04003965
Chris Masone02119d2008-09-05 16:13:11 -04003966 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003967 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003968 if (ret != 0 && ret != -ENOENT) {
3969 btrfs_abort_transaction(trans, root, ret);
3970 goto err;
3971 }
Chris Masone02119d2008-09-05 16:13:11 -04003972
3973 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3974 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003975 if (ret == -ENOENT)
3976 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003977 else if (ret)
3978 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003979err:
3980 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003981 if (ret)
3982 goto out;
3983
3984 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003985 inode_inc_iversion(inode);
3986 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003987 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003988 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003989out:
Chris Mason39279cc2007-06-12 06:35:45 -04003990 return ret;
3991}
3992
Al Viro92986792011-03-04 17:14:37 +00003993int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3994 struct btrfs_root *root,
3995 struct inode *dir, struct inode *inode,
3996 const char *name, int name_len)
3997{
3998 int ret;
3999 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4000 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004001 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004002 ret = btrfs_update_inode(trans, root, inode);
4003 }
4004 return ret;
4005}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004006
4007/*
4008 * helper to start transaction for unlink and rmdir.
4009 *
Josef Bacikd52be812013-05-29 14:54:47 -04004010 * unlink and rmdir are special in btrfs, they do not always free space, so
4011 * if we cannot make our reservations the normal way try and see if there is
4012 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4013 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004014 */
Josef Bacikd52be812013-05-29 14:54:47 -04004015static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004016{
4017 struct btrfs_trans_handle *trans;
4018 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004019 int ret;
4020
Josef Bacike70bea52011-10-11 14:18:24 -04004021 /*
4022 * 1 for the possible orphan item
4023 * 1 for the dir item
4024 * 1 for the dir index
4025 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004026 * 1 for the inode
4027 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04004028 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004029 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
4030 return trans;
4031
Josef Bacikd52be812013-05-29 14:54:47 -04004032 if (PTR_ERR(trans) == -ENOSPC) {
4033 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004034
Josef Bacikd52be812013-05-29 14:54:47 -04004035 trans = btrfs_start_transaction(root, 0);
4036 if (IS_ERR(trans))
4037 return trans;
4038 ret = btrfs_cond_migrate_bytes(root->fs_info,
4039 &root->fs_info->trans_block_rsv,
4040 num_bytes, 5);
4041 if (ret) {
4042 btrfs_end_transaction(trans, root);
4043 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004044 }
Josef Bacik5a77d762011-11-01 14:32:23 -04004045 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04004046 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004047 }
Josef Bacikd52be812013-05-29 14:54:47 -04004048 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004049}
4050
Chris Mason39279cc2007-06-12 06:35:45 -04004051static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4052{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004053 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004054 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004055 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004056 int ret;
4057
Josef Bacikd52be812013-05-29 14:54:47 -04004058 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004059 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004060 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004061
David Howells2b0143b2015-03-17 22:25:59 +00004062 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004063
David Howells2b0143b2015-03-17 22:25:59 +00004064 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004065 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004066 if (ret)
4067 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004068
Yan, Zhenga22285a2010-05-16 10:48:46 -04004069 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004070 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004071 if (ret)
4072 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004073 }
Josef Bacik7b128762008-07-24 12:17:14 -04004074
Tsutomu Itohb5324022011-07-19 07:27:20 +00004075out:
Josef Bacikd52be812013-05-29 14:54:47 -04004076 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004077 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004078 return ret;
4079}
4080
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004081int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4082 struct btrfs_root *root,
4083 struct inode *dir, u64 objectid,
4084 const char *name, int name_len)
4085{
4086 struct btrfs_path *path;
4087 struct extent_buffer *leaf;
4088 struct btrfs_dir_item *di;
4089 struct btrfs_key key;
4090 u64 index;
4091 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004092 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004093
4094 path = btrfs_alloc_path();
4095 if (!path)
4096 return -ENOMEM;
4097
Li Zefan33345d012011-04-20 10:31:50 +08004098 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004099 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004100 if (IS_ERR_OR_NULL(di)) {
4101 if (!di)
4102 ret = -ENOENT;
4103 else
4104 ret = PTR_ERR(di);
4105 goto out;
4106 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004107
4108 leaf = path->nodes[0];
4109 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4110 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4111 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004112 if (ret) {
4113 btrfs_abort_transaction(trans, root, ret);
4114 goto out;
4115 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004116 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004117
4118 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4119 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004120 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004121 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004122 if (ret != -ENOENT) {
4123 btrfs_abort_transaction(trans, root, ret);
4124 goto out;
4125 }
Li Zefan33345d012011-04-20 10:31:50 +08004126 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004127 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004128 if (IS_ERR_OR_NULL(di)) {
4129 if (!di)
4130 ret = -ENOENT;
4131 else
4132 ret = PTR_ERR(di);
4133 btrfs_abort_transaction(trans, root, ret);
4134 goto out;
4135 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004136
4137 leaf = path->nodes[0];
4138 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004139 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004140 index = key.offset;
4141 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004142 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004143
Miao Xie16cdcec2011-04-22 18:12:22 +08004144 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004145 if (ret) {
4146 btrfs_abort_transaction(trans, root, ret);
4147 goto out;
4148 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004149
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004150 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004151 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004152 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004153 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004154 if (ret)
4155 btrfs_abort_transaction(trans, root, ret);
4156out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004157 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004158 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004159}
4160
Chris Mason39279cc2007-06-12 06:35:45 -04004161static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4162{
David Howells2b0143b2015-03-17 22:25:59 +00004163 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004164 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004165 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004166 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004167
David Sterbab3ae2442012-09-13 16:04:34 -06004168 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004169 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004170 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4171 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004172
Josef Bacikd52be812013-05-29 14:54:47 -04004173 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004174 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004175 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004176
Li Zefan33345d012011-04-20 10:31:50 +08004177 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004178 err = btrfs_unlink_subvol(trans, root, dir,
4179 BTRFS_I(inode)->location.objectid,
4180 dentry->d_name.name,
4181 dentry->d_name.len);
4182 goto out;
4183 }
4184
Josef Bacik7b128762008-07-24 12:17:14 -04004185 err = btrfs_orphan_add(trans, inode);
4186 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004187 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004188
Chris Mason39279cc2007-06-12 06:35:45 -04004189 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004190 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004191 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004192 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004193 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004194out:
Josef Bacikd52be812013-05-29 14:54:47 -04004195 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004196 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004197
Chris Mason39279cc2007-06-12 06:35:45 -04004198 return err;
4199}
4200
Chris Mason28f75a02015-02-04 06:59:29 -08004201static int truncate_space_check(struct btrfs_trans_handle *trans,
4202 struct btrfs_root *root,
4203 u64 bytes_deleted)
4204{
4205 int ret;
4206
4207 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
4208 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4209 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4210 if (!ret)
4211 trans->bytes_reserved += bytes_deleted;
4212 return ret;
4213
4214}
4215
Chris Mason323ac952008-10-01 19:05:46 -04004216/*
Chris Mason39279cc2007-06-12 06:35:45 -04004217 * this can truncate away extent items, csum items and directory items.
4218 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004219 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004220 *
4221 * csum items that cross the new i_size are truncated to the new size
4222 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004223 *
4224 * min_type is the minimum key type to truncate down to. If set to 0, this
4225 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004226 */
Yan, Zheng80825102009-11-12 09:35:36 +00004227int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4228 struct btrfs_root *root,
4229 struct inode *inode,
4230 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004231{
Chris Mason39279cc2007-06-12 06:35:45 -04004232 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004233 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004234 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004235 struct btrfs_key key;
4236 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004237 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004238 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004239 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004240 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004241 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004242 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004243 int found_extent;
4244 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004245 int pending_del_nr = 0;
4246 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004247 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004248 int ret;
4249 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004250 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004251 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004252 bool be_nice = 0;
4253 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004254 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004255
4256 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004257
Chris Mason28ed1342014-12-17 09:41:04 -08004258 /*
4259 * for non-free space inodes and ref cows, we want to back off from
4260 * time to time
4261 */
4262 if (!btrfs_is_free_space_inode(inode) &&
4263 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4264 be_nice = 1;
4265
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004266 path = btrfs_alloc_path();
4267 if (!path)
4268 return -ENOMEM;
4269 path->reada = -1;
4270
Josef Bacik5dc562c2012-08-17 13:14:17 -04004271 /*
4272 * We want to drop from the next block forward in case this new size is
4273 * not block aligned since we will be keeping the last block of the
4274 * extent just the way it is.
4275 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004276 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4277 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004278 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4279 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004280
Miao Xie16cdcec2011-04-22 18:12:22 +08004281 /*
4282 * This function is also used to drop the items in the log tree before
4283 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4284 * it is used to drop the loged items. So we shouldn't kill the delayed
4285 * items.
4286 */
4287 if (min_type == 0 && root == BTRFS_I(inode)->root)
4288 btrfs_kill_delayed_inode_items(inode);
4289
Li Zefan33345d012011-04-20 10:31:50 +08004290 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004291 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004292 key.type = (u8)-1;
4293
Chris Mason85e21ba2008-01-29 15:11:36 -05004294search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004295 /*
4296 * with a 16K leaf size and 128MB extents, you can actually queue
4297 * up a huge file in a single leaf. Most of the time that
4298 * bytes_deleted is > 0, it will be huge by the time we get here
4299 */
4300 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
4301 if (btrfs_should_end_transaction(trans, root)) {
4302 err = -EAGAIN;
4303 goto error;
4304 }
4305 }
4306
4307
Chris Masonb9473432009-03-13 11:00:37 -04004308 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004309 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004310 if (ret < 0) {
4311 err = ret;
4312 goto out;
4313 }
Chris Masond3977122009-01-05 21:25:51 -05004314
Chris Mason85e21ba2008-01-29 15:11:36 -05004315 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004316 /* there are no items in the tree for us to truncate, we're
4317 * done
4318 */
Yan, Zheng80825102009-11-12 09:35:36 +00004319 if (path->slots[0] == 0)
4320 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004321 path->slots[0]--;
4322 }
4323
Chris Masond3977122009-01-05 21:25:51 -05004324 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004325 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004326 leaf = path->nodes[0];
4327 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004328 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004329
Li Zefan33345d012011-04-20 10:31:50 +08004330 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004331 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004332
Chris Mason85e21ba2008-01-29 15:11:36 -05004333 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004334 break;
4335
Chris Mason5f39d392007-10-15 16:14:19 -04004336 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004337 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004338 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004339 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004340 extent_type = btrfs_file_extent_type(leaf, fi);
4341 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004342 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004343 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004344 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004345 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004346 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004347 }
Yan008630c2007-11-07 13:31:09 -05004348 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004349 }
Yan, Zheng80825102009-11-12 09:35:36 +00004350 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004351 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004352 } else {
4353 if (item_end < new_size)
4354 break;
4355 if (found_key.offset >= new_size)
4356 del_item = 1;
4357 else
4358 del_item = 0;
4359 }
Chris Mason39279cc2007-06-12 06:35:45 -04004360 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004361 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004362 if (found_type != BTRFS_EXTENT_DATA_KEY)
4363 goto delete;
4364
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004365 if (del_item)
4366 last_size = found_key.offset;
4367 else
4368 last_size = new_size;
4369
Chris Mason179e29e2007-11-01 11:28:41 -04004370 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004371 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004372 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004373 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004374 u64 orig_num_bytes =
4375 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004376 extent_num_bytes = ALIGN(new_size -
4377 found_key.offset,
4378 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004379 btrfs_set_file_extent_num_bytes(leaf, fi,
4380 extent_num_bytes);
4381 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004382 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004383 if (test_bit(BTRFS_ROOT_REF_COWS,
4384 &root->state) &&
4385 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004386 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004387 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004388 } else {
Chris Masondb945352007-10-15 16:15:53 -04004389 extent_num_bytes =
4390 btrfs_file_extent_disk_num_bytes(leaf,
4391 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004392 extent_offset = found_key.offset -
4393 btrfs_file_extent_offset(leaf, fi);
4394
Chris Mason39279cc2007-06-12 06:35:45 -04004395 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004396 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004397 if (extent_start != 0) {
4398 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004399 if (test_bit(BTRFS_ROOT_REF_COWS,
4400 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004401 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004402 }
4403 }
Chris Mason90692182008-02-08 13:49:28 -05004404 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004405 /*
4406 * we can't truncate inline items that have had
4407 * special encodings
4408 */
4409 if (!del_item &&
4410 btrfs_file_extent_compression(leaf, fi) == 0 &&
4411 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4412 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004413 u32 size = new_size - found_key.offset;
4414
Miao Xie27cdeb72014-04-02 19:51:05 +08004415 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004416 inode_sub_bytes(inode, item_end + 1 -
4417 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004418
4419 /*
4420 * update the ram bytes to properly reflect
4421 * the new size of our item
4422 */
4423 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004424 size =
4425 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004426 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004427 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4428 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004429 inode_sub_bytes(inode, item_end + 1 -
4430 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004431 }
Chris Mason39279cc2007-06-12 06:35:45 -04004432 }
Chris Mason179e29e2007-11-01 11:28:41 -04004433delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004434 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004435 if (!pending_del_nr) {
4436 /* no pending yet, add ourselves */
4437 pending_del_slot = path->slots[0];
4438 pending_del_nr = 1;
4439 } else if (pending_del_nr &&
4440 path->slots[0] + 1 == pending_del_slot) {
4441 /* hop on the pending chunk */
4442 pending_del_nr++;
4443 pending_del_slot = path->slots[0];
4444 } else {
Chris Masond3977122009-01-05 21:25:51 -05004445 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004446 }
Chris Mason39279cc2007-06-12 06:35:45 -04004447 } else {
4448 break;
4449 }
Chris Mason28f75a02015-02-04 06:59:29 -08004450 should_throttle = 0;
4451
Miao Xie27cdeb72014-04-02 19:51:05 +08004452 if (found_extent &&
4453 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4454 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004455 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004456 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004457 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004458 extent_num_bytes, 0,
4459 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004460 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004461 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004462 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004463 btrfs_async_run_delayed_refs(root,
4464 trans->delayed_ref_updates * 2, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004465 if (be_nice) {
4466 if (truncate_space_check(trans, root,
4467 extent_num_bytes)) {
4468 should_end = 1;
4469 }
4470 if (btrfs_should_throttle_delayed_refs(trans,
4471 root)) {
4472 should_throttle = 1;
4473 }
4474 }
Chris Mason39279cc2007-06-12 06:35:45 -04004475 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004476
Yan, Zheng80825102009-11-12 09:35:36 +00004477 if (found_type == BTRFS_INODE_ITEM_KEY)
4478 break;
4479
4480 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004481 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004482 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004483 if (pending_del_nr) {
4484 ret = btrfs_del_items(trans, root, path,
4485 pending_del_slot,
4486 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004487 if (ret) {
4488 btrfs_abort_transaction(trans,
4489 root, ret);
4490 goto error;
4491 }
Yan, Zheng80825102009-11-12 09:35:36 +00004492 pending_del_nr = 0;
4493 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004494 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004495 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004496 unsigned long updates = trans->delayed_ref_updates;
4497 if (updates) {
4498 trans->delayed_ref_updates = 0;
4499 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4500 if (ret && !err)
4501 err = ret;
4502 }
4503 }
Chris Mason28f75a02015-02-04 06:59:29 -08004504 /*
4505 * if we failed to refill our space rsv, bail out
4506 * and let the transaction restart
4507 */
4508 if (should_end) {
4509 err = -EAGAIN;
4510 goto error;
4511 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004512 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004513 } else {
4514 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004515 }
Chris Mason39279cc2007-06-12 06:35:45 -04004516 }
Yan, Zheng80825102009-11-12 09:35:36 +00004517out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004518 if (pending_del_nr) {
4519 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4520 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004521 if (ret)
4522 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004523 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004524error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004525 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004526 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004527
Chris Mason39279cc2007-06-12 06:35:45 -04004528 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004529
Chris Mason28f75a02015-02-04 06:59:29 -08004530 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
Chris Mason28ed1342014-12-17 09:41:04 -08004531 unsigned long updates = trans->delayed_ref_updates;
4532 if (updates) {
4533 trans->delayed_ref_updates = 0;
4534 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4535 if (ret && !err)
4536 err = ret;
4537 }
4538 }
Yan, Zheng80825102009-11-12 09:35:36 +00004539 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004540}
4541
Chris Masona52d9a82007-08-27 16:49:44 -04004542/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004543 * btrfs_truncate_page - read, zero a chunk and write a page
4544 * @inode - inode that we're zeroing
4545 * @from - the offset to start zeroing
4546 * @len - the length to zero, 0 to zero the entire range respective to the
4547 * offset
4548 * @front - zero up to the offset instead of from the offset on
4549 *
4550 * This will find the page for the "from" offset and cow the page and zero the
4551 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004552 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004553int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4554 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004555{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004556 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004557 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004558 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4559 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004560 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004561 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004562 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004563 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4564 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4565 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004566 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004567 int ret = 0;
4568 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004569 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004570
Josef Bacik2aaa6652012-08-29 14:27:18 -04004571 if ((offset & (blocksize - 1)) == 0 &&
4572 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004573 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004574 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004575 if (ret)
4576 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004577
Chris Mason211c17f2008-05-15 09:13:45 -04004578again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004579 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004580 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004581 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004582 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004583 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004584 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004585
4586 page_start = page_offset(page);
4587 page_end = page_start + PAGE_CACHE_SIZE - 1;
4588
Chris Masona52d9a82007-08-27 16:49:44 -04004589 if (!PageUptodate(page)) {
4590 ret = btrfs_readpage(NULL, page);
4591 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004592 if (page->mapping != mapping) {
4593 unlock_page(page);
4594 page_cache_release(page);
4595 goto again;
4596 }
Chris Masona52d9a82007-08-27 16:49:44 -04004597 if (!PageUptodate(page)) {
4598 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004599 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004600 }
4601 }
Chris Mason211c17f2008-05-15 09:13:45 -04004602 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004603
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004604 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004605 set_page_extent_mapped(page);
4606
4607 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4608 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004609 unlock_extent_cached(io_tree, page_start, page_end,
4610 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004611 unlock_page(page);
4612 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004613 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004614 btrfs_put_ordered_extent(ordered);
4615 goto again;
4616 }
4617
Josef Bacik2ac55d42010-02-03 19:33:23 +00004618 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004619 EXTENT_DIRTY | EXTENT_DELALLOC |
4620 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004621 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004622
Josef Bacik2ac55d42010-02-03 19:33:23 +00004623 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4624 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004625 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004626 unlock_extent_cached(io_tree, page_start, page_end,
4627 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004628 goto out_unlock;
4629 }
4630
Chris Masone6dcd2d2008-07-17 12:53:50 -04004631 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004632 if (!len)
4633 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004634 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004635 if (front)
4636 memset(kaddr, 0, offset);
4637 else
4638 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004639 flush_dcache_page(page);
4640 kunmap(page);
4641 }
Chris Mason247e7432008-07-17 12:53:51 -04004642 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004643 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004644 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4645 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004646
Chris Mason89642222008-07-24 09:41:53 -04004647out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004648 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004649 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004650 unlock_page(page);
4651 page_cache_release(page);
4652out:
4653 return ret;
4654}
4655
Josef Bacik16e75492013-10-22 12:18:51 -04004656static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4657 u64 offset, u64 len)
4658{
4659 struct btrfs_trans_handle *trans;
4660 int ret;
4661
4662 /*
4663 * Still need to make sure the inode looks like it's been updated so
4664 * that any holes get logged if we fsync.
4665 */
4666 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4667 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4668 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4669 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4670 return 0;
4671 }
4672
4673 /*
4674 * 1 - for the one we're dropping
4675 * 1 - for the one we're adding
4676 * 1 - for updating the inode.
4677 */
4678 trans = btrfs_start_transaction(root, 3);
4679 if (IS_ERR(trans))
4680 return PTR_ERR(trans);
4681
4682 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4683 if (ret) {
4684 btrfs_abort_transaction(trans, root, ret);
4685 btrfs_end_transaction(trans, root);
4686 return ret;
4687 }
4688
4689 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4690 0, 0, len, 0, len, 0, 0, 0);
4691 if (ret)
4692 btrfs_abort_transaction(trans, root, ret);
4693 else
4694 btrfs_update_inode(trans, root, inode);
4695 btrfs_end_transaction(trans, root);
4696 return ret;
4697}
4698
Josef Bacik695a0d02011-03-04 15:46:53 -05004699/*
4700 * This function puts in dummy file extents for the area we're creating a hole
4701 * for. So if we are truncating this file to a larger size we need to insert
4702 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4703 * the range between oldsize and size
4704 */
Josef Bacika41ad392011-01-31 15:30:16 -05004705int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004706{
Yan Zheng9036c102008-10-30 14:19:41 -04004707 struct btrfs_root *root = BTRFS_I(inode)->root;
4708 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004709 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004710 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004711 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004712 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4713 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004714 u64 last_byte;
4715 u64 cur_offset;
4716 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004717 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004718
Josef Bacika71754f2013-06-17 17:14:39 -04004719 /*
4720 * If our size started in the middle of a page we need to zero out the
4721 * rest of the page before we expand the i_size, otherwise we could
4722 * expose stale data.
4723 */
4724 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4725 if (err)
4726 return err;
4727
Yan Zheng9036c102008-10-30 14:19:41 -04004728 if (size <= hole_start)
4729 return 0;
4730
Yan Zheng9036c102008-10-30 14:19:41 -04004731 while (1) {
4732 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004733
Josef Bacik2ac55d42010-02-03 19:33:23 +00004734 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004735 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004736 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4737 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004738 if (!ordered)
4739 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004740 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4741 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004742 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004743 btrfs_put_ordered_extent(ordered);
4744 }
4745
Yan Zheng9036c102008-10-30 14:19:41 -04004746 cur_offset = hole_start;
4747 while (1) {
4748 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4749 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004750 if (IS_ERR(em)) {
4751 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004752 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004753 break;
4754 }
Yan Zheng9036c102008-10-30 14:19:41 -04004755 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004756 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004757 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004758 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004759 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004760
Josef Bacik16e75492013-10-22 12:18:51 -04004761 err = maybe_insert_hole(root, inode, cur_offset,
4762 hole_size);
4763 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004764 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004765 btrfs_drop_extent_cache(inode, cur_offset,
4766 cur_offset + hole_size - 1, 0);
4767 hole_em = alloc_extent_map();
4768 if (!hole_em) {
4769 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4770 &BTRFS_I(inode)->runtime_flags);
4771 goto next;
4772 }
4773 hole_em->start = cur_offset;
4774 hole_em->len = hole_size;
4775 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004776
Josef Bacik5dc562c2012-08-17 13:14:17 -04004777 hole_em->block_start = EXTENT_MAP_HOLE;
4778 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004779 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004780 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004781 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4782 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004783 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004784
4785 while (1) {
4786 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004787 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004788 write_unlock(&em_tree->lock);
4789 if (err != -EEXIST)
4790 break;
4791 btrfs_drop_extent_cache(inode, cur_offset,
4792 cur_offset +
4793 hole_size - 1, 0);
4794 }
4795 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004796 }
Josef Bacik16e75492013-10-22 12:18:51 -04004797next:
Yan Zheng9036c102008-10-30 14:19:41 -04004798 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004799 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004800 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004801 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004802 break;
4803 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004804 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004805 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4806 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004807 return err;
4808}
4809
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004810static int wait_snapshoting_atomic_t(atomic_t *a)
4811{
4812 schedule();
4813 return 0;
4814}
4815
4816static void wait_for_snapshot_creation(struct btrfs_root *root)
4817{
4818 while (true) {
4819 int ret;
4820
4821 ret = btrfs_start_write_no_snapshoting(root);
4822 if (ret)
4823 break;
4824 wait_on_atomic_t(&root->will_be_snapshoted,
4825 wait_snapshoting_atomic_t,
4826 TASK_UNINTERRUPTIBLE);
4827 }
4828}
4829
Eric Sandeen3972f262013-01-12 02:57:22 +00004830static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004831{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004832 struct btrfs_root *root = BTRFS_I(inode)->root;
4833 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004834 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004835 loff_t newsize = attr->ia_size;
4836 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004837 int ret;
4838
Eric Sandeen3972f262013-01-12 02:57:22 +00004839 /*
4840 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4841 * special case where we need to update the times despite not having
4842 * these flags set. For all other operations the VFS set these flags
4843 * explicitly if it wants a timestamp update.
4844 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004845 if (newsize != oldsize) {
4846 inode_inc_iversion(inode);
4847 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4848 inode->i_ctime = inode->i_mtime =
4849 current_fs_time(inode->i_sb);
4850 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004851
Josef Bacika41ad392011-01-31 15:30:16 -05004852 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004853 truncate_pagecache(inode, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004854 /*
4855 * Don't do an expanding truncate while snapshoting is ongoing.
4856 * This is to ensure the snapshot captures a fully consistent
4857 * state of this file - if the snapshot captures this expanding
4858 * truncation, it must capture all writes that happened before
4859 * this truncation.
4860 */
4861 wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004862 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004863 if (ret) {
4864 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004865 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004866 }
Yan, Zheng80825102009-11-12 09:35:36 +00004867
Miao Xief4a2f4c2011-12-14 20:12:01 -05004868 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004869 if (IS_ERR(trans)) {
4870 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004871 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004872 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004873
4874 i_size_write(inode, newsize);
4875 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4876 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004877 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004878 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004879 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004880
Josef Bacika41ad392011-01-31 15:30:16 -05004881 /*
4882 * We're truncating a file that used to have good data down to
4883 * zero. Make sure it gets into the ordered flush list so that
4884 * any new writes get down to disk quickly.
4885 */
4886 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004887 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4888 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004889
Josef Bacikf3fe8202013-01-07 17:03:21 -05004890 /*
4891 * 1 for the orphan item we're going to add
4892 * 1 for the orphan item deletion.
4893 */
4894 trans = btrfs_start_transaction(root, 2);
4895 if (IS_ERR(trans))
4896 return PTR_ERR(trans);
4897
4898 /*
4899 * We need to do this in case we fail at _any_ point during the
4900 * actual truncate. Once we do the truncate_setsize we could
4901 * invalidate pages which forces any outstanding ordered io to
4902 * be instantly completed which will give us extents that need
4903 * to be truncated. If we fail to get an orphan inode down we
4904 * could have left over extents that were never meant to live,
4905 * so we need to garuntee from this point on that everything
4906 * will be consistent.
4907 */
4908 ret = btrfs_orphan_add(trans, inode);
4909 btrfs_end_transaction(trans, root);
4910 if (ret)
4911 return ret;
4912
Josef Bacika41ad392011-01-31 15:30:16 -05004913 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4914 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004915
4916 /* Disable nonlocked read DIO to avoid the end less truncate */
4917 btrfs_inode_block_unlocked_dio(inode);
4918 inode_dio_wait(inode);
4919 btrfs_inode_resume_unlocked_dio(inode);
4920
Josef Bacika41ad392011-01-31 15:30:16 -05004921 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004922 if (ret && inode->i_nlink) {
4923 int err;
4924
4925 /*
4926 * failed to truncate, disk_i_size is only adjusted down
4927 * as we remove extents, so it should represent the true
4928 * size of the inode, so reset the in memory size and
4929 * delete our orphan entry.
4930 */
4931 trans = btrfs_join_transaction(root);
4932 if (IS_ERR(trans)) {
4933 btrfs_orphan_del(NULL, inode);
4934 return ret;
4935 }
4936 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4937 err = btrfs_orphan_del(trans, inode);
4938 if (err)
4939 btrfs_abort_transaction(trans, root, err);
4940 btrfs_end_transaction(trans, root);
4941 }
Yan, Zheng80825102009-11-12 09:35:36 +00004942 }
4943
Josef Bacika41ad392011-01-31 15:30:16 -05004944 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004945}
4946
Chris Mason39279cc2007-06-12 06:35:45 -04004947static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4948{
David Howells2b0143b2015-03-17 22:25:59 +00004949 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004950 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004951 int err;
4952
Li Zefanb83cc962010-12-20 16:04:08 +08004953 if (btrfs_root_readonly(root))
4954 return -EROFS;
4955
Chris Mason39279cc2007-06-12 06:35:45 -04004956 err = inode_change_ok(inode, attr);
4957 if (err)
4958 return err;
4959
Chris Mason5a3f23d2009-03-31 13:27:11 -04004960 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004961 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004962 if (err)
4963 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004964 }
Yan Zheng9036c102008-10-30 14:19:41 -04004965
Christoph Hellwig10257742010-06-04 11:30:02 +02004966 if (attr->ia_valid) {
4967 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004968 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004969 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004970
Josef Bacik22c44fe2011-11-30 10:45:38 -05004971 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004972 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004973 }
4974
Chris Mason39279cc2007-06-12 06:35:45 -04004975 return err;
4976}
Chris Mason61295eb2008-01-14 16:24:38 -05004977
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004978/*
4979 * While truncating the inode pages during eviction, we get the VFS calling
4980 * btrfs_invalidatepage() against each page of the inode. This is slow because
4981 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4982 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4983 * extent_state structures over and over, wasting lots of time.
4984 *
4985 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4986 * those expensive operations on a per page basis and do only the ordered io
4987 * finishing, while we release here the extent_map and extent_state structures,
4988 * without the excessive merging and splitting.
4989 */
4990static void evict_inode_truncate_pages(struct inode *inode)
4991{
4992 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4993 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4994 struct rb_node *node;
4995
4996 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004997 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004998
4999 write_lock(&map_tree->lock);
5000 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5001 struct extent_map *em;
5002
5003 node = rb_first(&map_tree->map);
5004 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005005 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5006 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005007 remove_extent_mapping(map_tree, em);
5008 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005009 if (need_resched()) {
5010 write_unlock(&map_tree->lock);
5011 cond_resched();
5012 write_lock(&map_tree->lock);
5013 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005014 }
5015 write_unlock(&map_tree->lock);
5016
Filipe Manana6ca07092015-05-26 00:55:42 +01005017 /*
5018 * Keep looping until we have no more ranges in the io tree.
5019 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005020 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5021 * still in progress (unlocked the pages in the bio but did not yet
5022 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005023 * ranges can still be locked and eviction started because before
5024 * submitting those bios, which are executed by a separate task (work
5025 * queue kthread), inode references (inode->i_count) were not taken
5026 * (which would be dropped in the end io callback of each bio).
5027 * Therefore here we effectively end up waiting for those bios and
5028 * anyone else holding locked ranges without having bumped the inode's
5029 * reference count - if we don't do it, when they access the inode's
5030 * io_tree to unlock a range it may be too late, leading to an
5031 * use-after-free issue.
5032 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005033 spin_lock(&io_tree->lock);
5034 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5035 struct extent_state *state;
5036 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005037 u64 start;
5038 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005039
5040 node = rb_first(&io_tree->state);
5041 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005042 start = state->start;
5043 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005044 spin_unlock(&io_tree->lock);
5045
Filipe Manana6ca07092015-05-26 00:55:42 +01005046 lock_extent_bits(io_tree, start, end, 0, &cached_state);
5047 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005048 EXTENT_LOCKED | EXTENT_DIRTY |
5049 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5050 EXTENT_DEFRAG, 1, 1,
5051 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005052
Filipe Manana7064dd52014-08-08 02:47:05 +01005053 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005054 spin_lock(&io_tree->lock);
5055 }
5056 spin_unlock(&io_tree->lock);
5057}
5058
Al Virobd555972010-06-07 11:35:40 -04005059void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005060{
5061 struct btrfs_trans_handle *trans;
5062 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005063 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005064 int steal_from_global = 0;
Josef Bacik07127182011-08-19 10:29:59 -04005065 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04005066 int ret;
5067
liubo1abe9b82011-03-24 11:18:59 +00005068 trace_btrfs_inode_evict(inode);
5069
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005070 evict_inode_truncate_pages(inode);
5071
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005072 if (inode->i_nlink &&
5073 ((btrfs_root_refs(&root->root_item) != 0 &&
5074 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5075 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005076 goto no_delete;
5077
Chris Mason39279cc2007-06-12 06:35:45 -04005078 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005079 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005080 goto no_delete;
5081 }
Al Virobd555972010-06-07 11:35:40 -04005082 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04005083 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005084
Miao Xief6124962014-09-12 18:44:04 +08005085 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5086
Yan, Zhengc71bf092009-11-12 09:34:40 +00005087 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06005088 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005089 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005090 goto no_delete;
5091 }
5092
Yan, Zheng76dda932009-09-21 16:00:26 -04005093 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005094 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5095 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005096 goto no_delete;
5097 }
5098
Miao Xie0e8c36a2012-12-19 06:59:51 +00005099 ret = btrfs_commit_inode_delayed_inode(inode);
5100 if (ret) {
5101 btrfs_orphan_del(NULL, inode);
5102 goto no_delete;
5103 }
5104
Miao Xie66d8f3d2012-09-06 04:02:28 -06005105 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005106 if (!rsv) {
5107 btrfs_orphan_del(NULL, inode);
5108 goto no_delete;
5109 }
Josef Bacik4a338542011-08-29 11:01:31 -04005110 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005111 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005112 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005113
Chris Masondbe674a2008-07-17 12:54:05 -04005114 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005115
Josef Bacik4289a662011-08-05 13:22:24 -04005116 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005117 * This is a bit simpler than btrfs_truncate since we've already
5118 * reserved our space for our orphan item in the unlink, so we just
5119 * need to reserve some slack space in case we add bytes and update
5120 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005121 */
Yan, Zheng80825102009-11-12 09:35:36 +00005122 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005123 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5124 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005125
Josef Bacik726c35f2011-09-26 15:46:06 -04005126 /*
5127 * Try and steal from the global reserve since we will
5128 * likely not use this space anyway, we want to try as
5129 * hard as possible to get this to work.
5130 */
5131 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005132 steal_from_global++;
5133 else
5134 steal_from_global = 0;
5135 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005136
Josef Bacik3bce8762015-02-24 12:35:51 -08005137 /*
5138 * steal_from_global == 0: we reserved stuff, hooray!
5139 * steal_from_global == 1: we didn't reserve stuff, boo!
5140 * steal_from_global == 2: we've committed, still not a lot of
5141 * room but maybe we'll have room in the global reserve this
5142 * time.
5143 * steal_from_global == 3: abandon all hope!
5144 */
5145 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005146 btrfs_warn(root->fs_info,
5147 "Could not get space for a delete, will truncate on mount %d",
5148 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005149 btrfs_orphan_del(NULL, inode);
5150 btrfs_free_block_rsv(root, rsv);
5151 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005152 }
5153
Miao Xie0e8c36a2012-12-19 06:59:51 +00005154 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005155 if (IS_ERR(trans)) {
5156 btrfs_orphan_del(NULL, inode);
5157 btrfs_free_block_rsv(root, rsv);
5158 goto no_delete;
5159 }
5160
Josef Bacik3bce8762015-02-24 12:35:51 -08005161 /*
5162 * We can't just steal from the global reserve, we need tomake
5163 * sure there is room to do it, if not we need to commit and try
5164 * again.
5165 */
5166 if (steal_from_global) {
5167 if (!btrfs_check_space_for_delayed_refs(trans, root))
5168 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5169 min_size);
5170 else
5171 ret = -ENOSPC;
5172 }
5173
5174 /*
5175 * Couldn't steal from the global reserve, we have too much
5176 * pending stuff built up, commit the transaction and try it
5177 * again.
5178 */
5179 if (ret) {
5180 ret = btrfs_commit_transaction(trans, root);
5181 if (ret) {
5182 btrfs_orphan_del(NULL, inode);
5183 btrfs_free_block_rsv(root, rsv);
5184 goto no_delete;
5185 }
5186 continue;
5187 } else {
5188 steal_from_global = 0;
5189 }
5190
Josef Bacik4289a662011-08-05 13:22:24 -04005191 trans->block_rsv = rsv;
5192
Yan, Zhengd68fc572010-05-16 10:49:58 -04005193 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005194 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005195 break;
5196
Miao Xie8407aa42012-09-07 01:43:32 -06005197 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005198 btrfs_end_transaction(trans, root);
5199 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005200 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005201 }
5202
Josef Bacik4289a662011-08-05 13:22:24 -04005203 btrfs_free_block_rsv(root, rsv);
5204
Josef Bacik4ef31a42013-08-13 14:10:08 -04005205 /*
5206 * Errors here aren't a big deal, it just means we leave orphan items
5207 * in the tree. They will be cleaned up on the next mount.
5208 */
Yan, Zheng80825102009-11-12 09:35:36 +00005209 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005210 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005211 btrfs_orphan_del(trans, inode);
5212 } else {
5213 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005214 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005215
Josef Bacik4289a662011-08-05 13:22:24 -04005216 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005217 if (!(root == root->fs_info->tree_root ||
5218 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005219 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005220
Chris Mason54aa1f42007-06-22 14:16:25 -04005221 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005222 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005223no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005224 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005225 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005226 return;
Chris Mason39279cc2007-06-12 06:35:45 -04005227}
5228
5229/*
5230 * this returns the key found in the dir entry in the location pointer.
5231 * If no dir entries were found, location->objectid is 0.
5232 */
5233static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5234 struct btrfs_key *location)
5235{
5236 const char *name = dentry->d_name.name;
5237 int namelen = dentry->d_name.len;
5238 struct btrfs_dir_item *di;
5239 struct btrfs_path *path;
5240 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005241 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005242
5243 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005244 if (!path)
5245 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005246
Li Zefan33345d012011-04-20 10:31:50 +08005247 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005248 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005249 if (IS_ERR(di))
5250 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005251
David Sterbac7040052011-04-19 18:00:01 +02005252 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005253 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005254
Chris Mason5f39d392007-10-15 16:14:19 -04005255 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005256out:
Chris Mason39279cc2007-06-12 06:35:45 -04005257 btrfs_free_path(path);
5258 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005259out_err:
5260 location->objectid = 0;
5261 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005262}
5263
5264/*
5265 * when we hit a tree root in a directory, the btrfs part of the inode
5266 * needs to be changed to reflect the root directory of the tree root. This
5267 * is kind of like crossing a mount point.
5268 */
5269static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005270 struct inode *dir,
5271 struct dentry *dentry,
5272 struct btrfs_key *location,
5273 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005274{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005275 struct btrfs_path *path;
5276 struct btrfs_root *new_root;
5277 struct btrfs_root_ref *ref;
5278 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005279 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005280 int ret;
5281 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005282
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005283 path = btrfs_alloc_path();
5284 if (!path) {
5285 err = -ENOMEM;
5286 goto out;
5287 }
Chris Mason39279cc2007-06-12 06:35:45 -04005288
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005289 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005290 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5291 key.type = BTRFS_ROOT_REF_KEY;
5292 key.offset = location->objectid;
5293
5294 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5295 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005296 if (ret) {
5297 if (ret < 0)
5298 err = ret;
5299 goto out;
5300 }
Chris Mason39279cc2007-06-12 06:35:45 -04005301
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005302 leaf = path->nodes[0];
5303 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005304 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005305 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5306 goto out;
5307
5308 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5309 (unsigned long)(ref + 1),
5310 dentry->d_name.len);
5311 if (ret)
5312 goto out;
5313
David Sterbab3b4aa72011-04-21 01:20:15 +02005314 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005315
5316 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5317 if (IS_ERR(new_root)) {
5318 err = PTR_ERR(new_root);
5319 goto out;
5320 }
5321
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005322 *sub_root = new_root;
5323 location->objectid = btrfs_root_dirid(&new_root->root_item);
5324 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005325 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005326 err = 0;
5327out:
5328 btrfs_free_path(path);
5329 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005330}
5331
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005332static void inode_tree_add(struct inode *inode)
5333{
5334 struct btrfs_root *root = BTRFS_I(inode)->root;
5335 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005336 struct rb_node **p;
5337 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005338 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005339 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005340
Al Viro1d3382cb2010-10-23 15:19:20 -04005341 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005342 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005343 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005344 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005345 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005346 while (*p) {
5347 parent = *p;
5348 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5349
Li Zefan33345d012011-04-20 10:31:50 +08005350 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005351 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005352 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005353 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005354 else {
5355 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005356 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005357 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005358 RB_CLEAR_NODE(parent);
5359 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005360 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005361 }
5362 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005363 rb_link_node(new, parent, p);
5364 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005365 spin_unlock(&root->inode_lock);
5366}
5367
5368static void inode_tree_del(struct inode *inode)
5369{
5370 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005371 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005372
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005373 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005374 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005375 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005376 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005377 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005378 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005379 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005380
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005381 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005382 synchronize_srcu(&root->fs_info->subvol_srcu);
5383 spin_lock(&root->inode_lock);
5384 empty = RB_EMPTY_ROOT(&root->inode_tree);
5385 spin_unlock(&root->inode_lock);
5386 if (empty)
5387 btrfs_add_dead_root(root);
5388 }
5389}
5390
Jeff Mahoney143bede2012-03-01 14:56:26 +01005391void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005392{
5393 struct rb_node *node;
5394 struct rb_node *prev;
5395 struct btrfs_inode *entry;
5396 struct inode *inode;
5397 u64 objectid = 0;
5398
Liu Bo7813b3d2014-02-10 17:37:25 +08005399 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5400 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005401
5402 spin_lock(&root->inode_lock);
5403again:
5404 node = root->inode_tree.rb_node;
5405 prev = NULL;
5406 while (node) {
5407 prev = node;
5408 entry = rb_entry(node, struct btrfs_inode, rb_node);
5409
Li Zefan33345d012011-04-20 10:31:50 +08005410 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005411 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005412 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005413 node = node->rb_right;
5414 else
5415 break;
5416 }
5417 if (!node) {
5418 while (prev) {
5419 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005420 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005421 node = prev;
5422 break;
5423 }
5424 prev = rb_next(prev);
5425 }
5426 }
5427 while (node) {
5428 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005429 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005430 inode = igrab(&entry->vfs_inode);
5431 if (inode) {
5432 spin_unlock(&root->inode_lock);
5433 if (atomic_read(&inode->i_count) > 1)
5434 d_prune_aliases(inode);
5435 /*
Al Viro45321ac2010-06-07 13:43:19 -04005436 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005437 * the inode cache when its usage count
5438 * hits zero.
5439 */
5440 iput(inode);
5441 cond_resched();
5442 spin_lock(&root->inode_lock);
5443 goto again;
5444 }
5445
5446 if (cond_resched_lock(&root->inode_lock))
5447 goto again;
5448
5449 node = rb_next(node);
5450 }
5451 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005452}
5453
Chris Masone02119d2008-09-05 16:13:11 -04005454static int btrfs_init_locked_inode(struct inode *inode, void *p)
5455{
5456 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005457 inode->i_ino = args->location->objectid;
5458 memcpy(&BTRFS_I(inode)->location, args->location,
5459 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005460 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005461 return 0;
5462}
5463
5464static int btrfs_find_actor(struct inode *inode, void *opaque)
5465{
5466 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005467 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005468 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005469}
5470
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005471static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005472 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005473 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005474{
5475 struct inode *inode;
5476 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005477 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005478
Chris Mason90d3e592014-01-09 17:28:00 -08005479 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005480 args.root = root;
5481
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005482 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005483 btrfs_init_locked_inode,
5484 (void *)&args);
5485 return inode;
5486}
5487
Balaji Rao1a54ef82008-07-21 02:01:04 +05305488/* Get an inode object given its location and corresponding root.
5489 * Returns in *is_new if the inode was read from disk
5490 */
5491struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005492 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305493{
5494 struct inode *inode;
5495
Chris Mason90d3e592014-01-09 17:28:00 -08005496 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305497 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005498 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305499
5500 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305501 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005502 if (!is_bad_inode(inode)) {
5503 inode_tree_add(inode);
5504 unlock_new_inode(inode);
5505 if (new)
5506 *new = 1;
5507 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005508 unlock_new_inode(inode);
5509 iput(inode);
5510 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005511 }
5512 }
5513
Balaji Rao1a54ef82008-07-21 02:01:04 +05305514 return inode;
5515}
5516
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005517static struct inode *new_simple_dir(struct super_block *s,
5518 struct btrfs_key *key,
5519 struct btrfs_root *root)
5520{
5521 struct inode *inode = new_inode(s);
5522
5523 if (!inode)
5524 return ERR_PTR(-ENOMEM);
5525
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005526 BTRFS_I(inode)->root = root;
5527 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005528 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005529
5530 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005531 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005532 inode->i_fop = &simple_dir_operations;
5533 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
chandan r9cc97d62012-07-04 12:48:07 +05305534 inode->i_mtime = CURRENT_TIME;
5535 inode->i_atime = inode->i_mtime;
5536 inode->i_ctime = inode->i_mtime;
5537 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005538
5539 return inode;
5540}
5541
Chris Mason3de45862008-11-17 21:02:50 -05005542struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005543{
Chris Masond3977122009-01-05 21:25:51 -05005544 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005545 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005546 struct btrfs_root *sub_root = root;
5547 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005548 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005549 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005550
5551 if (dentry->d_name.len > BTRFS_NAME_LEN)
5552 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005553
Jeff Layton39e3c952012-11-28 11:30:53 -05005554 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005555 if (ret < 0)
5556 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005557
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005558 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005559 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005560
5561 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005562 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005563 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005564 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005565
5566 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5567
Yan, Zheng76dda932009-09-21 16:00:26 -04005568 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005569 ret = fixup_tree_root_location(root, dir, dentry,
5570 &location, &sub_root);
5571 if (ret < 0) {
5572 if (ret != -ENOENT)
5573 inode = ERR_PTR(ret);
5574 else
5575 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5576 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005577 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005578 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005579 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5580
Julia Lawall34d19ba2011-01-24 19:55:19 +00005581 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005582 down_read(&root->fs_info->cleanup_work_sem);
5583 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005584 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005585 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005586 if (ret) {
5587 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005588 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005589 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005590 }
5591
Chris Mason3de45862008-11-17 21:02:50 -05005592 return inode;
5593}
5594
Nick Pigginfe15ce42011-01-07 17:49:23 +11005595static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005596{
5597 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005598 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005599
Li Zefan848cce02012-02-21 17:04:28 +08005600 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005601 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005602
Li Zefan848cce02012-02-21 17:04:28 +08005603 if (inode) {
5604 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005605 if (btrfs_root_refs(&root->root_item) == 0)
5606 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005607
5608 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5609 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005610 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005611 return 0;
5612}
5613
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005614static void btrfs_dentry_release(struct dentry *dentry)
5615{
Daeseok Youn944a4512014-04-14 15:37:02 +09005616 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005617}
5618
Chris Mason3de45862008-11-17 21:02:50 -05005619static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005620 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005621{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005622 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005623
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005624 inode = btrfs_lookup_dentry(dir, dentry);
5625 if (IS_ERR(inode)) {
5626 if (PTR_ERR(inode) == -ENOENT)
5627 inode = NULL;
5628 else
5629 return ERR_CAST(inode);
5630 }
5631
Al Viro41d28bc2014-10-12 22:24:21 -04005632 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005633}
5634
Miao Xie16cdcec2011-04-22 18:12:22 +08005635unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005636 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5637};
5638
Al Viro9cdda8d2013-05-22 16:48:09 -04005639static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005640{
Al Viro9cdda8d2013-05-22 16:48:09 -04005641 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005642 struct btrfs_root *root = BTRFS_I(inode)->root;
5643 struct btrfs_item *item;
5644 struct btrfs_dir_item *di;
5645 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005646 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005647 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005648 struct list_head ins_list;
5649 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005650 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005651 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005652 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005653 unsigned char d_type;
5654 int over = 0;
5655 u32 di_cur;
5656 u32 di_total;
5657 u32 di_len;
5658 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005659 char tmp_name[32];
5660 char *name_ptr;
5661 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005662 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005663
5664 /* FIXME, use a real flag for deciding about the key type */
5665 if (root->fs_info->tree_root == root)
5666 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005667
Al Viro9cdda8d2013-05-22 16:48:09 -04005668 if (!dir_emit_dots(file, ctx))
5669 return 0;
5670
David Woodhouse49593bf2008-08-17 17:08:36 +01005671 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005672 if (!path)
5673 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005674
Josef Bacik026fd312011-05-13 10:32:11 -04005675 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005676
Miao Xie16cdcec2011-04-22 18:12:22 +08005677 if (key_type == BTRFS_DIR_INDEX_KEY) {
5678 INIT_LIST_HEAD(&ins_list);
5679 INIT_LIST_HEAD(&del_list);
5680 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5681 }
5682
David Sterba962a2982014-06-04 18:41:45 +02005683 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005684 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005685 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005686
Chris Mason39279cc2007-06-12 06:35:45 -04005687 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5688 if (ret < 0)
5689 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005690
5691 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005692 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005693 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005694 if (slot >= btrfs_header_nritems(leaf)) {
5695 ret = btrfs_next_leaf(root, path);
5696 if (ret < 0)
5697 goto err;
5698 else if (ret > 0)
5699 break;
5700 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005701 }
Chris Mason3de45862008-11-17 21:02:50 -05005702
Ross Kirkdd3cc162013-09-16 15:58:09 +01005703 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005704 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5705
5706 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005707 break;
David Sterba962a2982014-06-04 18:41:45 +02005708 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005709 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005710 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005711 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005712 if (key_type == BTRFS_DIR_INDEX_KEY &&
5713 btrfs_should_delete_dir_index(&del_list,
5714 found_key.offset))
5715 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005716
Al Viro9cdda8d2013-05-22 16:48:09 -04005717 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005718 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005719
Chris Mason39279cc2007-06-12 06:35:45 -04005720 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5721 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005722 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005723
5724 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005725 struct btrfs_key location;
5726
Josef Bacik22a94d42011-03-16 16:47:17 -04005727 if (verify_dir_item(root, leaf, di))
5728 break;
5729
Chris Mason5f39d392007-10-15 16:14:19 -04005730 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005731 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005732 name_ptr = tmp_name;
5733 } else {
5734 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005735 if (!name_ptr) {
5736 ret = -ENOMEM;
5737 goto err;
5738 }
Chris Mason5f39d392007-10-15 16:14:19 -04005739 }
5740 read_extent_buffer(leaf, name_ptr,
5741 (unsigned long)(di + 1), name_len);
5742
5743 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5744 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005745
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005746
Chris Mason3de45862008-11-17 21:02:50 -05005747 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005748 * skip it.
5749 *
5750 * In contrast to old kernels, we insert the snapshot's
5751 * dir item and dir index after it has been created, so
5752 * we won't find a reference to our own snapshot. We
5753 * still keep the following code for backward
5754 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005755 */
5756 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5757 location.objectid == root->root_key.objectid) {
5758 over = 0;
5759 goto skip;
5760 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005761 over = !dir_emit(ctx, name_ptr, name_len,
5762 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005763
Chris Mason3de45862008-11-17 21:02:50 -05005764skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005765 if (name_ptr != tmp_name)
5766 kfree(name_ptr);
5767
Chris Mason39279cc2007-06-12 06:35:45 -04005768 if (over)
5769 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005770 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005771 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005772 di_cur += di_len;
5773 di = (struct btrfs_dir_item *)((char *)di + di_len);
5774 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005775next:
5776 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005777 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005778
Miao Xie16cdcec2011-04-22 18:12:22 +08005779 if (key_type == BTRFS_DIR_INDEX_KEY) {
5780 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005781 ctx->pos++;
5782 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005783 if (ret)
5784 goto nopos;
5785 }
5786
David Woodhouse49593bf2008-08-17 17:08:36 +01005787 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005788 ctx->pos++;
5789
5790 /*
5791 * Stop new entries from being returned after we return the last
5792 * entry.
5793 *
5794 * New directory entries are assigned a strictly increasing
5795 * offset. This means that new entries created during readdir
5796 * are *guaranteed* to be seen in the future by that readdir.
5797 * This has broken buggy programs which operate on names as
5798 * they're returned by readdir. Until we re-use freed offsets
5799 * we have this hack to stop new entries from being returned
5800 * under the assumption that they'll never reach this huge
5801 * offset.
5802 *
5803 * This is being careful not to overflow 32bit loff_t unless the
5804 * last entry requires it because doing so has broken 32bit apps
5805 * in the past.
5806 */
5807 if (key_type == BTRFS_DIR_INDEX_KEY) {
5808 if (ctx->pos >= INT_MAX)
5809 ctx->pos = LLONG_MAX;
5810 else
5811 ctx->pos = INT_MAX;
5812 }
Chris Mason39279cc2007-06-12 06:35:45 -04005813nopos:
5814 ret = 0;
5815err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005816 if (key_type == BTRFS_DIR_INDEX_KEY)
5817 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005818 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005819 return ret;
5820}
5821
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005822int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005823{
5824 struct btrfs_root *root = BTRFS_I(inode)->root;
5825 struct btrfs_trans_handle *trans;
5826 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005827 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005828
Josef Bacik72ac3c02012-05-23 14:13:11 -04005829 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005830 return 0;
5831
Liu Bo83eea1f2012-07-10 05:28:39 -06005832 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005833 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005834
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005835 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005836 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005837 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005838 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005839 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005840 if (IS_ERR(trans))
5841 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005842 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005843 }
5844 return ret;
5845}
5846
5847/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005848 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005849 * inode changes. But, it is most likely to find the inode in cache.
5850 * FIXME, needs more benchmarking...there are no reasons other than performance
5851 * to keep or drop this code.
5852 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005853static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005854{
5855 struct btrfs_root *root = BTRFS_I(inode)->root;
5856 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005857 int ret;
5858
Josef Bacik72ac3c02012-05-23 14:13:11 -04005859 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005860 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005861
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005862 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005863 if (IS_ERR(trans))
5864 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005865
5866 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005867 if (ret && ret == -ENOSPC) {
5868 /* whoops, lets try again with the full transaction */
5869 btrfs_end_transaction(trans, root);
5870 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005871 if (IS_ERR(trans))
5872 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005873
Chris Mason94b60442010-05-26 11:02:00 -04005874 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005875 }
Chris Mason39279cc2007-06-12 06:35:45 -04005876 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005877 if (BTRFS_I(inode)->delayed_node)
5878 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005879
5880 return ret;
5881}
5882
5883/*
5884 * This is a copy of file_update_time. We need this so we can return error on
5885 * ENOSPC for updating the inode in the case of file write and mmap writes.
5886 */
Josef Bacike41f9412012-03-26 09:46:47 -04005887static int btrfs_update_time(struct inode *inode, struct timespec *now,
5888 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005889{
Alexander Block2bc5565282012-06-15 09:49:33 +02005890 struct btrfs_root *root = BTRFS_I(inode)->root;
5891
5892 if (btrfs_root_readonly(root))
5893 return -EROFS;
5894
Josef Bacike41f9412012-03-26 09:46:47 -04005895 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005896 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005897 if (flags & S_CTIME)
5898 inode->i_ctime = *now;
5899 if (flags & S_MTIME)
5900 inode->i_mtime = *now;
5901 if (flags & S_ATIME)
5902 inode->i_atime = *now;
5903 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005904}
5905
Chris Masond352ac62008-09-29 15:18:18 -04005906/*
5907 * find the highest existing sequence number in a directory
5908 * and then set the in-memory index_cnt variable to reflect
5909 * free sequence numbers
5910 */
Josef Bacikaec74772008-07-24 12:12:38 -04005911static int btrfs_set_inode_index_count(struct inode *inode)
5912{
5913 struct btrfs_root *root = BTRFS_I(inode)->root;
5914 struct btrfs_key key, found_key;
5915 struct btrfs_path *path;
5916 struct extent_buffer *leaf;
5917 int ret;
5918
Li Zefan33345d012011-04-20 10:31:50 +08005919 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005920 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005921 key.offset = (u64)-1;
5922
5923 path = btrfs_alloc_path();
5924 if (!path)
5925 return -ENOMEM;
5926
5927 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5928 if (ret < 0)
5929 goto out;
5930 /* FIXME: we should be able to handle this */
5931 if (ret == 0)
5932 goto out;
5933 ret = 0;
5934
5935 /*
5936 * MAGIC NUMBER EXPLANATION:
5937 * since we search a directory based on f_pos we have to start at 2
5938 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5939 * else has to start at 2
5940 */
5941 if (path->slots[0] == 0) {
5942 BTRFS_I(inode)->index_cnt = 2;
5943 goto out;
5944 }
5945
5946 path->slots[0]--;
5947
5948 leaf = path->nodes[0];
5949 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5950
Li Zefan33345d012011-04-20 10:31:50 +08005951 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005952 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005953 BTRFS_I(inode)->index_cnt = 2;
5954 goto out;
5955 }
5956
5957 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5958out:
5959 btrfs_free_path(path);
5960 return ret;
5961}
5962
Chris Masond352ac62008-09-29 15:18:18 -04005963/*
5964 * helper to find a free sequence number in a given directory. This current
5965 * code is very simple, later versions will do smarter things in the btree
5966 */
Chris Mason3de45862008-11-17 21:02:50 -05005967int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005968{
5969 int ret = 0;
5970
5971 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005972 ret = btrfs_inode_delayed_dir_index_count(dir);
5973 if (ret) {
5974 ret = btrfs_set_inode_index_count(dir);
5975 if (ret)
5976 return ret;
5977 }
Josef Bacikaec74772008-07-24 12:12:38 -04005978 }
5979
Chris Mason00e4e6b2008-08-05 11:18:09 -04005980 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005981 BTRFS_I(dir)->index_cnt++;
5982
5983 return ret;
5984}
5985
Chris Masonb0d5d102014-09-08 13:08:51 -07005986static int btrfs_insert_inode_locked(struct inode *inode)
5987{
5988 struct btrfs_iget_args args;
5989 args.location = &BTRFS_I(inode)->location;
5990 args.root = BTRFS_I(inode)->root;
5991
5992 return insert_inode_locked4(inode,
5993 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5994 btrfs_find_actor, &args);
5995}
5996
Chris Mason39279cc2007-06-12 06:35:45 -04005997static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5998 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005999 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006000 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006001 u64 ref_objectid, u64 objectid,
6002 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006003{
6004 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006005 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006006 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006007 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006008 struct btrfs_inode_ref *ref;
6009 struct btrfs_key key[2];
6010 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006011 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006012 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006013 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006014
Chris Mason5f39d392007-10-15 16:14:19 -04006015 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006016 if (!path)
6017 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006018
Chris Mason39279cc2007-06-12 06:35:45 -04006019 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006020 if (!inode) {
6021 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006022 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006023 }
Chris Mason39279cc2007-06-12 06:35:45 -04006024
Li Zefan581bb052011-04-20 10:06:11 +08006025 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006026 * O_TMPFILE, set link count to 0, so that after this point,
6027 * we fill in an inode item with the correct link count.
6028 */
6029 if (!name)
6030 set_nlink(inode, 0);
6031
6032 /*
Li Zefan581bb052011-04-20 10:06:11 +08006033 * we have to initialize this early, so we can reclaim the inode
6034 * number if we fail afterwards in this function.
6035 */
6036 inode->i_ino = objectid;
6037
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006038 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006039 trace_btrfs_inode_request(dir);
6040
Chris Mason3de45862008-11-17 21:02:50 -05006041 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006042 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006043 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006044 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006045 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006046 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006047 } else if (dir) {
6048 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006049 }
6050 /*
6051 * index_cnt is ignored for everything but a dir,
6052 * btrfs_get_inode_index_count has an explanation for the magic
6053 * number
6054 */
6055 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006056 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006057 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006058 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006059 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006060
Josef Bacik5dc562c2012-08-17 13:14:17 -04006061 /*
6062 * We could have gotten an inode number from somebody who was fsynced
6063 * and then removed in this same transaction, so let's just set full
6064 * sync since it will be a full sync anyway and this will blow away the
6065 * old info in the log.
6066 */
6067 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6068
Chris Mason9c583092008-01-29 15:15:18 -05006069 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006070 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006071 key[0].offset = 0;
6072
Chris Mason9c583092008-01-29 15:15:18 -05006073 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006074
6075 if (name) {
6076 /*
6077 * Start new inodes with an inode_ref. This is slightly more
6078 * efficient for small numbers of hard links since they will
6079 * be packed into one item. Extended refs will kick in if we
6080 * add more hard links than can fit in the ref item.
6081 */
6082 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006083 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006084 key[1].offset = ref_objectid;
6085
6086 sizes[1] = name_len + sizeof(*ref);
6087 }
Chris Mason9c583092008-01-29 15:15:18 -05006088
Chris Masonb0d5d102014-09-08 13:08:51 -07006089 location = &BTRFS_I(inode)->location;
6090 location->objectid = objectid;
6091 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006092 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006093
6094 ret = btrfs_insert_inode_locked(inode);
6095 if (ret < 0)
6096 goto fail;
6097
Chris Masonb9473432009-03-13 11:00:37 -04006098 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006099 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006100 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006101 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006102
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006103 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006104 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306105
6106 inode->i_mtime = CURRENT_TIME;
6107 inode->i_atime = inode->i_mtime;
6108 inode->i_ctime = inode->i_mtime;
6109 BTRFS_I(inode)->i_otime = inode->i_mtime;
6110
Chris Mason5f39d392007-10-15 16:14:19 -04006111 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6112 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006113 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6114 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006115 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006116
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006117 if (name) {
6118 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6119 struct btrfs_inode_ref);
6120 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6121 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6122 ptr = (unsigned long)(ref + 1);
6123 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6124 }
Chris Mason9c583092008-01-29 15:15:18 -05006125
Chris Mason5f39d392007-10-15 16:14:19 -04006126 btrfs_mark_buffer_dirty(path->nodes[0]);
6127 btrfs_free_path(path);
6128
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006129 btrfs_inherit_iflags(inode, dir);
6130
Al Viro569254b2011-07-24 17:08:40 -04006131 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04006132 if (btrfs_test_opt(root, NODATASUM))
6133 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06006134 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006135 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6136 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006137 }
6138
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006139 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006140
6141 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006142 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006143
Alexander Block8ea05e32012-07-25 17:35:53 +02006144 btrfs_update_root_times(trans, root);
6145
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006146 ret = btrfs_inode_inherit_props(trans, inode, dir);
6147 if (ret)
6148 btrfs_err(root->fs_info,
6149 "error inheriting props for ino %llu (root %llu): %d",
6150 btrfs_ino(inode), root->root_key.objectid, ret);
6151
Chris Mason39279cc2007-06-12 06:35:45 -04006152 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006153
6154fail_unlock:
6155 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006156fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006157 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006158 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006159 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006160 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006161 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006162}
6163
6164static inline u8 btrfs_inode_type(struct inode *inode)
6165{
6166 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6167}
6168
Chris Masond352ac62008-09-29 15:18:18 -04006169/*
6170 * utility function to add 'inode' into 'parent_inode' with
6171 * a give name and a given sequence number.
6172 * if 'add_backref' is true, also insert a backref from the
6173 * inode to the parent directory.
6174 */
Chris Masone02119d2008-09-05 16:13:11 -04006175int btrfs_add_link(struct btrfs_trans_handle *trans,
6176 struct inode *parent_inode, struct inode *inode,
6177 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006178{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006179 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006180 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006181 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006182 u64 ino = btrfs_ino(inode);
6183 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006184
Li Zefan33345d012011-04-20 10:31:50 +08006185 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006186 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6187 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006188 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006189 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006190 key.offset = 0;
6191 }
Chris Mason39279cc2007-06-12 06:35:45 -04006192
Li Zefan33345d012011-04-20 10:31:50 +08006193 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006194 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6195 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006196 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006197 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006198 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6199 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006200 }
6201
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006202 /* Nothing to clean up yet */
6203 if (ret)
6204 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006205
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006206 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6207 parent_inode, &key,
6208 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006209 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006210 goto fail_dir_item;
6211 else if (ret) {
6212 btrfs_abort_transaction(trans, root, ret);
6213 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006214 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006215
6216 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6217 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006218 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006219 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
6220 ret = btrfs_update_inode(trans, root, parent_inode);
6221 if (ret)
6222 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006223 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006224
6225fail_dir_item:
6226 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6227 u64 local_index;
6228 int err;
6229 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6230 key.objectid, root->root_key.objectid,
6231 parent_ino, &local_index, name, name_len);
6232
6233 } else if (add_backref) {
6234 u64 local_index;
6235 int err;
6236
6237 err = btrfs_del_inode_ref(trans, root, name, name_len,
6238 ino, parent_ino, &local_index);
6239 }
6240 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006241}
6242
6243static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006244 struct inode *dir, struct dentry *dentry,
6245 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006246{
Josef Bacika1b075d2010-11-19 20:36:11 +00006247 int err = btrfs_add_link(trans, dir, inode,
6248 dentry->d_name.name, dentry->d_name.len,
6249 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006250 if (err > 0)
6251 err = -EEXIST;
6252 return err;
6253}
6254
Josef Bacik618e21d2007-07-11 10:18:17 -04006255static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006256 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006257{
6258 struct btrfs_trans_handle *trans;
6259 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006260 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006261 int err;
6262 int drop_inode = 0;
6263 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006264 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006265
6266 if (!new_valid_dev(rdev))
6267 return -EINVAL;
6268
Josef Bacik9ed74f22009-09-11 16:12:44 -04006269 /*
6270 * 2 for inode item and ref
6271 * 2 for dir items
6272 * 1 for xattr if selinux is on
6273 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006274 trans = btrfs_start_transaction(root, 5);
6275 if (IS_ERR(trans))
6276 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006277
Li Zefan581bb052011-04-20 10:06:11 +08006278 err = btrfs_find_free_ino(root, &objectid);
6279 if (err)
6280 goto out_unlock;
6281
Josef Bacikaec74772008-07-24 12:12:38 -04006282 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006283 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006284 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006285 if (IS_ERR(inode)) {
6286 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006287 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006288 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006289
Casey Schauflerad19db72011-12-15 10:09:07 -05006290 /*
6291 * If the active LSM wants to access the inode during
6292 * d_instantiate it needs these. Smack checks to see
6293 * if the filesystem supports xattrs by looking at the
6294 * ops vector.
6295 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006296 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006297 init_special_inode(inode, inode->i_mode, rdev);
6298
6299 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006300 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006301 goto out_unlock_inode;
6302
6303 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6304 if (err) {
6305 goto out_unlock_inode;
6306 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006307 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006308 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006309 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006310 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006311
Josef Bacik618e21d2007-07-11 10:18:17 -04006312out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006313 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006314 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006315 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006316 if (drop_inode) {
6317 inode_dec_link_count(inode);
6318 iput(inode);
6319 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006320 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006321
6322out_unlock_inode:
6323 drop_inode = 1;
6324 unlock_new_inode(inode);
6325 goto out_unlock;
6326
Josef Bacik618e21d2007-07-11 10:18:17 -04006327}
6328
Chris Mason39279cc2007-06-12 06:35:45 -04006329static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006330 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -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;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006335 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006336 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006337 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006338 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006339
Josef Bacik9ed74f22009-09-11 16:12:44 -04006340 /*
6341 * 2 for inode item and ref
6342 * 2 for dir items
6343 * 1 for xattr if selinux is on
6344 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006345 trans = btrfs_start_transaction(root, 5);
6346 if (IS_ERR(trans))
6347 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006348
Li Zefan581bb052011-04-20 10:06:11 +08006349 err = btrfs_find_free_ino(root, &objectid);
6350 if (err)
6351 goto out_unlock;
6352
Josef Bacikaec74772008-07-24 12:12:38 -04006353 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006354 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006355 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006356 if (IS_ERR(inode)) {
6357 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006358 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006359 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006360 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006361 /*
6362 * If the active LSM wants to access the inode during
6363 * d_instantiate it needs these. Smack checks to see
6364 * if the filesystem supports xattrs by looking at the
6365 * ops vector.
6366 */
6367 inode->i_fop = &btrfs_file_operations;
6368 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006369 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006370
6371 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6372 if (err)
6373 goto out_unlock_inode;
6374
6375 err = btrfs_update_inode(trans, root, inode);
6376 if (err)
6377 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006378
Josef Bacika1b075d2010-11-19 20:36:11 +00006379 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006380 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006381 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006382
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006383 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006384 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006385 d_instantiate(dentry, inode);
6386
Chris Mason39279cc2007-06-12 06:35:45 -04006387out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006388 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006389 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006390 inode_dec_link_count(inode);
6391 iput(inode);
6392 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006393 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006394 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006395 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006396
6397out_unlock_inode:
6398 unlock_new_inode(inode);
6399 goto out_unlock;
6400
Chris Mason39279cc2007-06-12 06:35:45 -04006401}
6402
6403static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6404 struct dentry *dentry)
6405{
6406 struct btrfs_trans_handle *trans;
6407 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006408 struct inode *inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006409 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006410 int err;
6411 int drop_inode = 0;
6412
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006413 /* do not allow sys_link's with other subvols of the same device */
6414 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006415 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006416
Mark Fashehf1863732012-08-08 11:32:27 -07006417 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006418 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006419
Chris Mason3de45862008-11-17 21:02:50 -05006420 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006421 if (err)
6422 goto fail;
6423
Yan, Zhenga22285a2010-05-16 10:48:46 -04006424 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006425 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006426 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006427 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006428 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006429 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006430 if (IS_ERR(trans)) {
6431 err = PTR_ERR(trans);
6432 goto fail;
6433 }
Chris Mason5f39d392007-10-15 16:14:19 -04006434
Miao Xie67de1172013-12-26 13:07:06 +08006435 /* There are several dir indexes for this inode, clear the cache. */
6436 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006437 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006438 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006439 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006440 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006441 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006442
Josef Bacika1b075d2010-11-19 20:36:11 +00006443 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006444
Yan, Zhenga5719522009-09-24 09:17:31 -04006445 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006446 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006447 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006448 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006449 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006450 if (err)
6451 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006452 if (inode->i_nlink == 1) {
6453 /*
6454 * If new hard link count is 1, it's a file created
6455 * with open(2) O_TMPFILE flag.
6456 */
6457 err = btrfs_orphan_del(trans, inode);
6458 if (err)
6459 goto fail;
6460 }
Al Viro08c422c2011-12-23 07:58:13 -05006461 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006462 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006463 }
Chris Mason39279cc2007-06-12 06:35:45 -04006464
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006465 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006466 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006467fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006468 if (drop_inode) {
6469 inode_dec_link_count(inode);
6470 iput(inode);
6471 }
Liu Bob53d3f52012-11-14 14:34:34 +00006472 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006473 return err;
6474}
6475
Al Viro18bb1db2011-07-26 01:41:39 -04006476static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006477{
Chris Masonb9d86662008-05-02 16:13:49 -04006478 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006479 struct btrfs_trans_handle *trans;
6480 struct btrfs_root *root = BTRFS_I(dir)->root;
6481 int err = 0;
6482 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006483 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006484 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006485
Josef Bacik9ed74f22009-09-11 16:12:44 -04006486 /*
6487 * 2 items for inode and ref
6488 * 2 items for dir items
6489 * 1 for xattr if selinux is on
6490 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006491 trans = btrfs_start_transaction(root, 5);
6492 if (IS_ERR(trans))
6493 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006494
Li Zefan581bb052011-04-20 10:06:11 +08006495 err = btrfs_find_free_ino(root, &objectid);
6496 if (err)
6497 goto out_fail;
6498
Josef Bacikaec74772008-07-24 12:12:38 -04006499 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006500 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006501 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006502 if (IS_ERR(inode)) {
6503 err = PTR_ERR(inode);
6504 goto out_fail;
6505 }
Chris Mason5f39d392007-10-15 16:14:19 -04006506
Chris Mason39279cc2007-06-12 06:35:45 -04006507 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006508 /* these must be set before we unlock the inode */
6509 inode->i_op = &btrfs_dir_inode_operations;
6510 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006511
Eric Paris2a7dba32011-02-01 11:05:39 -05006512 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006513 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006514 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006515
Chris Masondbe674a2008-07-17 12:54:05 -04006516 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006517 err = btrfs_update_inode(trans, root, inode);
6518 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006519 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006520
Josef Bacika1b075d2010-11-19 20:36:11 +00006521 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6522 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006523 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006524 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006525
Chris Mason39279cc2007-06-12 06:35:45 -04006526 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006527 /*
6528 * mkdir is special. We're unlocking after we call d_instantiate
6529 * to avoid a race with nfsd calling d_instantiate.
6530 */
6531 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006532 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006533
6534out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006535 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006536 if (drop_on_err) {
6537 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006538 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006539 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006540 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006541 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006542 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006543
6544out_fail_inode:
6545 unlock_new_inode(inode);
6546 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006547}
6548
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006549/* Find next extent map of a given extent map, caller needs to ensure locks */
6550static struct extent_map *next_extent_map(struct extent_map *em)
6551{
6552 struct rb_node *next;
6553
6554 next = rb_next(&em->rb_node);
6555 if (!next)
6556 return NULL;
6557 return container_of(next, struct extent_map, rb_node);
6558}
6559
6560static struct extent_map *prev_extent_map(struct extent_map *em)
6561{
6562 struct rb_node *prev;
6563
6564 prev = rb_prev(&em->rb_node);
6565 if (!prev)
6566 return NULL;
6567 return container_of(prev, struct extent_map, rb_node);
6568}
6569
Chris Masond352ac62008-09-29 15:18:18 -04006570/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006571 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006572 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006573 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006574 */
Chris Mason3b951512008-04-17 11:29:12 -04006575static int merge_extent_mapping(struct extent_map_tree *em_tree,
6576 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006577 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006578 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006579{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006580 struct extent_map *prev;
6581 struct extent_map *next;
6582 u64 start;
6583 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006584 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006585
Chris Masone6dcd2d2008-07-17 12:53:50 -04006586 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006587
6588 if (existing->start > map_start) {
6589 next = existing;
6590 prev = prev_extent_map(next);
6591 } else {
6592 prev = existing;
6593 next = next_extent_map(prev);
6594 }
6595
6596 start = prev ? extent_map_end(prev) : em->start;
6597 start = max_t(u64, start, em->start);
6598 end = next ? next->start : extent_map_end(em);
6599 end = min_t(u64, end, extent_map_end(em));
6600 start_diff = start - em->start;
6601 em->start = start;
6602 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006603 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6604 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006605 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006606 em->block_len -= start_diff;
6607 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006608 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006609}
6610
Chris Masonc8b97812008-10-29 14:49:59 -04006611static noinline int uncompress_inline(struct btrfs_path *path,
6612 struct inode *inode, struct page *page,
6613 size_t pg_offset, u64 extent_offset,
6614 struct btrfs_file_extent_item *item)
6615{
6616 int ret;
6617 struct extent_buffer *leaf = path->nodes[0];
6618 char *tmp;
6619 size_t max_size;
6620 unsigned long inline_size;
6621 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006622 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006623
6624 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006625 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006626 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6627 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006628 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006629 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006630 if (!tmp)
6631 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006632 ptr = btrfs_file_extent_inline_start(item);
6633
6634 read_extent_buffer(leaf, tmp, ptr, inline_size);
6635
Chris Mason5b050f02008-11-11 09:34:41 -05006636 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006637 ret = btrfs_decompress(compress_type, tmp, page,
6638 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006639 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006640 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006641}
6642
Chris Masond352ac62008-09-29 15:18:18 -04006643/*
6644 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006645 * the ugly parts come from merging extents from the disk with the in-ram
6646 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006647 * where the in-ram extents might be locked pending data=ordered completion.
6648 *
6649 * This also copies inline extents directly into the page.
6650 */
Chris Masond3977122009-01-05 21:25:51 -05006651
Chris Masona52d9a82007-08-27 16:49:44 -04006652struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006653 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006654 int create)
6655{
6656 int ret;
6657 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006658 u64 extent_start = 0;
6659 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006660 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006661 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006662 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006663 struct btrfs_root *root = BTRFS_I(inode)->root;
6664 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006665 struct extent_buffer *leaf;
6666 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006667 struct extent_map *em = NULL;
6668 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006669 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006670 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006671 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006672
Chris Masona52d9a82007-08-27 16:49:44 -04006673again:
Chris Mason890871b2009-09-02 16:24:52 -04006674 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006675 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006676 if (em)
6677 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006678 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006679
Chris Masona52d9a82007-08-27 16:49:44 -04006680 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006681 if (em->start > start || em->start + em->len <= start)
6682 free_extent_map(em);
6683 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006684 free_extent_map(em);
6685 else
6686 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006687 }
David Sterba172ddd62011-04-21 00:48:27 +02006688 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006689 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006690 err = -ENOMEM;
6691 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006692 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006693 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006694 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006695 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006696 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006697 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006698
6699 if (!path) {
6700 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006701 if (!path) {
6702 err = -ENOMEM;
6703 goto out;
6704 }
6705 /*
6706 * Chances are we'll be called again, so go ahead and do
6707 * readahead
6708 */
6709 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006710 }
6711
Chris Mason179e29e2007-11-01 11:28:41 -04006712 ret = btrfs_lookup_file_extent(trans, root, path,
6713 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006714 if (ret < 0) {
6715 err = ret;
6716 goto out;
6717 }
6718
6719 if (ret != 0) {
6720 if (path->slots[0] == 0)
6721 goto not_found;
6722 path->slots[0]--;
6723 }
6724
Chris Mason5f39d392007-10-15 16:14:19 -04006725 leaf = path->nodes[0];
6726 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006727 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006728 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006729 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006730 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006731 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006732 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006733 /*
6734 * If we backup past the first extent we want to move forward
6735 * and see if there is an extent in front of us, otherwise we'll
6736 * say there is a hole for our whole search range which can
6737 * cause problems.
6738 */
6739 extent_end = start;
6740 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006741 }
6742
Chris Mason5f39d392007-10-15 16:14:19 -04006743 found_type = btrfs_file_extent_type(leaf, item);
6744 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006745 if (found_type == BTRFS_FILE_EXTENT_REG ||
6746 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006747 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006748 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006749 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6750 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006751 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006752 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006753 }
Josef Bacik25a50342013-10-14 12:08:38 -04006754next:
Yan Zheng9036c102008-10-30 14:19:41 -04006755 if (start >= extent_end) {
6756 path->slots[0]++;
6757 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6758 ret = btrfs_next_leaf(root, path);
6759 if (ret < 0) {
6760 err = ret;
6761 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006762 }
Yan Zheng9036c102008-10-30 14:19:41 -04006763 if (ret > 0)
6764 goto not_found;
6765 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006766 }
Yan Zheng9036c102008-10-30 14:19:41 -04006767 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6768 if (found_key.objectid != objectid ||
6769 found_key.type != BTRFS_EXTENT_DATA_KEY)
6770 goto not_found;
6771 if (start + len <= found_key.offset)
6772 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006773 if (start > found_key.offset)
6774 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006775 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006776 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006777 em->len = found_key.offset - start;
6778 goto not_found_em;
6779 }
6780
Filipe Manana7ffbb592014-06-09 03:48:05 +01006781 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6782
Yan Zhengd899e052008-10-30 14:25:28 -04006783 if (found_type == BTRFS_FILE_EXTENT_REG ||
6784 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006785 goto insert;
6786 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006787 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006788 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006789 size_t size;
6790 size_t extent_offset;
6791 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006792
Filipe Manana7ffbb592014-06-09 03:48:05 +01006793 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006794 goto out;
Yan689f9342007-10-29 11:41:07 -04006795
Chris Mason514ac8a2014-01-03 21:07:00 -08006796 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006797 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006798 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006799 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006800 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006801 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006802 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006803 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006804 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006805 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006806 if (btrfs_file_extent_compression(leaf, item) !=
6807 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006808 ret = uncompress_inline(path, inode, page,
6809 pg_offset,
6810 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006811 if (ret) {
6812 err = ret;
6813 goto out;
6814 }
Chris Masonc8b97812008-10-29 14:49:59 -04006815 } else {
6816 map = kmap(page);
6817 read_extent_buffer(leaf, map + pg_offset, ptr,
6818 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006819 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6820 memset(map + pg_offset + copy_size, 0,
6821 PAGE_CACHE_SIZE - pg_offset -
6822 copy_size);
6823 }
Chris Masonc8b97812008-10-29 14:49:59 -04006824 kunmap(page);
6825 }
Chris Mason179e29e2007-11-01 11:28:41 -04006826 flush_dcache_page(page);
6827 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006828 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006829 if (!trans) {
6830 kunmap(page);
6831 free_extent_map(em);
6832 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006833
David Sterbab3b4aa72011-04-21 01:20:15 +02006834 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006835 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006836
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006837 if (IS_ERR(trans))
6838 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006839 goto again;
6840 }
Chris Masonc8b97812008-10-29 14:49:59 -04006841 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006842 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006843 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006844 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006845 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006846 }
Chris Masond1310b22008-01-24 16:13:08 -05006847 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006848 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006849 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006850 }
6851not_found:
6852 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006853 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006854 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006855not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006856 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006857 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006858insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006859 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006860 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006861 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006862 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006863 err = -EIO;
6864 goto out;
6865 }
Chris Masond1310b22008-01-24 16:13:08 -05006866
6867 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006868 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006869 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006870 /* it is possible that someone inserted the extent into the tree
6871 * while we had the lock dropped. It is also possible that
6872 * an overlapping map exists in the tree
6873 */
Chris Masona52d9a82007-08-27 16:49:44 -04006874 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006875 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006876
6877 ret = 0;
6878
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006879 existing = search_extent_mapping(em_tree, start, len);
6880 /*
6881 * existing will always be non-NULL, since there must be
6882 * extent causing the -EEXIST.
6883 */
6884 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006885 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006886 /*
6887 * The existing extent map is the one nearest to
6888 * the [start, start + len) range which overlaps
6889 */
6890 err = merge_extent_mapping(em_tree, existing,
6891 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006892 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006893 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006894 free_extent_map(em);
6895 em = NULL;
6896 }
6897 } else {
6898 free_extent_map(em);
6899 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006900 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006901 }
Chris Masona52d9a82007-08-27 16:49:44 -04006902 }
Chris Mason890871b2009-09-02 16:24:52 -04006903 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006904out:
liubo1abe9b82011-03-24 11:18:59 +00006905
Steven Rostedt4cd85872013-11-14 22:57:29 -05006906 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006907
Chris Masonf4219502008-07-22 11:18:09 -04006908 if (path)
6909 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006910 if (trans) {
6911 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006912 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006913 err = ret;
6914 }
Chris Masona52d9a82007-08-27 16:49:44 -04006915 if (err) {
6916 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006917 return ERR_PTR(err);
6918 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006919 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006920 return em;
6921}
6922
Chris Masonec29ed52011-02-23 16:23:20 -05006923struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6924 size_t pg_offset, u64 start, u64 len,
6925 int create)
6926{
6927 struct extent_map *em;
6928 struct extent_map *hole_em = NULL;
6929 u64 range_start = start;
6930 u64 end;
6931 u64 found;
6932 u64 found_end;
6933 int err = 0;
6934
6935 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6936 if (IS_ERR(em))
6937 return em;
6938 if (em) {
6939 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006940 * if our em maps to
6941 * - a hole or
6942 * - a pre-alloc extent,
6943 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006944 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006945 if (em->block_start != EXTENT_MAP_HOLE &&
6946 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006947 return em;
6948 else
6949 hole_em = em;
6950 }
6951
6952 /* check to see if we've wrapped (len == -1 or similar) */
6953 end = start + len;
6954 if (end < start)
6955 end = (u64)-1;
6956 else
6957 end -= 1;
6958
6959 em = NULL;
6960
6961 /* ok, we didn't find anything, lets look for delalloc */
6962 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6963 end, len, EXTENT_DELALLOC, 1);
6964 found_end = range_start + found;
6965 if (found_end < range_start)
6966 found_end = (u64)-1;
6967
6968 /*
6969 * we didn't find anything useful, return
6970 * the original results from get_extent()
6971 */
6972 if (range_start > end || found_end <= start) {
6973 em = hole_em;
6974 hole_em = NULL;
6975 goto out;
6976 }
6977
6978 /* adjust the range_start to make sure it doesn't
6979 * go backwards from the start they passed in
6980 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306981 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006982 found = found_end - range_start;
6983
6984 if (found > 0) {
6985 u64 hole_start = start;
6986 u64 hole_len = len;
6987
David Sterba172ddd62011-04-21 00:48:27 +02006988 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006989 if (!em) {
6990 err = -ENOMEM;
6991 goto out;
6992 }
6993 /*
6994 * when btrfs_get_extent can't find anything it
6995 * returns one huge hole
6996 *
6997 * make sure what it found really fits our range, and
6998 * adjust to make sure it is based on the start from
6999 * the caller
7000 */
7001 if (hole_em) {
7002 u64 calc_end = extent_map_end(hole_em);
7003
7004 if (calc_end <= start || (hole_em->start > end)) {
7005 free_extent_map(hole_em);
7006 hole_em = NULL;
7007 } else {
7008 hole_start = max(hole_em->start, start);
7009 hole_len = calc_end - hole_start;
7010 }
7011 }
7012 em->bdev = NULL;
7013 if (hole_em && range_start > hole_start) {
7014 /* our hole starts before our delalloc, so we
7015 * have to return just the parts of the hole
7016 * that go until the delalloc starts
7017 */
7018 em->len = min(hole_len,
7019 range_start - hole_start);
7020 em->start = hole_start;
7021 em->orig_start = hole_start;
7022 /*
7023 * don't adjust block start at all,
7024 * it is fixed at EXTENT_MAP_HOLE
7025 */
7026 em->block_start = hole_em->block_start;
7027 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007028 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7029 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007030 } else {
7031 em->start = range_start;
7032 em->len = found;
7033 em->orig_start = range_start;
7034 em->block_start = EXTENT_MAP_DELALLOC;
7035 em->block_len = found;
7036 }
7037 } else if (hole_em) {
7038 return hole_em;
7039 }
7040out:
7041
7042 free_extent_map(hole_em);
7043 if (err) {
7044 free_extent_map(em);
7045 return ERR_PTR(err);
7046 }
7047 return em;
7048}
7049
Josef Bacik4b46fce2010-05-23 11:00:55 -04007050static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7051 u64 start, u64 len)
7052{
7053 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007054 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007055 struct btrfs_key ins;
7056 u64 alloc_hint;
7057 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007058
Josef Bacik4b46fce2010-05-23 11:00:55 -04007059 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04007060 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007061 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007062 if (ret)
7063 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007064
Josef Bacik70c8a912012-10-11 16:54:30 -04007065 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007066 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04007067 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08007068 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007069 return em;
7070 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007071
7072 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
7073 ins.offset, ins.offset, 0);
7074 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08007075 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007076 free_extent_map(em);
7077 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007078 }
Josef Bacik00361582013-08-14 14:02:47 -04007079
Josef Bacik4b46fce2010-05-23 11:00:55 -04007080 return em;
7081}
7082
Chris Mason46bfbb52010-05-26 11:04:10 -04007083/*
7084 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7085 * block must be cow'd
7086 */
Josef Bacik00361582013-08-14 14:02:47 -04007087noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007088 u64 *orig_start, u64 *orig_block_len,
7089 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007090{
Josef Bacik00361582013-08-14 14:02:47 -04007091 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007092 struct btrfs_path *path;
7093 int ret;
7094 struct extent_buffer *leaf;
7095 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007096 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007097 struct btrfs_file_extent_item *fi;
7098 struct btrfs_key key;
7099 u64 disk_bytenr;
7100 u64 backref_offset;
7101 u64 extent_end;
7102 u64 num_bytes;
7103 int slot;
7104 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007105 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007106
Chris Mason46bfbb52010-05-26 11:04:10 -04007107 path = btrfs_alloc_path();
7108 if (!path)
7109 return -ENOMEM;
7110
Josef Bacik00361582013-08-14 14:02:47 -04007111 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007112 offset, 0);
7113 if (ret < 0)
7114 goto out;
7115
7116 slot = path->slots[0];
7117 if (ret == 1) {
7118 if (slot == 0) {
7119 /* can't find the item, must cow */
7120 ret = 0;
7121 goto out;
7122 }
7123 slot--;
7124 }
7125 ret = 0;
7126 leaf = path->nodes[0];
7127 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007128 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007129 key.type != BTRFS_EXTENT_DATA_KEY) {
7130 /* not our file or wrong item type, must cow */
7131 goto out;
7132 }
7133
7134 if (key.offset > offset) {
7135 /* Wrong offset, must cow */
7136 goto out;
7137 }
7138
7139 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7140 found_type = btrfs_file_extent_type(leaf, fi);
7141 if (found_type != BTRFS_FILE_EXTENT_REG &&
7142 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7143 /* not a regular extent, must cow */
7144 goto out;
7145 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007146
7147 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7148 goto out;
7149
Miao Xiee77751a2013-12-27 21:11:50 +08007150 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7151 if (extent_end <= offset)
7152 goto out;
7153
Chris Mason46bfbb52010-05-26 11:04:10 -04007154 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007155 if (disk_bytenr == 0)
7156 goto out;
7157
7158 if (btrfs_file_extent_compression(leaf, fi) ||
7159 btrfs_file_extent_encryption(leaf, fi) ||
7160 btrfs_file_extent_other_encoding(leaf, fi))
7161 goto out;
7162
Chris Mason46bfbb52010-05-26 11:04:10 -04007163 backref_offset = btrfs_file_extent_offset(leaf, fi);
7164
Josef Bacik7ee9e442013-06-21 16:37:03 -04007165 if (orig_start) {
7166 *orig_start = key.offset - backref_offset;
7167 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7168 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7169 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007170
Chris Mason46bfbb52010-05-26 11:04:10 -04007171 if (btrfs_extent_readonly(root, disk_bytenr))
7172 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007173
7174 num_bytes = min(offset + *len, extent_end) - offset;
7175 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7176 u64 range_end;
7177
7178 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7179 ret = test_range_bit(io_tree, offset, range_end,
7180 EXTENT_DELALLOC, 0, NULL);
7181 if (ret) {
7182 ret = -EAGAIN;
7183 goto out;
7184 }
7185 }
7186
Josef Bacik1bda19e2013-10-18 12:10:36 -04007187 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007188
7189 /*
7190 * look for other files referencing this extent, if we
7191 * find any we must cow
7192 */
Josef Bacik00361582013-08-14 14:02:47 -04007193 trans = btrfs_join_transaction(root);
7194 if (IS_ERR(trans)) {
7195 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007196 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007197 }
7198
7199 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7200 key.offset - backref_offset, disk_bytenr);
7201 btrfs_end_transaction(trans, root);
7202 if (ret) {
7203 ret = 0;
7204 goto out;
7205 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007206
7207 /*
7208 * adjust disk_bytenr and num_bytes to cover just the bytes
7209 * in this extent we are about to write. If there
7210 * are any csums in that range we have to cow in order
7211 * to keep the csums correct
7212 */
7213 disk_bytenr += backref_offset;
7214 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007215 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7216 goto out;
7217 /*
7218 * all of the above have passed, it is safe to overwrite this extent
7219 * without cow
7220 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007221 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007222 ret = 1;
7223out:
7224 btrfs_free_path(path);
7225 return ret;
7226}
7227
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007228bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7229{
7230 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7231 int found = false;
7232 void **pagep = NULL;
7233 struct page *page = NULL;
7234 int start_idx;
7235 int end_idx;
7236
7237 start_idx = start >> PAGE_CACHE_SHIFT;
7238
7239 /*
7240 * end is the last byte in the last page. end == start is legal
7241 */
7242 end_idx = end >> PAGE_CACHE_SHIFT;
7243
7244 rcu_read_lock();
7245
7246 /* Most of the code in this while loop is lifted from
7247 * find_get_page. It's been modified to begin searching from a
7248 * page and return just the first page found in that range. If the
7249 * found idx is less than or equal to the end idx then we know that
7250 * a page exists. If no pages are found or if those pages are
7251 * outside of the range then we're fine (yay!) */
7252 while (page == NULL &&
7253 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7254 page = radix_tree_deref_slot(pagep);
7255 if (unlikely(!page))
7256 break;
7257
7258 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007259 if (radix_tree_deref_retry(page)) {
7260 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007261 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007262 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007263 /*
7264 * Otherwise, shmem/tmpfs must be storing a swap entry
7265 * here as an exceptional entry: so return it without
7266 * attempting to raise page count.
7267 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007268 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007269 break; /* TODO: Is this relevant for this use case? */
7270 }
7271
Filipe Manana91405152014-06-05 13:22:24 +01007272 if (!page_cache_get_speculative(page)) {
7273 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007274 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007275 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007276
7277 /*
7278 * Has the page moved?
7279 * This is part of the lockless pagecache protocol. See
7280 * include/linux/pagemap.h for details.
7281 */
7282 if (unlikely(page != *pagep)) {
7283 page_cache_release(page);
7284 page = NULL;
7285 }
7286 }
7287
7288 if (page) {
7289 if (page->index <= end_idx)
7290 found = true;
7291 page_cache_release(page);
7292 }
7293
7294 rcu_read_unlock();
7295 return found;
7296}
7297
Josef Bacikeb838e72012-07-31 16:28:48 -04007298static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7299 struct extent_state **cached_state, int writing)
7300{
7301 struct btrfs_ordered_extent *ordered;
7302 int ret = 0;
7303
7304 while (1) {
7305 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7306 0, cached_state);
7307 /*
7308 * We're concerned with the entire range that we're going to be
7309 * doing DIO to, so we need to make sure theres no ordered
7310 * extents in this range.
7311 */
7312 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7313 lockend - lockstart + 1);
7314
7315 /*
7316 * We need to make sure there are no buffered pages in this
7317 * range either, we could have raced between the invalidate in
7318 * generic_file_direct_write and locking the extent. The
7319 * invalidate needs to happen so that reads after a write do not
7320 * get stale data.
7321 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007322 if (!ordered &&
7323 (!writing ||
7324 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007325 break;
7326
7327 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7328 cached_state, GFP_NOFS);
7329
7330 if (ordered) {
7331 btrfs_start_ordered_extent(inode, ordered, 1);
7332 btrfs_put_ordered_extent(ordered);
7333 } else {
7334 /* Screw you mmap */
Filipe Manana728404d2014-10-10 09:43:11 +01007335 ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
Filipe Manana075bdbd2014-10-09 21:18:55 +01007336 if (ret)
7337 break;
7338 ret = filemap_fdatawait_range(inode->i_mapping,
7339 lockstart,
7340 lockend);
Josef Bacikeb838e72012-07-31 16:28:48 -04007341 if (ret)
7342 break;
7343
7344 /*
7345 * If we found a page that couldn't be invalidated just
7346 * fall back to buffered.
7347 */
7348 ret = invalidate_inode_pages2_range(inode->i_mapping,
7349 lockstart >> PAGE_CACHE_SHIFT,
7350 lockend >> PAGE_CACHE_SHIFT);
7351 if (ret)
7352 break;
7353 }
7354
7355 cond_resched();
7356 }
7357
7358 return ret;
7359}
7360
Josef Bacik69ffb542012-09-11 15:40:07 -04007361static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7362 u64 len, u64 orig_start,
7363 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007364 u64 orig_block_len, u64 ram_bytes,
7365 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007366{
7367 struct extent_map_tree *em_tree;
7368 struct extent_map *em;
7369 struct btrfs_root *root = BTRFS_I(inode)->root;
7370 int ret;
7371
7372 em_tree = &BTRFS_I(inode)->extent_tree;
7373 em = alloc_extent_map();
7374 if (!em)
7375 return ERR_PTR(-ENOMEM);
7376
7377 em->start = start;
7378 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007379 em->mod_start = start;
7380 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007381 em->len = len;
7382 em->block_len = block_len;
7383 em->block_start = block_start;
7384 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007385 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007386 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007387 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007388 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7389 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007390 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007391
7392 do {
7393 btrfs_drop_extent_cache(inode, em->start,
7394 em->start + em->len - 1, 0);
7395 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007396 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007397 write_unlock(&em_tree->lock);
7398 } while (ret == -EEXIST);
7399
7400 if (ret) {
7401 free_extent_map(em);
7402 return ERR_PTR(ret);
7403 }
7404
7405 return em;
7406}
7407
7408
Josef Bacik4b46fce2010-05-23 11:00:55 -04007409static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7410 struct buffer_head *bh_result, int create)
7411{
7412 struct extent_map *em;
7413 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007414 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007415 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007416 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007417 u64 len = bh_result->b_size;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007418 u64 *outstanding_extents = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007419 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007420 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007421
Miao Xie172a5042013-02-21 02:48:22 -07007422 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007423 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007424 else
Josef Bacikc3298612012-08-03 16:49:19 -04007425 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007426
Josef Bacikc3298612012-08-03 16:49:19 -04007427 lockstart = start;
7428 lockend = start + len - 1;
7429
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007430 if (current->journal_info) {
7431 /*
7432 * Need to pull our outstanding extents and set journal_info to NULL so
7433 * that anything that needs to check if there's a transction doesn't get
7434 * confused.
7435 */
7436 outstanding_extents = current->journal_info;
7437 current->journal_info = NULL;
7438 }
7439
Josef Bacikeb838e72012-07-31 16:28:48 -04007440 /*
7441 * If this errors out it's because we couldn't invalidate pagecache for
7442 * this range and we need to fallback to buffered.
7443 */
7444 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7445 return -ENOTBLK;
7446
Josef Bacik4b46fce2010-05-23 11:00:55 -04007447 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007448 if (IS_ERR(em)) {
7449 ret = PTR_ERR(em);
7450 goto unlock_err;
7451 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007452
7453 /*
7454 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7455 * io. INLINE is special, and we could probably kludge it in here, but
7456 * it's still buffered so for safety lets just fall back to the generic
7457 * buffered path.
7458 *
7459 * For COMPRESSED we _have_ to read the entire extent in so we can
7460 * decompress it, so there will be buffering required no matter what we
7461 * do, so go ahead and fallback to buffered.
7462 *
7463 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7464 * to buffered IO. Don't blame me, this is the price we pay for using
7465 * the generic code.
7466 */
7467 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7468 em->block_start == EXTENT_MAP_INLINE) {
7469 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007470 ret = -ENOTBLK;
7471 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007472 }
7473
7474 /* Just a good old fashioned hole, return */
7475 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7476 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7477 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007478 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007479 }
7480
7481 /*
7482 * We don't allocate a new extent in the following cases
7483 *
7484 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7485 * existing extent.
7486 * 2) The extent is marked as PREALLOC. We're good to go here and can
7487 * just use the extent.
7488 *
7489 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007490 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007491 len = min(len, em->len - (start - em->start));
7492 lockstart = start + len;
7493 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007494 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007495
7496 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7497 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7498 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007499 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007500 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007501
7502 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7503 type = BTRFS_ORDERED_PREALLOC;
7504 else
7505 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007506 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007507 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007508
Josef Bacik00361582013-08-14 14:02:47 -04007509 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007510 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007511 if (type == BTRFS_ORDERED_PREALLOC) {
7512 free_extent_map(em);
7513 em = create_pinned_em(inode, start, len,
7514 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007515 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007516 orig_block_len,
7517 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007518 if (IS_ERR(em)) {
7519 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007520 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007521 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007522 }
7523
Chris Mason46bfbb52010-05-26 11:04:10 -04007524 ret = btrfs_add_ordered_extent_dio(inode, start,
7525 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007526 if (ret) {
7527 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007528 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007529 }
7530 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007531 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007532 }
Josef Bacik00361582013-08-14 14:02:47 -04007533
Chris Mason46bfbb52010-05-26 11:04:10 -04007534 /*
7535 * this will cow the extent, reset the len in case we changed
7536 * it above
7537 */
7538 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007539 free_extent_map(em);
7540 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007541 if (IS_ERR(em)) {
7542 ret = PTR_ERR(em);
7543 goto unlock_err;
7544 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007545 len = min(len, em->len - (start - em->start));
7546unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007547 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7548 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007549 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007550 bh_result->b_bdev = em->bdev;
7551 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007552 if (create) {
7553 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7554 set_buffer_new(bh_result);
7555
7556 /*
7557 * Need to update the i_size under the extent lock so buffered
7558 * readers will get the updated i_size when we unlock.
7559 */
7560 if (start + len > i_size_read(inode))
7561 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007562
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007563 /*
7564 * If we have an outstanding_extents count still set then we're
7565 * within our reservation, otherwise we need to adjust our inode
7566 * counter appropriately.
7567 */
7568 if (*outstanding_extents) {
7569 (*outstanding_extents)--;
7570 } else {
Josef Bacik3e05bde2015-02-11 15:08:57 -05007571 spin_lock(&BTRFS_I(inode)->lock);
7572 BTRFS_I(inode)->outstanding_extents++;
7573 spin_unlock(&BTRFS_I(inode)->lock);
7574 }
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007575
7576 current->journal_info = outstanding_extents;
Josef Bacik32667892015-02-11 15:08:58 -05007577 btrfs_free_reserved_data_space(inode, len);
Liu Boddba1bf2015-06-17 16:59:58 +08007578 set_bit(BTRFS_INODE_DIO_READY, &BTRFS_I(inode)->runtime_flags);
Josef Bacikc3473e82012-06-19 10:59:00 -04007579 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007580
Josef Bacikeb838e72012-07-31 16:28:48 -04007581 /*
7582 * In the case of write we need to clear and unlock the entire range,
7583 * in the case of read we need to unlock only the end area that we
7584 * aren't using if there is any left over space.
7585 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007586 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007587 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7588 lockend, unlock_bits, 1, 0,
7589 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007590 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007591 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007592 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007593
Josef Bacik4b46fce2010-05-23 11:00:55 -04007594 free_extent_map(em);
7595
7596 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007597
7598unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007599 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7600 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007601 if (outstanding_extents)
7602 current->journal_info = outstanding_extents;
Josef Bacikeb838e72012-07-31 16:28:48 -04007603 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007604}
7605
Miao Xie8b110e32014-09-12 18:44:03 +08007606static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7607 int rw, int mirror_num)
7608{
7609 struct btrfs_root *root = BTRFS_I(inode)->root;
7610 int ret;
7611
7612 BUG_ON(rw & REQ_WRITE);
7613
7614 bio_get(bio);
7615
7616 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7617 BTRFS_WQ_ENDIO_DIO_REPAIR);
7618 if (ret)
7619 goto err;
7620
7621 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7622err:
7623 bio_put(bio);
7624 return ret;
7625}
7626
7627static int btrfs_check_dio_repairable(struct inode *inode,
7628 struct bio *failed_bio,
7629 struct io_failure_record *failrec,
7630 int failed_mirror)
7631{
7632 int num_copies;
7633
7634 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7635 failrec->logical, failrec->len);
7636 if (num_copies == 1) {
7637 /*
7638 * we only have a single copy of the data, so don't bother with
7639 * all the retry and error correction code that follows. no
7640 * matter what the error is, it is very likely to persist.
7641 */
7642 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7643 num_copies, failrec->this_mirror, failed_mirror);
7644 return 0;
7645 }
7646
7647 failrec->failed_mirror = failed_mirror;
7648 failrec->this_mirror++;
7649 if (failrec->this_mirror == failed_mirror)
7650 failrec->this_mirror++;
7651
7652 if (failrec->this_mirror > num_copies) {
7653 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7654 num_copies, failrec->this_mirror, failed_mirror);
7655 return 0;
7656 }
7657
7658 return 1;
7659}
7660
7661static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7662 struct page *page, u64 start, u64 end,
7663 int failed_mirror, bio_end_io_t *repair_endio,
7664 void *repair_arg)
7665{
7666 struct io_failure_record *failrec;
7667 struct bio *bio;
7668 int isector;
7669 int read_mode;
7670 int ret;
7671
7672 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7673
7674 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7675 if (ret)
7676 return ret;
7677
7678 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7679 failed_mirror);
7680 if (!ret) {
7681 free_io_failure(inode, failrec);
7682 return -EIO;
7683 }
7684
7685 if (failed_bio->bi_vcnt > 1)
7686 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7687 else
7688 read_mode = READ_SYNC;
7689
7690 isector = start - btrfs_io_bio(failed_bio)->logical;
7691 isector >>= inode->i_sb->s_blocksize_bits;
7692 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7693 0, isector, repair_endio, repair_arg);
7694 if (!bio) {
7695 free_io_failure(inode, failrec);
7696 return -EIO;
7697 }
7698
7699 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7700 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7701 read_mode, failrec->this_mirror, failrec->in_validation);
7702
7703 ret = submit_dio_repair_bio(inode, bio, read_mode,
7704 failrec->this_mirror);
7705 if (ret) {
7706 free_io_failure(inode, failrec);
7707 bio_put(bio);
7708 }
7709
7710 return ret;
7711}
7712
7713struct btrfs_retry_complete {
7714 struct completion done;
7715 struct inode *inode;
7716 u64 start;
7717 int uptodate;
7718};
7719
7720static void btrfs_retry_endio_nocsum(struct bio *bio, int err)
7721{
7722 struct btrfs_retry_complete *done = bio->bi_private;
7723 struct bio_vec *bvec;
7724 int i;
7725
7726 if (err)
7727 goto end;
7728
7729 done->uptodate = 1;
7730 bio_for_each_segment_all(bvec, bio, i)
7731 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7732end:
7733 complete(&done->done);
7734 bio_put(bio);
7735}
7736
7737static int __btrfs_correct_data_nocsum(struct inode *inode,
7738 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007739{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007740 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007741 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007742 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007743 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007744 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007745
Miao Xiec1dc0892014-09-12 18:43:56 +08007746 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007747 done.inode = inode;
7748
7749 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7750try_again:
7751 done.uptodate = 0;
7752 done.start = start;
7753 init_completion(&done.done);
7754
7755 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7756 start + bvec->bv_len - 1,
7757 io_bio->mirror_num,
7758 btrfs_retry_endio_nocsum, &done);
7759 if (ret)
7760 return ret;
7761
7762 wait_for_completion(&done.done);
7763
7764 if (!done.uptodate) {
7765 /* We might have another mirror, so try again */
7766 goto try_again;
7767 }
7768
7769 start += bvec->bv_len;
7770 }
7771
7772 return 0;
7773}
7774
7775static void btrfs_retry_endio(struct bio *bio, int err)
7776{
7777 struct btrfs_retry_complete *done = bio->bi_private;
7778 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7779 struct bio_vec *bvec;
7780 int uptodate;
7781 int ret;
7782 int i;
7783
7784 if (err)
7785 goto end;
7786
7787 uptodate = 1;
7788 bio_for_each_segment_all(bvec, bio, i) {
7789 ret = __readpage_endio_check(done->inode, io_bio, i,
7790 bvec->bv_page, 0,
7791 done->start, bvec->bv_len);
7792 if (!ret)
7793 clean_io_failure(done->inode, done->start,
7794 bvec->bv_page, 0);
7795 else
7796 uptodate = 0;
7797 }
7798
7799 done->uptodate = uptodate;
7800end:
7801 complete(&done->done);
7802 bio_put(bio);
7803}
7804
7805static int __btrfs_subio_endio_read(struct inode *inode,
7806 struct btrfs_io_bio *io_bio, int err)
7807{
7808 struct bio_vec *bvec;
7809 struct btrfs_retry_complete done;
7810 u64 start;
7811 u64 offset = 0;
7812 int i;
7813 int ret;
7814
7815 err = 0;
7816 start = io_bio->logical;
7817 done.inode = inode;
7818
Miao Xiec1dc0892014-09-12 18:43:56 +08007819 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007820 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7821 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007822 if (likely(!ret))
7823 goto next;
7824try_again:
7825 done.uptodate = 0;
7826 done.start = start;
7827 init_completion(&done.done);
7828
7829 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7830 start + bvec->bv_len - 1,
7831 io_bio->mirror_num,
7832 btrfs_retry_endio, &done);
7833 if (ret) {
7834 err = ret;
7835 goto next;
7836 }
7837
7838 wait_for_completion(&done.done);
7839
7840 if (!done.uptodate) {
7841 /* We might have another mirror, so try again */
7842 goto try_again;
7843 }
7844next:
7845 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007846 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007847 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007848
7849 return err;
7850}
7851
Miao Xie8b110e32014-09-12 18:44:03 +08007852static int btrfs_subio_endio_read(struct inode *inode,
7853 struct btrfs_io_bio *io_bio, int err)
7854{
7855 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7856
7857 if (skip_csum) {
7858 if (unlikely(err))
7859 return __btrfs_correct_data_nocsum(inode, io_bio);
7860 else
7861 return 0;
7862 } else {
7863 return __btrfs_subio_endio_read(inode, io_bio, err);
7864 }
7865}
7866
Miao Xiec1dc0892014-09-12 18:43:56 +08007867static void btrfs_endio_direct_read(struct bio *bio, int err)
7868{
7869 struct btrfs_dio_private *dip = bio->bi_private;
7870 struct inode *inode = dip->inode;
7871 struct bio *dio_bio;
7872 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7873
Miao Xie8b110e32014-09-12 18:44:03 +08007874 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7875 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007876
Josef Bacik4b46fce2010-05-23 11:00:55 -04007877 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007878 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007879 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007880
Josef Bacik4b46fce2010-05-23 11:00:55 -04007881 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007882
7883 /* If we had a csum failure make sure to clear the uptodate flag */
7884 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007885 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7886 dio_end_io(dio_bio, err);
Miao Xie23ea8e52014-09-12 18:43:54 +08007887
7888 if (io_bio->end_io)
7889 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007890 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007891}
7892
7893static void btrfs_endio_direct_write(struct bio *bio, int err)
7894{
7895 struct btrfs_dio_private *dip = bio->bi_private;
7896 struct inode *inode = dip->inode;
7897 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007898 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007899 u64 ordered_offset = dip->logical_offset;
7900 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007901 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007902 int ret;
7903
Chris Mason163cf092010-11-28 19:56:33 -05007904again:
7905 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7906 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007907 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007908 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007909 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007910
Liu Bo9e0af232014-08-15 23:36:53 +08007911 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7912 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007913 btrfs_queue_work(root->fs_info->endio_write_workers,
7914 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007915out_test:
7916 /*
7917 * our bio might span multiple ordered extents. If we haven't
7918 * completed the accounting for the whole dio, go back and try again
7919 */
7920 if (ordered_offset < dip->logical_offset + dip->bytes) {
7921 ordered_bytes = dip->logical_offset + dip->bytes -
7922 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007923 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007924 goto again;
7925 }
Chris Mason9be33952013-05-17 18:30:14 -04007926 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007927
Josef Bacik4b46fce2010-05-23 11:00:55 -04007928 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007929
7930 /* If we had an error make sure to clear the uptodate flag */
7931 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007932 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7933 dio_end_io(dio_bio, err);
7934 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007935}
7936
Chris Masoneaf25d92010-05-25 09:48:28 -04007937static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7938 struct bio *bio, int mirror_num,
7939 unsigned long bio_flags, u64 offset)
7940{
7941 int ret;
7942 struct btrfs_root *root = BTRFS_I(inode)->root;
7943 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007944 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007945 return 0;
7946}
7947
Miao Xiee65e1532010-11-22 03:04:43 +00007948static void btrfs_end_dio_bio(struct bio *bio, int err)
7949{
7950 struct btrfs_dio_private *dip = bio->bi_private;
7951
Miao Xie8b110e32014-09-12 18:44:03 +08007952 if (err)
7953 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
7954 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7955 btrfs_ino(dip->inode), bio->bi_rw,
7956 (unsigned long long)bio->bi_iter.bi_sector,
7957 bio->bi_iter.bi_size, err);
7958
7959 if (dip->subio_endio)
7960 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007961
7962 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007963 dip->errors = 1;
7964
7965 /*
7966 * before atomic variable goto zero, we must make sure
7967 * dip->errors is perceived to be set.
7968 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007969 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007970 }
7971
7972 /* if there are more bios still pending for this dio, just exit */
7973 if (!atomic_dec_and_test(&dip->pending_bios))
7974 goto out;
7975
Chris Mason9be33952013-05-17 18:30:14 -04007976 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007977 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007978 } else {
7979 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007980 bio_endio(dip->orig_bio, 0);
7981 }
7982out:
7983 bio_put(bio);
7984}
7985
7986static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7987 u64 first_sector, gfp_t gfp_flags)
7988{
7989 int nr_vecs = bio_get_nr_vecs(bdev);
7990 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7991}
7992
Miao Xiec1dc0892014-09-12 18:43:56 +08007993static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
7994 struct inode *inode,
7995 struct btrfs_dio_private *dip,
7996 struct bio *bio,
7997 u64 file_offset)
7998{
7999 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8000 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8001 int ret;
8002
8003 /*
8004 * We load all the csum data we need when we submit
8005 * the first bio to reduce the csum tree search and
8006 * contention.
8007 */
8008 if (dip->logical_offset == file_offset) {
8009 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
8010 file_offset);
8011 if (ret)
8012 return ret;
8013 }
8014
8015 if (bio == dip->orig_bio)
8016 return 0;
8017
8018 file_offset -= dip->logical_offset;
8019 file_offset >>= inode->i_sb->s_blocksize_bits;
8020 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8021
8022 return 0;
8023}
8024
Miao Xiee65e1532010-11-22 03:04:43 +00008025static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8026 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008027 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008028{
Miao Xiefacc8a222013-07-25 19:22:34 +08008029 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00008030 int write = rw & REQ_WRITE;
8031 struct btrfs_root *root = BTRFS_I(inode)->root;
8032 int ret;
8033
Josef Bacikb812ce22012-11-16 13:56:32 -05008034 if (async_submit)
8035 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8036
Miao Xiee65e1532010-11-22 03:04:43 +00008037 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008038
8039 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02008040 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8041 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008042 if (ret)
8043 goto err;
8044 }
Miao Xiee65e1532010-11-22 03:04:43 +00008045
Josef Bacik1ae39932011-04-06 14:41:34 -04008046 if (skip_sum)
8047 goto map;
8048
8049 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008050 ret = btrfs_wq_submit_bio(root->fs_info,
8051 inode, rw, bio, 0, 0,
8052 file_offset,
8053 __btrfs_submit_bio_start_direct_io,
8054 __btrfs_submit_bio_done);
8055 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008056 } else if (write) {
8057 /*
8058 * If we aren't doing async submit, calculate the csum of the
8059 * bio now.
8060 */
8061 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8062 if (ret)
8063 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008064 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008065 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8066 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008067 if (ret)
8068 goto err;
8069 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008070map:
8071 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008072err:
8073 bio_put(bio);
8074 return ret;
8075}
8076
8077static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8078 int skip_sum)
8079{
8080 struct inode *inode = dip->inode;
8081 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008082 struct bio *bio;
8083 struct bio *orig_bio = dip->orig_bio;
8084 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008085 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008086 u64 file_offset = dip->logical_offset;
8087 u64 submit_len = 0;
8088 u64 map_length;
8089 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08008090 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04008091 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008092
Kent Overstreet4f024f32013-10-11 15:44:27 -07008093 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008094 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008095 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008096 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008097 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008098
Kent Overstreet4f024f32013-10-11 15:44:27 -07008099 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008100 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008101 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008102 goto submit;
8103 }
8104
David Woodhouse53b381b2013-01-29 18:40:14 -05008105 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008106 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008107 async_submit = 0;
8108 else
8109 async_submit = 1;
8110
Josef Bacik02f57c72011-04-06 14:25:44 -04008111 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8112 if (!bio)
8113 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008114
Josef Bacik02f57c72011-04-06 14:25:44 -04008115 bio->bi_private = dip;
8116 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008117 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008118 atomic_inc(&dip->pending_bios);
8119
Miao Xiee65e1532010-11-22 03:04:43 +00008120 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02008121 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00008122 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02008123 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00008124 /*
8125 * inc the count before we submit the bio so
8126 * we know the end IO handler won't happen before
8127 * we inc the count. Otherwise, the dip might get freed
8128 * before we're done setting it up
8129 */
8130 atomic_inc(&dip->pending_bios);
8131 ret = __btrfs_submit_dio_bio(bio, inode, rw,
8132 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008133 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008134 if (ret) {
8135 bio_put(bio);
8136 atomic_dec(&dip->pending_bios);
8137 goto out_err;
8138 }
8139
Miao Xiee65e1532010-11-22 03:04:43 +00008140 start_sector += submit_len >> 9;
8141 file_offset += submit_len;
8142
8143 submit_len = 0;
8144 nr_pages = 0;
8145
8146 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8147 start_sector, GFP_NOFS);
8148 if (!bio)
8149 goto out_err;
8150 bio->bi_private = dip;
8151 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008152 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008153
Kent Overstreet4f024f32013-10-11 15:44:27 -07008154 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008155 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008156 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008157 &map_length, NULL, 0);
8158 if (ret) {
8159 bio_put(bio);
8160 goto out_err;
8161 }
8162 } else {
8163 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308164 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00008165 bvec++;
8166 }
8167 }
8168
Josef Bacik02f57c72011-04-06 14:25:44 -04008169submit:
Miao Xiee65e1532010-11-22 03:04:43 +00008170 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008171 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008172 if (!ret)
8173 return 0;
8174
8175 bio_put(bio);
8176out_err:
8177 dip->errors = 1;
8178 /*
8179 * before atomic variable goto zero, we must
8180 * make sure dip->errors is perceived to be set.
8181 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008182 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008183 if (atomic_dec_and_test(&dip->pending_bios))
8184 bio_io_error(dip->orig_bio);
8185
8186 /* bio_end_io() will handle error, so we needn't return it */
8187 return 0;
8188}
8189
Chris Mason9be33952013-05-17 18:30:14 -04008190static void btrfs_submit_direct(int rw, struct bio *dio_bio,
8191 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008192{
Filipe Manana61de7182015-07-01 12:13:10 +01008193 struct btrfs_dio_private *dip = NULL;
8194 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008195 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008196 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02008197 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008198 int ret = 0;
8199
8200 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8201
Chris Mason9be33952013-05-17 18:30:14 -04008202 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008203 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008204 ret = -ENOMEM;
8205 goto free_ordered;
8206 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008207
Miao Xiec1dc0892014-09-12 18:43:56 +08008208 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008209 if (!dip) {
8210 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008211 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008212 }
8213
8214 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008215 dip->inode = inode;
8216 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008217 dip->bytes = dio_bio->bi_iter.bi_size;
8218 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008219 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008220 dip->orig_bio = io_bio;
8221 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008222 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008223 btrfs_bio = btrfs_io_bio(io_bio);
8224 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008225
Miao Xiec1dc0892014-09-12 18:43:56 +08008226 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008227 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008228 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008229 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008230 dip->subio_endio = btrfs_subio_endio_read;
8231 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008232
Miao Xiee65e1532010-11-22 03:04:43 +00008233 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8234 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008235 return;
Chris Mason9be33952013-05-17 18:30:14 -04008236
Miao Xie23ea8e52014-09-12 18:43:54 +08008237 if (btrfs_bio->end_io)
8238 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008239
Josef Bacik4b46fce2010-05-23 11:00:55 -04008240free_ordered:
8241 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008242 * If we arrived here it means either we failed to submit the dip
8243 * or we either failed to clone the dio_bio or failed to allocate the
8244 * dip. If we cloned the dio_bio and allocated the dip, we can just
8245 * call bio_endio against our io_bio so that we get proper resource
8246 * cleanup if we fail to submit the dip, otherwise, we must do the
8247 * same as btrfs_endio_direct_[write|read] because we can't call these
8248 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008249 */
Filipe Manana61de7182015-07-01 12:13:10 +01008250 if (io_bio && dip) {
8251 bio_endio(io_bio, ret);
8252 /*
8253 * The end io callbacks free our dip, do the final put on io_bio
8254 * and all the cleanup and final put for dio_bio (through
8255 * dio_end_io()).
8256 */
8257 dip = NULL;
8258 io_bio = NULL;
8259 } else {
8260 if (write) {
8261 struct btrfs_ordered_extent *ordered;
8262
8263 ordered = btrfs_lookup_ordered_extent(inode,
8264 file_offset);
8265 set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
8266 /*
8267 * Decrements our ref on the ordered extent and removes
8268 * the ordered extent from the inode's ordered tree,
8269 * doing all the proper resource cleanup such as for the
8270 * reserved space and waking up any waiters for this
8271 * ordered extent (through btrfs_remove_ordered_extent).
8272 */
8273 btrfs_finish_ordered_io(ordered);
8274 } else {
8275 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8276 file_offset + dio_bio->bi_iter.bi_size - 1);
8277 }
8278 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
8279 /*
8280 * Releases and cleans up our dio_bio, no need to bio_put()
8281 * nor bio_endio()/bio_io_error() against dio_bio.
8282 */
8283 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008284 }
Filipe Manana61de7182015-07-01 12:13:10 +01008285 if (io_bio)
8286 bio_put(io_bio);
8287 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008288}
8289
Omar Sandoval6f673762015-03-16 04:33:52 -07008290static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008291 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008292{
8293 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008294 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008295 unsigned blocksize_mask = root->sectorsize - 1;
8296 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008297
8298 if (offset & blocksize_mask)
8299 goto out;
8300
Al Viro28060d52014-03-22 05:15:17 -04008301 if (iov_iter_alignment(iter) & blocksize_mask)
8302 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008303
Al Viro28060d52014-03-22 05:15:17 -04008304 /* If this is a write we don't need to check anymore */
Omar Sandoval6f673762015-03-16 04:33:52 -07008305 if (iov_iter_rw(iter) == WRITE)
Al Viro28060d52014-03-22 05:15:17 -04008306 return 0;
8307 /*
8308 * Check to make sure we don't have duplicate iov_base's in this
8309 * iovec, if so return EINVAL, otherwise we'll get csum errors
8310 * when reading back.
8311 */
8312 for (seg = 0; seg < iter->nr_segs; seg++) {
8313 for (i = seg + 1; i < iter->nr_segs; i++) {
8314 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008315 goto out;
8316 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008317 }
8318 retval = 0;
8319out:
8320 return retval;
8321}
Josef Bacikeb838e72012-07-31 16:28:48 -04008322
Omar Sandoval22c61862015-03-16 04:33:53 -07008323static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
8324 loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04008325{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008326 struct file *file = iocb->ki_filp;
8327 struct inode *inode = file->f_mapping->host;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008328 u64 outstanding_extents = 0;
Miao Xie09348562013-02-07 10:12:07 +00008329 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008330 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008331 bool wakeup = true;
8332 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008333 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008334
Omar Sandoval6f673762015-03-16 04:33:52 -07008335 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008336 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008337
Jens Axboefe0f07d2015-04-15 17:05:48 -06008338 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008339 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008340
Josef Bacik0e267c42013-07-02 10:38:02 -04008341 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008342 * The generic stuff only does filemap_write_and_wait_range, which
8343 * isn't enough if we've written compressed pages to this area, so
8344 * we need to flush the dirty pages again to make absolutely sure
8345 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008346 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008347 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008348 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8349 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008350 filemap_fdatawrite_range(inode->i_mapping, offset,
8351 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008352
Omar Sandoval6f673762015-03-16 04:33:52 -07008353 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008354 /*
8355 * If the write DIO is beyond the EOF, we need update
8356 * the isize, but it is protected by i_mutex. So we can
8357 * not unlock the i_mutex at this case.
8358 */
8359 if (offset + count <= inode->i_size) {
8360 mutex_unlock(&inode->i_mutex);
8361 relock = true;
8362 }
Miao Xie09348562013-02-07 10:12:07 +00008363 ret = btrfs_delalloc_reserve_space(inode, count);
8364 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008365 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008366 outstanding_extents = div64_u64(count +
8367 BTRFS_MAX_EXTENT_SIZE - 1,
8368 BTRFS_MAX_EXTENT_SIZE);
8369
8370 /*
8371 * We need to know how many extents we reserved so that we can
8372 * do the accounting properly if we go over the number we
8373 * originally calculated. Abuse current->journal_info for this.
8374 */
8375 current->journal_info = &outstanding_extents;
David Sterbaee39b432014-09-30 01:33:33 +02008376 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8377 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008378 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008379 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8380 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008381 }
8382
Omar Sandoval17f8c842015-03-16 04:33:50 -07008383 ret = __blockdev_direct_IO(iocb, inode,
8384 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
8385 iter, offset, btrfs_get_blocks_direct, NULL,
8386 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008387 if (iov_iter_rw(iter) == WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008388 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008389 if (ret < 0 && ret != -EIOCBQUEUED) {
8390 /*
8391 * If the error comes from submitting stage,
8392 * btrfs_get_blocsk_direct() has free'd data space,
8393 * and metadata space will be handled by
8394 * finish_ordered_fn, don't do that again to make
8395 * sure bytes_may_use is correct.
8396 */
8397 if (!test_and_clear_bit(BTRFS_INODE_DIO_READY,
8398 &BTRFS_I(inode)->runtime_flags))
8399 btrfs_delalloc_release_space(inode, count);
8400 } else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00008401 btrfs_delalloc_release_space(inode,
8402 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008403 }
Miao Xie38851cc2013-02-08 07:04:11 +00008404out:
Miao Xie2e60a512013-02-08 07:01:08 +00008405 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008406 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008407 if (relock)
8408 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008409
8410 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008411}
8412
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008413#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8414
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008415static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8416 __u64 start, __u64 len)
8417{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008418 int ret;
8419
8420 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8421 if (ret)
8422 return ret;
8423
Chris Masonec29ed52011-02-23 16:23:20 -05008424 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008425}
8426
Chris Mason9ebefb182007-06-15 13:50:00 -04008427int btrfs_readpage(struct file *file, struct page *page)
8428{
Chris Masond1310b22008-01-24 16:13:08 -05008429 struct extent_io_tree *tree;
8430 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008431 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008432}
Chris Mason1832a6d2007-12-21 16:27:21 -05008433
Chris Mason39279cc2007-06-12 06:35:45 -04008434static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8435{
Chris Masond1310b22008-01-24 16:13:08 -05008436 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04008437
8438
8439 if (current->flags & PF_MEMALLOC) {
8440 redirty_page_for_writepage(wbc, page);
8441 unlock_page(page);
8442 return 0;
8443 }
Chris Masond1310b22008-01-24 16:13:08 -05008444 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008445 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8446}
Chris Mason39279cc2007-06-12 06:35:45 -04008447
Eric Sandeen48a3b632013-04-25 20:41:01 +00008448static int btrfs_writepages(struct address_space *mapping,
8449 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008450{
Chris Masond1310b22008-01-24 16:13:08 -05008451 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008452
Chris Masond1310b22008-01-24 16:13:08 -05008453 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008454 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8455}
8456
Chris Mason3ab2fb52007-11-08 10:59:22 -05008457static int
8458btrfs_readpages(struct file *file, struct address_space *mapping,
8459 struct list_head *pages, unsigned nr_pages)
8460{
Chris Masond1310b22008-01-24 16:13:08 -05008461 struct extent_io_tree *tree;
8462 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008463 return extent_readpages(tree, mapping, pages, nr_pages,
8464 btrfs_get_extent);
8465}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008466static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008467{
Chris Masond1310b22008-01-24 16:13:08 -05008468 struct extent_io_tree *tree;
8469 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008470 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008471
Chris Masond1310b22008-01-24 16:13:08 -05008472 tree = &BTRFS_I(page->mapping->host)->io_tree;
8473 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008474 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008475 if (ret == 1) {
8476 ClearPagePrivate(page);
8477 set_page_private(page, 0);
8478 page_cache_release(page);
8479 }
8480 return ret;
8481}
Chris Mason39279cc2007-06-12 06:35:45 -04008482
Chris Masone6dcd2d2008-07-17 12:53:50 -04008483static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8484{
Chris Mason98509cf2008-09-11 15:51:43 -04008485 if (PageWriteback(page) || PageDirty(page))
8486 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008487 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008488}
8489
Lukas Czernerd47992f2013-05-21 23:17:23 -04008490static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8491 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008492{
Josef Bacik5fd02042012-05-02 14:00:54 -04008493 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008494 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008495 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008496 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008497 u64 page_start = page_offset(page);
8498 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008499 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008500
Chris Mason8b62b722009-09-02 16:53:46 -04008501 /*
8502 * we have the page locked, so new writeback can't start,
8503 * and the dirty bit won't be cleared while we are here.
8504 *
8505 * Wait for IO on this page so that we can safely clear
8506 * the PagePrivate2 bit and do ordered accounting
8507 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008508 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008509
Josef Bacik5fd02042012-05-02 14:00:54 -04008510 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008511 if (offset) {
8512 btrfs_releasepage(page, GFP_NOFS);
8513 return;
8514 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008515
8516 if (!inode_evicting)
8517 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8518 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008519 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008520 /*
8521 * IO on this page will never be started, so we need
8522 * to account for any ordered extents now
8523 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008524 if (!inode_evicting)
8525 clear_extent_bit(tree, page_start, page_end,
8526 EXTENT_DIRTY | EXTENT_DELALLOC |
8527 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8528 EXTENT_DEFRAG, 1, 0, &cached_state,
8529 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008530 /*
8531 * whoever cleared the private bit is responsible
8532 * for the finish_ordered_io
8533 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008534 if (TestClearPagePrivate2(page)) {
8535 struct btrfs_ordered_inode_tree *tree;
8536 u64 new_len;
8537
8538 tree = &BTRFS_I(inode)->ordered_tree;
8539
8540 spin_lock_irq(&tree->lock);
8541 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8542 new_len = page_start - ordered->file_offset;
8543 if (new_len < ordered->truncated_len)
8544 ordered->truncated_len = new_len;
8545 spin_unlock_irq(&tree->lock);
8546
8547 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8548 page_start,
8549 PAGE_CACHE_SIZE, 1))
8550 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008551 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008552 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008553 if (!inode_evicting) {
8554 cached_state = NULL;
8555 lock_extent_bits(tree, page_start, page_end, 0,
8556 &cached_state);
8557 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008558 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008559
8560 if (!inode_evicting) {
8561 clear_extent_bit(tree, page_start, page_end,
8562 EXTENT_LOCKED | EXTENT_DIRTY |
8563 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8564 EXTENT_DEFRAG, 1, 1,
8565 &cached_state, GFP_NOFS);
8566
8567 __btrfs_releasepage(page, GFP_NOFS);
8568 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008569
Chris Mason4a096752008-07-21 10:29:44 -04008570 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008571 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008572 ClearPagePrivate(page);
8573 set_page_private(page, 0);
8574 page_cache_release(page);
8575 }
Chris Mason39279cc2007-06-12 06:35:45 -04008576}
8577
Chris Mason9ebefb182007-06-15 13:50:00 -04008578/*
8579 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8580 * called from a page fault handler when a page is first dirtied. Hence we must
8581 * be careful to check for EOF conditions here. We set the page up correctly
8582 * for a written page which means we get ENOSPC checking when writing into
8583 * holes and correct delalloc and unwritten extent mapping on filesystems that
8584 * support these features.
8585 *
8586 * We are not allowed to take the i_mutex here so we have to play games to
8587 * protect against truncate races as the page could now be beyond EOF. Because
8588 * vmtruncate() writes the inode size before removing pages, once we have the
8589 * page lock we can determine safely if the page is beyond EOF. If it is not
8590 * beyond EOF, then the page is guaranteed safe against truncation until we
8591 * unlock the page.
8592 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008593int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008594{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008595 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008596 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008597 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008598 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8599 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008600 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008601 char *kaddr;
8602 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008603 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008604 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008605 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008606 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008607 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008608
Jan Karab2b5ef52012-06-12 16:20:45 +02008609 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04008610 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008611 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008612 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008613 reserved = 1;
8614 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008615 if (ret) {
8616 if (ret == -ENOMEM)
8617 ret = VM_FAULT_OOM;
8618 else /* -ENOSPC, -EIO, etc */
8619 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008620 if (reserved)
8621 goto out;
8622 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008623 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008624
Nick Piggin56a76f82009-03-31 15:23:23 -07008625 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008626again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008627 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008628 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008629 page_start = page_offset(page);
8630 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04008631
Chris Mason9ebefb182007-06-15 13:50:00 -04008632 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008633 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008634 /* page got truncated out from underneath us */
8635 goto out_unlock;
8636 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008637 wait_on_page_writeback(page);
8638
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008639 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008640 set_page_extent_mapped(page);
8641
Chris Masoneb84ae02008-07-17 13:53:27 -04008642 /*
8643 * we can't set the delalloc bits if there are pending ordered
8644 * extents. Drop our locks and wait for them to finish
8645 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008646 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8647 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008648 unlock_extent_cached(io_tree, page_start, page_end,
8649 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008650 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008651 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008652 btrfs_put_ordered_extent(ordered);
8653 goto again;
8654 }
8655
Josef Bacikfbf19082009-10-01 17:10:23 -04008656 /*
8657 * XXX - page_mkwrite gets called every time the page is dirtied, even
8658 * if it was already dirty, so for space accounting reasons we need to
8659 * clear any delalloc bits for the range we are fixing to save. There
8660 * is probably a better way to do this, but for now keep consistent with
8661 * prepare_pages in the normal write path.
8662 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008663 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008664 EXTENT_DIRTY | EXTENT_DELALLOC |
8665 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008666 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008667
Josef Bacik2ac55d42010-02-03 19:33:23 +00008668 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8669 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008670 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008671 unlock_extent_cached(io_tree, page_start, page_end,
8672 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008673 ret = VM_FAULT_SIGBUS;
8674 goto out_unlock;
8675 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008676 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008677
8678 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008679 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008680 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008681 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008682 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008683
Chris Masone6dcd2d2008-07-17 12:53:50 -04008684 if (zero_start != PAGE_CACHE_SIZE) {
8685 kaddr = kmap(page);
8686 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8687 flush_dcache_page(page);
8688 kunmap(page);
8689 }
Chris Mason247e7432008-07-17 12:53:51 -04008690 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008691 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008692 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008693
Chris Mason257c62e2009-10-13 13:21:08 -04008694 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8695 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008696 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008697
Josef Bacik2ac55d42010-02-03 19:33:23 +00008698 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008699
8700out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008701 if (!ret) {
8702 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008703 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008704 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008705 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008706out:
Josef Bacikec39e182012-01-12 19:10:12 -05008707 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008708out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008709 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008710 return ret;
8711}
8712
Josef Bacika41ad392011-01-31 15:30:16 -05008713static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008714{
8715 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008716 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008717 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008718 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008719 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008720 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008721 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008722
Josef Bacik0ef8b722013-10-25 16:13:35 -04008723 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8724 (u64)-1);
8725 if (ret)
8726 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008727
Josef Bacikfcb80c22011-05-03 10:40:22 -04008728 /*
8729 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8730 * 3 things going on here
8731 *
8732 * 1) We need to reserve space for our orphan item and the space to
8733 * delete our orphan item. Lord knows we don't want to have a dangling
8734 * orphan item because we didn't reserve space to remove it.
8735 *
8736 * 2) We need to reserve space to update our inode.
8737 *
8738 * 3) We need to have something to cache all the space that is going to
8739 * be free'd up by the truncate operation, but also have some slack
8740 * space reserved in case it uses space during the truncate (thank you
8741 * very much snapshotting).
8742 *
8743 * And we need these to all be seperate. The fact is we can use alot of
8744 * space doing the truncate, and we have no earthly idea how much space
8745 * we will use, so we need the truncate reservation to be seperate so it
8746 * doesn't end up using space reserved for updating the inode or
8747 * removing the orphan item. We also need to be able to stop the
8748 * transaction and start a new one, which means we need to be able to
8749 * update the inode several times, and we have no idea of knowing how
8750 * many times that will be, so we can't just reserve 1 item for the
8751 * entirety of the opration, so that has to be done seperately as well.
8752 * Then there is the orphan item, which does indeed need to be held on
8753 * to for the whole operation, and we need nobody to touch this reserved
8754 * space except the orphan code.
8755 *
8756 * So that leaves us with
8757 *
8758 * 1) root->orphan_block_rsv - for the orphan deletion.
8759 * 2) rsv - for the truncate reservation, which we will steal from the
8760 * transaction reservation.
8761 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8762 * updating the inode.
8763 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008764 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008765 if (!rsv)
8766 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008767 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008768 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008769
Josef Bacik907cbce2011-08-08 13:46:15 -04008770 /*
Josef Bacik07127182011-08-19 10:29:59 -04008771 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008772 * 1 for updating the inode.
8773 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008774 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008775 if (IS_ERR(trans)) {
8776 err = PTR_ERR(trans);
8777 goto out;
8778 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008779
Josef Bacik907cbce2011-08-08 13:46:15 -04008780 /* Migrate the slack space for the truncate to our reserve */
8781 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8782 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008783 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008784
Chris Mason5a3f23d2009-03-31 13:27:11 -04008785 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008786 * So if we truncate and then write and fsync we normally would just
8787 * write the extents that changed, which is a problem if we need to
8788 * first truncate that entire inode. So set this flag so we write out
8789 * all of the extents in the inode to the sync log so we're completely
8790 * safe.
8791 */
8792 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008793 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008794
Yan, Zheng80825102009-11-12 09:35:36 +00008795 while (1) {
8796 ret = btrfs_truncate_inode_items(trans, root, inode,
8797 inode->i_size,
8798 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08008799 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05008800 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008801 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008802 }
Chris Mason39279cc2007-06-12 06:35:45 -04008803
Josef Bacikfcb80c22011-05-03 10:40:22 -04008804 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008805 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008806 if (ret) {
8807 err = ret;
8808 break;
8809 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008810
Yan, Zheng80825102009-11-12 09:35:36 +00008811 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008812 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008813
8814 trans = btrfs_start_transaction(root, 2);
8815 if (IS_ERR(trans)) {
8816 ret = err = PTR_ERR(trans);
8817 trans = NULL;
8818 break;
8819 }
8820
8821 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8822 rsv, min_size);
8823 BUG_ON(ret); /* shouldn't happen */
8824 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008825 }
8826
8827 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008828 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008829 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008830 if (ret)
8831 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008832 }
8833
Chris Mason917c16b2011-11-08 14:49:59 -05008834 if (trans) {
8835 trans->block_rsv = &root->fs_info->trans_block_rsv;
8836 ret = btrfs_update_inode(trans, root, inode);
8837 if (ret && !err)
8838 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008839
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008840 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008841 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008842 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008843
8844out:
8845 btrfs_free_block_rsv(root, rsv);
8846
Josef Bacik3893e332011-01-31 16:03:11 -05008847 if (ret && !err)
8848 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008849
Josef Bacik3893e332011-01-31 16:03:11 -05008850 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008851}
8852
Sven Wegener3b963622008-06-09 21:57:42 -04008853/*
Chris Masond352ac62008-09-29 15:18:18 -04008854 * create a new subvolume directory/inode (helper for the ioctl).
8855 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008856int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008857 struct btrfs_root *new_root,
8858 struct btrfs_root *parent_root,
8859 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008860{
Chris Mason39279cc2007-06-12 06:35:45 -04008861 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008862 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008863 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008864
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008865 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8866 new_dirid, new_dirid,
8867 S_IFDIR | (~current_umask() & S_IRWXUGO),
8868 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008869 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008870 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008871 inode->i_op = &btrfs_dir_inode_operations;
8872 inode->i_fop = &btrfs_dir_file_operations;
8873
Miklos Szeredibfe86842011-10-28 14:13:29 +02008874 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008875 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008876 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008877
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008878 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8879 if (err)
8880 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008881 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008882 new_root->root_key.objectid, err);
8883
Yan, Zheng76dda932009-09-21 16:00:26 -04008884 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008885
Yan, Zheng76dda932009-09-21 16:00:26 -04008886 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008887 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008888}
8889
Chris Mason39279cc2007-06-12 06:35:45 -04008890struct inode *btrfs_alloc_inode(struct super_block *sb)
8891{
8892 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008893 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008894
8895 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8896 if (!ei)
8897 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008898
8899 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008900 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008901 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008902 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008903 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008904 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008905 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008906 ei->disk_i_size = 0;
8907 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008908 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008909 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008910 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008911 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008912 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008913
Josef Bacik9e0baf62011-07-15 15:16:44 +00008914 spin_lock_init(&ei->lock);
8915 ei->outstanding_extents = 0;
8916 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008917
Josef Bacik72ac3c02012-05-23 14:13:11 -04008918 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008919 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008920
Miao Xie16cdcec2011-04-22 18:12:22 +08008921 ei->delayed_node = NULL;
8922
chandan r9cc97d62012-07-04 12:48:07 +05308923 ei->i_otime.tv_sec = 0;
8924 ei->i_otime.tv_nsec = 0;
8925
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008926 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008927 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008928 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8929 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008930 ei->io_tree.track_uptodate = 1;
8931 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008932 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008933 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008934 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008935 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008936 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008937 RB_CLEAR_NODE(&ei->rb_node);
8938
8939 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008940}
8941
Josef Bacikaaedb552013-10-11 14:44:09 -04008942#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8943void btrfs_test_destroy_inode(struct inode *inode)
8944{
8945 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8946 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8947}
8948#endif
8949
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008950static void btrfs_i_callback(struct rcu_head *head)
8951{
8952 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008953 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8954}
8955
Chris Mason39279cc2007-06-12 06:35:45 -04008956void btrfs_destroy_inode(struct inode *inode)
8957{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008958 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008959 struct btrfs_root *root = BTRFS_I(inode)->root;
8960
Al Virob3d9b7a2012-06-09 13:51:19 -04008961 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008962 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008963 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8964 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008965 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8966 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008967 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008968
Chris Mason5a3f23d2009-03-31 13:27:11 -04008969 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008970 * This can happen where we create an inode, but somebody else also
8971 * created the same inode and we need to destroy the one we already
8972 * created.
8973 */
8974 if (!root)
8975 goto free;
8976
Josef Bacik8a35d952012-05-23 14:26:42 -04008977 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8978 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008979 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008980 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008981 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008982 }
Josef Bacik7b128762008-07-24 12:17:14 -04008983
Chris Masond3977122009-01-05 21:25:51 -05008984 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008985 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8986 if (!ordered)
8987 break;
8988 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008989 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008990 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008991 btrfs_remove_ordered_extent(inode, ordered);
8992 btrfs_put_ordered_extent(ordered);
8993 btrfs_put_ordered_extent(ordered);
8994 }
8995 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008996 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008997 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008998free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008999 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009000}
9001
Al Viro45321ac2010-06-07 13:43:19 -04009002int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009003{
9004 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009005
Naohiro Aota6379ef92013-06-06 09:56:34 +00009006 if (root == NULL)
9007 return 1;
9008
Liu Bofa6ac872013-02-20 14:10:23 +00009009 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009010 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009011 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009012 else
Al Viro45321ac2010-06-07 13:43:19 -04009013 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009014}
9015
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009016static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009017{
9018 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9019
9020 inode_init_once(&ei->vfs_inode);
9021}
9022
9023void btrfs_destroy_cachep(void)
9024{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009025 /*
9026 * Make sure all delayed rcu free inodes are flushed before we
9027 * destroy cache.
9028 */
9029 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04009030 if (btrfs_inode_cachep)
9031 kmem_cache_destroy(btrfs_inode_cachep);
9032 if (btrfs_trans_handle_cachep)
9033 kmem_cache_destroy(btrfs_trans_handle_cachep);
9034 if (btrfs_transaction_cachep)
9035 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009036 if (btrfs_path_cachep)
9037 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05009038 if (btrfs_free_space_cachep)
9039 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009040 if (btrfs_delalloc_work_cachep)
9041 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009042}
9043
9044int btrfs_init_cachep(void)
9045{
David Sterba837e1972012-09-07 03:00:48 -06009046 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009047 sizeof(struct btrfs_inode), 0,
9048 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009049 if (!btrfs_inode_cachep)
9050 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009051
David Sterba837e1972012-09-07 03:00:48 -06009052 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009053 sizeof(struct btrfs_trans_handle), 0,
9054 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009055 if (!btrfs_trans_handle_cachep)
9056 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009057
David Sterba837e1972012-09-07 03:00:48 -06009058 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009059 sizeof(struct btrfs_transaction), 0,
9060 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009061 if (!btrfs_transaction_cachep)
9062 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009063
David Sterba837e1972012-09-07 03:00:48 -06009064 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009065 sizeof(struct btrfs_path), 0,
9066 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009067 if (!btrfs_path_cachep)
9068 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009069
David Sterba837e1972012-09-07 03:00:48 -06009070 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009071 sizeof(struct btrfs_free_space), 0,
9072 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
9073 if (!btrfs_free_space_cachep)
9074 goto fail;
9075
Miao Xie8ccf6f192012-10-25 09:28:04 +00009076 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
9077 sizeof(struct btrfs_delalloc_work), 0,
9078 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
9079 NULL);
9080 if (!btrfs_delalloc_work_cachep)
9081 goto fail;
9082
Chris Mason39279cc2007-06-12 06:35:45 -04009083 return 0;
9084fail:
9085 btrfs_destroy_cachep();
9086 return -ENOMEM;
9087}
9088
9089static int btrfs_getattr(struct vfsmount *mnt,
9090 struct dentry *dentry, struct kstat *stat)
9091{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009092 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009093 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009094 u32 blocksize = inode->i_sb->s_blocksize;
9095
Chris Mason39279cc2007-06-12 06:35:45 -04009096 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009097 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05009098 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009099
9100 spin_lock(&BTRFS_I(inode)->lock);
9101 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9102 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009103 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009104 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009105 return 0;
9106}
9107
Chris Masond3977122009-01-05 21:25:51 -05009108static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9109 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009110{
9111 struct btrfs_trans_handle *trans;
9112 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009113 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009114 struct inode *new_inode = d_inode(new_dentry);
9115 struct inode *old_inode = d_inode(old_dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04009116 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009117 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009118 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009119 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009120 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009121
Li Zefan33345d012011-04-20 10:31:50 +08009122 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009123 return -EPERM;
9124
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009125 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009126 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009127 return -EXDEV;
9128
Li Zefan33345d012011-04-20 10:31:50 +08009129 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9130 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009131 return -ENOTEMPTY;
9132
Chris Mason39279cc2007-06-12 06:35:45 -04009133 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009134 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009135 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009136
9137
9138 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009139 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009140 new_dentry->d_name.name,
9141 new_dentry->d_name.len);
9142
9143 if (ret) {
9144 if (ret == -EEXIST) {
9145 /* we shouldn't get
9146 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309147 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009148 return ret;
9149 }
9150 } else {
9151 /* maybe -EOVERFLOW */
9152 return ret;
9153 }
9154 }
9155 ret = 0;
9156
Chris Mason5a3f23d2009-03-31 13:27:11 -04009157 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009158 * we're using rename to replace one file with another. Start IO on it
9159 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009160 */
Chris Mason8d875f92014-08-12 10:47:42 -07009161 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009162 filemap_flush(old_inode->i_mapping);
9163
Yan, Zheng76dda932009-09-21 16:00:26 -04009164 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009165 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009166 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009167 /*
9168 * We want to reserve the absolute worst case amount of items. So if
9169 * both inodes are subvols and we need to unlink them then that would
9170 * require 4 item modifications, but if they are both normal inodes it
9171 * would require 5 item modifications, so we'll assume their normal
9172 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9173 * should cover the worst case number of items we'll modify.
9174 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04009175 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009176 if (IS_ERR(trans)) {
9177 ret = PTR_ERR(trans);
9178 goto out_notrans;
9179 }
Chris Mason5f39d392007-10-15 16:14:19 -04009180
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009181 if (dest != root)
9182 btrfs_record_root_in_trans(trans, dest);
9183
Yan, Zhenga5719522009-09-24 09:17:31 -04009184 ret = btrfs_set_inode_index(new_dir, &index);
9185 if (ret)
9186 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009187
Miao Xie67de1172013-12-26 13:07:06 +08009188 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009189 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009190 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009191 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009192 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009193 ret = btrfs_insert_inode_ref(trans, dest,
9194 new_dentry->d_name.name,
9195 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009196 old_ino,
9197 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009198 if (ret)
9199 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009200 /*
9201 * this is an ugly little race, but the rename is required
9202 * to make sure that if we crash, the inode is either at the
9203 * old name or the new one. pinning the log transaction lets
9204 * us make sure we don't allow a log commit to come in after
9205 * we unlink the name but before we add the new name back in.
9206 */
9207 btrfs_pin_log_trans(root);
9208 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009209
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009210 inode_inc_iversion(old_dir);
9211 inode_inc_iversion(new_dir);
9212 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009213 old_dir->i_ctime = old_dir->i_mtime = ctime;
9214 new_dir->i_ctime = new_dir->i_mtime = ctime;
9215 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04009216
Chris Mason12fcfd22009-03-24 10:24:20 -04009217 if (old_dentry->d_parent != new_dentry->d_parent)
9218 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9219
Li Zefan33345d012011-04-20 10:31:50 +08009220 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009221 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9222 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9223 old_dentry->d_name.name,
9224 old_dentry->d_name.len);
9225 } else {
Al Viro92986792011-03-04 17:14:37 +00009226 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009227 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009228 old_dentry->d_name.name,
9229 old_dentry->d_name.len);
9230 if (!ret)
9231 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009232 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009233 if (ret) {
9234 btrfs_abort_transaction(trans, root, ret);
9235 goto out_fail;
9236 }
Chris Mason39279cc2007-06-12 06:35:45 -04009237
9238 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009239 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009240 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08009241 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009242 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9243 root_objectid = BTRFS_I(new_inode)->location.objectid;
9244 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9245 root_objectid,
9246 new_dentry->d_name.name,
9247 new_dentry->d_name.len);
9248 BUG_ON(new_inode->i_nlink == 0);
9249 } else {
9250 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009251 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009252 new_dentry->d_name.name,
9253 new_dentry->d_name.len);
9254 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009255 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009256 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009257 if (ret) {
9258 btrfs_abort_transaction(trans, root, ret);
9259 goto out_fail;
9260 }
Chris Mason39279cc2007-06-12 06:35:45 -04009261 }
Josef Bacikaec74772008-07-24 12:12:38 -04009262
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009263 ret = btrfs_add_link(trans, new_dir, old_inode,
9264 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009265 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009266 if (ret) {
9267 btrfs_abort_transaction(trans, root, ret);
9268 goto out_fail;
9269 }
Chris Mason39279cc2007-06-12 06:35:45 -04009270
Miao Xie67de1172013-12-26 13:07:06 +08009271 if (old_inode->i_nlink == 1)
9272 BTRFS_I(old_inode)->dir_index = index;
9273
Li Zefan33345d012011-04-20 10:31:50 +08009274 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04009275 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00009276 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009277 btrfs_end_log_trans(root);
9278 }
Chris Mason39279cc2007-06-12 06:35:45 -04009279out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009280 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009281out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009282 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009283 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009284
Chris Mason39279cc2007-06-12 06:35:45 -04009285 return ret;
9286}
9287
Miklos Szeredi80ace852014-07-23 15:15:32 +02009288static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9289 struct inode *new_dir, struct dentry *new_dentry,
9290 unsigned int flags)
9291{
9292 if (flags & ~RENAME_NOREPLACE)
9293 return -EINVAL;
9294
9295 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
9296}
9297
Miao Xie8ccf6f192012-10-25 09:28:04 +00009298static void btrfs_run_delalloc_work(struct btrfs_work *work)
9299{
9300 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009301 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009302
9303 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9304 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009305 inode = delalloc_work->inode;
9306 if (delalloc_work->wait) {
9307 btrfs_wait_ordered_range(inode, 0, (u64)-1);
9308 } else {
9309 filemap_flush(inode->i_mapping);
9310 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9311 &BTRFS_I(inode)->runtime_flags))
9312 filemap_flush(inode->i_mapping);
9313 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00009314
9315 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009316 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009317 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009318 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009319 complete(&delalloc_work->completion);
9320}
9321
9322struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
9323 int wait, int delay_iput)
9324{
9325 struct btrfs_delalloc_work *work;
9326
9327 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
9328 if (!work)
9329 return NULL;
9330
9331 init_completion(&work->completion);
9332 INIT_LIST_HEAD(&work->list);
9333 work->inode = inode;
9334 work->wait = wait;
9335 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009336 WARN_ON_ONCE(!inode);
9337 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9338 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009339
9340 return work;
9341}
9342
9343void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9344{
9345 wait_for_completion(&work->completion);
9346 kmem_cache_free(btrfs_delalloc_work_cachep, work);
9347}
9348
Chris Masond352ac62008-09-29 15:18:18 -04009349/*
9350 * some fairly slow code that needs optimization. This walks the list
9351 * of all the inodes with pending delalloc and forces them to disk.
9352 */
Miao Xie6c255e62014-03-06 13:55:01 +08009353static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9354 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009355{
Chris Masonea8c2812008-08-04 23:17:27 -04009356 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009357 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009358 struct btrfs_delalloc_work *work, *next;
9359 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009360 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009361 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009362
Miao Xie8ccf6f192012-10-25 09:28:04 +00009363 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009364 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009365
Miao Xie573bfb72014-03-06 13:55:03 +08009366 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009367 spin_lock(&root->delalloc_lock);
9368 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009369 while (!list_empty(&splice)) {
9370 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009371 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009372
Miao Xieeb73c1b2013-05-15 07:48:22 +00009373 list_move_tail(&binode->delalloc_inodes,
9374 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009375 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009376 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009377 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009378 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009379 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009380 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009381
9382 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009383 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009384 if (delay_iput)
9385 btrfs_add_delayed_iput(inode);
9386 else
9387 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009388 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009389 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009390 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009391 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009392 btrfs_queue_work(root->fs_info->flush_workers,
9393 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009394 ret++;
9395 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009396 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009397 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009398 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009399 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009400 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009401
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009402out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009403 list_for_each_entry_safe(work, next, &works, list) {
9404 list_del_init(&work->list);
9405 btrfs_wait_and_free_delalloc_work(work);
9406 }
9407
Miao Xieeb73c1b2013-05-15 07:48:22 +00009408 if (!list_empty_careful(&splice)) {
9409 spin_lock(&root->delalloc_lock);
9410 list_splice_tail(&splice, &root->delalloc_inodes);
9411 spin_unlock(&root->delalloc_lock);
9412 }
Miao Xie573bfb72014-03-06 13:55:03 +08009413 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009414 return ret;
9415}
9416
9417int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9418{
9419 int ret;
9420
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009421 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009422 return -EROFS;
9423
Miao Xie6c255e62014-03-06 13:55:01 +08009424 ret = __start_delalloc_inodes(root, delay_iput, -1);
9425 if (ret > 0)
9426 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009427 /*
9428 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009429 * we have to make sure the IO is actually started and that
9430 * ordered extents get created before we return
9431 */
9432 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009433 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009434 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009435 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009436 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9437 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009438 }
9439 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009440 return ret;
9441}
9442
Miao Xie6c255e62014-03-06 13:55:01 +08009443int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9444 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009445{
9446 struct btrfs_root *root;
9447 struct list_head splice;
9448 int ret;
9449
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009450 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009451 return -EROFS;
9452
9453 INIT_LIST_HEAD(&splice);
9454
Miao Xie573bfb72014-03-06 13:55:03 +08009455 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009456 spin_lock(&fs_info->delalloc_root_lock);
9457 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009458 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009459 root = list_first_entry(&splice, struct btrfs_root,
9460 delalloc_root);
9461 root = btrfs_grab_fs_root(root);
9462 BUG_ON(!root);
9463 list_move_tail(&root->delalloc_root,
9464 &fs_info->delalloc_roots);
9465 spin_unlock(&fs_info->delalloc_root_lock);
9466
Miao Xie6c255e62014-03-06 13:55:01 +08009467 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009468 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009469 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009470 goto out;
9471
Miao Xie6c255e62014-03-06 13:55:01 +08009472 if (nr != -1) {
9473 nr -= ret;
9474 WARN_ON(nr < 0);
9475 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009476 spin_lock(&fs_info->delalloc_root_lock);
9477 }
9478 spin_unlock(&fs_info->delalloc_root_lock);
9479
Miao Xie6c255e62014-03-06 13:55:01 +08009480 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009481 atomic_inc(&fs_info->async_submit_draining);
9482 while (atomic_read(&fs_info->nr_async_submits) ||
9483 atomic_read(&fs_info->async_delalloc_pages)) {
9484 wait_event(fs_info->async_submit_wait,
9485 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9486 atomic_read(&fs_info->async_delalloc_pages) == 0));
9487 }
9488 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009489out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009490 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009491 spin_lock(&fs_info->delalloc_root_lock);
9492 list_splice_tail(&splice, &fs_info->delalloc_roots);
9493 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009494 }
Miao Xie573bfb72014-03-06 13:55:03 +08009495 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009496 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009497}
9498
Chris Mason39279cc2007-06-12 06:35:45 -04009499static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9500 const char *symname)
9501{
9502 struct btrfs_trans_handle *trans;
9503 struct btrfs_root *root = BTRFS_I(dir)->root;
9504 struct btrfs_path *path;
9505 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009506 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009507 int err;
9508 int drop_inode = 0;
9509 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309510 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009511 int name_len;
9512 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009513 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009514 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009515 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009516
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009517 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009518 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9519 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009520
Josef Bacik9ed74f22009-09-11 16:12:44 -04009521 /*
9522 * 2 items for inode item and ref
9523 * 2 items for dir items
9524 * 1 item for xattr if selinux is on
9525 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009526 trans = btrfs_start_transaction(root, 5);
9527 if (IS_ERR(trans))
9528 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009529
Li Zefan581bb052011-04-20 10:06:11 +08009530 err = btrfs_find_free_ino(root, &objectid);
9531 if (err)
9532 goto out_unlock;
9533
Josef Bacikaec74772008-07-24 12:12:38 -04009534 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009535 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04009536 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009537 if (IS_ERR(inode)) {
9538 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009539 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009540 }
Chris Mason39279cc2007-06-12 06:35:45 -04009541
Casey Schauflerad19db72011-12-15 10:09:07 -05009542 /*
9543 * If the active LSM wants to access the inode during
9544 * d_instantiate it needs these. Smack checks to see
9545 * if the filesystem supports xattrs by looking at the
9546 * ops vector.
9547 */
9548 inode->i_fop = &btrfs_file_operations;
9549 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009550 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009551 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9552
9553 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9554 if (err)
9555 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009556
Josef Bacika1b075d2010-11-19 20:36:11 +00009557 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009558 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009559 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009560
9561 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009562 if (!path) {
9563 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009564 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009565 }
Li Zefan33345d012011-04-20 10:31:50 +08009566 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009567 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009568 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009569 datasize = btrfs_file_extent_calc_inline_size(name_len);
9570 err = btrfs_insert_empty_item(trans, root, path, &key,
9571 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009572 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009573 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009574 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009575 }
Chris Mason5f39d392007-10-15 16:14:19 -04009576 leaf = path->nodes[0];
9577 ei = btrfs_item_ptr(leaf, path->slots[0],
9578 struct btrfs_file_extent_item);
9579 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9580 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009581 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009582 btrfs_set_file_extent_encryption(leaf, ei, 0);
9583 btrfs_set_file_extent_compression(leaf, ei, 0);
9584 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9585 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9586
Chris Mason39279cc2007-06-12 06:35:45 -04009587 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009588 write_extent_buffer(leaf, symname, ptr, name_len);
9589 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009590 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009591
Chris Mason39279cc2007-06-12 06:35:45 -04009592 inode->i_op = &btrfs_symlink_inode_operations;
9593 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -04009594 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009595 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009596 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009597 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009598 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009599 goto out_unlock_inode;
9600 }
9601
9602 unlock_new_inode(inode);
9603 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009604
9605out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009606 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009607 if (drop_inode) {
9608 inode_dec_link_count(inode);
9609 iput(inode);
9610 }
Liu Bob53d3f52012-11-14 14:34:34 +00009611 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009612 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009613
9614out_unlock_inode:
9615 drop_inode = 1;
9616 unlock_new_inode(inode);
9617 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009618}
Chris Mason16432982008-04-10 10:23:21 -04009619
Josef Bacik0af3d002010-06-21 14:48:16 -04009620static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9621 u64 start, u64 num_bytes, u64 min_size,
9622 loff_t actual_len, u64 *alloc_hint,
9623 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009624{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009625 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9626 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009627 struct btrfs_root *root = BTRFS_I(inode)->root;
9628 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009629 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009630 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009631 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009632 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009633 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009634
Josef Bacik0af3d002010-06-21 14:48:16 -04009635 if (trans)
9636 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009637 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009638 if (own_trans) {
9639 trans = btrfs_start_transaction(root, 3);
9640 if (IS_ERR(trans)) {
9641 ret = PTR_ERR(trans);
9642 break;
9643 }
Yan Zhengd899e052008-10-30 14:25:28 -04009644 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009645
Chris Mason154ea282013-03-05 11:11:26 -05009646 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9647 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009648 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009649 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009650 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009651 if (own_trans)
9652 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009653 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009654 }
9655
Yan Zhengd899e052008-10-30 14:25:28 -04009656 ret = insert_reserved_file_extent(trans, inode,
9657 cur_offset, ins.objectid,
9658 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009659 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009660 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009661 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009662 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009663 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009664 btrfs_abort_transaction(trans, root, ret);
9665 if (own_trans)
9666 btrfs_end_transaction(trans, root);
9667 break;
9668 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009669
Chris Masona1ed8352009-09-11 12:27:37 -04009670 btrfs_drop_extent_cache(inode, cur_offset,
9671 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009672
Josef Bacik5dc562c2012-08-17 13:14:17 -04009673 em = alloc_extent_map();
9674 if (!em) {
9675 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9676 &BTRFS_I(inode)->runtime_flags);
9677 goto next;
9678 }
9679
9680 em->start = cur_offset;
9681 em->orig_start = cur_offset;
9682 em->len = ins.offset;
9683 em->block_start = ins.objectid;
9684 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009685 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009686 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009687 em->bdev = root->fs_info->fs_devices->latest_bdev;
9688 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9689 em->generation = trans->transid;
9690
9691 while (1) {
9692 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009693 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009694 write_unlock(&em_tree->lock);
9695 if (ret != -EEXIST)
9696 break;
9697 btrfs_drop_extent_cache(inode, cur_offset,
9698 cur_offset + ins.offset - 1,
9699 0);
9700 }
9701 free_extent_map(em);
9702next:
Yan Zhengd899e052008-10-30 14:25:28 -04009703 num_bytes -= ins.offset;
9704 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009705 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009706
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009707 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009708 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009709 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009710 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009711 (actual_len > inode->i_size) &&
9712 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009713 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009714 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009715 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009716 i_size = cur_offset;
9717 i_size_write(inode, i_size);
9718 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009719 }
9720
Yan Zhengd899e052008-10-30 14:25:28 -04009721 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009722
9723 if (ret) {
9724 btrfs_abort_transaction(trans, root, ret);
9725 if (own_trans)
9726 btrfs_end_transaction(trans, root);
9727 break;
9728 }
Yan Zhengd899e052008-10-30 14:25:28 -04009729
Josef Bacik0af3d002010-06-21 14:48:16 -04009730 if (own_trans)
9731 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009732 }
Yan Zhengd899e052008-10-30 14:25:28 -04009733 return ret;
9734}
9735
Josef Bacik0af3d002010-06-21 14:48:16 -04009736int btrfs_prealloc_file_range(struct inode *inode, int mode,
9737 u64 start, u64 num_bytes, u64 min_size,
9738 loff_t actual_len, u64 *alloc_hint)
9739{
9740 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9741 min_size, actual_len, alloc_hint,
9742 NULL);
9743}
9744
9745int btrfs_prealloc_file_range_trans(struct inode *inode,
9746 struct btrfs_trans_handle *trans, int mode,
9747 u64 start, u64 num_bytes, u64 min_size,
9748 loff_t actual_len, u64 *alloc_hint)
9749{
9750 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9751 min_size, actual_len, alloc_hint, trans);
9752}
9753
Chris Masone6dcd2d2008-07-17 12:53:50 -04009754static int btrfs_set_page_dirty(struct page *page)
9755{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009756 return __set_page_dirty_nobuffers(page);
9757}
9758
Al Viro10556cb2011-06-20 19:28:19 -04009759static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009760{
Li Zefanb83cc962010-12-20 16:04:08 +08009761 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009762 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009763
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009764 if (mask & MAY_WRITE &&
9765 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9766 if (btrfs_root_readonly(root))
9767 return -EROFS;
9768 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9769 return -EACCES;
9770 }
Al Viro2830ba72011-06-20 19:16:29 -04009771 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009772}
Chris Mason39279cc2007-06-12 06:35:45 -04009773
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009774static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9775{
9776 struct btrfs_trans_handle *trans;
9777 struct btrfs_root *root = BTRFS_I(dir)->root;
9778 struct inode *inode = NULL;
9779 u64 objectid;
9780 u64 index;
9781 int ret = 0;
9782
9783 /*
9784 * 5 units required for adding orphan entry
9785 */
9786 trans = btrfs_start_transaction(root, 5);
9787 if (IS_ERR(trans))
9788 return PTR_ERR(trans);
9789
9790 ret = btrfs_find_free_ino(root, &objectid);
9791 if (ret)
9792 goto out;
9793
9794 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9795 btrfs_ino(dir), objectid, mode, &index);
9796 if (IS_ERR(inode)) {
9797 ret = PTR_ERR(inode);
9798 inode = NULL;
9799 goto out;
9800 }
9801
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009802 inode->i_fop = &btrfs_file_operations;
9803 inode->i_op = &btrfs_file_inode_operations;
9804
9805 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009806 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9807
Chris Masonb0d5d102014-09-08 13:08:51 -07009808 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9809 if (ret)
9810 goto out_inode;
9811
9812 ret = btrfs_update_inode(trans, root, inode);
9813 if (ret)
9814 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009815 ret = btrfs_orphan_add(trans, inode);
9816 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009817 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009818
Filipe Manana5762b5c2014-08-01 00:10:32 +01009819 /*
9820 * We set number of links to 0 in btrfs_new_inode(), and here we set
9821 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9822 * through:
9823 *
9824 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9825 */
9826 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009827 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009828 d_tmpfile(dentry, inode);
9829 mark_inode_dirty(inode);
9830
9831out:
9832 btrfs_end_transaction(trans, root);
9833 if (ret)
9834 iput(inode);
9835 btrfs_balance_delayed_items(root);
9836 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009837 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009838
9839out_inode:
9840 unlock_new_inode(inode);
9841 goto out;
9842
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009843}
9844
Filipe Mananab38ef712014-11-13 17:01:45 +00009845/* Inspired by filemap_check_errors() */
9846int btrfs_inode_check_errors(struct inode *inode)
9847{
9848 int ret = 0;
9849
9850 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
9851 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
9852 ret = -ENOSPC;
9853 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
9854 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
9855 ret = -EIO;
9856
9857 return ret;
9858}
9859
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009860static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009861 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009862 .lookup = btrfs_lookup,
9863 .create = btrfs_create,
9864 .unlink = btrfs_unlink,
9865 .link = btrfs_link,
9866 .mkdir = btrfs_mkdir,
9867 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009868 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009869 .symlink = btrfs_symlink,
9870 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009871 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009872 .setxattr = btrfs_setxattr,
9873 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009874 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009875 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009876 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009877 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009878 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009879 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009880 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009881};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009882static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009883 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009884 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009885 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009886 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009887 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009888};
Yan, Zheng76dda932009-09-21 16:00:26 -04009889
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009890static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009891 .llseek = generic_file_llseek,
9892 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009893 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009894 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009895#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009896 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009897#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009898 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009899 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009900};
9901
Chris Masond1310b22008-01-24 16:13:08 -05009902static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009903 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009904 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009905 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009906 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009907 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009908 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009909 .set_bit_hook = btrfs_set_bit_hook,
9910 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009911 .merge_extent_hook = btrfs_merge_extent_hook,
9912 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009913};
9914
Chris Mason35054392009-01-21 13:11:13 -05009915/*
9916 * btrfs doesn't support the bmap operation because swapfiles
9917 * use bmap to make a mapping of extents in the file. They assume
9918 * these extents won't change over the life of the file and they
9919 * use the bmap result to do IO directly to the drive.
9920 *
9921 * the btrfs bmap call would return logical addresses that aren't
9922 * suitable for IO and they also will change frequently as COW
9923 * operations happen. So, swapfile + btrfs == corruption.
9924 *
9925 * For now we're avoiding this by dropping bmap.
9926 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009927static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009928 .readpage = btrfs_readpage,
9929 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04009930 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009931 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009932 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009933 .invalidatepage = btrfs_invalidatepage,
9934 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009935 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009936 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009937};
9938
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009939static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009940 .readpage = btrfs_readpage,
9941 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009942 .invalidatepage = btrfs_invalidatepage,
9943 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009944};
9945
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009946static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009947 .getattr = btrfs_getattr,
9948 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009949 .setxattr = btrfs_setxattr,
9950 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009951 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009952 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009953 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009954 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009955 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009956 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009957 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009958};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009959static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009960 .getattr = btrfs_getattr,
9961 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009962 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009963 .setxattr = btrfs_setxattr,
9964 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009965 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009966 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009967 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009968 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009969 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009970};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009971static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009972 .readlink = generic_readlink,
9973 .follow_link = page_follow_link_light,
9974 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009975 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009976 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009977 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009978 .setxattr = btrfs_setxattr,
9979 .getxattr = btrfs_getxattr,
9980 .listxattr = btrfs_listxattr,
9981 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009982 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009983};
Yan, Zheng76dda932009-09-21 16:00:26 -04009984
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009985const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009986 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009987 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009988};