blob: c21ed7b1469131d23bc786bd5b28b603217ee852 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050043#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040044#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080045#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080062#include "qgroup.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070069static const struct inode_operations btrfs_dir_inode_operations;
70static const struct inode_operations btrfs_symlink_inode_operations;
71static const struct inode_operations btrfs_dir_ro_inode_operations;
72static const struct inode_operations btrfs_special_inode_operations;
73static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070074static const struct address_space_operations btrfs_aops;
75static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070076static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050077static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040078
79static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000080static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081struct kmem_cache *btrfs_trans_handle_cachep;
82struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040083struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050084struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040085
86#define S_SHIFT 12
87static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
88 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
89 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
90 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
91 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
92 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
93 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
94 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
95};
96
Eric Sandeen3972f262013-01-12 02:57:22 +000097static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050098static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040099static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500100static noinline int cow_file_range(struct inode *inode,
101 struct page *locked_page,
102 u64 start, u64 end, int *page_started,
103 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400104static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
105 u64 len, u64 orig_start,
106 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400107 u64 orig_block_len, u64 ram_bytes,
108 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400109
Eric Sandeen48a3b632013-04-25 20:41:01 +0000110static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400111
Josef Bacik6a3891c2015-03-16 17:38:52 -0400112#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
113void btrfs_test_inode_set_ops(struct inode *inode)
114{
115 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
116}
117#endif
118
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000119static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500120 struct inode *inode, struct inode *dir,
121 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500122{
123 int err;
124
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000125 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500126 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500127 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500128 return err;
129}
130
Chris Masond352ac62008-09-29 15:18:18 -0400131/*
Chris Masonc8b97812008-10-29 14:49:59 -0400132 * this does all the hard work for inserting an inline extent into
133 * the btree. The caller should have done a btrfs_drop_extents so that
134 * no overlapping inline items exist in the btree
135 */
Chris Mason40f76582014-05-21 13:35:51 -0700136static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000137 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400138 struct btrfs_root *root, struct inode *inode,
139 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000140 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400141 struct page **compressed_pages)
142{
Chris Masonc8b97812008-10-29 14:49:59 -0400143 struct extent_buffer *leaf;
144 struct page *page = NULL;
145 char *kaddr;
146 unsigned long ptr;
147 struct btrfs_file_extent_item *ei;
148 int err = 0;
149 int ret;
150 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400151 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400152
Li Zefanfe3f5662011-03-28 08:30:38 +0000153 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400154 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400155
Chris Masonc8b97812008-10-29 14:49:59 -0400156 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000157
158 if (!extent_inserted) {
159 struct btrfs_key key;
160 size_t datasize;
161
162 key.objectid = btrfs_ino(inode);
163 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200164 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165
166 datasize = btrfs_file_extent_calc_inline_size(cur_size);
167 path->leave_spinning = 1;
168 ret = btrfs_insert_empty_item(trans, root, path, &key,
169 datasize);
170 if (ret) {
171 err = ret;
172 goto fail;
173 }
Chris Masonc8b97812008-10-29 14:49:59 -0400174 }
175 leaf = path->nodes[0];
176 ei = btrfs_item_ptr(leaf, path->slots[0],
177 struct btrfs_file_extent_item);
178 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
179 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
180 btrfs_set_file_extent_encryption(leaf, ei, 0);
181 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
182 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
183 ptr = btrfs_file_extent_inline_start(ei);
184
Li Zefan261507a02010-12-17 14:21:50 +0800185 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400186 struct page *cpage;
187 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500188 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400189 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500190 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400191 PAGE_CACHE_SIZE);
192
Cong Wang7ac687d2011-11-25 23:14:28 +0800193 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400194 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800195 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400196
197 i++;
198 ptr += cur_size;
199 compressed_size -= cur_size;
200 }
201 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800202 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400203 } else {
204 page = find_get_page(inode->i_mapping,
205 start >> PAGE_CACHE_SHIFT);
206 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800207 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400208 offset = start & (PAGE_CACHE_SIZE - 1);
209 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800210 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400211 page_cache_release(page);
212 }
213 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000214 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400215
Yan, Zhengc2167752009-11-12 09:34:21 +0000216 /*
217 * we're an inline extent, so nobody can
218 * extend the file past i_size without locking
219 * a page we already have locked.
220 *
221 * We must do any isize and inode updates
222 * before we unlock the pages. Otherwise we
223 * could end up racing with unlink.
224 */
Chris Masonc8b97812008-10-29 14:49:59 -0400225 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100226 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000227
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100228 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400229fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400230 return err;
231}
232
233
234/*
235 * conditionally insert an inline extent into the file. This
236 * does the checks required to make sure the data is small enough
237 * to fit as an inline extent.
238 */
Josef Bacik00361582013-08-14 14:02:47 -0400239static noinline int cow_file_range_inline(struct btrfs_root *root,
240 struct inode *inode, u64 start,
241 u64 end, size_t compressed_size,
242 int compress_type,
243 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400244{
Josef Bacik00361582013-08-14 14:02:47 -0400245 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400246 u64 isize = i_size_read(inode);
247 u64 actual_end = min(end + 1, isize);
248 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000249 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400250 u64 data_len = inline_len;
251 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000252 struct btrfs_path *path;
253 int extent_inserted = 0;
254 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400255
256 if (compressed_size)
257 data_len = compressed_size;
258
259 if (start > 0 ||
Wang Shilong354877b2014-07-17 11:44:11 +0800260 actual_end > PAGE_CACHE_SIZE ||
261 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400262 (!compressed_size &&
263 (actual_end & (root->sectorsize - 1)) == 0) ||
264 end + 1 < isize ||
265 data_len > root->fs_info->max_inline) {
266 return 1;
267 }
268
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000269 path = btrfs_alloc_path();
270 if (!path)
271 return -ENOMEM;
272
Josef Bacik00361582013-08-14 14:02:47 -0400273 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000274 if (IS_ERR(trans)) {
275 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400276 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000277 }
Josef Bacik00361582013-08-14 14:02:47 -0400278 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
279
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000280 if (compressed_size && compressed_pages)
281 extent_item_size = btrfs_file_extent_calc_inline_size(
282 compressed_size);
283 else
284 extent_item_size = btrfs_file_extent_calc_inline_size(
285 inline_len);
286
287 ret = __btrfs_drop_extents(trans, root, inode, path,
288 start, aligned_end, NULL,
289 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400290 if (ret) {
291 btrfs_abort_transaction(trans, root, ret);
292 goto out;
293 }
Chris Masonc8b97812008-10-29 14:49:59 -0400294
295 if (isize > actual_end)
296 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000297 ret = insert_inline_extent(trans, path, extent_inserted,
298 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400299 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000300 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400301 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100302 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400303 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400304 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400305 ret = 1;
306 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100307 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400308
Josef Bacikbdc20e62013-02-28 13:23:38 -0500309 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400310 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400311 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400312out:
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000313 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400314 btrfs_end_transaction(trans, root);
315 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400316}
317
Chris Mason771ed682008-11-06 22:02:51 -0500318struct async_extent {
319 u64 start;
320 u64 ram_size;
321 u64 compressed_size;
322 struct page **pages;
323 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800324 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500325 struct list_head list;
326};
327
328struct async_cow {
329 struct inode *inode;
330 struct btrfs_root *root;
331 struct page *locked_page;
332 u64 start;
333 u64 end;
334 struct list_head extents;
335 struct btrfs_work work;
336};
337
338static noinline int add_async_extent(struct async_cow *cow,
339 u64 start, u64 ram_size,
340 u64 compressed_size,
341 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800342 unsigned long nr_pages,
343 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500344{
345 struct async_extent *async_extent;
346
347 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100348 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500349 async_extent->start = start;
350 async_extent->ram_size = ram_size;
351 async_extent->compressed_size = compressed_size;
352 async_extent->pages = pages;
353 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800354 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500355 list_add_tail(&async_extent->list, &cow->extents);
356 return 0;
357}
358
Wang Shilongf79707b2014-07-17 11:44:09 +0800359static inline int inode_need_compress(struct inode *inode)
360{
361 struct btrfs_root *root = BTRFS_I(inode)->root;
362
363 /* force compress */
364 if (btrfs_test_opt(root, FORCE_COMPRESS))
365 return 1;
366 /* bad compression ratios */
367 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
368 return 0;
369 if (btrfs_test_opt(root, COMPRESS) ||
370 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
371 BTRFS_I(inode)->force_compress)
372 return 1;
373 return 0;
374}
375
Chris Masonc8b97812008-10-29 14:49:59 -0400376/*
Chris Mason771ed682008-11-06 22:02:51 -0500377 * we create compressed extents in two phases. The first
378 * phase compresses a range of pages that have already been
379 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400380 *
Chris Mason771ed682008-11-06 22:02:51 -0500381 * This is done inside an ordered work queue, and the compression
382 * is spread across many cpus. The actual IO submission is step
383 * two, and the ordered work queue takes care of making sure that
384 * happens in the same order things were put onto the queue by
385 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400386 *
Chris Mason771ed682008-11-06 22:02:51 -0500387 * If this code finds it can't get good compression, it puts an
388 * entry onto the work queue to write the uncompressed bytes. This
389 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300390 * are written in the same order that the flusher thread sent them
391 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400392 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100393static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500394 struct page *locked_page,
395 u64 start, u64 end,
396 struct async_cow *async_cow,
397 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400398{
399 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400400 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400401 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400402 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500403 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400404 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400405 struct page **pages = NULL;
406 unsigned long nr_pages;
407 unsigned long nr_pages_ret = 0;
408 unsigned long total_compressed = 0;
409 unsigned long total_in = 0;
410 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500411 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400412 int i;
413 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800414 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400415 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400416
Liu Bo4cb13e52012-03-29 09:57:45 -0400417 /* if this is a small write inside eof, kick off a defrag */
418 if ((end - start + 1) < 16 * 1024 &&
419 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400420 btrfs_add_inode_defrag(NULL, inode);
421
Chris Mason42dc7ba2008-12-15 11:44:56 -0500422 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400423again:
424 will_compress = 0;
425 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
426 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
427
Chris Masonf03d9301f2009-02-04 09:31:06 -0500428 /*
429 * we don't want to send crud past the end of i_size through
430 * compression, that's just a waste of CPU time. So, if the
431 * end of the file is before the start of our current
432 * requested range of bytes, we bail out to the uncompressed
433 * cleanup code that can deal with all of this.
434 *
435 * It isn't really the fastest way to fix things, but this is a
436 * very uncommon corner.
437 */
438 if (actual_end <= start)
439 goto cleanup_and_bail_uncompressed;
440
Chris Masonc8b97812008-10-29 14:49:59 -0400441 total_compressed = actual_end - start;
442
Shilong Wang4bcbb332014-10-07 18:44:35 -0400443 /*
444 * skip compression for a small file range(<=blocksize) that
445 * isn't an inline extent, since it dosen't save disk space at all.
446 */
447 if (total_compressed <= blocksize &&
448 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
449 goto cleanup_and_bail_uncompressed;
450
Chris Masonc8b97812008-10-29 14:49:59 -0400451 /* we want to make sure that amount of ram required to uncompress
452 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500453 * of a compressed extent to 128k. This is a crucial number
454 * because it also controls how easily we can spread reads across
455 * cpus for decompression.
456 *
457 * We also want to make sure the amount of IO required to do
458 * a random read is reasonably small, so we limit the size of
459 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400460 */
461 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000462 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500463 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400464 total_in = 0;
465 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400466
Chris Mason771ed682008-11-06 22:02:51 -0500467 /*
468 * we do compression for mount -o compress and when the
469 * inode has not been flagged as nocompress. This flag can
470 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400471 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800472 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400473 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100474 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800475 if (!pages) {
476 /* just bail out to the uncompressed code */
477 goto cont;
478 }
Chris Mason179e29e2007-11-01 11:28:41 -0400479
Li Zefan261507a02010-12-17 14:21:50 +0800480 if (BTRFS_I(inode)->force_compress)
481 compress_type = BTRFS_I(inode)->force_compress;
482
Chris Mason4adaa612013-03-26 13:07:00 -0400483 /*
484 * we need to call clear_page_dirty_for_io on each
485 * page in the range. Otherwise applications with the file
486 * mmap'd can wander in and change the page contents while
487 * we are compressing them.
488 *
489 * If the compression fails for any reason, we set the pages
490 * dirty again later on.
491 */
492 extent_range_clear_dirty_for_io(inode, start, end);
493 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800494 ret = btrfs_compress_pages(compress_type,
495 inode->i_mapping, start,
496 total_compressed, pages,
497 nr_pages, &nr_pages_ret,
498 &total_in,
499 &total_compressed,
500 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400501
502 if (!ret) {
503 unsigned long offset = total_compressed &
504 (PAGE_CACHE_SIZE - 1);
505 struct page *page = pages[nr_pages_ret - 1];
506 char *kaddr;
507
508 /* zero the tail end of the last page, we might be
509 * sending it down to disk
510 */
511 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800512 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400513 memset(kaddr + offset, 0,
514 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800515 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400516 }
517 will_compress = 1;
518 }
519 }
Li Zefan560f7d72011-09-08 10:22:01 +0800520cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400521 if (start == 0) {
522 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500523 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400524 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500525 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400526 */
Josef Bacik00361582013-08-14 14:02:47 -0400527 ret = cow_file_range_inline(root, inode, start, end,
528 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400529 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500530 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400531 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000532 total_compressed,
533 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400534 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100535 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400536 unsigned long clear_flags = EXTENT_DELALLOC |
537 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100538 unsigned long page_error_op;
539
Josef Bacik151a41b2013-07-29 13:22:24 -0400540 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100541 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400542
Chris Mason771ed682008-11-06 22:02:51 -0500543 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100544 * inline extent creation worked or returned error,
545 * we don't need to create any more async work items.
546 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500547 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400548 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400549 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400550 PAGE_CLEAR_DIRTY |
551 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100552 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400553 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400554 goto free_pages_out;
555 }
556 }
557
558 if (will_compress) {
559 /*
560 * we aren't doing an inline extent round the compressed size
561 * up to a block size boundary so the allocator does sane
562 * things
563 */
Qu Wenruofda28322013-02-26 08:10:22 +0000564 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400565
566 /*
567 * one last check to make sure the compression is really a
568 * win, compare the page count read with the blocks on disk
569 */
Qu Wenruofda28322013-02-26 08:10:22 +0000570 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400571 if (total_compressed >= total_in) {
572 will_compress = 0;
573 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400574 num_bytes = total_in;
575 }
576 }
577 if (!will_compress && pages) {
578 /*
579 * the compression code ran but failed to make things smaller,
580 * free any pages it allocated and our page pointer array
581 */
582 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400583 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400584 page_cache_release(pages[i]);
585 }
586 kfree(pages);
587 pages = NULL;
588 total_compressed = 0;
589 nr_pages_ret = 0;
590
591 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500592 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
593 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500594 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500595 }
Chris Masonc8b97812008-10-29 14:49:59 -0400596 }
Chris Mason771ed682008-11-06 22:02:51 -0500597 if (will_compress) {
598 *num_added += 1;
599
600 /* the async work queues will take care of doing actual
601 * allocation on disk for these compressed pages,
602 * and will submit them to the elevator.
603 */
604 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800605 total_compressed, pages, nr_pages_ret,
606 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500607
Yan, Zheng24ae6362010-12-06 07:02:36 +0000608 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500609 start += num_bytes;
610 pages = NULL;
611 cond_resched();
612 goto again;
613 }
614 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500615cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500616 /*
617 * No compression, but we still need to write the pages in
618 * the file we've been given so far. redirty the locked
619 * page if it corresponds to our extent and set things up
620 * for the async work queue to run cow_file_range to do
621 * the normal delalloc dance
622 */
623 if (page_offset(locked_page) >= start &&
624 page_offset(locked_page) <= end) {
625 __set_page_dirty_nobuffers(locked_page);
626 /* unlocked later on in the async handlers */
627 }
Chris Mason4adaa612013-03-26 13:07:00 -0400628 if (redirty)
629 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800630 add_async_extent(async_cow, start, end - start + 1,
631 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500632 *num_added += 1;
633 }
634
Filipe Mananac44f6492014-10-09 21:15:44 +0100635 return;
Chris Mason771ed682008-11-06 22:02:51 -0500636
637free_pages_out:
638 for (i = 0; i < nr_pages_ret; i++) {
639 WARN_ON(pages[i]->mapping);
640 page_cache_release(pages[i]);
641 }
Chris Masond3977122009-01-05 21:25:51 -0500642 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500643}
Chris Mason771ed682008-11-06 22:02:51 -0500644
Filipe Manana40ae8372014-10-06 22:14:24 +0100645static void free_async_extent_pages(struct async_extent *async_extent)
646{
647 int i;
648
649 if (!async_extent->pages)
650 return;
651
652 for (i = 0; i < async_extent->nr_pages; i++) {
653 WARN_ON(async_extent->pages[i]->mapping);
654 page_cache_release(async_extent->pages[i]);
655 }
656 kfree(async_extent->pages);
657 async_extent->nr_pages = 0;
658 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500659}
660
661/*
662 * phase two of compressed writeback. This is the ordered portion
663 * of the code, which only gets called in the order the work was
664 * queued. We walk all the async extents created by compress_file_range
665 * and send them down to the disk.
666 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100667static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500668 struct async_cow *async_cow)
669{
670 struct async_extent *async_extent;
671 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500672 struct btrfs_key ins;
673 struct extent_map *em;
674 struct btrfs_root *root = BTRFS_I(inode)->root;
675 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
676 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500677 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500678
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500679again:
Chris Masond3977122009-01-05 21:25:51 -0500680 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500681 async_extent = list_entry(async_cow->extents.next,
682 struct async_extent, list);
683 list_del(&async_extent->list);
684
685 io_tree = &BTRFS_I(inode)->io_tree;
686
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500687retry:
Chris Mason771ed682008-11-06 22:02:51 -0500688 /* did the compression code fall back to uncompressed IO? */
689 if (!async_extent->pages) {
690 int page_started = 0;
691 unsigned long nr_written = 0;
692
693 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000694 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100695 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500696
697 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500698 ret = cow_file_range(inode, async_cow->locked_page,
699 async_extent->start,
700 async_extent->start +
701 async_extent->ram_size - 1,
702 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500703
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100704 /* JDM XXX */
705
Chris Mason771ed682008-11-06 22:02:51 -0500706 /*
707 * if page_started, cow_file_range inserted an
708 * inline extent and took care of all the unlocking
709 * and IO for us. Otherwise, we need to submit
710 * all those pages down to the drive.
711 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500712 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500713 extent_write_locked_range(io_tree,
714 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500715 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500716 async_extent->ram_size - 1,
717 btrfs_get_extent,
718 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500719 else if (ret)
720 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500721 kfree(async_extent);
722 cond_resched();
723 continue;
724 }
725
726 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100727 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500728
Josef Bacik00361582013-08-14 14:02:47 -0400729 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500730 async_extent->compressed_size,
731 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800732 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500733 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100734 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500735
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400736 if (ret == -ENOSPC) {
737 unlock_extent(io_tree, async_extent->start,
738 async_extent->start +
739 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800740
741 /*
742 * we need to redirty the pages if we decide to
743 * fallback to uncompressed IO, otherwise we
744 * will not submit these pages down to lower
745 * layers.
746 */
747 extent_range_redirty_for_io(inode,
748 async_extent->start,
749 async_extent->start +
750 async_extent->ram_size - 1);
751
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100752 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400753 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500754 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500755 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000756 /*
757 * here we're doing allocation and writeback of the
758 * compressed pages
759 */
760 btrfs_drop_extent_cache(inode, async_extent->start,
761 async_extent->start +
762 async_extent->ram_size - 1, 0);
763
David Sterba172ddd62011-04-21 00:48:27 +0200764 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000765 if (!em) {
766 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500767 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000768 }
Chris Mason771ed682008-11-06 22:02:51 -0500769 em->start = async_extent->start;
770 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500771 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400772 em->mod_start = em->start;
773 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500774
775 em->block_start = ins.objectid;
776 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500777 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400778 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500779 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800780 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500781 set_bit(EXTENT_FLAG_PINNED, &em->flags);
782 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400783 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500784
Chris Masond3977122009-01-05 21:25:51 -0500785 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400786 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400787 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400788 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500789 if (ret != -EEXIST) {
790 free_extent_map(em);
791 break;
792 }
793 btrfs_drop_extent_cache(inode, async_extent->start,
794 async_extent->start +
795 async_extent->ram_size - 1, 0);
796 }
797
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500798 if (ret)
799 goto out_free_reserve;
800
Li Zefan261507a02010-12-17 14:21:50 +0800801 ret = btrfs_add_ordered_extent_compress(inode,
802 async_extent->start,
803 ins.objectid,
804 async_extent->ram_size,
805 ins.offset,
806 BTRFS_ORDERED_COMPRESSED,
807 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100808 if (ret) {
809 btrfs_drop_extent_cache(inode, async_extent->start,
810 async_extent->start +
811 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500812 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100813 }
Chris Mason771ed682008-11-06 22:02:51 -0500814
Chris Mason771ed682008-11-06 22:02:51 -0500815 /*
816 * clear dirty, set writeback and unlock the pages.
817 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400818 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400819 async_extent->start +
820 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400821 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
822 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400823 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500824 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500825 async_extent->start,
826 async_extent->ram_size,
827 ins.objectid,
828 ins.offset, async_extent->pages,
829 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100830 if (ret) {
831 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
832 struct page *p = async_extent->pages[0];
833 const u64 start = async_extent->start;
834 const u64 end = start + async_extent->ram_size - 1;
835
836 p->mapping = inode->i_mapping;
837 tree->ops->writepage_end_io_hook(p, start, end,
838 NULL, 0);
839 p->mapping = NULL;
840 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
841 PAGE_END_WRITEBACK |
842 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100843 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100844 }
Chris Mason771ed682008-11-06 22:02:51 -0500845 alloc_hint = ins.objectid + ins.offset;
846 kfree(async_extent);
847 cond_resched();
848 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100849 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500850out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800851 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100852out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400853 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500854 async_extent->start +
855 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400856 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400857 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
858 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100859 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
860 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100861 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100862 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500863 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500864}
865
Josef Bacik4b46fce2010-05-23 11:00:55 -0400866static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
867 u64 num_bytes)
868{
869 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
870 struct extent_map *em;
871 u64 alloc_hint = 0;
872
873 read_lock(&em_tree->lock);
874 em = search_extent_mapping(em_tree, start, num_bytes);
875 if (em) {
876 /*
877 * if block start isn't an actual block number then find the
878 * first block in this inode and use that as a hint. If that
879 * block is also bogus then just don't worry about it.
880 */
881 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
882 free_extent_map(em);
883 em = search_extent_mapping(em_tree, 0, 0);
884 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
885 alloc_hint = em->block_start;
886 if (em)
887 free_extent_map(em);
888 } else {
889 alloc_hint = em->block_start;
890 free_extent_map(em);
891 }
892 }
893 read_unlock(&em_tree->lock);
894
895 return alloc_hint;
896}
897
Chris Mason771ed682008-11-06 22:02:51 -0500898/*
899 * when extent_io.c finds a delayed allocation range in the file,
900 * the call backs end up in this code. The basic idea is to
901 * allocate extents on disk for the range, and create ordered data structs
902 * in ram to track those extents.
903 *
904 * locked_page is the page that writepage had locked already. We use
905 * it to make sure we don't do extra locks or unlocks.
906 *
907 * *page_started is set to one if we unlock locked_page and do everything
908 * required to start IO on it. It may be clean and already done with
909 * IO when we return.
910 */
Josef Bacik00361582013-08-14 14:02:47 -0400911static noinline int cow_file_range(struct inode *inode,
912 struct page *locked_page,
913 u64 start, u64 end, int *page_started,
914 unsigned long *nr_written,
915 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500916{
Josef Bacik00361582013-08-14 14:02:47 -0400917 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500918 u64 alloc_hint = 0;
919 u64 num_bytes;
920 unsigned long ram_size;
921 u64 disk_num_bytes;
922 u64 cur_alloc_size;
923 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500924 struct btrfs_key ins;
925 struct extent_map *em;
926 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
927 int ret = 0;
928
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400929 if (btrfs_is_free_space_inode(inode)) {
930 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500931 ret = -EINVAL;
932 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400933 }
Chris Mason771ed682008-11-06 22:02:51 -0500934
Qu Wenruofda28322013-02-26 08:10:22 +0000935 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500936 num_bytes = max(blocksize, num_bytes);
937 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500938
Chris Mason4cb53002011-05-24 15:35:30 -0400939 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400940 if (num_bytes < 64 * 1024 &&
941 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400942 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400943
Chris Mason771ed682008-11-06 22:02:51 -0500944 if (start == 0) {
945 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400946 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
947 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500948 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400949 extent_clear_unlock_delalloc(inode, start, end, NULL,
950 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400951 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400952 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
953 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000954
Chris Mason771ed682008-11-06 22:02:51 -0500955 *nr_written = *nr_written +
956 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
957 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500958 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100959 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100960 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500961 }
962 }
Chris Masonc8b97812008-10-29 14:49:59 -0400963
964 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200965 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400966
Josef Bacik4b46fce2010-05-23 11:00:55 -0400967 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400968 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400969
Chris Masond3977122009-01-05 21:25:51 -0500970 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400971 unsigned long op;
972
Josef Bacik287a0ab2010-03-19 18:07:23 +0000973 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400974 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500975 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800976 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400977 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100978 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500979
David Sterba172ddd62011-04-21 00:48:27 +0200980 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000981 if (!em) {
982 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000983 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000984 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400985 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500986 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500987 ram_size = ins.offset;
988 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400989 em->mod_start = em->start;
990 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400991
Chris Masone6dcd2d2008-07-17 12:53:50 -0400992 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400993 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500994 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400995 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400996 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400997 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400998 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400999
Chris Masond3977122009-01-05 21:25:51 -05001000 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001001 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001002 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001003 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001004 if (ret != -EEXIST) {
1005 free_extent_map(em);
1006 break;
1007 }
1008 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001009 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001010 }
Liu Boace68ba2013-04-22 10:53:47 +00001011 if (ret)
1012 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001013
Chris Mason98d20f62008-04-14 09:46:10 -04001014 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001015 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001016 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001017 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001018 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001019
Yan Zheng17d217f2008-12-12 10:03:38 -05001020 if (root->root_key.objectid ==
1021 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1022 ret = btrfs_reloc_clone_csums(inode, start,
1023 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001024 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001025 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001026 }
1027
Chris Masond3977122009-01-05 21:25:51 -05001028 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001029 break;
Chris Masond3977122009-01-05 21:25:51 -05001030
Chris Masonc8b97812008-10-29 14:49:59 -04001031 /* we're not doing compressed IO, don't unlock the first
1032 * page (which the caller expects to stay locked), don't
1033 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001034 *
1035 * Do set the Private2 bit so we know this page was properly
1036 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001037 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001038 op = unlock ? PAGE_UNLOCK : 0;
1039 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001040
Josef Bacikc2790a22013-07-29 11:20:47 -04001041 extent_clear_unlock_delalloc(inode, start,
1042 start + ram_size - 1, locked_page,
1043 EXTENT_LOCKED | EXTENT_DELALLOC,
1044 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001045 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001046 num_bytes -= cur_alloc_size;
1047 alloc_hint = ins.objectid + ins.offset;
1048 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001049 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001050out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001051 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001052
Filipe Mananad9f85962014-08-25 10:43:00 +01001053out_drop_extent_cache:
1054 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001055out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001056 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001057out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001058 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001059 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1060 EXTENT_DELALLOC | EXTENT_DEFRAG,
1061 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1062 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001063 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001064}
Chris Masonc8b97812008-10-29 14:49:59 -04001065
Chris Mason771ed682008-11-06 22:02:51 -05001066/*
1067 * work queue call back to started compression on a file and pages
1068 */
1069static noinline void async_cow_start(struct btrfs_work *work)
1070{
1071 struct async_cow *async_cow;
1072 int num_added = 0;
1073 async_cow = container_of(work, struct async_cow, work);
1074
1075 compress_file_range(async_cow->inode, async_cow->locked_page,
1076 async_cow->start, async_cow->end, async_cow,
1077 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001078 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001079 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001080 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001081 }
Chris Mason771ed682008-11-06 22:02:51 -05001082}
1083
1084/*
1085 * work queue call back to submit previously compressed pages
1086 */
1087static noinline void async_cow_submit(struct btrfs_work *work)
1088{
1089 struct async_cow *async_cow;
1090 struct btrfs_root *root;
1091 unsigned long nr_pages;
1092
1093 async_cow = container_of(work, struct async_cow, work);
1094
1095 root = async_cow->root;
1096 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1097 PAGE_CACHE_SHIFT;
1098
David Sterbaee863952015-02-16 19:41:40 +01001099 /*
1100 * atomic_sub_return implies a barrier for waitqueue_active
1101 */
Josef Bacik66657b32012-08-01 15:36:24 -04001102 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001103 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001104 waitqueue_active(&root->fs_info->async_submit_wait))
1105 wake_up(&root->fs_info->async_submit_wait);
1106
Chris Masond3977122009-01-05 21:25:51 -05001107 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001108 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001109}
Chris Masonc8b97812008-10-29 14:49:59 -04001110
Chris Mason771ed682008-11-06 22:02:51 -05001111static noinline void async_cow_free(struct btrfs_work *work)
1112{
1113 struct async_cow *async_cow;
1114 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001115 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001116 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001117 kfree(async_cow);
1118}
1119
1120static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1121 u64 start, u64 end, int *page_started,
1122 unsigned long *nr_written)
1123{
1124 struct async_cow *async_cow;
1125 struct btrfs_root *root = BTRFS_I(inode)->root;
1126 unsigned long nr_pages;
1127 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001128 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001129
Chris Masona3429ab2009-10-08 12:30:20 -04001130 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1131 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001132 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001133 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001134 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001135 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001136 async_cow->root = root;
1137 async_cow->locked_page = locked_page;
1138 async_cow->start = start;
1139
Wang Shilongf79707b2014-07-17 11:44:09 +08001140 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1141 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001142 cur_end = end;
1143 else
1144 cur_end = min(end, start + 512 * 1024 - 1);
1145
1146 async_cow->end = cur_end;
1147 INIT_LIST_HEAD(&async_cow->extents);
1148
Liu Bo9e0af232014-08-15 23:36:53 +08001149 btrfs_init_work(&async_cow->work,
1150 btrfs_delalloc_helper,
1151 async_cow_start, async_cow_submit,
1152 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001153
Chris Mason771ed682008-11-06 22:02:51 -05001154 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1155 PAGE_CACHE_SHIFT;
1156 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1157
Qu Wenruoafe3d242014-02-28 10:46:07 +08001158 btrfs_queue_work(root->fs_info->delalloc_workers,
1159 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001160
1161 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1162 wait_event(root->fs_info->async_submit_wait,
1163 (atomic_read(&root->fs_info->async_delalloc_pages) <
1164 limit));
1165 }
1166
Chris Masond3977122009-01-05 21:25:51 -05001167 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001168 atomic_read(&root->fs_info->async_delalloc_pages)) {
1169 wait_event(root->fs_info->async_submit_wait,
1170 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1171 0));
1172 }
1173
1174 *nr_written += nr_pages;
1175 start = cur_end + 1;
1176 }
1177 *page_started = 1;
1178 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001179}
1180
Chris Masond3977122009-01-05 21:25:51 -05001181static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001182 u64 bytenr, u64 num_bytes)
1183{
1184 int ret;
1185 struct btrfs_ordered_sum *sums;
1186 LIST_HEAD(list);
1187
Yan Zheng07d400a2009-01-06 11:42:00 -05001188 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001189 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001190 if (ret == 0 && list_empty(&list))
1191 return 0;
1192
1193 while (!list_empty(&list)) {
1194 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1195 list_del(&sums->list);
1196 kfree(sums);
1197 }
1198 return 1;
1199}
1200
Chris Masond352ac62008-09-29 15:18:18 -04001201/*
1202 * when nowcow writeback call back. This checks for snapshots or COW copies
1203 * of the extents that exist in the file, and COWs the file as required.
1204 *
1205 * If no cow copies or snapshots exist, we write directly to the existing
1206 * blocks on disk
1207 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001208static noinline int run_delalloc_nocow(struct inode *inode,
1209 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001210 u64 start, u64 end, int *page_started, int force,
1211 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001212{
Chris Masonbe20aa92007-12-17 20:14:01 -05001213 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001214 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001215 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001216 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001217 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001218 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001219 u64 cow_start;
1220 u64 cur_offset;
1221 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001222 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001223 u64 disk_bytenr;
1224 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001225 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001226 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001227 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001228 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001229 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001230 int nocow;
1231 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001232 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001233 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001234
1235 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001236 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001237 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1238 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001239 EXTENT_DO_ACCOUNTING |
1240 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001241 PAGE_CLEAR_DIRTY |
1242 PAGE_SET_WRITEBACK |
1243 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001244 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001245 }
Li Zefan82d59022011-04-20 10:33:24 +08001246
Liu Bo83eea1f2012-07-10 05:28:39 -06001247 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001248
1249 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001250 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001251 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001252 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001253
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001254 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001255 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1256 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001257 EXTENT_DO_ACCOUNTING |
1258 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001259 PAGE_CLEAR_DIRTY |
1260 PAGE_SET_WRITEBACK |
1261 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001262 btrfs_free_path(path);
1263 return PTR_ERR(trans);
1264 }
1265
Josef Bacik74b21072011-04-13 12:02:53 -04001266 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001267
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 cow_start = (u64)-1;
1269 cur_offset = start;
1270 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001271 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001273 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001274 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001275 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1276 leaf = path->nodes[0];
1277 btrfs_item_key_to_cpu(leaf, &found_key,
1278 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001279 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 found_key.type == BTRFS_EXTENT_DATA_KEY)
1281 path->slots[0]--;
1282 }
1283 check_prev = 0;
1284next_slot:
1285 leaf = path->nodes[0];
1286 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1287 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001288 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001289 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 if (ret > 0)
1291 break;
1292 leaf = path->nodes[0];
1293 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001294
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 nocow = 0;
1296 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001297 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001298 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001299
Li Zefan33345d012011-04-20 10:31:50 +08001300 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1302 found_key.offset > end)
1303 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001304
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 if (found_key.offset > cur_offset) {
1306 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001307 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 goto out_check;
1309 }
Chris Masonc31f8832008-01-08 15:46:31 -05001310
Yan Zheng80ff3852008-10-30 14:20:02 -04001311 fi = btrfs_item_ptr(leaf, path->slots[0],
1312 struct btrfs_file_extent_item);
1313 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001314
Josef Bacikcc95bef2013-04-04 14:31:27 -04001315 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001316 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1317 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001319 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001320 extent_end = found_key.offset +
1321 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001322 disk_num_bytes =
1323 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001324 if (extent_end <= start) {
1325 path->slots[0]++;
1326 goto next_slot;
1327 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001328 if (disk_bytenr == 0)
1329 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001330 if (btrfs_file_extent_compression(leaf, fi) ||
1331 btrfs_file_extent_encryption(leaf, fi) ||
1332 btrfs_file_extent_other_encoding(leaf, fi))
1333 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001334 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1335 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001336 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001337 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001338 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001339 found_key.offset -
1340 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001341 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001342 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001343 disk_bytenr += cur_offset - found_key.offset;
1344 num_bytes = min(end + 1, extent_end) - cur_offset;
1345 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001346 * if there are pending snapshots for this root,
1347 * we fall into common COW way.
1348 */
1349 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001350 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001351 if (!err)
1352 goto out_check;
1353 }
1354 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001355 * force cow if csum exists in the range.
1356 * this ensure that csum for a given extent are
1357 * either valid or do not exist.
1358 */
1359 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1360 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 nocow = 1;
1362 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1363 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001364 btrfs_file_extent_inline_len(leaf,
1365 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001366 extent_end = ALIGN(extent_end, root->sectorsize);
1367 } else {
1368 BUG_ON(1);
1369 }
1370out_check:
1371 if (extent_end <= start) {
1372 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001373 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001374 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001375 goto next_slot;
1376 }
1377 if (!nocow) {
1378 if (cow_start == (u64)-1)
1379 cow_start = cur_offset;
1380 cur_offset = extent_end;
1381 if (cur_offset > end)
1382 break;
1383 path->slots[0]++;
1384 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001385 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001386
David Sterbab3b4aa72011-04-21 01:20:15 +02001387 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001388 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001389 ret = cow_file_range(inode, locked_page,
1390 cow_start, found_key.offset - 1,
1391 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001392 if (ret) {
1393 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001394 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001395 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001396 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001397 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001398 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001399
Yan Zhengd899e052008-10-30 14:25:28 -04001400 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1401 struct extent_map *em;
1402 struct extent_map_tree *em_tree;
1403 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001404 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001405 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001406 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001407 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001408 em->len = num_bytes;
1409 em->block_len = num_bytes;
1410 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001411 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001412 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001413 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001414 em->mod_start = em->start;
1415 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001416 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001417 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001418 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001419 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001420 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001421 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001422 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001423 if (ret != -EEXIST) {
1424 free_extent_map(em);
1425 break;
1426 }
1427 btrfs_drop_extent_cache(inode, em->start,
1428 em->start + em->len - 1, 0);
1429 }
1430 type = BTRFS_ORDERED_PREALLOC;
1431 } else {
1432 type = BTRFS_ORDERED_NOCOW;
1433 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001434
1435 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001436 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001437 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001438
Yan, Zhengefa56462010-05-16 10:49:59 -04001439 if (root->root_key.objectid ==
1440 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1441 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1442 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001443 if (ret) {
1444 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001445 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001446 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001447 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001448 }
1449
Josef Bacikc2790a22013-07-29 11:20:47 -04001450 extent_clear_unlock_delalloc(inode, cur_offset,
1451 cur_offset + num_bytes - 1,
1452 locked_page, EXTENT_LOCKED |
1453 EXTENT_DELALLOC, PAGE_UNLOCK |
1454 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001455 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001456 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001457 cur_offset = extent_end;
1458 if (cur_offset > end)
1459 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001460 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001461 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001462
Josef Bacik17ca04a2012-05-31 15:58:55 -04001463 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001464 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001465 cur_offset = end;
1466 }
1467
Yan Zheng80ff3852008-10-30 14:20:02 -04001468 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001469 ret = cow_file_range(inode, locked_page, cow_start, end,
1470 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001471 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001472 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001473 }
1474
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001475error:
Miao Xiea698d0752012-09-20 01:51:59 -06001476 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001477 if (!ret)
1478 ret = err;
1479
Josef Bacik17ca04a2012-05-31 15:58:55 -04001480 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001481 extent_clear_unlock_delalloc(inode, cur_offset, end,
1482 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001483 EXTENT_DELALLOC | EXTENT_DEFRAG |
1484 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1485 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001486 PAGE_SET_WRITEBACK |
1487 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001488 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001489 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001490}
1491
Wang Shilong47059d92014-07-03 18:22:07 +08001492static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1493{
1494
1495 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1496 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1497 return 0;
1498
1499 /*
1500 * @defrag_bytes is a hint value, no spinlock held here,
1501 * if is not zero, it means the file is defragging.
1502 * Force cow if given extent needs to be defragged.
1503 */
1504 if (BTRFS_I(inode)->defrag_bytes &&
1505 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1506 EXTENT_DEFRAG, 0, NULL))
1507 return 1;
1508
1509 return 0;
1510}
1511
Chris Masond352ac62008-09-29 15:18:18 -04001512/*
1513 * extent_io.c call back to do delayed allocation processing
1514 */
Chris Masonc8b97812008-10-29 14:49:59 -04001515static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001516 u64 start, u64 end, int *page_started,
1517 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001518{
Chris Masonbe20aa92007-12-17 20:14:01 -05001519 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001520 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001521
Wang Shilong47059d92014-07-03 18:22:07 +08001522 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001523 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001524 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001525 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001526 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001527 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001528 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001529 ret = cow_file_range(inode, locked_page, start, end,
1530 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001531 } else {
1532 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1533 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001534 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001535 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001536 }
Chris Masonb888db22007-08-27 16:49:44 -04001537 return ret;
1538}
1539
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001540static void btrfs_split_extent_hook(struct inode *inode,
1541 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001542{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001543 u64 size;
1544
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001545 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001546 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001547 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001548
Josef Bacikdcab6a32015-02-11 15:08:59 -05001549 size = orig->end - orig->start + 1;
1550 if (size > BTRFS_MAX_EXTENT_SIZE) {
1551 u64 num_extents;
1552 u64 new_size;
1553
1554 /*
Josef Bacikba117212015-03-13 15:01:24 -04001555 * See the explanation in btrfs_merge_extent_hook, the same
1556 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001557 */
1558 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001559 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001560 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001561 new_size = split - orig->start;
1562 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1563 BTRFS_MAX_EXTENT_SIZE);
1564 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1565 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001566 return;
1567 }
1568
Josef Bacik9e0baf62011-07-15 15:16:44 +00001569 spin_lock(&BTRFS_I(inode)->lock);
1570 BTRFS_I(inode)->outstanding_extents++;
1571 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001572}
1573
1574/*
1575 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1576 * extents so we can keep track of new extents that are just merged onto old
1577 * extents, such as when we are doing sequential writes, so we can properly
1578 * account for the metadata space we'll need.
1579 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001580static void btrfs_merge_extent_hook(struct inode *inode,
1581 struct extent_state *new,
1582 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001583{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001584 u64 new_size, old_size;
1585 u64 num_extents;
1586
Josef Bacik9ed74f22009-09-11 16:12:44 -04001587 /* not delalloc, ignore it */
1588 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001589 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001590
Josef Bacik8461a3d2015-03-13 15:12:08 -04001591 if (new->start > other->start)
1592 new_size = new->end - other->start + 1;
1593 else
1594 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001595
1596 /* we're not bigger than the max, unreserve the space and go */
1597 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1598 spin_lock(&BTRFS_I(inode)->lock);
1599 BTRFS_I(inode)->outstanding_extents--;
1600 spin_unlock(&BTRFS_I(inode)->lock);
1601 return;
1602 }
1603
1604 /*
Josef Bacikba117212015-03-13 15:01:24 -04001605 * We have to add up either side to figure out how many extents were
1606 * accounted for before we merged into one big extent. If the number of
1607 * extents we accounted for is <= the amount we need for the new range
1608 * then we can return, otherwise drop. Think of it like this
1609 *
1610 * [ 4k][MAX_SIZE]
1611 *
1612 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1613 * need 2 outstanding extents, on one side we have 1 and the other side
1614 * we have 1 so they are == and we can return. But in this case
1615 *
1616 * [MAX_SIZE+4k][MAX_SIZE+4k]
1617 *
1618 * Each range on their own accounts for 2 extents, but merged together
1619 * they are only 3 extents worth of accounting, so we need to drop in
1620 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 */
Josef Bacikba117212015-03-13 15:01:24 -04001622 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001623 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1624 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001625 old_size = new->end - new->start + 1;
1626 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1627 BTRFS_MAX_EXTENT_SIZE);
1628
Josef Bacikdcab6a32015-02-11 15:08:59 -05001629 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001630 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001631 return;
1632
Josef Bacik9e0baf62011-07-15 15:16:44 +00001633 spin_lock(&BTRFS_I(inode)->lock);
1634 BTRFS_I(inode)->outstanding_extents--;
1635 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001636}
1637
Miao Xieeb73c1b2013-05-15 07:48:22 +00001638static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1639 struct inode *inode)
1640{
1641 spin_lock(&root->delalloc_lock);
1642 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1643 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1644 &root->delalloc_inodes);
1645 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1646 &BTRFS_I(inode)->runtime_flags);
1647 root->nr_delalloc_inodes++;
1648 if (root->nr_delalloc_inodes == 1) {
1649 spin_lock(&root->fs_info->delalloc_root_lock);
1650 BUG_ON(!list_empty(&root->delalloc_root));
1651 list_add_tail(&root->delalloc_root,
1652 &root->fs_info->delalloc_roots);
1653 spin_unlock(&root->fs_info->delalloc_root_lock);
1654 }
1655 }
1656 spin_unlock(&root->delalloc_lock);
1657}
1658
1659static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1660 struct inode *inode)
1661{
1662 spin_lock(&root->delalloc_lock);
1663 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1664 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1665 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1666 &BTRFS_I(inode)->runtime_flags);
1667 root->nr_delalloc_inodes--;
1668 if (!root->nr_delalloc_inodes) {
1669 spin_lock(&root->fs_info->delalloc_root_lock);
1670 BUG_ON(list_empty(&root->delalloc_root));
1671 list_del_init(&root->delalloc_root);
1672 spin_unlock(&root->fs_info->delalloc_root_lock);
1673 }
1674 }
1675 spin_unlock(&root->delalloc_lock);
1676}
1677
Chris Masond352ac62008-09-29 15:18:18 -04001678/*
1679 * extent_io.c set_bit_hook, used to track delayed allocation
1680 * bytes in this file, and to maintain the list of inodes that
1681 * have pending delalloc work to be done.
1682 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001683static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001684 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001685{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001686
Wang Shilong47059d92014-07-03 18:22:07 +08001687 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1688 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001689 /*
1690 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001691 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001692 * bit, which is only set or cleared with irqs on
1693 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001694 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001695 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001696 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001697 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001698
Josef Bacik9e0baf62011-07-15 15:16:44 +00001699 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001700 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001701 } else {
1702 spin_lock(&BTRFS_I(inode)->lock);
1703 BTRFS_I(inode)->outstanding_extents++;
1704 spin_unlock(&BTRFS_I(inode)->lock);
1705 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001706
Josef Bacik6a3891c2015-03-16 17:38:52 -04001707 /* For sanity tests */
1708 if (btrfs_test_is_dummy_root(root))
1709 return;
1710
Miao Xie963d6782013-01-29 10:10:51 +00001711 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1712 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001713 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001714 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001715 if (*bits & EXTENT_DEFRAG)
1716 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001717 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001718 &BTRFS_I(inode)->runtime_flags))
1719 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001720 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001721 }
Chris Mason291d6732008-01-29 15:55:23 -05001722}
1723
Chris Masond352ac62008-09-29 15:18:18 -04001724/*
1725 * extent_io.c clear_bit_hook, see set_bit_hook for why
1726 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001727static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001728 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001729 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001730{
Wang Shilong47059d92014-07-03 18:22:07 +08001731 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001732 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1733 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001734
1735 spin_lock(&BTRFS_I(inode)->lock);
1736 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1737 BTRFS_I(inode)->defrag_bytes -= len;
1738 spin_unlock(&BTRFS_I(inode)->lock);
1739
Chris Mason75eff682008-12-15 15:54:40 -05001740 /*
1741 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001742 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001743 * bit, which is only set or cleared with irqs on
1744 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001745 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001746 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001747 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001748
Josef Bacik9e0baf62011-07-15 15:16:44 +00001749 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001750 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001751 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1752 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001753 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001754 spin_unlock(&BTRFS_I(inode)->lock);
1755 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001756
Josef Bacikb6d08f02013-09-27 14:57:43 -04001757 /*
1758 * We don't reserve metadata space for space cache inodes so we
1759 * don't need to call dellalloc_release_metadata if there is an
1760 * error.
1761 */
1762 if (*bits & EXTENT_DO_ACCOUNTING &&
1763 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001764 btrfs_delalloc_release_metadata(inode, len);
1765
Josef Bacik6a3891c2015-03-16 17:38:52 -04001766 /* For sanity tests. */
1767 if (btrfs_test_is_dummy_root(root))
1768 return;
1769
Josef Bacik0cb59c92010-07-02 12:14:14 -04001770 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001771 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001772 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001773
Miao Xie963d6782013-01-29 10:10:51 +00001774 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1775 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001776 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001777 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001778 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001779 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001780 &BTRFS_I(inode)->runtime_flags))
1781 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001782 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001783 }
Chris Mason291d6732008-01-29 15:55:23 -05001784}
1785
Chris Masond352ac62008-09-29 15:18:18 -04001786/*
1787 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1788 * we don't create bios that span stripes or chunks
1789 */
David Woodhouse64a16702009-07-15 23:29:37 +01001790int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001791 size_t size, struct bio *bio,
1792 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001793{
1794 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001795 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001796 u64 length = 0;
1797 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001798 int ret;
1799
Chris Mason771ed682008-11-06 22:02:51 -05001800 if (bio_flags & EXTENT_BIO_COMPRESSED)
1801 return 0;
1802
Kent Overstreet4f024f32013-10-11 15:44:27 -07001803 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001804 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001805 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001806 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001807 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001808 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001809 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001810 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001811 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001812}
1813
Chris Masond352ac62008-09-29 15:18:18 -04001814/*
1815 * in order to insert checksums into the metadata in large chunks,
1816 * we wait until bio submission time. All the pages in the bio are
1817 * checksummed and sums are attached onto the ordered extent record.
1818 *
1819 * At IO completion time the cums attached on the ordered extent record
1820 * are inserted into the btree
1821 */
Chris Masond3977122009-01-05 21:25:51 -05001822static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1823 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001824 unsigned long bio_flags,
1825 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001826{
Chris Mason065631f2008-02-20 12:07:25 -05001827 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001828 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001829
Chris Masond20f7042008-12-08 16:58:54 -05001830 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001831 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001832 return 0;
1833}
Chris Masone0156402008-04-16 11:15:20 -04001834
Chris Mason4a69a412008-11-06 22:03:00 -05001835/*
1836 * in order to insert checksums into the metadata in large chunks,
1837 * we wait until bio submission time. All the pages in the bio are
1838 * checksummed and sums are attached onto the ordered extent record.
1839 *
1840 * At IO completion time the cums attached on the ordered extent record
1841 * are inserted into the btree
1842 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001843static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001844 int mirror_num, unsigned long bio_flags,
1845 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001846{
1847 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001848 int ret;
1849
1850 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001851 if (ret) {
1852 bio->bi_error = ret;
1853 bio_endio(bio);
1854 }
Stefan Behrens61891922012-11-05 18:51:52 +01001855 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001856}
1857
Chris Masond352ac62008-09-29 15:18:18 -04001858/*
Chris Masoncad321a2008-12-17 14:51:42 -05001859 * extent_io.c submission hook. This does the right thing for csum calculation
1860 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001861 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001862static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001863 int mirror_num, unsigned long bio_flags,
1864 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001865{
1866 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301867 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001868 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001869 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001870 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001871
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001872 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001873
Liu Bo83eea1f2012-07-10 05:28:39 -06001874 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301875 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001876
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001877 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001878 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1879 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001880 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001881
Chris Masond20f7042008-12-08 16:58:54 -05001882 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001883 ret = btrfs_submit_compressed_read(inode, bio,
1884 mirror_num,
1885 bio_flags);
1886 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001887 } else if (!skip_sum) {
1888 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1889 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001890 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001891 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001892 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001893 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001894 /* csum items have already been cloned */
1895 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1896 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001897 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001898 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001899 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001900 bio_flags, bio_offset,
1901 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001902 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001903 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001904 } else if (!skip_sum) {
1905 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1906 if (ret)
1907 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001908 }
1909
Chris Mason0b86a832008-03-24 15:01:56 -04001910mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001911 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1912
1913out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001914 if (ret < 0) {
1915 bio->bi_error = ret;
1916 bio_endio(bio);
1917 }
Stefan Behrens61891922012-11-05 18:51:52 +01001918 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001919}
Chris Mason6885f302008-02-20 16:11:05 -05001920
Chris Masond352ac62008-09-29 15:18:18 -04001921/*
1922 * given a list of ordered sums record them in the inode. This happens
1923 * at IO completion time based on sums calculated at bio submission time.
1924 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001925static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001926 struct inode *inode, u64 file_offset,
1927 struct list_head *list)
1928{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001929 struct btrfs_ordered_sum *sum;
1930
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001931 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001932 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001933 btrfs_csum_file_blocks(trans,
1934 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001935 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001936 }
1937 return 0;
1938}
1939
Josef Bacik2ac55d42010-02-03 19:33:23 +00001940int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1941 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001942{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001943 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001944 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001945 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001946}
1947
Chris Masond352ac62008-09-29 15:18:18 -04001948/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001949struct btrfs_writepage_fixup {
1950 struct page *page;
1951 struct btrfs_work work;
1952};
1953
Christoph Hellwigb2950862008-12-02 09:54:17 -05001954static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001955{
1956 struct btrfs_writepage_fixup *fixup;
1957 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001958 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001959 struct page *page;
1960 struct inode *inode;
1961 u64 page_start;
1962 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001963 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001964
1965 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1966 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001967again:
Chris Mason247e7432008-07-17 12:53:51 -04001968 lock_page(page);
1969 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1970 ClearPageChecked(page);
1971 goto out_page;
1972 }
1973
1974 inode = page->mapping->host;
1975 page_start = page_offset(page);
1976 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1977
Josef Bacik2ac55d42010-02-03 19:33:23 +00001978 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001979 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001980
1981 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001982 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001983 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001984
1985 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1986 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001987 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1988 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001989 unlock_page(page);
1990 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001991 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001992 goto again;
1993 }
Chris Mason247e7432008-07-17 12:53:51 -04001994
Jeff Mahoney87826df2012-02-15 16:23:57 +01001995 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1996 if (ret) {
1997 mapping_set_error(page->mapping, ret);
1998 end_extent_writepage(page, ret, page_start, page_end);
1999 ClearPageChecked(page);
2000 goto out;
2001 }
2002
Josef Bacik2ac55d42010-02-03 19:33:23 +00002003 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002004 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002005 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002006out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002007 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2008 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002009out_page:
2010 unlock_page(page);
2011 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002012 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002013}
2014
2015/*
2016 * There are a few paths in the higher layers of the kernel that directly
2017 * set the page dirty bit without asking the filesystem if it is a
2018 * good idea. This causes problems because we want to make sure COW
2019 * properly happens and the data=ordered rules are followed.
2020 *
Chris Masonc8b97812008-10-29 14:49:59 -04002021 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002022 * hasn't been properly setup for IO. We kick off an async process
2023 * to fix it up. The async helper will wait for ordered extents, set
2024 * the delalloc bit and make it safe to write the page.
2025 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002026static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002027{
2028 struct inode *inode = page->mapping->host;
2029 struct btrfs_writepage_fixup *fixup;
2030 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002031
Chris Mason8b62b722009-09-02 16:53:46 -04002032 /* this page is properly in the ordered list */
2033 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002034 return 0;
2035
2036 if (PageChecked(page))
2037 return -EAGAIN;
2038
2039 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2040 if (!fixup)
2041 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002042
Chris Mason247e7432008-07-17 12:53:51 -04002043 SetPageChecked(page);
2044 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002045 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2046 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002047 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002048 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002049 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002050}
2051
Yan Zhengd899e052008-10-30 14:25:28 -04002052static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2053 struct inode *inode, u64 file_pos,
2054 u64 disk_bytenr, u64 disk_num_bytes,
2055 u64 num_bytes, u64 ram_bytes,
2056 u8 compression, u8 encryption,
2057 u16 other_encoding, int extent_type)
2058{
2059 struct btrfs_root *root = BTRFS_I(inode)->root;
2060 struct btrfs_file_extent_item *fi;
2061 struct btrfs_path *path;
2062 struct extent_buffer *leaf;
2063 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002064 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002065 int ret;
2066
2067 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002068 if (!path)
2069 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002070
Chris Masona1ed8352009-09-11 12:27:37 -04002071 /*
2072 * we may be replacing one extent in the tree with another.
2073 * The new extent is pinned in the extent map, and we don't want
2074 * to drop it from the cache until it is completely in the btree.
2075 *
2076 * So, tell btrfs_drop_extents to leave this extent in the cache.
2077 * the caller is expected to unpin it and allow it to be merged
2078 * with the others.
2079 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002080 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2081 file_pos + num_bytes, NULL, 0,
2082 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002083 if (ret)
2084 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002085
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002086 if (!extent_inserted) {
2087 ins.objectid = btrfs_ino(inode);
2088 ins.offset = file_pos;
2089 ins.type = BTRFS_EXTENT_DATA_KEY;
2090
2091 path->leave_spinning = 1;
2092 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2093 sizeof(*fi));
2094 if (ret)
2095 goto out;
2096 }
Yan Zhengd899e052008-10-30 14:25:28 -04002097 leaf = path->nodes[0];
2098 fi = btrfs_item_ptr(leaf, path->slots[0],
2099 struct btrfs_file_extent_item);
2100 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2101 btrfs_set_file_extent_type(leaf, fi, extent_type);
2102 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2103 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2104 btrfs_set_file_extent_offset(leaf, fi, 0);
2105 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2106 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2107 btrfs_set_file_extent_compression(leaf, fi, compression);
2108 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2109 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002110
Yan Zhengd899e052008-10-30 14:25:28 -04002111 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002112 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002113
2114 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002115
2116 ins.objectid = disk_bytenr;
2117 ins.offset = disk_num_bytes;
2118 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002119 ret = btrfs_alloc_reserved_file_extent(trans, root,
2120 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002121 btrfs_ino(inode), file_pos, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002122 if (ret < 0)
2123 goto out;
2124 /*
2125 * Release the reserved range from inode dirty range map, and
2126 * move it to delayed ref codes, as now accounting only happens at
2127 * commit_transaction() time.
2128 */
2129 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2130 ret = btrfs_add_delayed_qgroup_reserve(root->fs_info, trans,
2131 root->objectid, disk_bytenr, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002132out:
Yan Zhengd899e052008-10-30 14:25:28 -04002133 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002134
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002135 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002136}
2137
Liu Bo38c227d2013-01-29 03:18:40 +00002138/* snapshot-aware defrag */
2139struct sa_defrag_extent_backref {
2140 struct rb_node node;
2141 struct old_sa_defrag_extent *old;
2142 u64 root_id;
2143 u64 inum;
2144 u64 file_pos;
2145 u64 extent_offset;
2146 u64 num_bytes;
2147 u64 generation;
2148};
2149
2150struct old_sa_defrag_extent {
2151 struct list_head list;
2152 struct new_sa_defrag_extent *new;
2153
2154 u64 extent_offset;
2155 u64 bytenr;
2156 u64 offset;
2157 u64 len;
2158 int count;
2159};
2160
2161struct new_sa_defrag_extent {
2162 struct rb_root root;
2163 struct list_head head;
2164 struct btrfs_path *path;
2165 struct inode *inode;
2166 u64 file_pos;
2167 u64 len;
2168 u64 bytenr;
2169 u64 disk_len;
2170 u8 compress_type;
2171};
2172
2173static int backref_comp(struct sa_defrag_extent_backref *b1,
2174 struct sa_defrag_extent_backref *b2)
2175{
2176 if (b1->root_id < b2->root_id)
2177 return -1;
2178 else if (b1->root_id > b2->root_id)
2179 return 1;
2180
2181 if (b1->inum < b2->inum)
2182 return -1;
2183 else if (b1->inum > b2->inum)
2184 return 1;
2185
2186 if (b1->file_pos < b2->file_pos)
2187 return -1;
2188 else if (b1->file_pos > b2->file_pos)
2189 return 1;
2190
2191 /*
2192 * [------------------------------] ===> (a range of space)
2193 * |<--->| |<---->| =============> (fs/file tree A)
2194 * |<---------------------------->| ===> (fs/file tree B)
2195 *
2196 * A range of space can refer to two file extents in one tree while
2197 * refer to only one file extent in another tree.
2198 *
2199 * So we may process a disk offset more than one time(two extents in A)
2200 * and locate at the same extent(one extent in B), then insert two same
2201 * backrefs(both refer to the extent in B).
2202 */
2203 return 0;
2204}
2205
2206static void backref_insert(struct rb_root *root,
2207 struct sa_defrag_extent_backref *backref)
2208{
2209 struct rb_node **p = &root->rb_node;
2210 struct rb_node *parent = NULL;
2211 struct sa_defrag_extent_backref *entry;
2212 int ret;
2213
2214 while (*p) {
2215 parent = *p;
2216 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2217
2218 ret = backref_comp(backref, entry);
2219 if (ret < 0)
2220 p = &(*p)->rb_left;
2221 else
2222 p = &(*p)->rb_right;
2223 }
2224
2225 rb_link_node(&backref->node, parent, p);
2226 rb_insert_color(&backref->node, root);
2227}
2228
2229/*
2230 * Note the backref might has changed, and in this case we just return 0.
2231 */
2232static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2233 void *ctx)
2234{
2235 struct btrfs_file_extent_item *extent;
2236 struct btrfs_fs_info *fs_info;
2237 struct old_sa_defrag_extent *old = ctx;
2238 struct new_sa_defrag_extent *new = old->new;
2239 struct btrfs_path *path = new->path;
2240 struct btrfs_key key;
2241 struct btrfs_root *root;
2242 struct sa_defrag_extent_backref *backref;
2243 struct extent_buffer *leaf;
2244 struct inode *inode = new->inode;
2245 int slot;
2246 int ret;
2247 u64 extent_offset;
2248 u64 num_bytes;
2249
2250 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2251 inum == btrfs_ino(inode))
2252 return 0;
2253
2254 key.objectid = root_id;
2255 key.type = BTRFS_ROOT_ITEM_KEY;
2256 key.offset = (u64)-1;
2257
2258 fs_info = BTRFS_I(inode)->root->fs_info;
2259 root = btrfs_read_fs_root_no_name(fs_info, &key);
2260 if (IS_ERR(root)) {
2261 if (PTR_ERR(root) == -ENOENT)
2262 return 0;
2263 WARN_ON(1);
2264 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2265 inum, offset, root_id);
2266 return PTR_ERR(root);
2267 }
2268
2269 key.objectid = inum;
2270 key.type = BTRFS_EXTENT_DATA_KEY;
2271 if (offset > (u64)-1 << 32)
2272 key.offset = 0;
2273 else
2274 key.offset = offset;
2275
2276 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302277 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002278 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002279 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002280
2281 while (1) {
2282 cond_resched();
2283
2284 leaf = path->nodes[0];
2285 slot = path->slots[0];
2286
2287 if (slot >= btrfs_header_nritems(leaf)) {
2288 ret = btrfs_next_leaf(root, path);
2289 if (ret < 0) {
2290 goto out;
2291 } else if (ret > 0) {
2292 ret = 0;
2293 goto out;
2294 }
2295 continue;
2296 }
2297
2298 path->slots[0]++;
2299
2300 btrfs_item_key_to_cpu(leaf, &key, slot);
2301
2302 if (key.objectid > inum)
2303 goto out;
2304
2305 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2306 continue;
2307
2308 extent = btrfs_item_ptr(leaf, slot,
2309 struct btrfs_file_extent_item);
2310
2311 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2312 continue;
2313
Liu Boe68afa42013-07-01 22:13:26 +08002314 /*
2315 * 'offset' refers to the exact key.offset,
2316 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2317 * (key.offset - extent_offset).
2318 */
2319 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002320 continue;
2321
Liu Boe68afa42013-07-01 22:13:26 +08002322 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002323 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002324
Liu Bo38c227d2013-01-29 03:18:40 +00002325 if (extent_offset >= old->extent_offset + old->offset +
2326 old->len || extent_offset + num_bytes <=
2327 old->extent_offset + old->offset)
2328 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002329 break;
2330 }
2331
2332 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2333 if (!backref) {
2334 ret = -ENOENT;
2335 goto out;
2336 }
2337
2338 backref->root_id = root_id;
2339 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002340 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002341 backref->num_bytes = num_bytes;
2342 backref->extent_offset = extent_offset;
2343 backref->generation = btrfs_file_extent_generation(leaf, extent);
2344 backref->old = old;
2345 backref_insert(&new->root, backref);
2346 old->count++;
2347out:
2348 btrfs_release_path(path);
2349 WARN_ON(ret);
2350 return ret;
2351}
2352
2353static noinline bool record_extent_backrefs(struct btrfs_path *path,
2354 struct new_sa_defrag_extent *new)
2355{
2356 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2357 struct old_sa_defrag_extent *old, *tmp;
2358 int ret;
2359
2360 new->path = path;
2361
2362 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002363 ret = iterate_inodes_from_logical(old->bytenr +
2364 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002365 path, record_one_backref,
2366 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002367 if (ret < 0 && ret != -ENOENT)
2368 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002369
2370 /* no backref to be processed for this extent */
2371 if (!old->count) {
2372 list_del(&old->list);
2373 kfree(old);
2374 }
2375 }
2376
2377 if (list_empty(&new->head))
2378 return false;
2379
2380 return true;
2381}
2382
2383static int relink_is_mergable(struct extent_buffer *leaf,
2384 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002385 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002386{
Liu Bo116e0022013-08-02 16:30:40 +08002387 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002388 return 0;
2389
2390 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2391 return 0;
2392
Liu Bo116e0022013-08-02 16:30:40 +08002393 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2394 return 0;
2395
2396 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002397 btrfs_file_extent_other_encoding(leaf, fi))
2398 return 0;
2399
2400 return 1;
2401}
2402
2403/*
2404 * Note the backref might has changed, and in this case we just return 0.
2405 */
2406static noinline int relink_extent_backref(struct btrfs_path *path,
2407 struct sa_defrag_extent_backref *prev,
2408 struct sa_defrag_extent_backref *backref)
2409{
2410 struct btrfs_file_extent_item *extent;
2411 struct btrfs_file_extent_item *item;
2412 struct btrfs_ordered_extent *ordered;
2413 struct btrfs_trans_handle *trans;
2414 struct btrfs_fs_info *fs_info;
2415 struct btrfs_root *root;
2416 struct btrfs_key key;
2417 struct extent_buffer *leaf;
2418 struct old_sa_defrag_extent *old = backref->old;
2419 struct new_sa_defrag_extent *new = old->new;
2420 struct inode *src_inode = new->inode;
2421 struct inode *inode;
2422 struct extent_state *cached = NULL;
2423 int ret = 0;
2424 u64 start;
2425 u64 len;
2426 u64 lock_start;
2427 u64 lock_end;
2428 bool merge = false;
2429 int index;
2430
2431 if (prev && prev->root_id == backref->root_id &&
2432 prev->inum == backref->inum &&
2433 prev->file_pos + prev->num_bytes == backref->file_pos)
2434 merge = true;
2435
2436 /* step 1: get root */
2437 key.objectid = backref->root_id;
2438 key.type = BTRFS_ROOT_ITEM_KEY;
2439 key.offset = (u64)-1;
2440
2441 fs_info = BTRFS_I(src_inode)->root->fs_info;
2442 index = srcu_read_lock(&fs_info->subvol_srcu);
2443
2444 root = btrfs_read_fs_root_no_name(fs_info, &key);
2445 if (IS_ERR(root)) {
2446 srcu_read_unlock(&fs_info->subvol_srcu, index);
2447 if (PTR_ERR(root) == -ENOENT)
2448 return 0;
2449 return PTR_ERR(root);
2450 }
Liu Bo38c227d2013-01-29 03:18:40 +00002451
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002452 if (btrfs_root_readonly(root)) {
2453 srcu_read_unlock(&fs_info->subvol_srcu, index);
2454 return 0;
2455 }
2456
Liu Bo38c227d2013-01-29 03:18:40 +00002457 /* step 2: get inode */
2458 key.objectid = backref->inum;
2459 key.type = BTRFS_INODE_ITEM_KEY;
2460 key.offset = 0;
2461
2462 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2463 if (IS_ERR(inode)) {
2464 srcu_read_unlock(&fs_info->subvol_srcu, index);
2465 return 0;
2466 }
2467
2468 srcu_read_unlock(&fs_info->subvol_srcu, index);
2469
2470 /* step 3: relink backref */
2471 lock_start = backref->file_pos;
2472 lock_end = backref->file_pos + backref->num_bytes - 1;
2473 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2474 0, &cached);
2475
2476 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2477 if (ordered) {
2478 btrfs_put_ordered_extent(ordered);
2479 goto out_unlock;
2480 }
2481
2482 trans = btrfs_join_transaction(root);
2483 if (IS_ERR(trans)) {
2484 ret = PTR_ERR(trans);
2485 goto out_unlock;
2486 }
2487
2488 key.objectid = backref->inum;
2489 key.type = BTRFS_EXTENT_DATA_KEY;
2490 key.offset = backref->file_pos;
2491
2492 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2493 if (ret < 0) {
2494 goto out_free_path;
2495 } else if (ret > 0) {
2496 ret = 0;
2497 goto out_free_path;
2498 }
2499
2500 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2501 struct btrfs_file_extent_item);
2502
2503 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2504 backref->generation)
2505 goto out_free_path;
2506
2507 btrfs_release_path(path);
2508
2509 start = backref->file_pos;
2510 if (backref->extent_offset < old->extent_offset + old->offset)
2511 start += old->extent_offset + old->offset -
2512 backref->extent_offset;
2513
2514 len = min(backref->extent_offset + backref->num_bytes,
2515 old->extent_offset + old->offset + old->len);
2516 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2517
2518 ret = btrfs_drop_extents(trans, root, inode, start,
2519 start + len, 1);
2520 if (ret)
2521 goto out_free_path;
2522again:
2523 key.objectid = btrfs_ino(inode);
2524 key.type = BTRFS_EXTENT_DATA_KEY;
2525 key.offset = start;
2526
Liu Boa09a0a72013-03-11 09:20:58 +00002527 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002528 if (merge) {
2529 struct btrfs_file_extent_item *fi;
2530 u64 extent_len;
2531 struct btrfs_key found_key;
2532
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002533 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002534 if (ret < 0)
2535 goto out_free_path;
2536
2537 path->slots[0]--;
2538 leaf = path->nodes[0];
2539 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2540
2541 fi = btrfs_item_ptr(leaf, path->slots[0],
2542 struct btrfs_file_extent_item);
2543 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2544
Liu Bo116e0022013-08-02 16:30:40 +08002545 if (extent_len + found_key.offset == start &&
2546 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002547 btrfs_set_file_extent_num_bytes(leaf, fi,
2548 extent_len + len);
2549 btrfs_mark_buffer_dirty(leaf);
2550 inode_add_bytes(inode, len);
2551
2552 ret = 1;
2553 goto out_free_path;
2554 } else {
2555 merge = false;
2556 btrfs_release_path(path);
2557 goto again;
2558 }
2559 }
2560
2561 ret = btrfs_insert_empty_item(trans, root, path, &key,
2562 sizeof(*extent));
2563 if (ret) {
2564 btrfs_abort_transaction(trans, root, ret);
2565 goto out_free_path;
2566 }
2567
2568 leaf = path->nodes[0];
2569 item = btrfs_item_ptr(leaf, path->slots[0],
2570 struct btrfs_file_extent_item);
2571 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2572 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2573 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2574 btrfs_set_file_extent_num_bytes(leaf, item, len);
2575 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2576 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2577 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2578 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2579 btrfs_set_file_extent_encryption(leaf, item, 0);
2580 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2581
2582 btrfs_mark_buffer_dirty(leaf);
2583 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002584 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002585
2586 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2587 new->disk_len, 0,
2588 backref->root_id, backref->inum,
2589 new->file_pos, 0); /* start - extent_offset */
2590 if (ret) {
2591 btrfs_abort_transaction(trans, root, ret);
2592 goto out_free_path;
2593 }
2594
2595 ret = 1;
2596out_free_path:
2597 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002598 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002599 btrfs_end_transaction(trans, root);
2600out_unlock:
2601 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2602 &cached, GFP_NOFS);
2603 iput(inode);
2604 return ret;
2605}
2606
Liu Bo6f519562013-10-29 10:45:05 +08002607static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2608{
2609 struct old_sa_defrag_extent *old, *tmp;
2610
2611 if (!new)
2612 return;
2613
2614 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002615 kfree(old);
2616 }
2617 kfree(new);
2618}
2619
Liu Bo38c227d2013-01-29 03:18:40 +00002620static void relink_file_extents(struct new_sa_defrag_extent *new)
2621{
2622 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002623 struct sa_defrag_extent_backref *backref;
2624 struct sa_defrag_extent_backref *prev = NULL;
2625 struct inode *inode;
2626 struct btrfs_root *root;
2627 struct rb_node *node;
2628 int ret;
2629
2630 inode = new->inode;
2631 root = BTRFS_I(inode)->root;
2632
2633 path = btrfs_alloc_path();
2634 if (!path)
2635 return;
2636
2637 if (!record_extent_backrefs(path, new)) {
2638 btrfs_free_path(path);
2639 goto out;
2640 }
2641 btrfs_release_path(path);
2642
2643 while (1) {
2644 node = rb_first(&new->root);
2645 if (!node)
2646 break;
2647 rb_erase(node, &new->root);
2648
2649 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2650
2651 ret = relink_extent_backref(path, prev, backref);
2652 WARN_ON(ret < 0);
2653
2654 kfree(prev);
2655
2656 if (ret == 1)
2657 prev = backref;
2658 else
2659 prev = NULL;
2660 cond_resched();
2661 }
2662 kfree(prev);
2663
2664 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002665out:
Liu Bo6f519562013-10-29 10:45:05 +08002666 free_sa_defrag_extent(new);
2667
Liu Bo38c227d2013-01-29 03:18:40 +00002668 atomic_dec(&root->fs_info->defrag_running);
2669 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002670}
2671
2672static struct new_sa_defrag_extent *
2673record_old_file_extents(struct inode *inode,
2674 struct btrfs_ordered_extent *ordered)
2675{
2676 struct btrfs_root *root = BTRFS_I(inode)->root;
2677 struct btrfs_path *path;
2678 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002679 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002680 struct new_sa_defrag_extent *new;
2681 int ret;
2682
2683 new = kmalloc(sizeof(*new), GFP_NOFS);
2684 if (!new)
2685 return NULL;
2686
2687 new->inode = inode;
2688 new->file_pos = ordered->file_offset;
2689 new->len = ordered->len;
2690 new->bytenr = ordered->start;
2691 new->disk_len = ordered->disk_len;
2692 new->compress_type = ordered->compress_type;
2693 new->root = RB_ROOT;
2694 INIT_LIST_HEAD(&new->head);
2695
2696 path = btrfs_alloc_path();
2697 if (!path)
2698 goto out_kfree;
2699
2700 key.objectid = btrfs_ino(inode);
2701 key.type = BTRFS_EXTENT_DATA_KEY;
2702 key.offset = new->file_pos;
2703
2704 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2705 if (ret < 0)
2706 goto out_free_path;
2707 if (ret > 0 && path->slots[0] > 0)
2708 path->slots[0]--;
2709
2710 /* find out all the old extents for the file range */
2711 while (1) {
2712 struct btrfs_file_extent_item *extent;
2713 struct extent_buffer *l;
2714 int slot;
2715 u64 num_bytes;
2716 u64 offset;
2717 u64 end;
2718 u64 disk_bytenr;
2719 u64 extent_offset;
2720
2721 l = path->nodes[0];
2722 slot = path->slots[0];
2723
2724 if (slot >= btrfs_header_nritems(l)) {
2725 ret = btrfs_next_leaf(root, path);
2726 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002727 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002728 else if (ret > 0)
2729 break;
2730 continue;
2731 }
2732
2733 btrfs_item_key_to_cpu(l, &key, slot);
2734
2735 if (key.objectid != btrfs_ino(inode))
2736 break;
2737 if (key.type != BTRFS_EXTENT_DATA_KEY)
2738 break;
2739 if (key.offset >= new->file_pos + new->len)
2740 break;
2741
2742 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2743
2744 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2745 if (key.offset + num_bytes < new->file_pos)
2746 goto next;
2747
2748 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2749 if (!disk_bytenr)
2750 goto next;
2751
2752 extent_offset = btrfs_file_extent_offset(l, extent);
2753
2754 old = kmalloc(sizeof(*old), GFP_NOFS);
2755 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002756 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002757
2758 offset = max(new->file_pos, key.offset);
2759 end = min(new->file_pos + new->len, key.offset + num_bytes);
2760
2761 old->bytenr = disk_bytenr;
2762 old->extent_offset = extent_offset;
2763 old->offset = offset - key.offset;
2764 old->len = end - offset;
2765 old->new = new;
2766 old->count = 0;
2767 list_add_tail(&old->list, &new->head);
2768next:
2769 path->slots[0]++;
2770 cond_resched();
2771 }
2772
2773 btrfs_free_path(path);
2774 atomic_inc(&root->fs_info->defrag_running);
2775
2776 return new;
2777
Liu Bo38c227d2013-01-29 03:18:40 +00002778out_free_path:
2779 btrfs_free_path(path);
2780out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002781 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002782 return NULL;
2783}
2784
Miao Xiee570fd22014-06-19 10:42:50 +08002785static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2786 u64 start, u64 len)
2787{
2788 struct btrfs_block_group_cache *cache;
2789
2790 cache = btrfs_lookup_block_group(root->fs_info, start);
2791 ASSERT(cache);
2792
2793 spin_lock(&cache->lock);
2794 cache->delalloc_bytes -= len;
2795 spin_unlock(&cache->lock);
2796
2797 btrfs_put_block_group(cache);
2798}
2799
Chris Masond352ac62008-09-29 15:18:18 -04002800/* as ordered data IO finishes, this gets called so we can finish
2801 * an ordered extent if the range of bytes in the file it covers are
2802 * fully written.
2803 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002804static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002805{
Josef Bacik5fd02042012-05-02 14:00:54 -04002806 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002807 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002808 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002809 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002810 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002811 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002812 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002813 int ret = 0;
2814 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002815 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002816 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002817
Liu Bo83eea1f2012-07-10 05:28:39 -06002818 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002819
Josef Bacik5fd02042012-05-02 14:00:54 -04002820 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2821 ret = -EIO;
2822 goto out;
2823 }
2824
Miao Xief6124962014-09-12 18:44:04 +08002825 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2826 ordered_extent->file_offset +
2827 ordered_extent->len - 1);
2828
Josef Bacik77cef2e2013-08-29 13:57:21 -04002829 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2830 truncated = true;
2831 logical_len = ordered_extent->truncated_len;
2832 /* Truncated the entire extent, don't bother adding */
2833 if (!logical_len)
2834 goto out;
2835 }
2836
Yan, Zhengc2167752009-11-12 09:34:21 +00002837 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002838 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002839 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2840 if (nolock)
2841 trans = btrfs_join_transaction_nolock(root);
2842 else
2843 trans = btrfs_join_transaction(root);
2844 if (IS_ERR(trans)) {
2845 ret = PTR_ERR(trans);
2846 trans = NULL;
2847 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002848 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002849 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2850 ret = btrfs_update_inode_fallback(trans, root, inode);
2851 if (ret) /* -ENOMEM or corruption */
2852 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002853 goto out;
2854 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002855
Josef Bacik2ac55d42010-02-03 19:33:23 +00002856 lock_extent_bits(io_tree, ordered_extent->file_offset,
2857 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002858 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002859
Liu Bo38c227d2013-01-29 03:18:40 +00002860 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2861 ordered_extent->file_offset + ordered_extent->len - 1,
2862 EXTENT_DEFRAG, 1, cached_state);
2863 if (ret) {
2864 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002865 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002866 /* the inode is shared */
2867 new = record_old_file_extents(inode, ordered_extent);
2868
2869 clear_extent_bit(io_tree, ordered_extent->file_offset,
2870 ordered_extent->file_offset + ordered_extent->len - 1,
2871 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2872 }
2873
Josef Bacik0cb59c92010-07-02 12:14:14 -04002874 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002875 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002876 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002877 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002878 if (IS_ERR(trans)) {
2879 ret = PTR_ERR(trans);
2880 trans = NULL;
2881 goto out_unlock;
2882 }
Chris Masona79b7d42014-05-22 16:18:52 -07002883
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002884 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002885
Chris Masonc8b97812008-10-29 14:49:59 -04002886 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002887 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002888 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002889 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002890 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002891 ordered_extent->file_offset,
2892 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002893 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002894 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002895 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002896 ret = insert_reserved_file_extent(trans, inode,
2897 ordered_extent->file_offset,
2898 ordered_extent->start,
2899 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002900 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002901 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002902 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002903 if (!ret)
2904 btrfs_release_delalloc_bytes(root,
2905 ordered_extent->start,
2906 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002907 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002908 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2909 ordered_extent->file_offset, ordered_extent->len,
2910 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002911 if (ret < 0) {
2912 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002913 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002914 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002915
Chris Masone6dcd2d2008-07-17 12:53:50 -04002916 add_pending_csums(trans, inode, ordered_extent->file_offset,
2917 &ordered_extent->list);
2918
Josef Bacik6c760c02012-11-09 10:53:21 -05002919 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2920 ret = btrfs_update_inode_fallback(trans, root, inode);
2921 if (ret) { /* -ENOMEM or corruption */
2922 btrfs_abort_transaction(trans, root, ret);
2923 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002924 }
2925 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002926out_unlock:
2927 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2928 ordered_extent->file_offset +
2929 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002930out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002931 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002932 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002933 if (trans)
2934 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002935
Josef Bacik77cef2e2013-08-29 13:57:21 -04002936 if (ret || truncated) {
2937 u64 start, end;
2938
2939 if (truncated)
2940 start = ordered_extent->file_offset + logical_len;
2941 else
2942 start = ordered_extent->file_offset;
2943 end = ordered_extent->file_offset + ordered_extent->len - 1;
2944 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2945
2946 /* Drop the cache for the part of the extent we didn't write. */
2947 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002948
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002949 /*
2950 * If the ordered extent had an IOERR or something else went
2951 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002952 * back to the allocator. We only free the extent in the
2953 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002954 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002955 if ((ret || !logical_len) &&
2956 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002957 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2958 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002959 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002960 }
2961
2962
Josef Bacik5fd02042012-05-02 14:00:54 -04002963 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002964 * This needs to be done to make sure anybody waiting knows we are done
2965 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002966 */
2967 btrfs_remove_ordered_extent(inode, ordered_extent);
2968
Liu Bo38c227d2013-01-29 03:18:40 +00002969 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002970 if (new) {
2971 if (ret) {
2972 free_sa_defrag_extent(new);
2973 atomic_dec(&root->fs_info->defrag_running);
2974 } else {
2975 relink_file_extents(new);
2976 }
2977 }
Liu Bo38c227d2013-01-29 03:18:40 +00002978
Chris Masone6dcd2d2008-07-17 12:53:50 -04002979 /* once for us */
2980 btrfs_put_ordered_extent(ordered_extent);
2981 /* once for the tree */
2982 btrfs_put_ordered_extent(ordered_extent);
2983
Josef Bacik5fd02042012-05-02 14:00:54 -04002984 return ret;
2985}
2986
2987static void finish_ordered_fn(struct btrfs_work *work)
2988{
2989 struct btrfs_ordered_extent *ordered_extent;
2990 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2991 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002992}
2993
Christoph Hellwigb2950862008-12-02 09:54:17 -05002994static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002995 struct extent_state *state, int uptodate)
2996{
Josef Bacik5fd02042012-05-02 14:00:54 -04002997 struct inode *inode = page->mapping->host;
2998 struct btrfs_root *root = BTRFS_I(inode)->root;
2999 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003000 struct btrfs_workqueue *wq;
3001 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003002
liubo1abe9b82011-03-24 11:18:59 +00003003 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3004
Chris Mason8b62b722009-09-02 16:53:46 -04003005 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003006 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3007 end - start + 1, uptodate))
3008 return 0;
3009
Liu Bo9e0af232014-08-15 23:36:53 +08003010 if (btrfs_is_free_space_inode(inode)) {
3011 wq = root->fs_info->endio_freespace_worker;
3012 func = btrfs_freespace_write_helper;
3013 } else {
3014 wq = root->fs_info->endio_write_workers;
3015 func = btrfs_endio_write_helper;
3016 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003017
Liu Bo9e0af232014-08-15 23:36:53 +08003018 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3019 NULL);
3020 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003021
3022 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003023}
3024
Miao Xiedc380ae2014-09-12 18:43:55 +08003025static int __readpage_endio_check(struct inode *inode,
3026 struct btrfs_io_bio *io_bio,
3027 int icsum, struct page *page,
3028 int pgoff, u64 start, size_t len)
3029{
3030 char *kaddr;
3031 u32 csum_expected;
3032 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003033
3034 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3035
3036 kaddr = kmap_atomic(page);
3037 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3038 btrfs_csum_final(csum, (char *)&csum);
3039 if (csum != csum_expected)
3040 goto zeroit;
3041
3042 kunmap_atomic(kaddr);
3043 return 0;
3044zeroit:
David Sterba94647322015-10-08 11:01:36 +02003045 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3046 "csum failed ino %llu off %llu csum %u expected csum %u",
Miao Xiedc380ae2014-09-12 18:43:55 +08003047 btrfs_ino(inode), start, csum, csum_expected);
3048 memset(kaddr + pgoff, 1, len);
3049 flush_dcache_page(page);
3050 kunmap_atomic(kaddr);
3051 if (csum_expected == 0)
3052 return 0;
3053 return -EIO;
3054}
3055
Chris Masond352ac62008-09-29 15:18:18 -04003056/*
Chris Masond352ac62008-09-29 15:18:18 -04003057 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003058 * if there's a match, we allow the bio to finish. If not, the code in
3059 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003060 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003061static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3062 u64 phy_offset, struct page *page,
3063 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003064{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003065 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003066 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003067 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003068 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003069
Chris Masond20f7042008-12-08 16:58:54 -05003070 if (PageChecked(page)) {
3071 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003072 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003073 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003074
3075 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003076 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003077
Yan Zheng17d217f2008-12-12 10:03:38 -05003078 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003079 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05003080 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
3081 GFP_NOFS);
3082 return 0;
3083 }
3084
Miao Xiefacc8a222013-07-25 19:22:34 +08003085 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003086 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3087 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003088}
Chris Masonb888db22007-08-27 16:49:44 -04003089
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003090struct delayed_iput {
3091 struct list_head list;
3092 struct inode *inode;
3093};
3094
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003095/* JDM: If this is fs-wide, why can't we add a pointer to
3096 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003097void btrfs_add_delayed_iput(struct inode *inode)
3098{
3099 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3100 struct delayed_iput *delayed;
3101
3102 if (atomic_add_unless(&inode->i_count, -1, 1))
3103 return;
3104
3105 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3106 delayed->inode = inode;
3107
3108 spin_lock(&fs_info->delayed_iput_lock);
3109 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3110 spin_unlock(&fs_info->delayed_iput_lock);
3111}
3112
3113void btrfs_run_delayed_iputs(struct btrfs_root *root)
3114{
3115 LIST_HEAD(list);
3116 struct btrfs_fs_info *fs_info = root->fs_info;
3117 struct delayed_iput *delayed;
3118 int empty;
3119
3120 spin_lock(&fs_info->delayed_iput_lock);
3121 empty = list_empty(&fs_info->delayed_iputs);
3122 spin_unlock(&fs_info->delayed_iput_lock);
3123 if (empty)
3124 return;
3125
Zhao Leid7c15172015-02-26 10:49:20 +08003126 down_read(&fs_info->delayed_iput_sem);
3127
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003128 spin_lock(&fs_info->delayed_iput_lock);
3129 list_splice_init(&fs_info->delayed_iputs, &list);
3130 spin_unlock(&fs_info->delayed_iput_lock);
3131
3132 while (!list_empty(&list)) {
3133 delayed = list_entry(list.next, struct delayed_iput, list);
3134 list_del(&delayed->list);
3135 iput(delayed->inode);
3136 kfree(delayed);
3137 }
Zhao Leid7c15172015-02-26 10:49:20 +08003138
3139 up_read(&root->fs_info->delayed_iput_sem);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003140}
3141
Yan, Zhengd68fc572010-05-16 10:49:58 -04003142/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003143 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003144 * files in the subvolume, it removes orphan item and frees block_rsv
3145 * structure.
3146 */
3147void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3148 struct btrfs_root *root)
3149{
Josef Bacik90290e12011-12-02 15:44:12 -05003150 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003151 int ret;
3152
Josef Bacik8a35d952012-05-23 14:26:42 -04003153 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003154 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3155 return;
3156
Josef Bacik90290e12011-12-02 15:44:12 -05003157 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003158 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003159 spin_unlock(&root->orphan_lock);
3160 return;
3161 }
3162
3163 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3164 spin_unlock(&root->orphan_lock);
3165 return;
3166 }
3167
3168 block_rsv = root->orphan_block_rsv;
3169 root->orphan_block_rsv = NULL;
3170 spin_unlock(&root->orphan_lock);
3171
Miao Xie27cdeb72014-04-02 19:51:05 +08003172 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003173 btrfs_root_refs(&root->root_item) > 0) {
3174 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3175 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003176 if (ret)
3177 btrfs_abort_transaction(trans, root, ret);
3178 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003179 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3180 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003181 }
3182
Josef Bacik90290e12011-12-02 15:44:12 -05003183 if (block_rsv) {
3184 WARN_ON(block_rsv->size > 0);
3185 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003186 }
3187}
3188
3189/*
Josef Bacik7b128762008-07-24 12:17:14 -04003190 * This creates an orphan entry for the given inode in case something goes
3191 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003192 *
3193 * NOTE: caller of this function should reserve 5 units of metadata for
3194 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003195 */
3196int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3197{
3198 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003199 struct btrfs_block_rsv *block_rsv = NULL;
3200 int reserve = 0;
3201 int insert = 0;
3202 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003203
Yan, Zhengd68fc572010-05-16 10:49:58 -04003204 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003205 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003206 if (!block_rsv)
3207 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003208 }
3209
Yan, Zhengd68fc572010-05-16 10:49:58 -04003210 spin_lock(&root->orphan_lock);
3211 if (!root->orphan_block_rsv) {
3212 root->orphan_block_rsv = block_rsv;
3213 } else if (block_rsv) {
3214 btrfs_free_block_rsv(root, block_rsv);
3215 block_rsv = NULL;
3216 }
Josef Bacik7b128762008-07-24 12:17:14 -04003217
Josef Bacik8a35d952012-05-23 14:26:42 -04003218 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3219 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003220#if 0
3221 /*
3222 * For proper ENOSPC handling, we should do orphan
3223 * cleanup when mounting. But this introduces backward
3224 * compatibility issue.
3225 */
3226 if (!xchg(&root->orphan_item_inserted, 1))
3227 insert = 2;
3228 else
3229 insert = 1;
3230#endif
3231 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003232 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003233 }
Josef Bacik7b128762008-07-24 12:17:14 -04003234
Josef Bacik72ac3c02012-05-23 14:13:11 -04003235 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3236 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003237 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003238 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003239
Yan, Zhengd68fc572010-05-16 10:49:58 -04003240 /* grab metadata reservation from transaction handle */
3241 if (reserve) {
3242 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003243 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003244 }
3245
3246 /* insert an orphan item to track this unlinked/truncated file */
3247 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003248 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003249 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003250 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003251 if (reserve) {
3252 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3253 &BTRFS_I(inode)->runtime_flags);
3254 btrfs_orphan_release_metadata(inode);
3255 }
3256 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003257 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3258 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003259 btrfs_abort_transaction(trans, root, ret);
3260 return ret;
3261 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003262 }
3263 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003264 }
3265
3266 /* insert an orphan item to track subvolume contains orphan files */
3267 if (insert >= 2) {
3268 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3269 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003270 if (ret && ret != -EEXIST) {
3271 btrfs_abort_transaction(trans, root, ret);
3272 return ret;
3273 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003274 }
3275 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003276}
3277
3278/*
3279 * We have done the truncate/delete so we can go ahead and remove the orphan
3280 * item for this particular inode.
3281 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003282static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3283 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003284{
3285 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003286 int delete_item = 0;
3287 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003288 int ret = 0;
3289
Yan, Zhengd68fc572010-05-16 10:49:58 -04003290 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003291 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3292 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003293 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003294
Josef Bacik72ac3c02012-05-23 14:13:11 -04003295 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3296 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003297 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003298 spin_unlock(&root->orphan_lock);
3299
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003300 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003301 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003302 if (trans)
3303 ret = btrfs_del_orphan_item(trans, root,
3304 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003305 }
Josef Bacik7b128762008-07-24 12:17:14 -04003306
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003307 if (release_rsv)
3308 btrfs_orphan_release_metadata(inode);
3309
Josef Bacik4ef31a42013-08-13 14:10:08 -04003310 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003311}
3312
3313/*
3314 * this cleans up any orphans that may be left on the list from the last use
3315 * of this root.
3316 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003317int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003318{
3319 struct btrfs_path *path;
3320 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003321 struct btrfs_key key, found_key;
3322 struct btrfs_trans_handle *trans;
3323 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003324 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003325 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3326
Yan, Zhengd68fc572010-05-16 10:49:58 -04003327 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003328 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003329
3330 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003331 if (!path) {
3332 ret = -ENOMEM;
3333 goto out;
3334 }
Josef Bacik7b128762008-07-24 12:17:14 -04003335 path->reada = -1;
3336
3337 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003338 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003339 key.offset = (u64)-1;
3340
Josef Bacik7b128762008-07-24 12:17:14 -04003341 while (1) {
3342 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003343 if (ret < 0)
3344 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003345
3346 /*
3347 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003348 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003349 * find the key and see if we have stuff that matches
3350 */
3351 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003352 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003353 if (path->slots[0] == 0)
3354 break;
3355 path->slots[0]--;
3356 }
3357
3358 /* pull out the item */
3359 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003360 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3361
3362 /* make sure the item matches what we want */
3363 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3364 break;
David Sterba962a2982014-06-04 18:41:45 +02003365 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003366 break;
3367
3368 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003369 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003370
3371 /*
3372 * this is where we are basically btrfs_lookup, without the
3373 * crossing root thing. we store the inode number in the
3374 * offset of the orphan item.
3375 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003376
3377 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003378 btrfs_err(root->fs_info,
3379 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003380 ret = -EINVAL;
3381 goto out;
3382 }
3383
3384 last_objectid = found_key.offset;
3385
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003386 found_key.objectid = found_key.offset;
3387 found_key.type = BTRFS_INODE_ITEM_KEY;
3388 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003389 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303390 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003391 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003392 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003393
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003394 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3395 struct btrfs_root *dead_root;
3396 struct btrfs_fs_info *fs_info = root->fs_info;
3397 int is_dead_root = 0;
3398
3399 /*
3400 * this is an orphan in the tree root. Currently these
3401 * could come from 2 sources:
3402 * a) a snapshot deletion in progress
3403 * b) a free space cache inode
3404 * We need to distinguish those two, as the snapshot
3405 * orphan must not get deleted.
3406 * find_dead_roots already ran before us, so if this
3407 * is a snapshot deletion, we should find the root
3408 * in the dead_roots list
3409 */
3410 spin_lock(&fs_info->trans_lock);
3411 list_for_each_entry(dead_root, &fs_info->dead_roots,
3412 root_list) {
3413 if (dead_root->root_key.objectid ==
3414 found_key.objectid) {
3415 is_dead_root = 1;
3416 break;
3417 }
3418 }
3419 spin_unlock(&fs_info->trans_lock);
3420 if (is_dead_root) {
3421 /* prevent this orphan from being found again */
3422 key.offset = found_key.objectid - 1;
3423 continue;
3424 }
3425 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003426 /*
3427 * Inode is already gone but the orphan item is still there,
3428 * kill the orphan item.
3429 */
3430 if (ret == -ESTALE) {
3431 trans = btrfs_start_transaction(root, 1);
3432 if (IS_ERR(trans)) {
3433 ret = PTR_ERR(trans);
3434 goto out;
3435 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003436 btrfs_debug(root->fs_info, "auto deleting %Lu",
3437 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003438 ret = btrfs_del_orphan_item(trans, root,
3439 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003440 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003441 if (ret)
3442 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003443 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003444 }
Josef Bacik7b128762008-07-24 12:17:14 -04003445
Josef Bacik7b128762008-07-24 12:17:14 -04003446 /*
3447 * add this inode to the orphan list so btrfs_orphan_del does
3448 * the proper thing when we hit it
3449 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003450 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3451 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003452 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003453
Josef Bacik7b128762008-07-24 12:17:14 -04003454 /* if we have links, this was a truncate, lets do that */
3455 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303456 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003457 iput(inode);
3458 continue;
3459 }
Josef Bacik7b128762008-07-24 12:17:14 -04003460 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003461
3462 /* 1 for the orphan item deletion. */
3463 trans = btrfs_start_transaction(root, 1);
3464 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003465 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003466 ret = PTR_ERR(trans);
3467 goto out;
3468 }
3469 ret = btrfs_orphan_add(trans, inode);
3470 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003471 if (ret) {
3472 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003473 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003474 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003475
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003476 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003477 if (ret)
3478 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003479 } else {
3480 nr_unlink++;
3481 }
3482
3483 /* this will do delete_inode and everything for us */
3484 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003485 if (ret)
3486 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003487 }
Miao Xie3254c872011-11-10 20:45:05 -05003488 /* release the path since we're done with it */
3489 btrfs_release_path(path);
3490
Yan, Zhengd68fc572010-05-16 10:49:58 -04003491 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3492
3493 if (root->orphan_block_rsv)
3494 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3495 (u64)-1);
3496
Miao Xie27cdeb72014-04-02 19:51:05 +08003497 if (root->orphan_block_rsv ||
3498 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003499 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003500 if (!IS_ERR(trans))
3501 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003502 }
Josef Bacik7b128762008-07-24 12:17:14 -04003503
3504 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003505 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003506 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003507 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003508
3509out:
3510 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003511 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003512 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003513 btrfs_free_path(path);
3514 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003515}
3516
Chris Masond352ac62008-09-29 15:18:18 -04003517/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003518 * very simple check to peek ahead in the leaf looking for xattrs. If we
3519 * don't find any xattrs, we know there can't be any acls.
3520 *
3521 * slot is the slot the inode is in, objectid is the objectid of the inode
3522 */
3523static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003524 int slot, u64 objectid,
3525 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003526{
3527 u32 nritems = btrfs_header_nritems(leaf);
3528 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003529 static u64 xattr_access = 0;
3530 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003531 int scanned = 0;
3532
Josef Bacikf23b5a52013-06-19 10:16:26 -04003533 if (!xattr_access) {
3534 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3535 strlen(POSIX_ACL_XATTR_ACCESS));
3536 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3537 strlen(POSIX_ACL_XATTR_DEFAULT));
3538 }
3539
Chris Mason46a53cc2009-04-27 11:47:50 -04003540 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003541 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003542 while (slot < nritems) {
3543 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3544
3545 /* we found a different objectid, there must not be acls */
3546 if (found_key.objectid != objectid)
3547 return 0;
3548
3549 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003550 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003551 if (*first_xattr_slot == -1)
3552 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003553 if (found_key.offset == xattr_access ||
3554 found_key.offset == xattr_default)
3555 return 1;
3556 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003557
3558 /*
3559 * we found a key greater than an xattr key, there can't
3560 * be any acls later on
3561 */
3562 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3563 return 0;
3564
3565 slot++;
3566 scanned++;
3567
3568 /*
3569 * it goes inode, inode backrefs, xattrs, extents,
3570 * so if there are a ton of hard links to an inode there can
3571 * be a lot of backrefs. Don't waste time searching too hard,
3572 * this is just an optimization
3573 */
3574 if (scanned >= 8)
3575 break;
3576 }
3577 /* we hit the end of the leaf before we found an xattr or
3578 * something larger than an xattr. We have to assume the inode
3579 * has acls
3580 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003581 if (*first_xattr_slot == -1)
3582 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003583 return 1;
3584}
3585
3586/*
Chris Masond352ac62008-09-29 15:18:18 -04003587 * read an inode from the btree into the in-memory inode
3588 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003589static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003590{
3591 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003592 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003593 struct btrfs_inode_item *inode_item;
3594 struct btrfs_root *root = BTRFS_I(inode)->root;
3595 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003596 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003597 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003598 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003599 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003600 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003601 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003602
3603 ret = btrfs_fill_inode(inode, &rdev);
3604 if (!ret)
3605 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003606
3607 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003608 if (!path)
3609 goto make_bad;
3610
Chris Mason39279cc2007-06-12 06:35:45 -04003611 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003612
Chris Mason39279cc2007-06-12 06:35:45 -04003613 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003614 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003615 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003616
Chris Mason5f39d392007-10-15 16:14:19 -04003617 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003618
3619 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003620 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003621
Chris Mason5f39d392007-10-15 16:14:19 -04003622 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3623 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003624 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003625 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003626 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3627 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003628 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003629
David Sterbaa937b972014-12-12 17:39:12 +01003630 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3631 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003632
David Sterbaa937b972014-12-12 17:39:12 +01003633 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3634 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003635
David Sterbaa937b972014-12-12 17:39:12 +01003636 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3637 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003638
chandan r9cc97d62012-07-04 12:48:07 +05303639 BTRFS_I(inode)->i_otime.tv_sec =
3640 btrfs_timespec_sec(leaf, &inode_item->otime);
3641 BTRFS_I(inode)->i_otime.tv_nsec =
3642 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003643
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003644 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003645 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003646 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3647
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003648 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003649 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003650 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003651 rdev = btrfs_inode_rdev(leaf, inode_item);
3652
Josef Bacikaec74772008-07-24 12:12:38 -04003653 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003654 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003655
3656cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003657 /*
3658 * If we were modified in the current generation and evicted from memory
3659 * and then re-read we need to do a full sync since we don't have any
3660 * idea about which extents were modified before we were evicted from
3661 * cache.
3662 *
3663 * This is required for both inode re-read from disk and delayed inode
3664 * in delayed_nodes_tree.
3665 */
3666 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3667 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3668 &BTRFS_I(inode)->runtime_flags);
3669
Filipe Mananabde6c242015-07-24 00:00:19 +01003670 /*
3671 * We don't persist the id of the transaction where an unlink operation
3672 * against the inode was last made. So here we assume the inode might
3673 * have been evicted, and therefore the exact value of last_unlink_trans
3674 * lost, and set it to last_trans to avoid metadata inconsistencies
3675 * between the inode and its parent if the inode is fsync'ed and the log
3676 * replayed. For example, in the scenario:
3677 *
3678 * touch mydir/foo
3679 * ln mydir/foo mydir/bar
3680 * sync
3681 * unlink mydir/bar
3682 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3683 * xfs_io -c fsync mydir/foo
3684 * <power failure>
3685 * mount fs, triggers fsync log replay
3686 *
3687 * We must make sure that when we fsync our inode foo we also log its
3688 * parent inode, otherwise after log replay the parent still has the
3689 * dentry with the "bar" name but our inode foo has a link count of 1
3690 * and doesn't have an inode ref with the name "bar" anymore.
3691 *
3692 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3693 * but it guarantees correctness at the expense of ocassional full
3694 * transaction commits on fsync if our inode is a directory, or if our
3695 * inode is not a directory, logging its parent unnecessarily.
3696 */
3697 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3698
Miao Xie67de1172013-12-26 13:07:06 +08003699 path->slots[0]++;
3700 if (inode->i_nlink != 1 ||
3701 path->slots[0] >= btrfs_header_nritems(leaf))
3702 goto cache_acl;
3703
3704 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3705 if (location.objectid != btrfs_ino(inode))
3706 goto cache_acl;
3707
3708 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3709 if (location.type == BTRFS_INODE_REF_KEY) {
3710 struct btrfs_inode_ref *ref;
3711
3712 ref = (struct btrfs_inode_ref *)ptr;
3713 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3714 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3715 struct btrfs_inode_extref *extref;
3716
3717 extref = (struct btrfs_inode_extref *)ptr;
3718 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3719 extref);
3720 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003721cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003722 /*
3723 * try to precache a NULL acl entry for files that don't have
3724 * any xattrs or acls
3725 */
Li Zefan33345d012011-04-20 10:31:50 +08003726 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003727 btrfs_ino(inode), &first_xattr_slot);
3728 if (first_xattr_slot != -1) {
3729 path->slots[0] = first_xattr_slot;
3730 ret = btrfs_load_inode_props(inode, path);
3731 if (ret)
3732 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003733 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003734 btrfs_ino(inode),
3735 root->root_key.objectid, ret);
3736 }
3737 btrfs_free_path(path);
3738
Al Viro72c04902009-06-24 16:58:48 -04003739 if (!maybe_acls)
3740 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003741
Chris Mason39279cc2007-06-12 06:35:45 -04003742 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003743 case S_IFREG:
3744 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003745 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003746 inode->i_fop = &btrfs_file_operations;
3747 inode->i_op = &btrfs_file_inode_operations;
3748 break;
3749 case S_IFDIR:
3750 inode->i_fop = &btrfs_dir_file_operations;
3751 if (root == root->fs_info->tree_root)
3752 inode->i_op = &btrfs_dir_ro_inode_operations;
3753 else
3754 inode->i_op = &btrfs_dir_inode_operations;
3755 break;
3756 case S_IFLNK:
3757 inode->i_op = &btrfs_symlink_inode_operations;
3758 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3759 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003760 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003761 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003762 init_special_inode(inode, inode->i_mode, rdev);
3763 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003764 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003765
3766 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003767 return;
3768
3769make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003770 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003771 make_bad_inode(inode);
3772}
3773
Chris Masond352ac62008-09-29 15:18:18 -04003774/*
3775 * given a leaf and an inode, copy the inode fields into the leaf
3776 */
Chris Masone02119d2008-09-05 16:13:11 -04003777static void fill_inode_item(struct btrfs_trans_handle *trans,
3778 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003779 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003780 struct inode *inode)
3781{
Liu Bo51fab692012-12-27 09:01:21 +00003782 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003783
Liu Bo51fab692012-12-27 09:01:21 +00003784 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003785
Liu Bo51fab692012-12-27 09:01:21 +00003786 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3787 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3788 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3789 &token);
3790 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3791 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003792
David Sterbaa937b972014-12-12 17:39:12 +01003793 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003794 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003795 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003796 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003797
David Sterbaa937b972014-12-12 17:39:12 +01003798 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003799 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003800 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003801 inode->i_mtime.tv_nsec, &token);
3802
David Sterbaa937b972014-12-12 17:39:12 +01003803 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003804 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003805 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003806 inode->i_ctime.tv_nsec, &token);
3807
chandan r9cc97d62012-07-04 12:48:07 +05303808 btrfs_set_token_timespec_sec(leaf, &item->otime,
3809 BTRFS_I(inode)->i_otime.tv_sec, &token);
3810 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3811 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3812
Liu Bo51fab692012-12-27 09:01:21 +00003813 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3814 &token);
3815 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3816 &token);
3817 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3818 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3819 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3820 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3821 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003822}
3823
Chris Masond352ac62008-09-29 15:18:18 -04003824/*
3825 * copy everything in the in-memory inode into the btree.
3826 */
Chris Mason21151332011-11-10 20:39:08 -05003827static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003828 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003829{
3830 struct btrfs_inode_item *inode_item;
3831 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003832 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003833 int ret;
3834
3835 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003836 if (!path)
3837 return -ENOMEM;
3838
Chris Masonb9473432009-03-13 11:00:37 -04003839 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003840 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3841 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003842 if (ret) {
3843 if (ret > 0)
3844 ret = -ENOENT;
3845 goto failed;
3846 }
3847
Chris Mason5f39d392007-10-15 16:14:19 -04003848 leaf = path->nodes[0];
3849 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003850 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003851
Chris Masone02119d2008-09-05 16:13:11 -04003852 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003853 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003854 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003855 ret = 0;
3856failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003857 btrfs_free_path(path);
3858 return ret;
3859}
3860
Chris Masond352ac62008-09-29 15:18:18 -04003861/*
Chris Mason21151332011-11-10 20:39:08 -05003862 * copy everything in the in-memory inode into the btree.
3863 */
3864noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3865 struct btrfs_root *root, struct inode *inode)
3866{
3867 int ret;
3868
3869 /*
3870 * If the inode is a free space inode, we can deadlock during commit
3871 * if we put it into the delayed code.
3872 *
3873 * The data relocation inode should also be directly updated
3874 * without delay
3875 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003876 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003877 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3878 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003879 btrfs_update_root_times(trans, root);
3880
Chris Mason21151332011-11-10 20:39:08 -05003881 ret = btrfs_delayed_update_inode(trans, root, inode);
3882 if (!ret)
3883 btrfs_set_inode_last_trans(trans, inode);
3884 return ret;
3885 }
3886
3887 return btrfs_update_inode_item(trans, root, inode);
3888}
3889
Josef Bacikbe6aef62012-10-22 15:43:12 -04003890noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3891 struct btrfs_root *root,
3892 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003893{
3894 int ret;
3895
3896 ret = btrfs_update_inode(trans, root, inode);
3897 if (ret == -ENOSPC)
3898 return btrfs_update_inode_item(trans, root, inode);
3899 return ret;
3900}
3901
3902/*
Chris Masond352ac62008-09-29 15:18:18 -04003903 * unlink helper that gets used here in inode.c and in the tree logging
3904 * recovery code. It remove a link in a directory with a given name, and
3905 * also drops the back refs in the inode to the directory
3906 */
Al Viro92986792011-03-04 17:14:37 +00003907static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3908 struct btrfs_root *root,
3909 struct inode *dir, struct inode *inode,
3910 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003911{
3912 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003913 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003914 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003915 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003916 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003917 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003918 u64 ino = btrfs_ino(inode);
3919 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003920
3921 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003922 if (!path) {
3923 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003924 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003925 }
3926
Chris Masonb9473432009-03-13 11:00:37 -04003927 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003928 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003929 name, name_len, -1);
3930 if (IS_ERR(di)) {
3931 ret = PTR_ERR(di);
3932 goto err;
3933 }
3934 if (!di) {
3935 ret = -ENOENT;
3936 goto err;
3937 }
Chris Mason5f39d392007-10-15 16:14:19 -04003938 leaf = path->nodes[0];
3939 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003940 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003941 if (ret)
3942 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003943 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003944
Miao Xie67de1172013-12-26 13:07:06 +08003945 /*
3946 * If we don't have dir index, we have to get it by looking up
3947 * the inode ref, since we get the inode ref, remove it directly,
3948 * it is unnecessary to do delayed deletion.
3949 *
3950 * But if we have dir index, needn't search inode ref to get it.
3951 * Since the inode ref is close to the inode item, it is better
3952 * that we delay to delete it, and just do this deletion when
3953 * we update the inode item.
3954 */
3955 if (BTRFS_I(inode)->dir_index) {
3956 ret = btrfs_delayed_delete_inode_ref(inode);
3957 if (!ret) {
3958 index = BTRFS_I(inode)->dir_index;
3959 goto skip_backref;
3960 }
3961 }
3962
Li Zefan33345d012011-04-20 10:31:50 +08003963 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3964 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003965 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003966 btrfs_info(root->fs_info,
3967 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003968 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003969 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003970 goto err;
3971 }
Miao Xie67de1172013-12-26 13:07:06 +08003972skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003973 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003974 if (ret) {
3975 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003976 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003977 }
Chris Mason39279cc2007-06-12 06:35:45 -04003978
Chris Masone02119d2008-09-05 16:13:11 -04003979 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003980 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003981 if (ret != 0 && ret != -ENOENT) {
3982 btrfs_abort_transaction(trans, root, ret);
3983 goto err;
3984 }
Chris Masone02119d2008-09-05 16:13:11 -04003985
3986 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3987 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003988 if (ret == -ENOENT)
3989 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003990 else if (ret)
3991 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003992err:
3993 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003994 if (ret)
3995 goto out;
3996
3997 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003998 inode_inc_iversion(inode);
3999 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04004000 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06004001 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004002out:
Chris Mason39279cc2007-06-12 06:35:45 -04004003 return ret;
4004}
4005
Al Viro92986792011-03-04 17:14:37 +00004006int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4007 struct btrfs_root *root,
4008 struct inode *dir, struct inode *inode,
4009 const char *name, int name_len)
4010{
4011 int ret;
4012 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4013 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004014 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004015 ret = btrfs_update_inode(trans, root, inode);
4016 }
4017 return ret;
4018}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004019
4020/*
4021 * helper to start transaction for unlink and rmdir.
4022 *
Josef Bacikd52be812013-05-29 14:54:47 -04004023 * unlink and rmdir are special in btrfs, they do not always free space, so
4024 * if we cannot make our reservations the normal way try and see if there is
4025 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4026 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004027 */
Josef Bacikd52be812013-05-29 14:54:47 -04004028static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004029{
4030 struct btrfs_trans_handle *trans;
4031 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004032 int ret;
4033
Josef Bacike70bea52011-10-11 14:18:24 -04004034 /*
4035 * 1 for the possible orphan item
4036 * 1 for the dir item
4037 * 1 for the dir index
4038 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004039 * 1 for the inode
4040 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04004041 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004042 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
4043 return trans;
4044
Josef Bacikd52be812013-05-29 14:54:47 -04004045 if (PTR_ERR(trans) == -ENOSPC) {
4046 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004047
Josef Bacikd52be812013-05-29 14:54:47 -04004048 trans = btrfs_start_transaction(root, 0);
4049 if (IS_ERR(trans))
4050 return trans;
4051 ret = btrfs_cond_migrate_bytes(root->fs_info,
4052 &root->fs_info->trans_block_rsv,
4053 num_bytes, 5);
4054 if (ret) {
4055 btrfs_end_transaction(trans, root);
4056 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004057 }
Josef Bacik5a77d762011-11-01 14:32:23 -04004058 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04004059 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004060 }
Josef Bacikd52be812013-05-29 14:54:47 -04004061 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004062}
4063
Chris Mason39279cc2007-06-12 06:35:45 -04004064static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4065{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004066 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004067 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004068 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004069 int ret;
4070
Josef Bacikd52be812013-05-29 14:54:47 -04004071 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004072 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004073 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004074
David Howells2b0143b2015-03-17 22:25:59 +00004075 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004076
David Howells2b0143b2015-03-17 22:25:59 +00004077 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004078 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004079 if (ret)
4080 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004081
Yan, Zhenga22285a2010-05-16 10:48:46 -04004082 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004083 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004084 if (ret)
4085 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004086 }
Josef Bacik7b128762008-07-24 12:17:14 -04004087
Tsutomu Itohb5324022011-07-19 07:27:20 +00004088out:
Josef Bacikd52be812013-05-29 14:54:47 -04004089 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004090 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004091 return ret;
4092}
4093
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004094int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4095 struct btrfs_root *root,
4096 struct inode *dir, u64 objectid,
4097 const char *name, int name_len)
4098{
4099 struct btrfs_path *path;
4100 struct extent_buffer *leaf;
4101 struct btrfs_dir_item *di;
4102 struct btrfs_key key;
4103 u64 index;
4104 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004105 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004106
4107 path = btrfs_alloc_path();
4108 if (!path)
4109 return -ENOMEM;
4110
Li Zefan33345d012011-04-20 10:31:50 +08004111 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004112 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004113 if (IS_ERR_OR_NULL(di)) {
4114 if (!di)
4115 ret = -ENOENT;
4116 else
4117 ret = PTR_ERR(di);
4118 goto out;
4119 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004120
4121 leaf = path->nodes[0];
4122 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4123 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4124 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004125 if (ret) {
4126 btrfs_abort_transaction(trans, root, ret);
4127 goto out;
4128 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004129 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004130
4131 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4132 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004133 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004134 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004135 if (ret != -ENOENT) {
4136 btrfs_abort_transaction(trans, root, ret);
4137 goto out;
4138 }
Li Zefan33345d012011-04-20 10:31:50 +08004139 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004140 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004141 if (IS_ERR_OR_NULL(di)) {
4142 if (!di)
4143 ret = -ENOENT;
4144 else
4145 ret = PTR_ERR(di);
4146 btrfs_abort_transaction(trans, root, ret);
4147 goto out;
4148 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004149
4150 leaf = path->nodes[0];
4151 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004152 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004153 index = key.offset;
4154 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004155 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004156
Miao Xie16cdcec2011-04-22 18:12:22 +08004157 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004158 if (ret) {
4159 btrfs_abort_transaction(trans, root, ret);
4160 goto out;
4161 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004162
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004163 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004164 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004165 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004166 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004167 if (ret)
4168 btrfs_abort_transaction(trans, root, ret);
4169out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004170 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004171 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004172}
4173
Chris Mason39279cc2007-06-12 06:35:45 -04004174static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4175{
David Howells2b0143b2015-03-17 22:25:59 +00004176 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004177 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004178 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004179 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004180
David Sterbab3ae2442012-09-13 16:04:34 -06004181 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004182 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004183 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4184 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004185
Josef Bacikd52be812013-05-29 14:54:47 -04004186 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004187 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004188 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004189
Li Zefan33345d012011-04-20 10:31:50 +08004190 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004191 err = btrfs_unlink_subvol(trans, root, dir,
4192 BTRFS_I(inode)->location.objectid,
4193 dentry->d_name.name,
4194 dentry->d_name.len);
4195 goto out;
4196 }
4197
Josef Bacik7b128762008-07-24 12:17:14 -04004198 err = btrfs_orphan_add(trans, inode);
4199 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004200 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004201
Chris Mason39279cc2007-06-12 06:35:45 -04004202 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004203 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004204 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004205 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004206 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004207out:
Josef Bacikd52be812013-05-29 14:54:47 -04004208 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004209 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004210
Chris Mason39279cc2007-06-12 06:35:45 -04004211 return err;
4212}
4213
Chris Mason28f75a02015-02-04 06:59:29 -08004214static int truncate_space_check(struct btrfs_trans_handle *trans,
4215 struct btrfs_root *root,
4216 u64 bytes_deleted)
4217{
4218 int ret;
4219
4220 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
4221 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4222 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4223 if (!ret)
4224 trans->bytes_reserved += bytes_deleted;
4225 return ret;
4226
4227}
4228
Filipe Manana0305cd52015-10-16 12:34:25 +01004229static int truncate_inline_extent(struct inode *inode,
4230 struct btrfs_path *path,
4231 struct btrfs_key *found_key,
4232 const u64 item_end,
4233 const u64 new_size)
4234{
4235 struct extent_buffer *leaf = path->nodes[0];
4236 int slot = path->slots[0];
4237 struct btrfs_file_extent_item *fi;
4238 u32 size = (u32)(new_size - found_key->offset);
4239 struct btrfs_root *root = BTRFS_I(inode)->root;
4240
4241 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4242
4243 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4244 loff_t offset = new_size;
4245 loff_t page_end = ALIGN(offset, PAGE_CACHE_SIZE);
4246
4247 /*
4248 * Zero out the remaining of the last page of our inline extent,
4249 * instead of directly truncating our inline extent here - that
4250 * would be much more complex (decompressing all the data, then
4251 * compressing the truncated data, which might be bigger than
4252 * the size of the inline extent, resize the extent, etc).
4253 * We release the path because to get the page we might need to
4254 * read the extent item from disk (data not in the page cache).
4255 */
4256 btrfs_release_path(path);
4257 return btrfs_truncate_page(inode, offset, page_end - offset, 0);
4258 }
4259
4260 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4261 size = btrfs_file_extent_calc_inline_size(size);
4262 btrfs_truncate_item(root, path, size, 1);
4263
4264 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4265 inode_sub_bytes(inode, item_end + 1 - new_size);
4266
4267 return 0;
4268}
4269
Chris Mason323ac952008-10-01 19:05:46 -04004270/*
Chris Mason39279cc2007-06-12 06:35:45 -04004271 * this can truncate away extent items, csum items and directory items.
4272 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004273 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004274 *
4275 * csum items that cross the new i_size are truncated to the new size
4276 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004277 *
4278 * min_type is the minimum key type to truncate down to. If set to 0, this
4279 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004280 */
Yan, Zheng80825102009-11-12 09:35:36 +00004281int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4282 struct btrfs_root *root,
4283 struct inode *inode,
4284 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004285{
Chris Mason39279cc2007-06-12 06:35:45 -04004286 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004287 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004288 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004289 struct btrfs_key key;
4290 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004291 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004292 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004293 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004294 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004295 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004296 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004297 int found_extent;
4298 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004299 int pending_del_nr = 0;
4300 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004301 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004302 int ret;
4303 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004304 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004305 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004306 bool be_nice = 0;
4307 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004308 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004309
4310 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004311
Chris Mason28ed1342014-12-17 09:41:04 -08004312 /*
4313 * for non-free space inodes and ref cows, we want to back off from
4314 * time to time
4315 */
4316 if (!btrfs_is_free_space_inode(inode) &&
4317 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4318 be_nice = 1;
4319
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004320 path = btrfs_alloc_path();
4321 if (!path)
4322 return -ENOMEM;
4323 path->reada = -1;
4324
Josef Bacik5dc562c2012-08-17 13:14:17 -04004325 /*
4326 * We want to drop from the next block forward in case this new size is
4327 * not block aligned since we will be keeping the last block of the
4328 * extent just the way it is.
4329 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004330 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4331 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004332 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4333 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004334
Miao Xie16cdcec2011-04-22 18:12:22 +08004335 /*
4336 * This function is also used to drop the items in the log tree before
4337 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4338 * it is used to drop the loged items. So we shouldn't kill the delayed
4339 * items.
4340 */
4341 if (min_type == 0 && root == BTRFS_I(inode)->root)
4342 btrfs_kill_delayed_inode_items(inode);
4343
Li Zefan33345d012011-04-20 10:31:50 +08004344 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004345 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004346 key.type = (u8)-1;
4347
Chris Mason85e21ba2008-01-29 15:11:36 -05004348search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004349 /*
4350 * with a 16K leaf size and 128MB extents, you can actually queue
4351 * up a huge file in a single leaf. Most of the time that
4352 * bytes_deleted is > 0, it will be huge by the time we get here
4353 */
4354 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
4355 if (btrfs_should_end_transaction(trans, root)) {
4356 err = -EAGAIN;
4357 goto error;
4358 }
4359 }
4360
4361
Chris Masonb9473432009-03-13 11:00:37 -04004362 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004363 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004364 if (ret < 0) {
4365 err = ret;
4366 goto out;
4367 }
Chris Masond3977122009-01-05 21:25:51 -05004368
Chris Mason85e21ba2008-01-29 15:11:36 -05004369 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004370 /* there are no items in the tree for us to truncate, we're
4371 * done
4372 */
Yan, Zheng80825102009-11-12 09:35:36 +00004373 if (path->slots[0] == 0)
4374 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004375 path->slots[0]--;
4376 }
4377
Chris Masond3977122009-01-05 21:25:51 -05004378 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004379 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004380 leaf = path->nodes[0];
4381 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004382 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004383
Li Zefan33345d012011-04-20 10:31:50 +08004384 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004385 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004386
Chris Mason85e21ba2008-01-29 15:11:36 -05004387 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004388 break;
4389
Chris Mason5f39d392007-10-15 16:14:19 -04004390 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004391 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004392 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004393 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004394 extent_type = btrfs_file_extent_type(leaf, fi);
4395 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004396 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004397 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004398 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004399 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004400 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004401 }
Yan008630c2007-11-07 13:31:09 -05004402 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004403 }
Yan, Zheng80825102009-11-12 09:35:36 +00004404 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004405 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004406 } else {
4407 if (item_end < new_size)
4408 break;
4409 if (found_key.offset >= new_size)
4410 del_item = 1;
4411 else
4412 del_item = 0;
4413 }
Chris Mason39279cc2007-06-12 06:35:45 -04004414 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004415 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004416 if (found_type != BTRFS_EXTENT_DATA_KEY)
4417 goto delete;
4418
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004419 if (del_item)
4420 last_size = found_key.offset;
4421 else
4422 last_size = new_size;
4423
Chris Mason179e29e2007-11-01 11:28:41 -04004424 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004425 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004426 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004427 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004428 u64 orig_num_bytes =
4429 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004430 extent_num_bytes = ALIGN(new_size -
4431 found_key.offset,
4432 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004433 btrfs_set_file_extent_num_bytes(leaf, fi,
4434 extent_num_bytes);
4435 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004436 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004437 if (test_bit(BTRFS_ROOT_REF_COWS,
4438 &root->state) &&
4439 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004440 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004441 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004442 } else {
Chris Masondb945352007-10-15 16:15:53 -04004443 extent_num_bytes =
4444 btrfs_file_extent_disk_num_bytes(leaf,
4445 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004446 extent_offset = found_key.offset -
4447 btrfs_file_extent_offset(leaf, fi);
4448
Chris Mason39279cc2007-06-12 06:35:45 -04004449 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004450 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004451 if (extent_start != 0) {
4452 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004453 if (test_bit(BTRFS_ROOT_REF_COWS,
4454 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004455 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004456 }
4457 }
Chris Mason90692182008-02-08 13:49:28 -05004458 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004459 /*
4460 * we can't truncate inline items that have had
4461 * special encodings
4462 */
4463 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004464 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4465 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004466
4467 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004468 * Need to release path in order to truncate a
4469 * compressed extent. So delete any accumulated
4470 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004471 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004472 if (btrfs_file_extent_compression(leaf, fi) !=
4473 BTRFS_COMPRESS_NONE && pending_del_nr) {
4474 err = btrfs_del_items(trans, root, path,
4475 pending_del_slot,
4476 pending_del_nr);
4477 if (err) {
4478 btrfs_abort_transaction(trans,
4479 root,
4480 err);
4481 goto error;
4482 }
4483 pending_del_nr = 0;
4484 }
4485
4486 err = truncate_inline_extent(inode, path,
4487 &found_key,
4488 item_end,
4489 new_size);
4490 if (err) {
4491 btrfs_abort_transaction(trans,
4492 root, err);
4493 goto error;
4494 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004495 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4496 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004497 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004498 }
Chris Mason39279cc2007-06-12 06:35:45 -04004499 }
Chris Mason179e29e2007-11-01 11:28:41 -04004500delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004501 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004502 if (!pending_del_nr) {
4503 /* no pending yet, add ourselves */
4504 pending_del_slot = path->slots[0];
4505 pending_del_nr = 1;
4506 } else if (pending_del_nr &&
4507 path->slots[0] + 1 == pending_del_slot) {
4508 /* hop on the pending chunk */
4509 pending_del_nr++;
4510 pending_del_slot = path->slots[0];
4511 } else {
Chris Masond3977122009-01-05 21:25:51 -05004512 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004513 }
Chris Mason39279cc2007-06-12 06:35:45 -04004514 } else {
4515 break;
4516 }
Chris Mason28f75a02015-02-04 06:59:29 -08004517 should_throttle = 0;
4518
Miao Xie27cdeb72014-04-02 19:51:05 +08004519 if (found_extent &&
4520 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4521 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004522 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004523 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004524 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004525 extent_num_bytes, 0,
4526 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004527 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004528 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004529 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004530 btrfs_async_run_delayed_refs(root,
4531 trans->delayed_ref_updates * 2, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004532 if (be_nice) {
4533 if (truncate_space_check(trans, root,
4534 extent_num_bytes)) {
4535 should_end = 1;
4536 }
4537 if (btrfs_should_throttle_delayed_refs(trans,
4538 root)) {
4539 should_throttle = 1;
4540 }
4541 }
Chris Mason39279cc2007-06-12 06:35:45 -04004542 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004543
Yan, Zheng80825102009-11-12 09:35:36 +00004544 if (found_type == BTRFS_INODE_ITEM_KEY)
4545 break;
4546
4547 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004548 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004549 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004550 if (pending_del_nr) {
4551 ret = btrfs_del_items(trans, root, path,
4552 pending_del_slot,
4553 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004554 if (ret) {
4555 btrfs_abort_transaction(trans,
4556 root, ret);
4557 goto error;
4558 }
Yan, Zheng80825102009-11-12 09:35:36 +00004559 pending_del_nr = 0;
4560 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004561 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004562 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004563 unsigned long updates = trans->delayed_ref_updates;
4564 if (updates) {
4565 trans->delayed_ref_updates = 0;
4566 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4567 if (ret && !err)
4568 err = ret;
4569 }
4570 }
Chris Mason28f75a02015-02-04 06:59:29 -08004571 /*
4572 * if we failed to refill our space rsv, bail out
4573 * and let the transaction restart
4574 */
4575 if (should_end) {
4576 err = -EAGAIN;
4577 goto error;
4578 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004579 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004580 } else {
4581 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004582 }
Chris Mason39279cc2007-06-12 06:35:45 -04004583 }
Yan, Zheng80825102009-11-12 09:35:36 +00004584out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004585 if (pending_del_nr) {
4586 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4587 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004588 if (ret)
4589 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004590 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004591error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004592 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004593 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004594
Chris Mason39279cc2007-06-12 06:35:45 -04004595 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004596
Chris Mason28f75a02015-02-04 06:59:29 -08004597 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
Chris Mason28ed1342014-12-17 09:41:04 -08004598 unsigned long updates = trans->delayed_ref_updates;
4599 if (updates) {
4600 trans->delayed_ref_updates = 0;
4601 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4602 if (ret && !err)
4603 err = ret;
4604 }
4605 }
Yan, Zheng80825102009-11-12 09:35:36 +00004606 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004607}
4608
Chris Masona52d9a82007-08-27 16:49:44 -04004609/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004610 * btrfs_truncate_page - read, zero a chunk and write a page
4611 * @inode - inode that we're zeroing
4612 * @from - the offset to start zeroing
4613 * @len - the length to zero, 0 to zero the entire range respective to the
4614 * offset
4615 * @front - zero up to the offset instead of from the offset on
4616 *
4617 * This will find the page for the "from" offset and cow the page and zero the
4618 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004619 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004620int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4621 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004622{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004623 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004624 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004625 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4626 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004627 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004628 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004629 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004630 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4631 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4632 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004633 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004634 int ret = 0;
4635 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004636 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004637
Josef Bacik2aaa6652012-08-29 14:27:18 -04004638 if ((offset & (blocksize - 1)) == 0 &&
4639 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004640 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004641 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004642 if (ret)
4643 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004644
Chris Mason211c17f2008-05-15 09:13:45 -04004645again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004646 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004647 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004648 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004649 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004650 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004651 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004652
4653 page_start = page_offset(page);
4654 page_end = page_start + PAGE_CACHE_SIZE - 1;
4655
Chris Masona52d9a82007-08-27 16:49:44 -04004656 if (!PageUptodate(page)) {
4657 ret = btrfs_readpage(NULL, page);
4658 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004659 if (page->mapping != mapping) {
4660 unlock_page(page);
4661 page_cache_release(page);
4662 goto again;
4663 }
Chris Masona52d9a82007-08-27 16:49:44 -04004664 if (!PageUptodate(page)) {
4665 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004666 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004667 }
4668 }
Chris Mason211c17f2008-05-15 09:13:45 -04004669 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004670
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004671 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004672 set_page_extent_mapped(page);
4673
4674 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4675 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004676 unlock_extent_cached(io_tree, page_start, page_end,
4677 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004678 unlock_page(page);
4679 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004680 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004681 btrfs_put_ordered_extent(ordered);
4682 goto again;
4683 }
4684
Josef Bacik2ac55d42010-02-03 19:33:23 +00004685 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004686 EXTENT_DIRTY | EXTENT_DELALLOC |
4687 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004688 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004689
Josef Bacik2ac55d42010-02-03 19:33:23 +00004690 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4691 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004692 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004693 unlock_extent_cached(io_tree, page_start, page_end,
4694 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004695 goto out_unlock;
4696 }
4697
Chris Masone6dcd2d2008-07-17 12:53:50 -04004698 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004699 if (!len)
4700 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004701 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004702 if (front)
4703 memset(kaddr, 0, offset);
4704 else
4705 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004706 flush_dcache_page(page);
4707 kunmap(page);
4708 }
Chris Mason247e7432008-07-17 12:53:51 -04004709 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004710 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004711 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4712 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004713
Chris Mason89642222008-07-24 09:41:53 -04004714out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004715 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004716 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004717 unlock_page(page);
4718 page_cache_release(page);
4719out:
4720 return ret;
4721}
4722
Josef Bacik16e75492013-10-22 12:18:51 -04004723static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4724 u64 offset, u64 len)
4725{
4726 struct btrfs_trans_handle *trans;
4727 int ret;
4728
4729 /*
4730 * Still need to make sure the inode looks like it's been updated so
4731 * that any holes get logged if we fsync.
4732 */
4733 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4734 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4735 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4736 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4737 return 0;
4738 }
4739
4740 /*
4741 * 1 - for the one we're dropping
4742 * 1 - for the one we're adding
4743 * 1 - for updating the inode.
4744 */
4745 trans = btrfs_start_transaction(root, 3);
4746 if (IS_ERR(trans))
4747 return PTR_ERR(trans);
4748
4749 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4750 if (ret) {
4751 btrfs_abort_transaction(trans, root, ret);
4752 btrfs_end_transaction(trans, root);
4753 return ret;
4754 }
4755
4756 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4757 0, 0, len, 0, len, 0, 0, 0);
4758 if (ret)
4759 btrfs_abort_transaction(trans, root, ret);
4760 else
4761 btrfs_update_inode(trans, root, inode);
4762 btrfs_end_transaction(trans, root);
4763 return ret;
4764}
4765
Josef Bacik695a0d02011-03-04 15:46:53 -05004766/*
4767 * This function puts in dummy file extents for the area we're creating a hole
4768 * for. So if we are truncating this file to a larger size we need to insert
4769 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4770 * the range between oldsize and size
4771 */
Josef Bacika41ad392011-01-31 15:30:16 -05004772int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004773{
Yan Zheng9036c102008-10-30 14:19:41 -04004774 struct btrfs_root *root = BTRFS_I(inode)->root;
4775 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004776 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004777 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004778 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004779 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4780 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004781 u64 last_byte;
4782 u64 cur_offset;
4783 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004784 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004785
Josef Bacika71754f2013-06-17 17:14:39 -04004786 /*
4787 * If our size started in the middle of a page we need to zero out the
4788 * rest of the page before we expand the i_size, otherwise we could
4789 * expose stale data.
4790 */
4791 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4792 if (err)
4793 return err;
4794
Yan Zheng9036c102008-10-30 14:19:41 -04004795 if (size <= hole_start)
4796 return 0;
4797
Yan Zheng9036c102008-10-30 14:19:41 -04004798 while (1) {
4799 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004800
Josef Bacik2ac55d42010-02-03 19:33:23 +00004801 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004802 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004803 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4804 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004805 if (!ordered)
4806 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004807 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4808 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004809 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004810 btrfs_put_ordered_extent(ordered);
4811 }
4812
Yan Zheng9036c102008-10-30 14:19:41 -04004813 cur_offset = hole_start;
4814 while (1) {
4815 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4816 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004817 if (IS_ERR(em)) {
4818 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004819 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004820 break;
4821 }
Yan Zheng9036c102008-10-30 14:19:41 -04004822 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004823 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004824 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004825 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004826 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004827
Josef Bacik16e75492013-10-22 12:18:51 -04004828 err = maybe_insert_hole(root, inode, cur_offset,
4829 hole_size);
4830 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004831 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004832 btrfs_drop_extent_cache(inode, cur_offset,
4833 cur_offset + hole_size - 1, 0);
4834 hole_em = alloc_extent_map();
4835 if (!hole_em) {
4836 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4837 &BTRFS_I(inode)->runtime_flags);
4838 goto next;
4839 }
4840 hole_em->start = cur_offset;
4841 hole_em->len = hole_size;
4842 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004843
Josef Bacik5dc562c2012-08-17 13:14:17 -04004844 hole_em->block_start = EXTENT_MAP_HOLE;
4845 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004846 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004847 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004848 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4849 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004850 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004851
4852 while (1) {
4853 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004854 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004855 write_unlock(&em_tree->lock);
4856 if (err != -EEXIST)
4857 break;
4858 btrfs_drop_extent_cache(inode, cur_offset,
4859 cur_offset +
4860 hole_size - 1, 0);
4861 }
4862 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004863 }
Josef Bacik16e75492013-10-22 12:18:51 -04004864next:
Yan Zheng9036c102008-10-30 14:19:41 -04004865 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004866 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004867 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004868 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004869 break;
4870 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004871 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004872 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4873 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004874 return err;
4875}
4876
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004877static int wait_snapshoting_atomic_t(atomic_t *a)
4878{
4879 schedule();
4880 return 0;
4881}
4882
4883static void wait_for_snapshot_creation(struct btrfs_root *root)
4884{
4885 while (true) {
4886 int ret;
4887
4888 ret = btrfs_start_write_no_snapshoting(root);
4889 if (ret)
4890 break;
4891 wait_on_atomic_t(&root->will_be_snapshoted,
4892 wait_snapshoting_atomic_t,
4893 TASK_UNINTERRUPTIBLE);
4894 }
4895}
4896
Eric Sandeen3972f262013-01-12 02:57:22 +00004897static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004898{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004899 struct btrfs_root *root = BTRFS_I(inode)->root;
4900 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004901 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004902 loff_t newsize = attr->ia_size;
4903 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004904 int ret;
4905
Eric Sandeen3972f262013-01-12 02:57:22 +00004906 /*
4907 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4908 * special case where we need to update the times despite not having
4909 * these flags set. For all other operations the VFS set these flags
4910 * explicitly if it wants a timestamp update.
4911 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004912 if (newsize != oldsize) {
4913 inode_inc_iversion(inode);
4914 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4915 inode->i_ctime = inode->i_mtime =
4916 current_fs_time(inode->i_sb);
4917 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004918
Josef Bacika41ad392011-01-31 15:30:16 -05004919 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004920 truncate_pagecache(inode, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004921 /*
4922 * Don't do an expanding truncate while snapshoting is ongoing.
4923 * This is to ensure the snapshot captures a fully consistent
4924 * state of this file - if the snapshot captures this expanding
4925 * truncation, it must capture all writes that happened before
4926 * this truncation.
4927 */
4928 wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004929 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004930 if (ret) {
4931 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004932 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004933 }
Yan, Zheng80825102009-11-12 09:35:36 +00004934
Miao Xief4a2f4c2011-12-14 20:12:01 -05004935 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004936 if (IS_ERR(trans)) {
4937 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004938 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004939 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004940
4941 i_size_write(inode, newsize);
4942 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4943 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004944 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004945 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004946 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004947
Josef Bacika41ad392011-01-31 15:30:16 -05004948 /*
4949 * We're truncating a file that used to have good data down to
4950 * zero. Make sure it gets into the ordered flush list so that
4951 * any new writes get down to disk quickly.
4952 */
4953 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004954 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4955 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004956
Josef Bacikf3fe8202013-01-07 17:03:21 -05004957 /*
4958 * 1 for the orphan item we're going to add
4959 * 1 for the orphan item deletion.
4960 */
4961 trans = btrfs_start_transaction(root, 2);
4962 if (IS_ERR(trans))
4963 return PTR_ERR(trans);
4964
4965 /*
4966 * We need to do this in case we fail at _any_ point during the
4967 * actual truncate. Once we do the truncate_setsize we could
4968 * invalidate pages which forces any outstanding ordered io to
4969 * be instantly completed which will give us extents that need
4970 * to be truncated. If we fail to get an orphan inode down we
4971 * could have left over extents that were never meant to live,
4972 * so we need to garuntee from this point on that everything
4973 * will be consistent.
4974 */
4975 ret = btrfs_orphan_add(trans, inode);
4976 btrfs_end_transaction(trans, root);
4977 if (ret)
4978 return ret;
4979
Josef Bacika41ad392011-01-31 15:30:16 -05004980 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4981 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004982
4983 /* Disable nonlocked read DIO to avoid the end less truncate */
4984 btrfs_inode_block_unlocked_dio(inode);
4985 inode_dio_wait(inode);
4986 btrfs_inode_resume_unlocked_dio(inode);
4987
Josef Bacika41ad392011-01-31 15:30:16 -05004988 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004989 if (ret && inode->i_nlink) {
4990 int err;
4991
4992 /*
4993 * failed to truncate, disk_i_size is only adjusted down
4994 * as we remove extents, so it should represent the true
4995 * size of the inode, so reset the in memory size and
4996 * delete our orphan entry.
4997 */
4998 trans = btrfs_join_transaction(root);
4999 if (IS_ERR(trans)) {
5000 btrfs_orphan_del(NULL, inode);
5001 return ret;
5002 }
5003 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5004 err = btrfs_orphan_del(trans, inode);
5005 if (err)
5006 btrfs_abort_transaction(trans, root, err);
5007 btrfs_end_transaction(trans, root);
5008 }
Yan, Zheng80825102009-11-12 09:35:36 +00005009 }
5010
Josef Bacika41ad392011-01-31 15:30:16 -05005011 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005012}
5013
Chris Mason39279cc2007-06-12 06:35:45 -04005014static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5015{
David Howells2b0143b2015-03-17 22:25:59 +00005016 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005017 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005018 int err;
5019
Li Zefanb83cc962010-12-20 16:04:08 +08005020 if (btrfs_root_readonly(root))
5021 return -EROFS;
5022
Chris Mason39279cc2007-06-12 06:35:45 -04005023 err = inode_change_ok(inode, attr);
5024 if (err)
5025 return err;
5026
Chris Mason5a3f23d2009-03-31 13:27:11 -04005027 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005028 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005029 if (err)
5030 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005031 }
Yan Zheng9036c102008-10-30 14:19:41 -04005032
Christoph Hellwig10257742010-06-04 11:30:02 +02005033 if (attr->ia_valid) {
5034 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005035 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005036 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005037
Josef Bacik22c44fe2011-11-30 10:45:38 -05005038 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005039 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005040 }
5041
Chris Mason39279cc2007-06-12 06:35:45 -04005042 return err;
5043}
Chris Mason61295eb2008-01-14 16:24:38 -05005044
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005045/*
5046 * While truncating the inode pages during eviction, we get the VFS calling
5047 * btrfs_invalidatepage() against each page of the inode. This is slow because
5048 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5049 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5050 * extent_state structures over and over, wasting lots of time.
5051 *
5052 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5053 * those expensive operations on a per page basis and do only the ordered io
5054 * finishing, while we release here the extent_map and extent_state structures,
5055 * without the excessive merging and splitting.
5056 */
5057static void evict_inode_truncate_pages(struct inode *inode)
5058{
5059 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5060 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5061 struct rb_node *node;
5062
5063 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005064 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005065
5066 write_lock(&map_tree->lock);
5067 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5068 struct extent_map *em;
5069
5070 node = rb_first(&map_tree->map);
5071 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005072 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5073 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005074 remove_extent_mapping(map_tree, em);
5075 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005076 if (need_resched()) {
5077 write_unlock(&map_tree->lock);
5078 cond_resched();
5079 write_lock(&map_tree->lock);
5080 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005081 }
5082 write_unlock(&map_tree->lock);
5083
Filipe Manana6ca07092015-05-26 00:55:42 +01005084 /*
5085 * Keep looping until we have no more ranges in the io tree.
5086 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005087 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5088 * still in progress (unlocked the pages in the bio but did not yet
5089 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005090 * ranges can still be locked and eviction started because before
5091 * submitting those bios, which are executed by a separate task (work
5092 * queue kthread), inode references (inode->i_count) were not taken
5093 * (which would be dropped in the end io callback of each bio).
5094 * Therefore here we effectively end up waiting for those bios and
5095 * anyone else holding locked ranges without having bumped the inode's
5096 * reference count - if we don't do it, when they access the inode's
5097 * io_tree to unlock a range it may be too late, leading to an
5098 * use-after-free issue.
5099 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005100 spin_lock(&io_tree->lock);
5101 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5102 struct extent_state *state;
5103 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005104 u64 start;
5105 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005106
5107 node = rb_first(&io_tree->state);
5108 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005109 start = state->start;
5110 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005111 spin_unlock(&io_tree->lock);
5112
Filipe Manana6ca07092015-05-26 00:55:42 +01005113 lock_extent_bits(io_tree, start, end, 0, &cached_state);
5114 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005115 EXTENT_LOCKED | EXTENT_DIRTY |
5116 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5117 EXTENT_DEFRAG, 1, 1,
5118 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005119
Filipe Manana7064dd52014-08-08 02:47:05 +01005120 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005121 spin_lock(&io_tree->lock);
5122 }
5123 spin_unlock(&io_tree->lock);
5124}
5125
Al Virobd555972010-06-07 11:35:40 -04005126void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005127{
5128 struct btrfs_trans_handle *trans;
5129 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005130 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005131 int steal_from_global = 0;
Josef Bacik07127182011-08-19 10:29:59 -04005132 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04005133 int ret;
5134
liubo1abe9b82011-03-24 11:18:59 +00005135 trace_btrfs_inode_evict(inode);
5136
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005137 evict_inode_truncate_pages(inode);
5138
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005139 if (inode->i_nlink &&
5140 ((btrfs_root_refs(&root->root_item) != 0 &&
5141 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5142 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005143 goto no_delete;
5144
Chris Mason39279cc2007-06-12 06:35:45 -04005145 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005146 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005147 goto no_delete;
5148 }
Al Virobd555972010-06-07 11:35:40 -04005149 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005150 if (!special_file(inode->i_mode))
5151 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005152
Miao Xief6124962014-09-12 18:44:04 +08005153 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5154
Yan, Zhengc71bf092009-11-12 09:34:40 +00005155 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06005156 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005157 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005158 goto no_delete;
5159 }
5160
Yan, Zheng76dda932009-09-21 16:00:26 -04005161 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005162 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5163 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005164 goto no_delete;
5165 }
5166
Miao Xie0e8c36a2012-12-19 06:59:51 +00005167 ret = btrfs_commit_inode_delayed_inode(inode);
5168 if (ret) {
5169 btrfs_orphan_del(NULL, inode);
5170 goto no_delete;
5171 }
5172
Miao Xie66d8f3d2012-09-06 04:02:28 -06005173 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005174 if (!rsv) {
5175 btrfs_orphan_del(NULL, inode);
5176 goto no_delete;
5177 }
Josef Bacik4a338542011-08-29 11:01:31 -04005178 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005179 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005180 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005181
Chris Masondbe674a2008-07-17 12:54:05 -04005182 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005183
Josef Bacik4289a662011-08-05 13:22:24 -04005184 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005185 * This is a bit simpler than btrfs_truncate since we've already
5186 * reserved our space for our orphan item in the unlink, so we just
5187 * need to reserve some slack space in case we add bytes and update
5188 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005189 */
Yan, Zheng80825102009-11-12 09:35:36 +00005190 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005191 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5192 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005193
Josef Bacik726c35f2011-09-26 15:46:06 -04005194 /*
5195 * Try and steal from the global reserve since we will
5196 * likely not use this space anyway, we want to try as
5197 * hard as possible to get this to work.
5198 */
5199 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005200 steal_from_global++;
5201 else
5202 steal_from_global = 0;
5203 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005204
Josef Bacik3bce8762015-02-24 12:35:51 -08005205 /*
5206 * steal_from_global == 0: we reserved stuff, hooray!
5207 * steal_from_global == 1: we didn't reserve stuff, boo!
5208 * steal_from_global == 2: we've committed, still not a lot of
5209 * room but maybe we'll have room in the global reserve this
5210 * time.
5211 * steal_from_global == 3: abandon all hope!
5212 */
5213 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005214 btrfs_warn(root->fs_info,
5215 "Could not get space for a delete, will truncate on mount %d",
5216 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005217 btrfs_orphan_del(NULL, inode);
5218 btrfs_free_block_rsv(root, rsv);
5219 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005220 }
5221
Miao Xie0e8c36a2012-12-19 06:59:51 +00005222 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005223 if (IS_ERR(trans)) {
5224 btrfs_orphan_del(NULL, inode);
5225 btrfs_free_block_rsv(root, rsv);
5226 goto no_delete;
5227 }
5228
Josef Bacik3bce8762015-02-24 12:35:51 -08005229 /*
5230 * We can't just steal from the global reserve, we need tomake
5231 * sure there is room to do it, if not we need to commit and try
5232 * again.
5233 */
5234 if (steal_from_global) {
5235 if (!btrfs_check_space_for_delayed_refs(trans, root))
5236 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5237 min_size);
5238 else
5239 ret = -ENOSPC;
5240 }
5241
5242 /*
5243 * Couldn't steal from the global reserve, we have too much
5244 * pending stuff built up, commit the transaction and try it
5245 * again.
5246 */
5247 if (ret) {
5248 ret = btrfs_commit_transaction(trans, root);
5249 if (ret) {
5250 btrfs_orphan_del(NULL, inode);
5251 btrfs_free_block_rsv(root, rsv);
5252 goto no_delete;
5253 }
5254 continue;
5255 } else {
5256 steal_from_global = 0;
5257 }
5258
Josef Bacik4289a662011-08-05 13:22:24 -04005259 trans->block_rsv = rsv;
5260
Yan, Zhengd68fc572010-05-16 10:49:58 -04005261 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005262 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005263 break;
5264
Miao Xie8407aa42012-09-07 01:43:32 -06005265 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005266 btrfs_end_transaction(trans, root);
5267 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005268 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005269 }
5270
Josef Bacik4289a662011-08-05 13:22:24 -04005271 btrfs_free_block_rsv(root, rsv);
5272
Josef Bacik4ef31a42013-08-13 14:10:08 -04005273 /*
5274 * Errors here aren't a big deal, it just means we leave orphan items
5275 * in the tree. They will be cleaned up on the next mount.
5276 */
Yan, Zheng80825102009-11-12 09:35:36 +00005277 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005278 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005279 btrfs_orphan_del(trans, inode);
5280 } else {
5281 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005282 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005283
Josef Bacik4289a662011-08-05 13:22:24 -04005284 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005285 if (!(root == root->fs_info->tree_root ||
5286 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005287 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005288
Chris Mason54aa1f42007-06-22 14:16:25 -04005289 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005290 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005291no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005292 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005293 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005294 return;
Chris Mason39279cc2007-06-12 06:35:45 -04005295}
5296
5297/*
5298 * this returns the key found in the dir entry in the location pointer.
5299 * If no dir entries were found, location->objectid is 0.
5300 */
5301static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5302 struct btrfs_key *location)
5303{
5304 const char *name = dentry->d_name.name;
5305 int namelen = dentry->d_name.len;
5306 struct btrfs_dir_item *di;
5307 struct btrfs_path *path;
5308 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005309 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005310
5311 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005312 if (!path)
5313 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005314
Li Zefan33345d012011-04-20 10:31:50 +08005315 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005316 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005317 if (IS_ERR(di))
5318 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005319
David Sterbac7040052011-04-19 18:00:01 +02005320 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005321 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005322
Chris Mason5f39d392007-10-15 16:14:19 -04005323 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005324out:
Chris Mason39279cc2007-06-12 06:35:45 -04005325 btrfs_free_path(path);
5326 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005327out_err:
5328 location->objectid = 0;
5329 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005330}
5331
5332/*
5333 * when we hit a tree root in a directory, the btrfs part of the inode
5334 * needs to be changed to reflect the root directory of the tree root. This
5335 * is kind of like crossing a mount point.
5336 */
5337static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005338 struct inode *dir,
5339 struct dentry *dentry,
5340 struct btrfs_key *location,
5341 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005342{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005343 struct btrfs_path *path;
5344 struct btrfs_root *new_root;
5345 struct btrfs_root_ref *ref;
5346 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005347 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005348 int ret;
5349 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005350
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005351 path = btrfs_alloc_path();
5352 if (!path) {
5353 err = -ENOMEM;
5354 goto out;
5355 }
Chris Mason39279cc2007-06-12 06:35:45 -04005356
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005357 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005358 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5359 key.type = BTRFS_ROOT_REF_KEY;
5360 key.offset = location->objectid;
5361
5362 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5363 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005364 if (ret) {
5365 if (ret < 0)
5366 err = ret;
5367 goto out;
5368 }
Chris Mason39279cc2007-06-12 06:35:45 -04005369
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005370 leaf = path->nodes[0];
5371 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005372 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005373 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5374 goto out;
5375
5376 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5377 (unsigned long)(ref + 1),
5378 dentry->d_name.len);
5379 if (ret)
5380 goto out;
5381
David Sterbab3b4aa72011-04-21 01:20:15 +02005382 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005383
5384 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5385 if (IS_ERR(new_root)) {
5386 err = PTR_ERR(new_root);
5387 goto out;
5388 }
5389
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005390 *sub_root = new_root;
5391 location->objectid = btrfs_root_dirid(&new_root->root_item);
5392 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005393 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005394 err = 0;
5395out:
5396 btrfs_free_path(path);
5397 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005398}
5399
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005400static void inode_tree_add(struct inode *inode)
5401{
5402 struct btrfs_root *root = BTRFS_I(inode)->root;
5403 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005404 struct rb_node **p;
5405 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005406 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005407 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005408
Al Viro1d3382cb2010-10-23 15:19:20 -04005409 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005410 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005411 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005412 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005413 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005414 while (*p) {
5415 parent = *p;
5416 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5417
Li Zefan33345d012011-04-20 10:31:50 +08005418 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005419 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005420 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005421 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005422 else {
5423 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005424 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005425 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005426 RB_CLEAR_NODE(parent);
5427 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005428 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005429 }
5430 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005431 rb_link_node(new, parent, p);
5432 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005433 spin_unlock(&root->inode_lock);
5434}
5435
5436static void inode_tree_del(struct inode *inode)
5437{
5438 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005439 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005440
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005441 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005442 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005443 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005444 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005445 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005446 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005447 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005448
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005449 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005450 synchronize_srcu(&root->fs_info->subvol_srcu);
5451 spin_lock(&root->inode_lock);
5452 empty = RB_EMPTY_ROOT(&root->inode_tree);
5453 spin_unlock(&root->inode_lock);
5454 if (empty)
5455 btrfs_add_dead_root(root);
5456 }
5457}
5458
Jeff Mahoney143bede2012-03-01 14:56:26 +01005459void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005460{
5461 struct rb_node *node;
5462 struct rb_node *prev;
5463 struct btrfs_inode *entry;
5464 struct inode *inode;
5465 u64 objectid = 0;
5466
Liu Bo7813b3d2014-02-10 17:37:25 +08005467 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5468 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005469
5470 spin_lock(&root->inode_lock);
5471again:
5472 node = root->inode_tree.rb_node;
5473 prev = NULL;
5474 while (node) {
5475 prev = node;
5476 entry = rb_entry(node, struct btrfs_inode, rb_node);
5477
Li Zefan33345d012011-04-20 10:31:50 +08005478 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005479 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005480 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005481 node = node->rb_right;
5482 else
5483 break;
5484 }
5485 if (!node) {
5486 while (prev) {
5487 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005488 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005489 node = prev;
5490 break;
5491 }
5492 prev = rb_next(prev);
5493 }
5494 }
5495 while (node) {
5496 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005497 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005498 inode = igrab(&entry->vfs_inode);
5499 if (inode) {
5500 spin_unlock(&root->inode_lock);
5501 if (atomic_read(&inode->i_count) > 1)
5502 d_prune_aliases(inode);
5503 /*
Al Viro45321ac2010-06-07 13:43:19 -04005504 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005505 * the inode cache when its usage count
5506 * hits zero.
5507 */
5508 iput(inode);
5509 cond_resched();
5510 spin_lock(&root->inode_lock);
5511 goto again;
5512 }
5513
5514 if (cond_resched_lock(&root->inode_lock))
5515 goto again;
5516
5517 node = rb_next(node);
5518 }
5519 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005520}
5521
Chris Masone02119d2008-09-05 16:13:11 -04005522static int btrfs_init_locked_inode(struct inode *inode, void *p)
5523{
5524 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005525 inode->i_ino = args->location->objectid;
5526 memcpy(&BTRFS_I(inode)->location, args->location,
5527 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005528 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005529 return 0;
5530}
5531
5532static int btrfs_find_actor(struct inode *inode, void *opaque)
5533{
5534 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005535 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005536 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005537}
5538
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005539static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005540 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005541 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005542{
5543 struct inode *inode;
5544 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005545 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005546
Chris Mason90d3e592014-01-09 17:28:00 -08005547 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005548 args.root = root;
5549
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005550 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005551 btrfs_init_locked_inode,
5552 (void *)&args);
5553 return inode;
5554}
5555
Balaji Rao1a54ef82008-07-21 02:01:04 +05305556/* Get an inode object given its location and corresponding root.
5557 * Returns in *is_new if the inode was read from disk
5558 */
5559struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005560 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305561{
5562 struct inode *inode;
5563
Chris Mason90d3e592014-01-09 17:28:00 -08005564 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305565 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005566 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305567
5568 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305569 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005570 if (!is_bad_inode(inode)) {
5571 inode_tree_add(inode);
5572 unlock_new_inode(inode);
5573 if (new)
5574 *new = 1;
5575 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005576 unlock_new_inode(inode);
5577 iput(inode);
5578 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005579 }
5580 }
5581
Balaji Rao1a54ef82008-07-21 02:01:04 +05305582 return inode;
5583}
5584
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005585static struct inode *new_simple_dir(struct super_block *s,
5586 struct btrfs_key *key,
5587 struct btrfs_root *root)
5588{
5589 struct inode *inode = new_inode(s);
5590
5591 if (!inode)
5592 return ERR_PTR(-ENOMEM);
5593
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005594 BTRFS_I(inode)->root = root;
5595 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005596 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005597
5598 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005599 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005600 inode->i_fop = &simple_dir_operations;
5601 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
chandan r9cc97d62012-07-04 12:48:07 +05305602 inode->i_mtime = CURRENT_TIME;
5603 inode->i_atime = inode->i_mtime;
5604 inode->i_ctime = inode->i_mtime;
5605 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005606
5607 return inode;
5608}
5609
Chris Mason3de45862008-11-17 21:02:50 -05005610struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005611{
Chris Masond3977122009-01-05 21:25:51 -05005612 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005613 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005614 struct btrfs_root *sub_root = root;
5615 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005616 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005617 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005618
5619 if (dentry->d_name.len > BTRFS_NAME_LEN)
5620 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005621
Jeff Layton39e3c952012-11-28 11:30:53 -05005622 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005623 if (ret < 0)
5624 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005625
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005626 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005627 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005628
5629 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005630 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005631 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005632 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005633
5634 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5635
Yan, Zheng76dda932009-09-21 16:00:26 -04005636 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005637 ret = fixup_tree_root_location(root, dir, dentry,
5638 &location, &sub_root);
5639 if (ret < 0) {
5640 if (ret != -ENOENT)
5641 inode = ERR_PTR(ret);
5642 else
5643 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5644 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005645 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005646 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005647 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5648
Julia Lawall34d19ba2011-01-24 19:55:19 +00005649 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005650 down_read(&root->fs_info->cleanup_work_sem);
5651 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005652 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005653 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005654 if (ret) {
5655 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005656 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005657 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005658 }
5659
Chris Mason3de45862008-11-17 21:02:50 -05005660 return inode;
5661}
5662
Nick Pigginfe15ce42011-01-07 17:49:23 +11005663static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005664{
5665 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005666 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005667
Li Zefan848cce02012-02-21 17:04:28 +08005668 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005669 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005670
Li Zefan848cce02012-02-21 17:04:28 +08005671 if (inode) {
5672 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005673 if (btrfs_root_refs(&root->root_item) == 0)
5674 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005675
5676 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5677 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005678 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005679 return 0;
5680}
5681
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005682static void btrfs_dentry_release(struct dentry *dentry)
5683{
Daeseok Youn944a4512014-04-14 15:37:02 +09005684 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005685}
5686
Chris Mason3de45862008-11-17 21:02:50 -05005687static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005688 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005689{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005690 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005691
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005692 inode = btrfs_lookup_dentry(dir, dentry);
5693 if (IS_ERR(inode)) {
5694 if (PTR_ERR(inode) == -ENOENT)
5695 inode = NULL;
5696 else
5697 return ERR_CAST(inode);
5698 }
5699
Al Viro41d28bc2014-10-12 22:24:21 -04005700 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005701}
5702
Miao Xie16cdcec2011-04-22 18:12:22 +08005703unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005704 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5705};
5706
Al Viro9cdda8d2013-05-22 16:48:09 -04005707static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005708{
Al Viro9cdda8d2013-05-22 16:48:09 -04005709 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005710 struct btrfs_root *root = BTRFS_I(inode)->root;
5711 struct btrfs_item *item;
5712 struct btrfs_dir_item *di;
5713 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005714 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005715 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005716 struct list_head ins_list;
5717 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005718 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005719 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005720 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005721 unsigned char d_type;
5722 int over = 0;
5723 u32 di_cur;
5724 u32 di_total;
5725 u32 di_len;
5726 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005727 char tmp_name[32];
5728 char *name_ptr;
5729 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005730 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005731
5732 /* FIXME, use a real flag for deciding about the key type */
5733 if (root->fs_info->tree_root == root)
5734 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005735
Al Viro9cdda8d2013-05-22 16:48:09 -04005736 if (!dir_emit_dots(file, ctx))
5737 return 0;
5738
David Woodhouse49593bf2008-08-17 17:08:36 +01005739 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005740 if (!path)
5741 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005742
Josef Bacik026fd312011-05-13 10:32:11 -04005743 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005744
Miao Xie16cdcec2011-04-22 18:12:22 +08005745 if (key_type == BTRFS_DIR_INDEX_KEY) {
5746 INIT_LIST_HEAD(&ins_list);
5747 INIT_LIST_HEAD(&del_list);
5748 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5749 }
5750
David Sterba962a2982014-06-04 18:41:45 +02005751 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005752 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005753 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005754
Chris Mason39279cc2007-06-12 06:35:45 -04005755 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5756 if (ret < 0)
5757 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005758
5759 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005760 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005761 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005762 if (slot >= btrfs_header_nritems(leaf)) {
5763 ret = btrfs_next_leaf(root, path);
5764 if (ret < 0)
5765 goto err;
5766 else if (ret > 0)
5767 break;
5768 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005769 }
Chris Mason3de45862008-11-17 21:02:50 -05005770
Ross Kirkdd3cc162013-09-16 15:58:09 +01005771 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005772 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5773
5774 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005775 break;
David Sterba962a2982014-06-04 18:41:45 +02005776 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005777 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005778 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005779 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005780 if (key_type == BTRFS_DIR_INDEX_KEY &&
5781 btrfs_should_delete_dir_index(&del_list,
5782 found_key.offset))
5783 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005784
Al Viro9cdda8d2013-05-22 16:48:09 -04005785 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005786 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005787
Chris Mason39279cc2007-06-12 06:35:45 -04005788 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5789 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005790 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005791
5792 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005793 struct btrfs_key location;
5794
Josef Bacik22a94d42011-03-16 16:47:17 -04005795 if (verify_dir_item(root, leaf, di))
5796 break;
5797
Chris Mason5f39d392007-10-15 16:14:19 -04005798 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005799 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005800 name_ptr = tmp_name;
5801 } else {
5802 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005803 if (!name_ptr) {
5804 ret = -ENOMEM;
5805 goto err;
5806 }
Chris Mason5f39d392007-10-15 16:14:19 -04005807 }
5808 read_extent_buffer(leaf, name_ptr,
5809 (unsigned long)(di + 1), name_len);
5810
5811 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5812 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005813
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005814
Chris Mason3de45862008-11-17 21:02:50 -05005815 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005816 * skip it.
5817 *
5818 * In contrast to old kernels, we insert the snapshot's
5819 * dir item and dir index after it has been created, so
5820 * we won't find a reference to our own snapshot. We
5821 * still keep the following code for backward
5822 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005823 */
5824 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5825 location.objectid == root->root_key.objectid) {
5826 over = 0;
5827 goto skip;
5828 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005829 over = !dir_emit(ctx, name_ptr, name_len,
5830 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005831
Chris Mason3de45862008-11-17 21:02:50 -05005832skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005833 if (name_ptr != tmp_name)
5834 kfree(name_ptr);
5835
Chris Mason39279cc2007-06-12 06:35:45 -04005836 if (over)
5837 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005838 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005839 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005840 di_cur += di_len;
5841 di = (struct btrfs_dir_item *)((char *)di + di_len);
5842 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005843next:
5844 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005845 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005846
Miao Xie16cdcec2011-04-22 18:12:22 +08005847 if (key_type == BTRFS_DIR_INDEX_KEY) {
5848 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005849 ctx->pos++;
5850 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005851 if (ret)
5852 goto nopos;
5853 }
5854
David Woodhouse49593bf2008-08-17 17:08:36 +01005855 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005856 ctx->pos++;
5857
5858 /*
5859 * Stop new entries from being returned after we return the last
5860 * entry.
5861 *
5862 * New directory entries are assigned a strictly increasing
5863 * offset. This means that new entries created during readdir
5864 * are *guaranteed* to be seen in the future by that readdir.
5865 * This has broken buggy programs which operate on names as
5866 * they're returned by readdir. Until we re-use freed offsets
5867 * we have this hack to stop new entries from being returned
5868 * under the assumption that they'll never reach this huge
5869 * offset.
5870 *
5871 * This is being careful not to overflow 32bit loff_t unless the
5872 * last entry requires it because doing so has broken 32bit apps
5873 * in the past.
5874 */
5875 if (key_type == BTRFS_DIR_INDEX_KEY) {
5876 if (ctx->pos >= INT_MAX)
5877 ctx->pos = LLONG_MAX;
5878 else
5879 ctx->pos = INT_MAX;
5880 }
Chris Mason39279cc2007-06-12 06:35:45 -04005881nopos:
5882 ret = 0;
5883err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005884 if (key_type == BTRFS_DIR_INDEX_KEY)
5885 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005886 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005887 return ret;
5888}
5889
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005890int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005891{
5892 struct btrfs_root *root = BTRFS_I(inode)->root;
5893 struct btrfs_trans_handle *trans;
5894 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005895 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005896
Josef Bacik72ac3c02012-05-23 14:13:11 -04005897 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005898 return 0;
5899
Liu Bo83eea1f2012-07-10 05:28:39 -06005900 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005901 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005902
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005903 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005904 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005905 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005906 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005907 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005908 if (IS_ERR(trans))
5909 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005910 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005911 }
5912 return ret;
5913}
5914
5915/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005916 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005917 * inode changes. But, it is most likely to find the inode in cache.
5918 * FIXME, needs more benchmarking...there are no reasons other than performance
5919 * to keep or drop this code.
5920 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005921static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005922{
5923 struct btrfs_root *root = BTRFS_I(inode)->root;
5924 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005925 int ret;
5926
Josef Bacik72ac3c02012-05-23 14:13:11 -04005927 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005928 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005929
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005930 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005931 if (IS_ERR(trans))
5932 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005933
5934 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005935 if (ret && ret == -ENOSPC) {
5936 /* whoops, lets try again with the full transaction */
5937 btrfs_end_transaction(trans, root);
5938 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005939 if (IS_ERR(trans))
5940 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005941
Chris Mason94b60442010-05-26 11:02:00 -04005942 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005943 }
Chris Mason39279cc2007-06-12 06:35:45 -04005944 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005945 if (BTRFS_I(inode)->delayed_node)
5946 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005947
5948 return ret;
5949}
5950
5951/*
5952 * This is a copy of file_update_time. We need this so we can return error on
5953 * ENOSPC for updating the inode in the case of file write and mmap writes.
5954 */
Josef Bacike41f9412012-03-26 09:46:47 -04005955static int btrfs_update_time(struct inode *inode, struct timespec *now,
5956 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005957{
Alexander Block2bc5565282012-06-15 09:49:33 +02005958 struct btrfs_root *root = BTRFS_I(inode)->root;
5959
5960 if (btrfs_root_readonly(root))
5961 return -EROFS;
5962
Josef Bacike41f9412012-03-26 09:46:47 -04005963 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005964 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005965 if (flags & S_CTIME)
5966 inode->i_ctime = *now;
5967 if (flags & S_MTIME)
5968 inode->i_mtime = *now;
5969 if (flags & S_ATIME)
5970 inode->i_atime = *now;
5971 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005972}
5973
Chris Masond352ac62008-09-29 15:18:18 -04005974/*
5975 * find the highest existing sequence number in a directory
5976 * and then set the in-memory index_cnt variable to reflect
5977 * free sequence numbers
5978 */
Josef Bacikaec74772008-07-24 12:12:38 -04005979static int btrfs_set_inode_index_count(struct inode *inode)
5980{
5981 struct btrfs_root *root = BTRFS_I(inode)->root;
5982 struct btrfs_key key, found_key;
5983 struct btrfs_path *path;
5984 struct extent_buffer *leaf;
5985 int ret;
5986
Li Zefan33345d012011-04-20 10:31:50 +08005987 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005988 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005989 key.offset = (u64)-1;
5990
5991 path = btrfs_alloc_path();
5992 if (!path)
5993 return -ENOMEM;
5994
5995 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5996 if (ret < 0)
5997 goto out;
5998 /* FIXME: we should be able to handle this */
5999 if (ret == 0)
6000 goto out;
6001 ret = 0;
6002
6003 /*
6004 * MAGIC NUMBER EXPLANATION:
6005 * since we search a directory based on f_pos we have to start at 2
6006 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6007 * else has to start at 2
6008 */
6009 if (path->slots[0] == 0) {
6010 BTRFS_I(inode)->index_cnt = 2;
6011 goto out;
6012 }
6013
6014 path->slots[0]--;
6015
6016 leaf = path->nodes[0];
6017 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6018
Li Zefan33345d012011-04-20 10:31:50 +08006019 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006020 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006021 BTRFS_I(inode)->index_cnt = 2;
6022 goto out;
6023 }
6024
6025 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6026out:
6027 btrfs_free_path(path);
6028 return ret;
6029}
6030
Chris Masond352ac62008-09-29 15:18:18 -04006031/*
6032 * helper to find a free sequence number in a given directory. This current
6033 * code is very simple, later versions will do smarter things in the btree
6034 */
Chris Mason3de45862008-11-17 21:02:50 -05006035int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006036{
6037 int ret = 0;
6038
6039 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006040 ret = btrfs_inode_delayed_dir_index_count(dir);
6041 if (ret) {
6042 ret = btrfs_set_inode_index_count(dir);
6043 if (ret)
6044 return ret;
6045 }
Josef Bacikaec74772008-07-24 12:12:38 -04006046 }
6047
Chris Mason00e4e6b2008-08-05 11:18:09 -04006048 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006049 BTRFS_I(dir)->index_cnt++;
6050
6051 return ret;
6052}
6053
Chris Masonb0d5d102014-09-08 13:08:51 -07006054static int btrfs_insert_inode_locked(struct inode *inode)
6055{
6056 struct btrfs_iget_args args;
6057 args.location = &BTRFS_I(inode)->location;
6058 args.root = BTRFS_I(inode)->root;
6059
6060 return insert_inode_locked4(inode,
6061 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6062 btrfs_find_actor, &args);
6063}
6064
Chris Mason39279cc2007-06-12 06:35:45 -04006065static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6066 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006067 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006068 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006069 u64 ref_objectid, u64 objectid,
6070 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006071{
6072 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006073 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006074 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006075 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006076 struct btrfs_inode_ref *ref;
6077 struct btrfs_key key[2];
6078 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006079 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006080 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006081 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006082
Chris Mason5f39d392007-10-15 16:14:19 -04006083 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006084 if (!path)
6085 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006086
Chris Mason39279cc2007-06-12 06:35:45 -04006087 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006088 if (!inode) {
6089 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006090 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006091 }
Chris Mason39279cc2007-06-12 06:35:45 -04006092
Li Zefan581bb052011-04-20 10:06:11 +08006093 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006094 * O_TMPFILE, set link count to 0, so that after this point,
6095 * we fill in an inode item with the correct link count.
6096 */
6097 if (!name)
6098 set_nlink(inode, 0);
6099
6100 /*
Li Zefan581bb052011-04-20 10:06:11 +08006101 * we have to initialize this early, so we can reclaim the inode
6102 * number if we fail afterwards in this function.
6103 */
6104 inode->i_ino = objectid;
6105
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006106 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006107 trace_btrfs_inode_request(dir);
6108
Chris Mason3de45862008-11-17 21:02:50 -05006109 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006110 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006111 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006112 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006113 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006114 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006115 } else if (dir) {
6116 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006117 }
6118 /*
6119 * index_cnt is ignored for everything but a dir,
6120 * btrfs_get_inode_index_count has an explanation for the magic
6121 * number
6122 */
6123 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006124 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006125 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006126 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006127 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006128
Josef Bacik5dc562c2012-08-17 13:14:17 -04006129 /*
6130 * We could have gotten an inode number from somebody who was fsynced
6131 * and then removed in this same transaction, so let's just set full
6132 * sync since it will be a full sync anyway and this will blow away the
6133 * old info in the log.
6134 */
6135 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6136
Chris Mason9c583092008-01-29 15:15:18 -05006137 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006138 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006139 key[0].offset = 0;
6140
Chris Mason9c583092008-01-29 15:15:18 -05006141 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006142
6143 if (name) {
6144 /*
6145 * Start new inodes with an inode_ref. This is slightly more
6146 * efficient for small numbers of hard links since they will
6147 * be packed into one item. Extended refs will kick in if we
6148 * add more hard links than can fit in the ref item.
6149 */
6150 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006151 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006152 key[1].offset = ref_objectid;
6153
6154 sizes[1] = name_len + sizeof(*ref);
6155 }
Chris Mason9c583092008-01-29 15:15:18 -05006156
Chris Masonb0d5d102014-09-08 13:08:51 -07006157 location = &BTRFS_I(inode)->location;
6158 location->objectid = objectid;
6159 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006160 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006161
6162 ret = btrfs_insert_inode_locked(inode);
6163 if (ret < 0)
6164 goto fail;
6165
Chris Masonb9473432009-03-13 11:00:37 -04006166 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006167 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006168 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006169 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006170
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006171 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006172 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306173
6174 inode->i_mtime = CURRENT_TIME;
6175 inode->i_atime = inode->i_mtime;
6176 inode->i_ctime = inode->i_mtime;
6177 BTRFS_I(inode)->i_otime = inode->i_mtime;
6178
Chris Mason5f39d392007-10-15 16:14:19 -04006179 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6180 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006181 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6182 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006183 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006184
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006185 if (name) {
6186 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6187 struct btrfs_inode_ref);
6188 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6189 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6190 ptr = (unsigned long)(ref + 1);
6191 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6192 }
Chris Mason9c583092008-01-29 15:15:18 -05006193
Chris Mason5f39d392007-10-15 16:14:19 -04006194 btrfs_mark_buffer_dirty(path->nodes[0]);
6195 btrfs_free_path(path);
6196
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006197 btrfs_inherit_iflags(inode, dir);
6198
Al Viro569254b2011-07-24 17:08:40 -04006199 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04006200 if (btrfs_test_opt(root, NODATASUM))
6201 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06006202 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006203 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6204 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006205 }
6206
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006207 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006208
6209 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006210 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006211
Alexander Block8ea05e32012-07-25 17:35:53 +02006212 btrfs_update_root_times(trans, root);
6213
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006214 ret = btrfs_inode_inherit_props(trans, inode, dir);
6215 if (ret)
6216 btrfs_err(root->fs_info,
6217 "error inheriting props for ino %llu (root %llu): %d",
6218 btrfs_ino(inode), root->root_key.objectid, ret);
6219
Chris Mason39279cc2007-06-12 06:35:45 -04006220 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006221
6222fail_unlock:
6223 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006224fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006225 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006226 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006227 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006228 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006229 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006230}
6231
6232static inline u8 btrfs_inode_type(struct inode *inode)
6233{
6234 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6235}
6236
Chris Masond352ac62008-09-29 15:18:18 -04006237/*
6238 * utility function to add 'inode' into 'parent_inode' with
6239 * a give name and a given sequence number.
6240 * if 'add_backref' is true, also insert a backref from the
6241 * inode to the parent directory.
6242 */
Chris Masone02119d2008-09-05 16:13:11 -04006243int btrfs_add_link(struct btrfs_trans_handle *trans,
6244 struct inode *parent_inode, struct inode *inode,
6245 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006246{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006247 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006248 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006249 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006250 u64 ino = btrfs_ino(inode);
6251 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006252
Li Zefan33345d012011-04-20 10:31:50 +08006253 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006254 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6255 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006256 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006257 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006258 key.offset = 0;
6259 }
Chris Mason39279cc2007-06-12 06:35:45 -04006260
Li Zefan33345d012011-04-20 10:31:50 +08006261 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006262 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6263 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006264 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006265 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006266 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6267 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006268 }
6269
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006270 /* Nothing to clean up yet */
6271 if (ret)
6272 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006273
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006274 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6275 parent_inode, &key,
6276 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006277 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006278 goto fail_dir_item;
6279 else if (ret) {
6280 btrfs_abort_transaction(trans, root, ret);
6281 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006282 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006283
6284 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6285 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006286 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006287 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
6288 ret = btrfs_update_inode(trans, root, parent_inode);
6289 if (ret)
6290 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006291 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006292
6293fail_dir_item:
6294 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6295 u64 local_index;
6296 int err;
6297 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6298 key.objectid, root->root_key.objectid,
6299 parent_ino, &local_index, name, name_len);
6300
6301 } else if (add_backref) {
6302 u64 local_index;
6303 int err;
6304
6305 err = btrfs_del_inode_ref(trans, root, name, name_len,
6306 ino, parent_ino, &local_index);
6307 }
6308 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006309}
6310
6311static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006312 struct inode *dir, struct dentry *dentry,
6313 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006314{
Josef Bacika1b075d2010-11-19 20:36:11 +00006315 int err = btrfs_add_link(trans, dir, inode,
6316 dentry->d_name.name, dentry->d_name.len,
6317 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006318 if (err > 0)
6319 err = -EEXIST;
6320 return err;
6321}
6322
Josef Bacik618e21d2007-07-11 10:18:17 -04006323static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006324 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006325{
6326 struct btrfs_trans_handle *trans;
6327 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006328 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006329 int err;
6330 int drop_inode = 0;
6331 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006332 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006333
6334 if (!new_valid_dev(rdev))
6335 return -EINVAL;
6336
Josef Bacik9ed74f22009-09-11 16:12:44 -04006337 /*
6338 * 2 for inode item and ref
6339 * 2 for dir items
6340 * 1 for xattr if selinux is on
6341 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006342 trans = btrfs_start_transaction(root, 5);
6343 if (IS_ERR(trans))
6344 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006345
Li Zefan581bb052011-04-20 10:06:11 +08006346 err = btrfs_find_free_ino(root, &objectid);
6347 if (err)
6348 goto out_unlock;
6349
Josef Bacikaec74772008-07-24 12:12:38 -04006350 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006351 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006352 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006353 if (IS_ERR(inode)) {
6354 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006355 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006356 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006357
Casey Schauflerad19db72011-12-15 10:09:07 -05006358 /*
6359 * If the active LSM wants to access the inode during
6360 * d_instantiate it needs these. Smack checks to see
6361 * if the filesystem supports xattrs by looking at the
6362 * ops vector.
6363 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006364 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006365 init_special_inode(inode, inode->i_mode, rdev);
6366
6367 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006368 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006369 goto out_unlock_inode;
6370
6371 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6372 if (err) {
6373 goto out_unlock_inode;
6374 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006375 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006376 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006377 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006378 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006379
Josef Bacik618e21d2007-07-11 10:18:17 -04006380out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006381 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006382 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006383 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006384 if (drop_inode) {
6385 inode_dec_link_count(inode);
6386 iput(inode);
6387 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006388 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006389
6390out_unlock_inode:
6391 drop_inode = 1;
6392 unlock_new_inode(inode);
6393 goto out_unlock;
6394
Josef Bacik618e21d2007-07-11 10:18:17 -04006395}
6396
Chris Mason39279cc2007-06-12 06:35:45 -04006397static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006398 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006399{
6400 struct btrfs_trans_handle *trans;
6401 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006402 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006403 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006404 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006405 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006406 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006407
Josef Bacik9ed74f22009-09-11 16:12:44 -04006408 /*
6409 * 2 for inode item and ref
6410 * 2 for dir items
6411 * 1 for xattr if selinux is on
6412 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006413 trans = btrfs_start_transaction(root, 5);
6414 if (IS_ERR(trans))
6415 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006416
Li Zefan581bb052011-04-20 10:06:11 +08006417 err = btrfs_find_free_ino(root, &objectid);
6418 if (err)
6419 goto out_unlock;
6420
Josef Bacikaec74772008-07-24 12:12:38 -04006421 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006422 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006423 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006424 if (IS_ERR(inode)) {
6425 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006426 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006427 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006428 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006429 /*
6430 * If the active LSM wants to access the inode during
6431 * d_instantiate it needs these. Smack checks to see
6432 * if the filesystem supports xattrs by looking at the
6433 * ops vector.
6434 */
6435 inode->i_fop = &btrfs_file_operations;
6436 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006437 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006438
6439 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6440 if (err)
6441 goto out_unlock_inode;
6442
6443 err = btrfs_update_inode(trans, root, inode);
6444 if (err)
6445 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006446
Josef Bacika1b075d2010-11-19 20:36:11 +00006447 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006448 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006449 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006450
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006451 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006452 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006453 d_instantiate(dentry, inode);
6454
Chris Mason39279cc2007-06-12 06:35:45 -04006455out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006456 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006457 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006458 inode_dec_link_count(inode);
6459 iput(inode);
6460 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006461 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006462 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006463 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006464
6465out_unlock_inode:
6466 unlock_new_inode(inode);
6467 goto out_unlock;
6468
Chris Mason39279cc2007-06-12 06:35:45 -04006469}
6470
6471static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6472 struct dentry *dentry)
6473{
6474 struct btrfs_trans_handle *trans;
6475 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006476 struct inode *inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006477 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006478 int err;
6479 int drop_inode = 0;
6480
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006481 /* do not allow sys_link's with other subvols of the same device */
6482 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006483 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006484
Mark Fashehf1863732012-08-08 11:32:27 -07006485 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006486 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006487
Chris Mason3de45862008-11-17 21:02:50 -05006488 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006489 if (err)
6490 goto fail;
6491
Yan, Zhenga22285a2010-05-16 10:48:46 -04006492 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006493 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006494 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006495 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006496 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006497 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006498 if (IS_ERR(trans)) {
6499 err = PTR_ERR(trans);
6500 goto fail;
6501 }
Chris Mason5f39d392007-10-15 16:14:19 -04006502
Miao Xie67de1172013-12-26 13:07:06 +08006503 /* There are several dir indexes for this inode, clear the cache. */
6504 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006505 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006506 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006507 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006508 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006509 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006510
Josef Bacika1b075d2010-11-19 20:36:11 +00006511 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006512
Yan, Zhenga5719522009-09-24 09:17:31 -04006513 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006514 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006515 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006516 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006517 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006518 if (err)
6519 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006520 if (inode->i_nlink == 1) {
6521 /*
6522 * If new hard link count is 1, it's a file created
6523 * with open(2) O_TMPFILE flag.
6524 */
6525 err = btrfs_orphan_del(trans, inode);
6526 if (err)
6527 goto fail;
6528 }
Al Viro08c422c2011-12-23 07:58:13 -05006529 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006530 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006531 }
Chris Mason39279cc2007-06-12 06:35:45 -04006532
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006533 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006534 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006535fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006536 if (drop_inode) {
6537 inode_dec_link_count(inode);
6538 iput(inode);
6539 }
Liu Bob53d3f52012-11-14 14:34:34 +00006540 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006541 return err;
6542}
6543
Al Viro18bb1db2011-07-26 01:41:39 -04006544static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006545{
Chris Masonb9d86662008-05-02 16:13:49 -04006546 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006547 struct btrfs_trans_handle *trans;
6548 struct btrfs_root *root = BTRFS_I(dir)->root;
6549 int err = 0;
6550 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006551 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006552 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006553
Josef Bacik9ed74f22009-09-11 16:12:44 -04006554 /*
6555 * 2 items for inode and ref
6556 * 2 items for dir items
6557 * 1 for xattr if selinux is on
6558 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006559 trans = btrfs_start_transaction(root, 5);
6560 if (IS_ERR(trans))
6561 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006562
Li Zefan581bb052011-04-20 10:06:11 +08006563 err = btrfs_find_free_ino(root, &objectid);
6564 if (err)
6565 goto out_fail;
6566
Josef Bacikaec74772008-07-24 12:12:38 -04006567 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006568 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006569 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006570 if (IS_ERR(inode)) {
6571 err = PTR_ERR(inode);
6572 goto out_fail;
6573 }
Chris Mason5f39d392007-10-15 16:14:19 -04006574
Chris Mason39279cc2007-06-12 06:35:45 -04006575 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006576 /* these must be set before we unlock the inode */
6577 inode->i_op = &btrfs_dir_inode_operations;
6578 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006579
Eric Paris2a7dba32011-02-01 11:05:39 -05006580 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006581 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006582 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006583
Chris Masondbe674a2008-07-17 12:54:05 -04006584 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006585 err = btrfs_update_inode(trans, root, inode);
6586 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006587 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006588
Josef Bacika1b075d2010-11-19 20:36:11 +00006589 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6590 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006591 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006592 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006593
Chris Mason39279cc2007-06-12 06:35:45 -04006594 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006595 /*
6596 * mkdir is special. We're unlocking after we call d_instantiate
6597 * to avoid a race with nfsd calling d_instantiate.
6598 */
6599 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006600 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006601
6602out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006603 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006604 if (drop_on_err) {
6605 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006606 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006607 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006608 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006609 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006610 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006611
6612out_fail_inode:
6613 unlock_new_inode(inode);
6614 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006615}
6616
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006617/* Find next extent map of a given extent map, caller needs to ensure locks */
6618static struct extent_map *next_extent_map(struct extent_map *em)
6619{
6620 struct rb_node *next;
6621
6622 next = rb_next(&em->rb_node);
6623 if (!next)
6624 return NULL;
6625 return container_of(next, struct extent_map, rb_node);
6626}
6627
6628static struct extent_map *prev_extent_map(struct extent_map *em)
6629{
6630 struct rb_node *prev;
6631
6632 prev = rb_prev(&em->rb_node);
6633 if (!prev)
6634 return NULL;
6635 return container_of(prev, struct extent_map, rb_node);
6636}
6637
Chris Masond352ac62008-09-29 15:18:18 -04006638/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006639 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006640 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006641 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006642 */
Chris Mason3b951512008-04-17 11:29:12 -04006643static int merge_extent_mapping(struct extent_map_tree *em_tree,
6644 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006645 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006646 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006647{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006648 struct extent_map *prev;
6649 struct extent_map *next;
6650 u64 start;
6651 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006652 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006653
Chris Masone6dcd2d2008-07-17 12:53:50 -04006654 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006655
6656 if (existing->start > map_start) {
6657 next = existing;
6658 prev = prev_extent_map(next);
6659 } else {
6660 prev = existing;
6661 next = next_extent_map(prev);
6662 }
6663
6664 start = prev ? extent_map_end(prev) : em->start;
6665 start = max_t(u64, start, em->start);
6666 end = next ? next->start : extent_map_end(em);
6667 end = min_t(u64, end, extent_map_end(em));
6668 start_diff = start - em->start;
6669 em->start = start;
6670 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006671 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6672 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006673 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006674 em->block_len -= start_diff;
6675 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006676 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006677}
6678
Chris Masonc8b97812008-10-29 14:49:59 -04006679static noinline int uncompress_inline(struct btrfs_path *path,
6680 struct inode *inode, struct page *page,
6681 size_t pg_offset, u64 extent_offset,
6682 struct btrfs_file_extent_item *item)
6683{
6684 int ret;
6685 struct extent_buffer *leaf = path->nodes[0];
6686 char *tmp;
6687 size_t max_size;
6688 unsigned long inline_size;
6689 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006690 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006691
6692 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006693 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006694 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6695 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006696 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006697 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006698 if (!tmp)
6699 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006700 ptr = btrfs_file_extent_inline_start(item);
6701
6702 read_extent_buffer(leaf, tmp, ptr, inline_size);
6703
Chris Mason5b050f02008-11-11 09:34:41 -05006704 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006705 ret = btrfs_decompress(compress_type, tmp, page,
6706 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006707 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006708 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006709}
6710
Chris Masond352ac62008-09-29 15:18:18 -04006711/*
6712 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006713 * the ugly parts come from merging extents from the disk with the in-ram
6714 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006715 * where the in-ram extents might be locked pending data=ordered completion.
6716 *
6717 * This also copies inline extents directly into the page.
6718 */
Chris Masond3977122009-01-05 21:25:51 -05006719
Chris Masona52d9a82007-08-27 16:49:44 -04006720struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006721 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006722 int create)
6723{
6724 int ret;
6725 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006726 u64 extent_start = 0;
6727 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006728 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006729 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006730 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006731 struct btrfs_root *root = BTRFS_I(inode)->root;
6732 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006733 struct extent_buffer *leaf;
6734 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006735 struct extent_map *em = NULL;
6736 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006737 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006738 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006739 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006740
Chris Masona52d9a82007-08-27 16:49:44 -04006741again:
Chris Mason890871b2009-09-02 16:24:52 -04006742 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006743 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006744 if (em)
6745 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006746 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006747
Chris Masona52d9a82007-08-27 16:49:44 -04006748 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006749 if (em->start > start || em->start + em->len <= start)
6750 free_extent_map(em);
6751 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006752 free_extent_map(em);
6753 else
6754 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006755 }
David Sterba172ddd62011-04-21 00:48:27 +02006756 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006757 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006758 err = -ENOMEM;
6759 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006760 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006761 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006762 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006763 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006764 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006765 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006766
6767 if (!path) {
6768 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006769 if (!path) {
6770 err = -ENOMEM;
6771 goto out;
6772 }
6773 /*
6774 * Chances are we'll be called again, so go ahead and do
6775 * readahead
6776 */
6777 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006778 }
6779
Chris Mason179e29e2007-11-01 11:28:41 -04006780 ret = btrfs_lookup_file_extent(trans, root, path,
6781 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006782 if (ret < 0) {
6783 err = ret;
6784 goto out;
6785 }
6786
6787 if (ret != 0) {
6788 if (path->slots[0] == 0)
6789 goto not_found;
6790 path->slots[0]--;
6791 }
6792
Chris Mason5f39d392007-10-15 16:14:19 -04006793 leaf = path->nodes[0];
6794 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006795 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006796 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006797 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006798 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006799 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006800 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006801 /*
6802 * If we backup past the first extent we want to move forward
6803 * and see if there is an extent in front of us, otherwise we'll
6804 * say there is a hole for our whole search range which can
6805 * cause problems.
6806 */
6807 extent_end = start;
6808 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006809 }
6810
Chris Mason5f39d392007-10-15 16:14:19 -04006811 found_type = btrfs_file_extent_type(leaf, item);
6812 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006813 if (found_type == BTRFS_FILE_EXTENT_REG ||
6814 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006815 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006816 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006817 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6818 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006819 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006820 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006821 }
Josef Bacik25a50342013-10-14 12:08:38 -04006822next:
Yan Zheng9036c102008-10-30 14:19:41 -04006823 if (start >= extent_end) {
6824 path->slots[0]++;
6825 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6826 ret = btrfs_next_leaf(root, path);
6827 if (ret < 0) {
6828 err = ret;
6829 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006830 }
Yan Zheng9036c102008-10-30 14:19:41 -04006831 if (ret > 0)
6832 goto not_found;
6833 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006834 }
Yan Zheng9036c102008-10-30 14:19:41 -04006835 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6836 if (found_key.objectid != objectid ||
6837 found_key.type != BTRFS_EXTENT_DATA_KEY)
6838 goto not_found;
6839 if (start + len <= found_key.offset)
6840 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006841 if (start > found_key.offset)
6842 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006843 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006844 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006845 em->len = found_key.offset - start;
6846 goto not_found_em;
6847 }
6848
Filipe Manana7ffbb592014-06-09 03:48:05 +01006849 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6850
Yan Zhengd899e052008-10-30 14:25:28 -04006851 if (found_type == BTRFS_FILE_EXTENT_REG ||
6852 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006853 goto insert;
6854 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006855 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006856 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006857 size_t size;
6858 size_t extent_offset;
6859 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006860
Filipe Manana7ffbb592014-06-09 03:48:05 +01006861 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006862 goto out;
Yan689f9342007-10-29 11:41:07 -04006863
Chris Mason514ac8a2014-01-03 21:07:00 -08006864 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006865 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006866 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006867 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006868 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006869 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006870 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006871 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006872 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006873 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006874 if (btrfs_file_extent_compression(leaf, item) !=
6875 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006876 ret = uncompress_inline(path, inode, page,
6877 pg_offset,
6878 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006879 if (ret) {
6880 err = ret;
6881 goto out;
6882 }
Chris Masonc8b97812008-10-29 14:49:59 -04006883 } else {
6884 map = kmap(page);
6885 read_extent_buffer(leaf, map + pg_offset, ptr,
6886 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006887 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6888 memset(map + pg_offset + copy_size, 0,
6889 PAGE_CACHE_SIZE - pg_offset -
6890 copy_size);
6891 }
Chris Masonc8b97812008-10-29 14:49:59 -04006892 kunmap(page);
6893 }
Chris Mason179e29e2007-11-01 11:28:41 -04006894 flush_dcache_page(page);
6895 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006896 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006897 if (!trans) {
6898 kunmap(page);
6899 free_extent_map(em);
6900 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006901
David Sterbab3b4aa72011-04-21 01:20:15 +02006902 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006903 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006904
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006905 if (IS_ERR(trans))
6906 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006907 goto again;
6908 }
Chris Masonc8b97812008-10-29 14:49:59 -04006909 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006910 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006911 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006912 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006913 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006914 }
Chris Masond1310b22008-01-24 16:13:08 -05006915 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006916 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006917 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006918 }
6919not_found:
6920 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006921 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006922 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006923not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006924 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006925 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006926insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006927 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006928 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006929 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006930 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006931 err = -EIO;
6932 goto out;
6933 }
Chris Masond1310b22008-01-24 16:13:08 -05006934
6935 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006936 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006937 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006938 /* it is possible that someone inserted the extent into the tree
6939 * while we had the lock dropped. It is also possible that
6940 * an overlapping map exists in the tree
6941 */
Chris Masona52d9a82007-08-27 16:49:44 -04006942 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006943 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006944
6945 ret = 0;
6946
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006947 existing = search_extent_mapping(em_tree, start, len);
6948 /*
6949 * existing will always be non-NULL, since there must be
6950 * extent causing the -EEXIST.
6951 */
6952 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006953 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006954 /*
6955 * The existing extent map is the one nearest to
6956 * the [start, start + len) range which overlaps
6957 */
6958 err = merge_extent_mapping(em_tree, existing,
6959 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006960 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006961 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006962 free_extent_map(em);
6963 em = NULL;
6964 }
6965 } else {
6966 free_extent_map(em);
6967 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006968 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006969 }
Chris Masona52d9a82007-08-27 16:49:44 -04006970 }
Chris Mason890871b2009-09-02 16:24:52 -04006971 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006972out:
liubo1abe9b82011-03-24 11:18:59 +00006973
Steven Rostedt4cd85872013-11-14 22:57:29 -05006974 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006975
Tsutomu Itoh527afb42015-08-19 14:55:00 +09006976 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006977 if (trans) {
6978 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006979 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006980 err = ret;
6981 }
Chris Masona52d9a82007-08-27 16:49:44 -04006982 if (err) {
6983 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006984 return ERR_PTR(err);
6985 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006986 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006987 return em;
6988}
6989
Chris Masonec29ed52011-02-23 16:23:20 -05006990struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6991 size_t pg_offset, u64 start, u64 len,
6992 int create)
6993{
6994 struct extent_map *em;
6995 struct extent_map *hole_em = NULL;
6996 u64 range_start = start;
6997 u64 end;
6998 u64 found;
6999 u64 found_end;
7000 int err = 0;
7001
7002 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7003 if (IS_ERR(em))
7004 return em;
7005 if (em) {
7006 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007007 * if our em maps to
7008 * - a hole or
7009 * - a pre-alloc extent,
7010 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007011 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007012 if (em->block_start != EXTENT_MAP_HOLE &&
7013 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007014 return em;
7015 else
7016 hole_em = em;
7017 }
7018
7019 /* check to see if we've wrapped (len == -1 or similar) */
7020 end = start + len;
7021 if (end < start)
7022 end = (u64)-1;
7023 else
7024 end -= 1;
7025
7026 em = NULL;
7027
7028 /* ok, we didn't find anything, lets look for delalloc */
7029 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7030 end, len, EXTENT_DELALLOC, 1);
7031 found_end = range_start + found;
7032 if (found_end < range_start)
7033 found_end = (u64)-1;
7034
7035 /*
7036 * we didn't find anything useful, return
7037 * the original results from get_extent()
7038 */
7039 if (range_start > end || found_end <= start) {
7040 em = hole_em;
7041 hole_em = NULL;
7042 goto out;
7043 }
7044
7045 /* adjust the range_start to make sure it doesn't
7046 * go backwards from the start they passed in
7047 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307048 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007049 found = found_end - range_start;
7050
7051 if (found > 0) {
7052 u64 hole_start = start;
7053 u64 hole_len = len;
7054
David Sterba172ddd62011-04-21 00:48:27 +02007055 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007056 if (!em) {
7057 err = -ENOMEM;
7058 goto out;
7059 }
7060 /*
7061 * when btrfs_get_extent can't find anything it
7062 * returns one huge hole
7063 *
7064 * make sure what it found really fits our range, and
7065 * adjust to make sure it is based on the start from
7066 * the caller
7067 */
7068 if (hole_em) {
7069 u64 calc_end = extent_map_end(hole_em);
7070
7071 if (calc_end <= start || (hole_em->start > end)) {
7072 free_extent_map(hole_em);
7073 hole_em = NULL;
7074 } else {
7075 hole_start = max(hole_em->start, start);
7076 hole_len = calc_end - hole_start;
7077 }
7078 }
7079 em->bdev = NULL;
7080 if (hole_em && range_start > hole_start) {
7081 /* our hole starts before our delalloc, so we
7082 * have to return just the parts of the hole
7083 * that go until the delalloc starts
7084 */
7085 em->len = min(hole_len,
7086 range_start - hole_start);
7087 em->start = hole_start;
7088 em->orig_start = hole_start;
7089 /*
7090 * don't adjust block start at all,
7091 * it is fixed at EXTENT_MAP_HOLE
7092 */
7093 em->block_start = hole_em->block_start;
7094 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007095 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7096 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007097 } else {
7098 em->start = range_start;
7099 em->len = found;
7100 em->orig_start = range_start;
7101 em->block_start = EXTENT_MAP_DELALLOC;
7102 em->block_len = found;
7103 }
7104 } else if (hole_em) {
7105 return hole_em;
7106 }
7107out:
7108
7109 free_extent_map(hole_em);
7110 if (err) {
7111 free_extent_map(em);
7112 return ERR_PTR(err);
7113 }
7114 return em;
7115}
7116
Josef Bacik4b46fce2010-05-23 11:00:55 -04007117static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7118 u64 start, u64 len)
7119{
7120 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007121 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007122 struct btrfs_key ins;
7123 u64 alloc_hint;
7124 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007125
Josef Bacik4b46fce2010-05-23 11:00:55 -04007126 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04007127 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007128 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007129 if (ret)
7130 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007131
Josef Bacik70c8a912012-10-11 16:54:30 -04007132 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007133 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04007134 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08007135 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007136 return em;
7137 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007138
7139 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
7140 ins.offset, ins.offset, 0);
7141 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08007142 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007143 free_extent_map(em);
7144 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007145 }
Josef Bacik00361582013-08-14 14:02:47 -04007146
Josef Bacik4b46fce2010-05-23 11:00:55 -04007147 return em;
7148}
7149
Chris Mason46bfbb52010-05-26 11:04:10 -04007150/*
7151 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7152 * block must be cow'd
7153 */
Josef Bacik00361582013-08-14 14:02:47 -04007154noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007155 u64 *orig_start, u64 *orig_block_len,
7156 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007157{
Josef Bacik00361582013-08-14 14:02:47 -04007158 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007159 struct btrfs_path *path;
7160 int ret;
7161 struct extent_buffer *leaf;
7162 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007163 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007164 struct btrfs_file_extent_item *fi;
7165 struct btrfs_key key;
7166 u64 disk_bytenr;
7167 u64 backref_offset;
7168 u64 extent_end;
7169 u64 num_bytes;
7170 int slot;
7171 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007172 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007173
Chris Mason46bfbb52010-05-26 11:04:10 -04007174 path = btrfs_alloc_path();
7175 if (!path)
7176 return -ENOMEM;
7177
Josef Bacik00361582013-08-14 14:02:47 -04007178 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007179 offset, 0);
7180 if (ret < 0)
7181 goto out;
7182
7183 slot = path->slots[0];
7184 if (ret == 1) {
7185 if (slot == 0) {
7186 /* can't find the item, must cow */
7187 ret = 0;
7188 goto out;
7189 }
7190 slot--;
7191 }
7192 ret = 0;
7193 leaf = path->nodes[0];
7194 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007195 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007196 key.type != BTRFS_EXTENT_DATA_KEY) {
7197 /* not our file or wrong item type, must cow */
7198 goto out;
7199 }
7200
7201 if (key.offset > offset) {
7202 /* Wrong offset, must cow */
7203 goto out;
7204 }
7205
7206 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7207 found_type = btrfs_file_extent_type(leaf, fi);
7208 if (found_type != BTRFS_FILE_EXTENT_REG &&
7209 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7210 /* not a regular extent, must cow */
7211 goto out;
7212 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007213
7214 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7215 goto out;
7216
Miao Xiee77751a2013-12-27 21:11:50 +08007217 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7218 if (extent_end <= offset)
7219 goto out;
7220
Chris Mason46bfbb52010-05-26 11:04:10 -04007221 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007222 if (disk_bytenr == 0)
7223 goto out;
7224
7225 if (btrfs_file_extent_compression(leaf, fi) ||
7226 btrfs_file_extent_encryption(leaf, fi) ||
7227 btrfs_file_extent_other_encoding(leaf, fi))
7228 goto out;
7229
Chris Mason46bfbb52010-05-26 11:04:10 -04007230 backref_offset = btrfs_file_extent_offset(leaf, fi);
7231
Josef Bacik7ee9e442013-06-21 16:37:03 -04007232 if (orig_start) {
7233 *orig_start = key.offset - backref_offset;
7234 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7235 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7236 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007237
Chris Mason46bfbb52010-05-26 11:04:10 -04007238 if (btrfs_extent_readonly(root, disk_bytenr))
7239 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007240
7241 num_bytes = min(offset + *len, extent_end) - offset;
7242 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7243 u64 range_end;
7244
7245 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7246 ret = test_range_bit(io_tree, offset, range_end,
7247 EXTENT_DELALLOC, 0, NULL);
7248 if (ret) {
7249 ret = -EAGAIN;
7250 goto out;
7251 }
7252 }
7253
Josef Bacik1bda19e2013-10-18 12:10:36 -04007254 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007255
7256 /*
7257 * look for other files referencing this extent, if we
7258 * find any we must cow
7259 */
Josef Bacik00361582013-08-14 14:02:47 -04007260 trans = btrfs_join_transaction(root);
7261 if (IS_ERR(trans)) {
7262 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007263 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007264 }
7265
7266 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7267 key.offset - backref_offset, disk_bytenr);
7268 btrfs_end_transaction(trans, root);
7269 if (ret) {
7270 ret = 0;
7271 goto out;
7272 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007273
7274 /*
7275 * adjust disk_bytenr and num_bytes to cover just the bytes
7276 * in this extent we are about to write. If there
7277 * are any csums in that range we have to cow in order
7278 * to keep the csums correct
7279 */
7280 disk_bytenr += backref_offset;
7281 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007282 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7283 goto out;
7284 /*
7285 * all of the above have passed, it is safe to overwrite this extent
7286 * without cow
7287 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007288 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007289 ret = 1;
7290out:
7291 btrfs_free_path(path);
7292 return ret;
7293}
7294
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007295bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7296{
7297 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7298 int found = false;
7299 void **pagep = NULL;
7300 struct page *page = NULL;
7301 int start_idx;
7302 int end_idx;
7303
7304 start_idx = start >> PAGE_CACHE_SHIFT;
7305
7306 /*
7307 * end is the last byte in the last page. end == start is legal
7308 */
7309 end_idx = end >> PAGE_CACHE_SHIFT;
7310
7311 rcu_read_lock();
7312
7313 /* Most of the code in this while loop is lifted from
7314 * find_get_page. It's been modified to begin searching from a
7315 * page and return just the first page found in that range. If the
7316 * found idx is less than or equal to the end idx then we know that
7317 * a page exists. If no pages are found or if those pages are
7318 * outside of the range then we're fine (yay!) */
7319 while (page == NULL &&
7320 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7321 page = radix_tree_deref_slot(pagep);
7322 if (unlikely(!page))
7323 break;
7324
7325 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007326 if (radix_tree_deref_retry(page)) {
7327 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007328 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007329 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007330 /*
7331 * Otherwise, shmem/tmpfs must be storing a swap entry
7332 * here as an exceptional entry: so return it without
7333 * attempting to raise page count.
7334 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007335 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007336 break; /* TODO: Is this relevant for this use case? */
7337 }
7338
Filipe Manana91405152014-06-05 13:22:24 +01007339 if (!page_cache_get_speculative(page)) {
7340 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007341 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007342 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007343
7344 /*
7345 * Has the page moved?
7346 * This is part of the lockless pagecache protocol. See
7347 * include/linux/pagemap.h for details.
7348 */
7349 if (unlikely(page != *pagep)) {
7350 page_cache_release(page);
7351 page = NULL;
7352 }
7353 }
7354
7355 if (page) {
7356 if (page->index <= end_idx)
7357 found = true;
7358 page_cache_release(page);
7359 }
7360
7361 rcu_read_unlock();
7362 return found;
7363}
7364
Josef Bacikeb838e72012-07-31 16:28:48 -04007365static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7366 struct extent_state **cached_state, int writing)
7367{
7368 struct btrfs_ordered_extent *ordered;
7369 int ret = 0;
7370
7371 while (1) {
7372 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7373 0, cached_state);
7374 /*
7375 * We're concerned with the entire range that we're going to be
7376 * doing DIO to, so we need to make sure theres no ordered
7377 * extents in this range.
7378 */
7379 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7380 lockend - lockstart + 1);
7381
7382 /*
7383 * We need to make sure there are no buffered pages in this
7384 * range either, we could have raced between the invalidate in
7385 * generic_file_direct_write and locking the extent. The
7386 * invalidate needs to happen so that reads after a write do not
7387 * get stale data.
7388 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007389 if (!ordered &&
7390 (!writing ||
7391 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007392 break;
7393
7394 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7395 cached_state, GFP_NOFS);
7396
7397 if (ordered) {
7398 btrfs_start_ordered_extent(inode, ordered, 1);
7399 btrfs_put_ordered_extent(ordered);
7400 } else {
7401 /* Screw you mmap */
Filipe Manana728404d2014-10-10 09:43:11 +01007402 ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
Filipe Manana075bdbd2014-10-09 21:18:55 +01007403 if (ret)
7404 break;
7405 ret = filemap_fdatawait_range(inode->i_mapping,
7406 lockstart,
7407 lockend);
Josef Bacikeb838e72012-07-31 16:28:48 -04007408 if (ret)
7409 break;
7410
7411 /*
7412 * If we found a page that couldn't be invalidated just
7413 * fall back to buffered.
7414 */
7415 ret = invalidate_inode_pages2_range(inode->i_mapping,
7416 lockstart >> PAGE_CACHE_SHIFT,
7417 lockend >> PAGE_CACHE_SHIFT);
7418 if (ret)
7419 break;
7420 }
7421
7422 cond_resched();
7423 }
7424
7425 return ret;
7426}
7427
Josef Bacik69ffb542012-09-11 15:40:07 -04007428static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7429 u64 len, u64 orig_start,
7430 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007431 u64 orig_block_len, u64 ram_bytes,
7432 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007433{
7434 struct extent_map_tree *em_tree;
7435 struct extent_map *em;
7436 struct btrfs_root *root = BTRFS_I(inode)->root;
7437 int ret;
7438
7439 em_tree = &BTRFS_I(inode)->extent_tree;
7440 em = alloc_extent_map();
7441 if (!em)
7442 return ERR_PTR(-ENOMEM);
7443
7444 em->start = start;
7445 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007446 em->mod_start = start;
7447 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007448 em->len = len;
7449 em->block_len = block_len;
7450 em->block_start = block_start;
7451 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007452 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007453 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007454 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007455 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7456 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007457 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007458
7459 do {
7460 btrfs_drop_extent_cache(inode, em->start,
7461 em->start + em->len - 1, 0);
7462 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007463 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007464 write_unlock(&em_tree->lock);
7465 } while (ret == -EEXIST);
7466
7467 if (ret) {
7468 free_extent_map(em);
7469 return ERR_PTR(ret);
7470 }
7471
7472 return em;
7473}
7474
chandan50745b02015-08-28 21:10:13 +05307475struct btrfs_dio_data {
7476 u64 outstanding_extents;
7477 u64 reserve;
7478};
Josef Bacik69ffb542012-09-11 15:40:07 -04007479
Josef Bacik4b46fce2010-05-23 11:00:55 -04007480static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7481 struct buffer_head *bh_result, int create)
7482{
7483 struct extent_map *em;
7484 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007485 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307486 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007487 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007488 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007489 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007490 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007491 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007492
Miao Xie172a5042013-02-21 02:48:22 -07007493 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007494 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007495 else
Josef Bacikc3298612012-08-03 16:49:19 -04007496 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007497
Josef Bacikc3298612012-08-03 16:49:19 -04007498 lockstart = start;
7499 lockend = start + len - 1;
7500
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007501 if (current->journal_info) {
7502 /*
7503 * Need to pull our outstanding extents and set journal_info to NULL so
7504 * that anything that needs to check if there's a transction doesn't get
7505 * confused.
7506 */
chandan50745b02015-08-28 21:10:13 +05307507 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007508 current->journal_info = NULL;
7509 }
7510
Josef Bacikeb838e72012-07-31 16:28:48 -04007511 /*
7512 * If this errors out it's because we couldn't invalidate pagecache for
7513 * this range and we need to fallback to buffered.
7514 */
7515 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7516 return -ENOTBLK;
7517
Josef Bacik4b46fce2010-05-23 11:00:55 -04007518 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007519 if (IS_ERR(em)) {
7520 ret = PTR_ERR(em);
7521 goto unlock_err;
7522 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007523
7524 /*
7525 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7526 * io. INLINE is special, and we could probably kludge it in here, but
7527 * it's still buffered so for safety lets just fall back to the generic
7528 * buffered path.
7529 *
7530 * For COMPRESSED we _have_ to read the entire extent in so we can
7531 * decompress it, so there will be buffering required no matter what we
7532 * do, so go ahead and fallback to buffered.
7533 *
7534 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7535 * to buffered IO. Don't blame me, this is the price we pay for using
7536 * the generic code.
7537 */
7538 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7539 em->block_start == EXTENT_MAP_INLINE) {
7540 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007541 ret = -ENOTBLK;
7542 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007543 }
7544
7545 /* Just a good old fashioned hole, return */
7546 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7547 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7548 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007549 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007550 }
7551
7552 /*
7553 * We don't allocate a new extent in the following cases
7554 *
7555 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7556 * existing extent.
7557 * 2) The extent is marked as PREALLOC. We're good to go here and can
7558 * just use the extent.
7559 *
7560 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007561 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007562 len = min(len, em->len - (start - em->start));
7563 lockstart = start + len;
7564 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007565 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007566
7567 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7568 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7569 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007570 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007571 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007572
7573 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7574 type = BTRFS_ORDERED_PREALLOC;
7575 else
7576 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007577 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007578 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007579
Josef Bacik00361582013-08-14 14:02:47 -04007580 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007581 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007582 if (type == BTRFS_ORDERED_PREALLOC) {
7583 free_extent_map(em);
7584 em = create_pinned_em(inode, start, len,
7585 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007586 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007587 orig_block_len,
7588 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007589 if (IS_ERR(em)) {
7590 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007591 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007592 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007593 }
7594
Chris Mason46bfbb52010-05-26 11:04:10 -04007595 ret = btrfs_add_ordered_extent_dio(inode, start,
7596 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007597 if (ret) {
7598 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007599 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007600 }
7601 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007602 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007603 }
Josef Bacik00361582013-08-14 14:02:47 -04007604
Chris Mason46bfbb52010-05-26 11:04:10 -04007605 /*
7606 * this will cow the extent, reset the len in case we changed
7607 * it above
7608 */
7609 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007610 free_extent_map(em);
7611 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007612 if (IS_ERR(em)) {
7613 ret = PTR_ERR(em);
7614 goto unlock_err;
7615 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007616 len = min(len, em->len - (start - em->start));
7617unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007618 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7619 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007620 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007621 bh_result->b_bdev = em->bdev;
7622 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007623 if (create) {
7624 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7625 set_buffer_new(bh_result);
7626
7627 /*
7628 * Need to update the i_size under the extent lock so buffered
7629 * readers will get the updated i_size when we unlock.
7630 */
7631 if (start + len > i_size_read(inode))
7632 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007633
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007634 /*
7635 * If we have an outstanding_extents count still set then we're
7636 * within our reservation, otherwise we need to adjust our inode
7637 * counter appropriately.
7638 */
chandan50745b02015-08-28 21:10:13 +05307639 if (dio_data->outstanding_extents) {
7640 (dio_data->outstanding_extents)--;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007641 } else {
Josef Bacik3e05bde2015-02-11 15:08:57 -05007642 spin_lock(&BTRFS_I(inode)->lock);
7643 BTRFS_I(inode)->outstanding_extents++;
7644 spin_unlock(&BTRFS_I(inode)->lock);
7645 }
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007646
Josef Bacik32667892015-02-11 15:08:58 -05007647 btrfs_free_reserved_data_space(inode, len);
chandan50745b02015-08-28 21:10:13 +05307648 WARN_ON(dio_data->reserve < len);
7649 dio_data->reserve -= len;
7650 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007651 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007652
Josef Bacikeb838e72012-07-31 16:28:48 -04007653 /*
7654 * In the case of write we need to clear and unlock the entire range,
7655 * in the case of read we need to unlock only the end area that we
7656 * aren't using if there is any left over space.
7657 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007658 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007659 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7660 lockend, unlock_bits, 1, 0,
7661 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007662 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007663 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007664 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007665
Josef Bacik4b46fce2010-05-23 11:00:55 -04007666 free_extent_map(em);
7667
7668 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007669
7670unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007671 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7672 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
chandan50745b02015-08-28 21:10:13 +05307673 if (dio_data)
7674 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007675 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007676}
7677
Miao Xie8b110e32014-09-12 18:44:03 +08007678static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7679 int rw, int mirror_num)
7680{
7681 struct btrfs_root *root = BTRFS_I(inode)->root;
7682 int ret;
7683
7684 BUG_ON(rw & REQ_WRITE);
7685
7686 bio_get(bio);
7687
7688 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7689 BTRFS_WQ_ENDIO_DIO_REPAIR);
7690 if (ret)
7691 goto err;
7692
7693 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7694err:
7695 bio_put(bio);
7696 return ret;
7697}
7698
7699static int btrfs_check_dio_repairable(struct inode *inode,
7700 struct bio *failed_bio,
7701 struct io_failure_record *failrec,
7702 int failed_mirror)
7703{
7704 int num_copies;
7705
7706 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7707 failrec->logical, failrec->len);
7708 if (num_copies == 1) {
7709 /*
7710 * we only have a single copy of the data, so don't bother with
7711 * all the retry and error correction code that follows. no
7712 * matter what the error is, it is very likely to persist.
7713 */
7714 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7715 num_copies, failrec->this_mirror, failed_mirror);
7716 return 0;
7717 }
7718
7719 failrec->failed_mirror = failed_mirror;
7720 failrec->this_mirror++;
7721 if (failrec->this_mirror == failed_mirror)
7722 failrec->this_mirror++;
7723
7724 if (failrec->this_mirror > num_copies) {
7725 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7726 num_copies, failrec->this_mirror, failed_mirror);
7727 return 0;
7728 }
7729
7730 return 1;
7731}
7732
7733static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7734 struct page *page, u64 start, u64 end,
7735 int failed_mirror, bio_end_io_t *repair_endio,
7736 void *repair_arg)
7737{
7738 struct io_failure_record *failrec;
7739 struct bio *bio;
7740 int isector;
7741 int read_mode;
7742 int ret;
7743
7744 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7745
7746 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7747 if (ret)
7748 return ret;
7749
7750 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7751 failed_mirror);
7752 if (!ret) {
7753 free_io_failure(inode, failrec);
7754 return -EIO;
7755 }
7756
7757 if (failed_bio->bi_vcnt > 1)
7758 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7759 else
7760 read_mode = READ_SYNC;
7761
7762 isector = start - btrfs_io_bio(failed_bio)->logical;
7763 isector >>= inode->i_sb->s_blocksize_bits;
7764 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7765 0, isector, repair_endio, repair_arg);
7766 if (!bio) {
7767 free_io_failure(inode, failrec);
7768 return -EIO;
7769 }
7770
7771 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7772 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7773 read_mode, failrec->this_mirror, failrec->in_validation);
7774
7775 ret = submit_dio_repair_bio(inode, bio, read_mode,
7776 failrec->this_mirror);
7777 if (ret) {
7778 free_io_failure(inode, failrec);
7779 bio_put(bio);
7780 }
7781
7782 return ret;
7783}
7784
7785struct btrfs_retry_complete {
7786 struct completion done;
7787 struct inode *inode;
7788 u64 start;
7789 int uptodate;
7790};
7791
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007792static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007793{
7794 struct btrfs_retry_complete *done = bio->bi_private;
7795 struct bio_vec *bvec;
7796 int i;
7797
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007798 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007799 goto end;
7800
7801 done->uptodate = 1;
7802 bio_for_each_segment_all(bvec, bio, i)
7803 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7804end:
7805 complete(&done->done);
7806 bio_put(bio);
7807}
7808
7809static int __btrfs_correct_data_nocsum(struct inode *inode,
7810 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007811{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007812 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007813 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007814 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007815 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007816 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007817
Miao Xiec1dc0892014-09-12 18:43:56 +08007818 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007819 done.inode = inode;
7820
7821 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7822try_again:
7823 done.uptodate = 0;
7824 done.start = start;
7825 init_completion(&done.done);
7826
7827 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7828 start + bvec->bv_len - 1,
7829 io_bio->mirror_num,
7830 btrfs_retry_endio_nocsum, &done);
7831 if (ret)
7832 return ret;
7833
7834 wait_for_completion(&done.done);
7835
7836 if (!done.uptodate) {
7837 /* We might have another mirror, so try again */
7838 goto try_again;
7839 }
7840
7841 start += bvec->bv_len;
7842 }
7843
7844 return 0;
7845}
7846
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007847static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007848{
7849 struct btrfs_retry_complete *done = bio->bi_private;
7850 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7851 struct bio_vec *bvec;
7852 int uptodate;
7853 int ret;
7854 int i;
7855
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007856 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007857 goto end;
7858
7859 uptodate = 1;
7860 bio_for_each_segment_all(bvec, bio, i) {
7861 ret = __readpage_endio_check(done->inode, io_bio, i,
7862 bvec->bv_page, 0,
7863 done->start, bvec->bv_len);
7864 if (!ret)
7865 clean_io_failure(done->inode, done->start,
7866 bvec->bv_page, 0);
7867 else
7868 uptodate = 0;
7869 }
7870
7871 done->uptodate = uptodate;
7872end:
7873 complete(&done->done);
7874 bio_put(bio);
7875}
7876
7877static int __btrfs_subio_endio_read(struct inode *inode,
7878 struct btrfs_io_bio *io_bio, int err)
7879{
7880 struct bio_vec *bvec;
7881 struct btrfs_retry_complete done;
7882 u64 start;
7883 u64 offset = 0;
7884 int i;
7885 int ret;
7886
7887 err = 0;
7888 start = io_bio->logical;
7889 done.inode = inode;
7890
Miao Xiec1dc0892014-09-12 18:43:56 +08007891 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007892 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7893 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007894 if (likely(!ret))
7895 goto next;
7896try_again:
7897 done.uptodate = 0;
7898 done.start = start;
7899 init_completion(&done.done);
7900
7901 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7902 start + bvec->bv_len - 1,
7903 io_bio->mirror_num,
7904 btrfs_retry_endio, &done);
7905 if (ret) {
7906 err = ret;
7907 goto next;
7908 }
7909
7910 wait_for_completion(&done.done);
7911
7912 if (!done.uptodate) {
7913 /* We might have another mirror, so try again */
7914 goto try_again;
7915 }
7916next:
7917 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007918 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007919 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007920
7921 return err;
7922}
7923
Miao Xie8b110e32014-09-12 18:44:03 +08007924static int btrfs_subio_endio_read(struct inode *inode,
7925 struct btrfs_io_bio *io_bio, int err)
7926{
7927 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7928
7929 if (skip_csum) {
7930 if (unlikely(err))
7931 return __btrfs_correct_data_nocsum(inode, io_bio);
7932 else
7933 return 0;
7934 } else {
7935 return __btrfs_subio_endio_read(inode, io_bio, err);
7936 }
7937}
7938
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007939static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08007940{
7941 struct btrfs_dio_private *dip = bio->bi_private;
7942 struct inode *inode = dip->inode;
7943 struct bio *dio_bio;
7944 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007945 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08007946
Miao Xie8b110e32014-09-12 18:44:03 +08007947 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7948 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007949
Josef Bacik4b46fce2010-05-23 11:00:55 -04007950 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007951 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007952 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007953
Josef Bacik4b46fce2010-05-23 11:00:55 -04007954 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007955
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007956 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08007957
7958 if (io_bio->end_io)
7959 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007960 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007961}
7962
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007963static void btrfs_endio_direct_write(struct bio *bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007964{
7965 struct btrfs_dio_private *dip = bio->bi_private;
7966 struct inode *inode = dip->inode;
7967 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007968 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007969 u64 ordered_offset = dip->logical_offset;
7970 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007971 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007972 int ret;
7973
Chris Mason163cf092010-11-28 19:56:33 -05007974again:
7975 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7976 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007977 ordered_bytes,
7978 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007979 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007980 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007981
Liu Bo9e0af232014-08-15 23:36:53 +08007982 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7983 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007984 btrfs_queue_work(root->fs_info->endio_write_workers,
7985 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007986out_test:
7987 /*
7988 * our bio might span multiple ordered extents. If we haven't
7989 * completed the accounting for the whole dio, go back and try again
7990 */
7991 if (ordered_offset < dip->logical_offset + dip->bytes) {
7992 ordered_bytes = dip->logical_offset + dip->bytes -
7993 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007994 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007995 goto again;
7996 }
Chris Mason9be33952013-05-17 18:30:14 -04007997 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007998
Josef Bacik4b46fce2010-05-23 11:00:55 -04007999 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008000
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008001 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008002 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008003}
8004
Chris Masoneaf25d92010-05-25 09:48:28 -04008005static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
8006 struct bio *bio, int mirror_num,
8007 unsigned long bio_flags, u64 offset)
8008{
8009 int ret;
8010 struct btrfs_root *root = BTRFS_I(inode)->root;
8011 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008012 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008013 return 0;
8014}
8015
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008016static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008017{
8018 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008019 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008020
Miao Xie8b110e32014-09-12 18:44:03 +08008021 if (err)
8022 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
8023 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
8024 btrfs_ino(dip->inode), bio->bi_rw,
8025 (unsigned long long)bio->bi_iter.bi_sector,
8026 bio->bi_iter.bi_size, err);
8027
8028 if (dip->subio_endio)
8029 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008030
8031 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008032 dip->errors = 1;
8033
8034 /*
8035 * before atomic variable goto zero, we must make sure
8036 * dip->errors is perceived to be set.
8037 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008038 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008039 }
8040
8041 /* if there are more bios still pending for this dio, just exit */
8042 if (!atomic_dec_and_test(&dip->pending_bios))
8043 goto out;
8044
Chris Mason9be33952013-05-17 18:30:14 -04008045 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008046 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008047 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008048 dip->dio_bio->bi_error = 0;
8049 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008050 }
8051out:
8052 bio_put(bio);
8053}
8054
8055static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8056 u64 first_sector, gfp_t gfp_flags)
8057{
Chris Masonda2f0f72015-07-02 13:57:22 -07008058 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008059 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008060 if (bio)
8061 bio_associate_current(bio);
8062 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008063}
8064
Miao Xiec1dc0892014-09-12 18:43:56 +08008065static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
8066 struct inode *inode,
8067 struct btrfs_dio_private *dip,
8068 struct bio *bio,
8069 u64 file_offset)
8070{
8071 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8072 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8073 int ret;
8074
8075 /*
8076 * We load all the csum data we need when we submit
8077 * the first bio to reduce the csum tree search and
8078 * contention.
8079 */
8080 if (dip->logical_offset == file_offset) {
8081 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
8082 file_offset);
8083 if (ret)
8084 return ret;
8085 }
8086
8087 if (bio == dip->orig_bio)
8088 return 0;
8089
8090 file_offset -= dip->logical_offset;
8091 file_offset >>= inode->i_sb->s_blocksize_bits;
8092 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8093
8094 return 0;
8095}
8096
Miao Xiee65e1532010-11-22 03:04:43 +00008097static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8098 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008099 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008100{
Miao Xiefacc8a222013-07-25 19:22:34 +08008101 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00008102 int write = rw & REQ_WRITE;
8103 struct btrfs_root *root = BTRFS_I(inode)->root;
8104 int ret;
8105
Josef Bacikb812ce22012-11-16 13:56:32 -05008106 if (async_submit)
8107 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8108
Miao Xiee65e1532010-11-22 03:04:43 +00008109 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008110
8111 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02008112 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8113 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008114 if (ret)
8115 goto err;
8116 }
Miao Xiee65e1532010-11-22 03:04:43 +00008117
Josef Bacik1ae39932011-04-06 14:41:34 -04008118 if (skip_sum)
8119 goto map;
8120
8121 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008122 ret = btrfs_wq_submit_bio(root->fs_info,
8123 inode, rw, bio, 0, 0,
8124 file_offset,
8125 __btrfs_submit_bio_start_direct_io,
8126 __btrfs_submit_bio_done);
8127 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008128 } else if (write) {
8129 /*
8130 * If we aren't doing async submit, calculate the csum of the
8131 * bio now.
8132 */
8133 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8134 if (ret)
8135 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008136 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008137 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8138 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008139 if (ret)
8140 goto err;
8141 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008142map:
8143 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008144err:
8145 bio_put(bio);
8146 return ret;
8147}
8148
8149static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8150 int skip_sum)
8151{
8152 struct inode *inode = dip->inode;
8153 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008154 struct bio *bio;
8155 struct bio *orig_bio = dip->orig_bio;
8156 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008157 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008158 u64 file_offset = dip->logical_offset;
8159 u64 submit_len = 0;
8160 u64 map_length;
8161 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08008162 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04008163 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008164
Kent Overstreet4f024f32013-10-11 15:44:27 -07008165 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008166 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008167 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008168 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008169 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008170
Kent Overstreet4f024f32013-10-11 15:44:27 -07008171 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008172 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008173 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008174 goto submit;
8175 }
8176
David Woodhouse53b381b2013-01-29 18:40:14 -05008177 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008178 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008179 async_submit = 0;
8180 else
8181 async_submit = 1;
8182
Josef Bacik02f57c72011-04-06 14:25:44 -04008183 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8184 if (!bio)
8185 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008186
Josef Bacik02f57c72011-04-06 14:25:44 -04008187 bio->bi_private = dip;
8188 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008189 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008190 atomic_inc(&dip->pending_bios);
8191
Miao Xiee65e1532010-11-22 03:04:43 +00008192 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02008193 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00008194 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02008195 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00008196 /*
8197 * inc the count before we submit the bio so
8198 * we know the end IO handler won't happen before
8199 * we inc the count. Otherwise, the dip might get freed
8200 * before we're done setting it up
8201 */
8202 atomic_inc(&dip->pending_bios);
8203 ret = __btrfs_submit_dio_bio(bio, inode, rw,
8204 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008205 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008206 if (ret) {
8207 bio_put(bio);
8208 atomic_dec(&dip->pending_bios);
8209 goto out_err;
8210 }
8211
Miao Xiee65e1532010-11-22 03:04:43 +00008212 start_sector += submit_len >> 9;
8213 file_offset += submit_len;
8214
8215 submit_len = 0;
8216 nr_pages = 0;
8217
8218 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8219 start_sector, GFP_NOFS);
8220 if (!bio)
8221 goto out_err;
8222 bio->bi_private = dip;
8223 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008224 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008225
Kent Overstreet4f024f32013-10-11 15:44:27 -07008226 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008227 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008228 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008229 &map_length, NULL, 0);
8230 if (ret) {
8231 bio_put(bio);
8232 goto out_err;
8233 }
8234 } else {
8235 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308236 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00008237 bvec++;
8238 }
8239 }
8240
Josef Bacik02f57c72011-04-06 14:25:44 -04008241submit:
Miao Xiee65e1532010-11-22 03:04:43 +00008242 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008243 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008244 if (!ret)
8245 return 0;
8246
8247 bio_put(bio);
8248out_err:
8249 dip->errors = 1;
8250 /*
8251 * before atomic variable goto zero, we must
8252 * make sure dip->errors is perceived to be set.
8253 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008254 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008255 if (atomic_dec_and_test(&dip->pending_bios))
8256 bio_io_error(dip->orig_bio);
8257
8258 /* bio_end_io() will handle error, so we needn't return it */
8259 return 0;
8260}
8261
Chris Mason9be33952013-05-17 18:30:14 -04008262static void btrfs_submit_direct(int rw, struct bio *dio_bio,
8263 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008264{
Filipe Manana61de7182015-07-01 12:13:10 +01008265 struct btrfs_dio_private *dip = NULL;
8266 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008267 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008268 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02008269 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008270 int ret = 0;
8271
8272 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8273
Chris Mason9be33952013-05-17 18:30:14 -04008274 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008275 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008276 ret = -ENOMEM;
8277 goto free_ordered;
8278 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008279
Miao Xiec1dc0892014-09-12 18:43:56 +08008280 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008281 if (!dip) {
8282 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008283 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008284 }
8285
8286 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008287 dip->inode = inode;
8288 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008289 dip->bytes = dio_bio->bi_iter.bi_size;
8290 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008291 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008292 dip->orig_bio = io_bio;
8293 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008294 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008295 btrfs_bio = btrfs_io_bio(io_bio);
8296 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008297
Miao Xiec1dc0892014-09-12 18:43:56 +08008298 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008299 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008300 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008301 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008302 dip->subio_endio = btrfs_subio_endio_read;
8303 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008304
Miao Xiee65e1532010-11-22 03:04:43 +00008305 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8306 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008307 return;
Chris Mason9be33952013-05-17 18:30:14 -04008308
Miao Xie23ea8e52014-09-12 18:43:54 +08008309 if (btrfs_bio->end_io)
8310 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008311
Josef Bacik4b46fce2010-05-23 11:00:55 -04008312free_ordered:
8313 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008314 * If we arrived here it means either we failed to submit the dip
8315 * or we either failed to clone the dio_bio or failed to allocate the
8316 * dip. If we cloned the dio_bio and allocated the dip, we can just
8317 * call bio_endio against our io_bio so that we get proper resource
8318 * cleanup if we fail to submit the dip, otherwise, we must do the
8319 * same as btrfs_endio_direct_[write|read] because we can't call these
8320 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008321 */
Filipe Manana61de7182015-07-01 12:13:10 +01008322 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008323 io_bio->bi_error = -EIO;
8324 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008325 /*
8326 * The end io callbacks free our dip, do the final put on io_bio
8327 * and all the cleanup and final put for dio_bio (through
8328 * dio_end_io()).
8329 */
8330 dip = NULL;
8331 io_bio = NULL;
8332 } else {
8333 if (write) {
8334 struct btrfs_ordered_extent *ordered;
8335
8336 ordered = btrfs_lookup_ordered_extent(inode,
8337 file_offset);
8338 set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
8339 /*
8340 * Decrements our ref on the ordered extent and removes
8341 * the ordered extent from the inode's ordered tree,
8342 * doing all the proper resource cleanup such as for the
8343 * reserved space and waking up any waiters for this
8344 * ordered extent (through btrfs_remove_ordered_extent).
8345 */
8346 btrfs_finish_ordered_io(ordered);
8347 } else {
8348 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8349 file_offset + dio_bio->bi_iter.bi_size - 1);
8350 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008351 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008352 /*
8353 * Releases and cleans up our dio_bio, no need to bio_put()
8354 * nor bio_endio()/bio_io_error() against dio_bio.
8355 */
8356 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008357 }
Filipe Manana61de7182015-07-01 12:13:10 +01008358 if (io_bio)
8359 bio_put(io_bio);
8360 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008361}
8362
Omar Sandoval6f673762015-03-16 04:33:52 -07008363static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008364 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008365{
8366 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008367 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008368 unsigned blocksize_mask = root->sectorsize - 1;
8369 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008370
8371 if (offset & blocksize_mask)
8372 goto out;
8373
Al Viro28060d52014-03-22 05:15:17 -04008374 if (iov_iter_alignment(iter) & blocksize_mask)
8375 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008376
Al Viro28060d52014-03-22 05:15:17 -04008377 /* If this is a write we don't need to check anymore */
Omar Sandoval6f673762015-03-16 04:33:52 -07008378 if (iov_iter_rw(iter) == WRITE)
Al Viro28060d52014-03-22 05:15:17 -04008379 return 0;
8380 /*
8381 * Check to make sure we don't have duplicate iov_base's in this
8382 * iovec, if so return EINVAL, otherwise we'll get csum errors
8383 * when reading back.
8384 */
8385 for (seg = 0; seg < iter->nr_segs; seg++) {
8386 for (i = seg + 1; i < iter->nr_segs; i++) {
8387 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008388 goto out;
8389 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008390 }
8391 retval = 0;
8392out:
8393 return retval;
8394}
Josef Bacikeb838e72012-07-31 16:28:48 -04008395
Omar Sandoval22c61862015-03-16 04:33:53 -07008396static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
8397 loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04008398{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008399 struct file *file = iocb->ki_filp;
8400 struct inode *inode = file->f_mapping->host;
chandan50745b02015-08-28 21:10:13 +05308401 struct btrfs_root *root = BTRFS_I(inode)->root;
8402 struct btrfs_dio_data dio_data = { 0 };
Miao Xie09348562013-02-07 10:12:07 +00008403 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008404 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008405 bool wakeup = true;
8406 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008407 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008408
Omar Sandoval6f673762015-03-16 04:33:52 -07008409 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008410 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008411
Jens Axboefe0f07d2015-04-15 17:05:48 -06008412 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008413 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008414
Josef Bacik0e267c42013-07-02 10:38:02 -04008415 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008416 * The generic stuff only does filemap_write_and_wait_range, which
8417 * isn't enough if we've written compressed pages to this area, so
8418 * we need to flush the dirty pages again to make absolutely sure
8419 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008420 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008421 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008422 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8423 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008424 filemap_fdatawrite_range(inode->i_mapping, offset,
8425 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008426
Omar Sandoval6f673762015-03-16 04:33:52 -07008427 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008428 /*
8429 * If the write DIO is beyond the EOF, we need update
8430 * the isize, but it is protected by i_mutex. So we can
8431 * not unlock the i_mutex at this case.
8432 */
8433 if (offset + count <= inode->i_size) {
8434 mutex_unlock(&inode->i_mutex);
8435 relock = true;
8436 }
Miao Xie09348562013-02-07 10:12:07 +00008437 ret = btrfs_delalloc_reserve_space(inode, count);
8438 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008439 goto out;
chandan50745b02015-08-28 21:10:13 +05308440 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008441 BTRFS_MAX_EXTENT_SIZE - 1,
8442 BTRFS_MAX_EXTENT_SIZE);
8443
8444 /*
8445 * We need to know how many extents we reserved so that we can
8446 * do the accounting properly if we go over the number we
8447 * originally calculated. Abuse current->journal_info for this.
8448 */
chandan50745b02015-08-28 21:10:13 +05308449 dio_data.reserve = round_up(count, root->sectorsize);
8450 current->journal_info = &dio_data;
David Sterbaee39b432014-09-30 01:33:33 +02008451 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8452 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008453 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008454 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8455 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008456 }
8457
Omar Sandoval17f8c842015-03-16 04:33:50 -07008458 ret = __blockdev_direct_IO(iocb, inode,
8459 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
8460 iter, offset, btrfs_get_blocks_direct, NULL,
8461 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008462 if (iov_iter_rw(iter) == WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008463 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008464 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308465 if (dio_data.reserve)
8466 btrfs_delalloc_release_space(inode,
8467 dio_data.reserve);
Liu Boddba1bf2015-06-17 16:59:58 +08008468 } else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00008469 btrfs_delalloc_release_space(inode,
8470 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008471 }
Miao Xie38851cc2013-02-08 07:04:11 +00008472out:
Miao Xie2e60a512013-02-08 07:01:08 +00008473 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008474 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008475 if (relock)
8476 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008477
8478 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008479}
8480
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008481#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8482
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008483static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8484 __u64 start, __u64 len)
8485{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008486 int ret;
8487
8488 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8489 if (ret)
8490 return ret;
8491
Chris Masonec29ed52011-02-23 16:23:20 -05008492 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008493}
8494
Chris Mason9ebefb182007-06-15 13:50:00 -04008495int btrfs_readpage(struct file *file, struct page *page)
8496{
Chris Masond1310b22008-01-24 16:13:08 -05008497 struct extent_io_tree *tree;
8498 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008499 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008500}
Chris Mason1832a6d2007-12-21 16:27:21 -05008501
Chris Mason39279cc2007-06-12 06:35:45 -04008502static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8503{
Chris Masond1310b22008-01-24 16:13:08 -05008504 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04008505
8506
8507 if (current->flags & PF_MEMALLOC) {
8508 redirty_page_for_writepage(wbc, page);
8509 unlock_page(page);
8510 return 0;
8511 }
Chris Masond1310b22008-01-24 16:13:08 -05008512 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008513 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8514}
Chris Mason39279cc2007-06-12 06:35:45 -04008515
Eric Sandeen48a3b632013-04-25 20:41:01 +00008516static int btrfs_writepages(struct address_space *mapping,
8517 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008518{
Chris Masond1310b22008-01-24 16:13:08 -05008519 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008520
Chris Masond1310b22008-01-24 16:13:08 -05008521 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008522 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8523}
8524
Chris Mason3ab2fb52007-11-08 10:59:22 -05008525static int
8526btrfs_readpages(struct file *file, struct address_space *mapping,
8527 struct list_head *pages, unsigned nr_pages)
8528{
Chris Masond1310b22008-01-24 16:13:08 -05008529 struct extent_io_tree *tree;
8530 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008531 return extent_readpages(tree, mapping, pages, nr_pages,
8532 btrfs_get_extent);
8533}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008534static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008535{
Chris Masond1310b22008-01-24 16:13:08 -05008536 struct extent_io_tree *tree;
8537 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008538 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008539
Chris Masond1310b22008-01-24 16:13:08 -05008540 tree = &BTRFS_I(page->mapping->host)->io_tree;
8541 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008542 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008543 if (ret == 1) {
8544 ClearPagePrivate(page);
8545 set_page_private(page, 0);
8546 page_cache_release(page);
8547 }
8548 return ret;
8549}
Chris Mason39279cc2007-06-12 06:35:45 -04008550
Chris Masone6dcd2d2008-07-17 12:53:50 -04008551static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8552{
Chris Mason98509cf2008-09-11 15:51:43 -04008553 if (PageWriteback(page) || PageDirty(page))
8554 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008555 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008556}
8557
Lukas Czernerd47992f2013-05-21 23:17:23 -04008558static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8559 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008560{
Josef Bacik5fd02042012-05-02 14:00:54 -04008561 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008562 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008563 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008564 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008565 u64 page_start = page_offset(page);
8566 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008567 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008568
Chris Mason8b62b722009-09-02 16:53:46 -04008569 /*
8570 * we have the page locked, so new writeback can't start,
8571 * and the dirty bit won't be cleared while we are here.
8572 *
8573 * Wait for IO on this page so that we can safely clear
8574 * the PagePrivate2 bit and do ordered accounting
8575 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008576 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008577
Josef Bacik5fd02042012-05-02 14:00:54 -04008578 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008579 if (offset) {
8580 btrfs_releasepage(page, GFP_NOFS);
8581 return;
8582 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008583
8584 if (!inode_evicting)
8585 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8586 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008587 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008588 /*
8589 * IO on this page will never be started, so we need
8590 * to account for any ordered extents now
8591 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008592 if (!inode_evicting)
8593 clear_extent_bit(tree, page_start, page_end,
8594 EXTENT_DIRTY | EXTENT_DELALLOC |
8595 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8596 EXTENT_DEFRAG, 1, 0, &cached_state,
8597 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008598 /*
8599 * whoever cleared the private bit is responsible
8600 * for the finish_ordered_io
8601 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008602 if (TestClearPagePrivate2(page)) {
8603 struct btrfs_ordered_inode_tree *tree;
8604 u64 new_len;
8605
8606 tree = &BTRFS_I(inode)->ordered_tree;
8607
8608 spin_lock_irq(&tree->lock);
8609 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8610 new_len = page_start - ordered->file_offset;
8611 if (new_len < ordered->truncated_len)
8612 ordered->truncated_len = new_len;
8613 spin_unlock_irq(&tree->lock);
8614
8615 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8616 page_start,
8617 PAGE_CACHE_SIZE, 1))
8618 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008619 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008620 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008621 if (!inode_evicting) {
8622 cached_state = NULL;
8623 lock_extent_bits(tree, page_start, page_end, 0,
8624 &cached_state);
8625 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008626 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008627
8628 if (!inode_evicting) {
8629 clear_extent_bit(tree, page_start, page_end,
8630 EXTENT_LOCKED | EXTENT_DIRTY |
8631 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8632 EXTENT_DEFRAG, 1, 1,
8633 &cached_state, GFP_NOFS);
8634
8635 __btrfs_releasepage(page, GFP_NOFS);
8636 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008637
Chris Mason4a096752008-07-21 10:29:44 -04008638 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008639 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008640 ClearPagePrivate(page);
8641 set_page_private(page, 0);
8642 page_cache_release(page);
8643 }
Chris Mason39279cc2007-06-12 06:35:45 -04008644}
8645
Chris Mason9ebefb182007-06-15 13:50:00 -04008646/*
8647 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8648 * called from a page fault handler when a page is first dirtied. Hence we must
8649 * be careful to check for EOF conditions here. We set the page up correctly
8650 * for a written page which means we get ENOSPC checking when writing into
8651 * holes and correct delalloc and unwritten extent mapping on filesystems that
8652 * support these features.
8653 *
8654 * We are not allowed to take the i_mutex here so we have to play games to
8655 * protect against truncate races as the page could now be beyond EOF. Because
8656 * vmtruncate() writes the inode size before removing pages, once we have the
8657 * page lock we can determine safely if the page is beyond EOF. If it is not
8658 * beyond EOF, then the page is guaranteed safe against truncation until we
8659 * unlock the page.
8660 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008661int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008662{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008663 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008664 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008665 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008666 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8667 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008668 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008669 char *kaddr;
8670 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008671 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008672 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008673 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008674 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008675 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008676
Jan Karab2b5ef52012-06-12 16:20:45 +02008677 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04008678 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008679 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008680 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008681 reserved = 1;
8682 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008683 if (ret) {
8684 if (ret == -ENOMEM)
8685 ret = VM_FAULT_OOM;
8686 else /* -ENOSPC, -EIO, etc */
8687 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008688 if (reserved)
8689 goto out;
8690 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008691 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008692
Nick Piggin56a76f82009-03-31 15:23:23 -07008693 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008694again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008695 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008696 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008697 page_start = page_offset(page);
8698 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04008699
Chris Mason9ebefb182007-06-15 13:50:00 -04008700 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008701 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008702 /* page got truncated out from underneath us */
8703 goto out_unlock;
8704 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008705 wait_on_page_writeback(page);
8706
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008707 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008708 set_page_extent_mapped(page);
8709
Chris Masoneb84ae02008-07-17 13:53:27 -04008710 /*
8711 * we can't set the delalloc bits if there are pending ordered
8712 * extents. Drop our locks and wait for them to finish
8713 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008714 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8715 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008716 unlock_extent_cached(io_tree, page_start, page_end,
8717 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008718 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008719 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008720 btrfs_put_ordered_extent(ordered);
8721 goto again;
8722 }
8723
Josef Bacikfbf19082009-10-01 17:10:23 -04008724 /*
8725 * XXX - page_mkwrite gets called every time the page is dirtied, even
8726 * if it was already dirty, so for space accounting reasons we need to
8727 * clear any delalloc bits for the range we are fixing to save. There
8728 * is probably a better way to do this, but for now keep consistent with
8729 * prepare_pages in the normal write path.
8730 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008731 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008732 EXTENT_DIRTY | EXTENT_DELALLOC |
8733 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008734 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008735
Josef Bacik2ac55d42010-02-03 19:33:23 +00008736 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8737 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008738 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008739 unlock_extent_cached(io_tree, page_start, page_end,
8740 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008741 ret = VM_FAULT_SIGBUS;
8742 goto out_unlock;
8743 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008744 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008745
8746 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008747 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008748 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008749 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008750 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008751
Chris Masone6dcd2d2008-07-17 12:53:50 -04008752 if (zero_start != PAGE_CACHE_SIZE) {
8753 kaddr = kmap(page);
8754 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8755 flush_dcache_page(page);
8756 kunmap(page);
8757 }
Chris Mason247e7432008-07-17 12:53:51 -04008758 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008759 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008760 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008761
Chris Mason257c62e2009-10-13 13:21:08 -04008762 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8763 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008764 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008765
Josef Bacik2ac55d42010-02-03 19:33:23 +00008766 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008767
8768out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008769 if (!ret) {
8770 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008771 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008772 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008773 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008774out:
Josef Bacikec39e182012-01-12 19:10:12 -05008775 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008776out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008777 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008778 return ret;
8779}
8780
Josef Bacika41ad392011-01-31 15:30:16 -05008781static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008782{
8783 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008784 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008785 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008786 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008787 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008788 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008789 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008790
Josef Bacik0ef8b722013-10-25 16:13:35 -04008791 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8792 (u64)-1);
8793 if (ret)
8794 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008795
Josef Bacikfcb80c22011-05-03 10:40:22 -04008796 /*
8797 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8798 * 3 things going on here
8799 *
8800 * 1) We need to reserve space for our orphan item and the space to
8801 * delete our orphan item. Lord knows we don't want to have a dangling
8802 * orphan item because we didn't reserve space to remove it.
8803 *
8804 * 2) We need to reserve space to update our inode.
8805 *
8806 * 3) We need to have something to cache all the space that is going to
8807 * be free'd up by the truncate operation, but also have some slack
8808 * space reserved in case it uses space during the truncate (thank you
8809 * very much snapshotting).
8810 *
8811 * And we need these to all be seperate. The fact is we can use alot of
8812 * space doing the truncate, and we have no earthly idea how much space
8813 * we will use, so we need the truncate reservation to be seperate so it
8814 * doesn't end up using space reserved for updating the inode or
8815 * removing the orphan item. We also need to be able to stop the
8816 * transaction and start a new one, which means we need to be able to
8817 * update the inode several times, and we have no idea of knowing how
8818 * many times that will be, so we can't just reserve 1 item for the
8819 * entirety of the opration, so that has to be done seperately as well.
8820 * Then there is the orphan item, which does indeed need to be held on
8821 * to for the whole operation, and we need nobody to touch this reserved
8822 * space except the orphan code.
8823 *
8824 * So that leaves us with
8825 *
8826 * 1) root->orphan_block_rsv - for the orphan deletion.
8827 * 2) rsv - for the truncate reservation, which we will steal from the
8828 * transaction reservation.
8829 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8830 * updating the inode.
8831 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008832 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008833 if (!rsv)
8834 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008835 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008836 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008837
Josef Bacik907cbce2011-08-08 13:46:15 -04008838 /*
Josef Bacik07127182011-08-19 10:29:59 -04008839 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008840 * 1 for updating the inode.
8841 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008842 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008843 if (IS_ERR(trans)) {
8844 err = PTR_ERR(trans);
8845 goto out;
8846 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008847
Josef Bacik907cbce2011-08-08 13:46:15 -04008848 /* Migrate the slack space for the truncate to our reserve */
8849 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8850 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008851 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008852
Chris Mason5a3f23d2009-03-31 13:27:11 -04008853 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008854 * So if we truncate and then write and fsync we normally would just
8855 * write the extents that changed, which is a problem if we need to
8856 * first truncate that entire inode. So set this flag so we write out
8857 * all of the extents in the inode to the sync log so we're completely
8858 * safe.
8859 */
8860 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008861 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008862
Yan, Zheng80825102009-11-12 09:35:36 +00008863 while (1) {
8864 ret = btrfs_truncate_inode_items(trans, root, inode,
8865 inode->i_size,
8866 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08008867 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05008868 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008869 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008870 }
Chris Mason39279cc2007-06-12 06:35:45 -04008871
Josef Bacikfcb80c22011-05-03 10:40:22 -04008872 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008873 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008874 if (ret) {
8875 err = ret;
8876 break;
8877 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008878
Yan, Zheng80825102009-11-12 09:35:36 +00008879 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008880 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008881
8882 trans = btrfs_start_transaction(root, 2);
8883 if (IS_ERR(trans)) {
8884 ret = err = PTR_ERR(trans);
8885 trans = NULL;
8886 break;
8887 }
8888
8889 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8890 rsv, min_size);
8891 BUG_ON(ret); /* shouldn't happen */
8892 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008893 }
8894
8895 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008896 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008897 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008898 if (ret)
8899 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008900 }
8901
Chris Mason917c16b2011-11-08 14:49:59 -05008902 if (trans) {
8903 trans->block_rsv = &root->fs_info->trans_block_rsv;
8904 ret = btrfs_update_inode(trans, root, inode);
8905 if (ret && !err)
8906 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008907
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008908 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008909 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008910 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008911
8912out:
8913 btrfs_free_block_rsv(root, rsv);
8914
Josef Bacik3893e332011-01-31 16:03:11 -05008915 if (ret && !err)
8916 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008917
Josef Bacik3893e332011-01-31 16:03:11 -05008918 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008919}
8920
Sven Wegener3b963622008-06-09 21:57:42 -04008921/*
Chris Masond352ac62008-09-29 15:18:18 -04008922 * create a new subvolume directory/inode (helper for the ioctl).
8923 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008924int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008925 struct btrfs_root *new_root,
8926 struct btrfs_root *parent_root,
8927 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008928{
Chris Mason39279cc2007-06-12 06:35:45 -04008929 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008930 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008931 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008932
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008933 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8934 new_dirid, new_dirid,
8935 S_IFDIR | (~current_umask() & S_IRWXUGO),
8936 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008937 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008938 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008939 inode->i_op = &btrfs_dir_inode_operations;
8940 inode->i_fop = &btrfs_dir_file_operations;
8941
Miklos Szeredibfe86842011-10-28 14:13:29 +02008942 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008943 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008944 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008945
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008946 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8947 if (err)
8948 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008949 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008950 new_root->root_key.objectid, err);
8951
Yan, Zheng76dda932009-09-21 16:00:26 -04008952 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008953
Yan, Zheng76dda932009-09-21 16:00:26 -04008954 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008955 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008956}
8957
Chris Mason39279cc2007-06-12 06:35:45 -04008958struct inode *btrfs_alloc_inode(struct super_block *sb)
8959{
8960 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008961 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008962
8963 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8964 if (!ei)
8965 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008966
8967 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008968 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008969 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008970 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008971 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008972 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008973 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008974 ei->disk_i_size = 0;
8975 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008976 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008977 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008978 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008979 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008980 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008981
Josef Bacik9e0baf62011-07-15 15:16:44 +00008982 spin_lock_init(&ei->lock);
8983 ei->outstanding_extents = 0;
8984 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008985
Josef Bacik72ac3c02012-05-23 14:13:11 -04008986 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008987 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008988
Miao Xie16cdcec2011-04-22 18:12:22 +08008989 ei->delayed_node = NULL;
8990
chandan r9cc97d62012-07-04 12:48:07 +05308991 ei->i_otime.tv_sec = 0;
8992 ei->i_otime.tv_nsec = 0;
8993
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008994 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008995 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008996 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8997 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008998 ei->io_tree.track_uptodate = 1;
8999 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009000 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009001 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009002 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009003 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009004 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009005 RB_CLEAR_NODE(&ei->rb_node);
9006
9007 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009008}
9009
Josef Bacikaaedb552013-10-11 14:44:09 -04009010#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9011void btrfs_test_destroy_inode(struct inode *inode)
9012{
9013 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9014 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9015}
9016#endif
9017
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009018static void btrfs_i_callback(struct rcu_head *head)
9019{
9020 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009021 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9022}
9023
Chris Mason39279cc2007-06-12 06:35:45 -04009024void btrfs_destroy_inode(struct inode *inode)
9025{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009026 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009027 struct btrfs_root *root = BTRFS_I(inode)->root;
9028
Al Virob3d9b7a2012-06-09 13:51:19 -04009029 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009030 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009031 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9032 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009033 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9034 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009035 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009036
Chris Mason5a3f23d2009-03-31 13:27:11 -04009037 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009038 * This can happen where we create an inode, but somebody else also
9039 * created the same inode and we need to destroy the one we already
9040 * created.
9041 */
9042 if (!root)
9043 goto free;
9044
Josef Bacik8a35d952012-05-23 14:26:42 -04009045 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9046 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009047 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009048 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009049 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009050 }
Josef Bacik7b128762008-07-24 12:17:14 -04009051
Chris Masond3977122009-01-05 21:25:51 -05009052 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009053 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9054 if (!ordered)
9055 break;
9056 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009057 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009058 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009059 btrfs_remove_ordered_extent(inode, ordered);
9060 btrfs_put_ordered_extent(ordered);
9061 btrfs_put_ordered_extent(ordered);
9062 }
9063 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009064 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009065 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009066free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009067 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009068}
9069
Al Viro45321ac2010-06-07 13:43:19 -04009070int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009071{
9072 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009073
Naohiro Aota6379ef92013-06-06 09:56:34 +00009074 if (root == NULL)
9075 return 1;
9076
Liu Bofa6ac872013-02-20 14:10:23 +00009077 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009078 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009079 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009080 else
Al Viro45321ac2010-06-07 13:43:19 -04009081 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009082}
9083
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009084static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009085{
9086 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9087
9088 inode_init_once(&ei->vfs_inode);
9089}
9090
9091void btrfs_destroy_cachep(void)
9092{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009093 /*
9094 * Make sure all delayed rcu free inodes are flushed before we
9095 * destroy cache.
9096 */
9097 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04009098 if (btrfs_inode_cachep)
9099 kmem_cache_destroy(btrfs_inode_cachep);
9100 if (btrfs_trans_handle_cachep)
9101 kmem_cache_destroy(btrfs_trans_handle_cachep);
9102 if (btrfs_transaction_cachep)
9103 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009104 if (btrfs_path_cachep)
9105 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05009106 if (btrfs_free_space_cachep)
9107 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009108 if (btrfs_delalloc_work_cachep)
9109 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009110}
9111
9112int btrfs_init_cachep(void)
9113{
David Sterba837e1972012-09-07 03:00:48 -06009114 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009115 sizeof(struct btrfs_inode), 0,
9116 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009117 if (!btrfs_inode_cachep)
9118 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009119
David Sterba837e1972012-09-07 03:00:48 -06009120 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009121 sizeof(struct btrfs_trans_handle), 0,
9122 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009123 if (!btrfs_trans_handle_cachep)
9124 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009125
David Sterba837e1972012-09-07 03:00:48 -06009126 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009127 sizeof(struct btrfs_transaction), 0,
9128 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009129 if (!btrfs_transaction_cachep)
9130 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009131
David Sterba837e1972012-09-07 03:00:48 -06009132 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009133 sizeof(struct btrfs_path), 0,
9134 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009135 if (!btrfs_path_cachep)
9136 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009137
David Sterba837e1972012-09-07 03:00:48 -06009138 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009139 sizeof(struct btrfs_free_space), 0,
9140 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
9141 if (!btrfs_free_space_cachep)
9142 goto fail;
9143
Miao Xie8ccf6f192012-10-25 09:28:04 +00009144 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
9145 sizeof(struct btrfs_delalloc_work), 0,
9146 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
9147 NULL);
9148 if (!btrfs_delalloc_work_cachep)
9149 goto fail;
9150
Chris Mason39279cc2007-06-12 06:35:45 -04009151 return 0;
9152fail:
9153 btrfs_destroy_cachep();
9154 return -ENOMEM;
9155}
9156
9157static int btrfs_getattr(struct vfsmount *mnt,
9158 struct dentry *dentry, struct kstat *stat)
9159{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009160 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009161 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009162 u32 blocksize = inode->i_sb->s_blocksize;
9163
Chris Mason39279cc2007-06-12 06:35:45 -04009164 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009165 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05009166 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009167
9168 spin_lock(&BTRFS_I(inode)->lock);
9169 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9170 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009171 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009172 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009173 return 0;
9174}
9175
Chris Masond3977122009-01-05 21:25:51 -05009176static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9177 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009178{
9179 struct btrfs_trans_handle *trans;
9180 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009181 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009182 struct inode *new_inode = d_inode(new_dentry);
9183 struct inode *old_inode = d_inode(old_dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04009184 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009185 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009186 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009187 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009188 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009189
Li Zefan33345d012011-04-20 10:31:50 +08009190 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009191 return -EPERM;
9192
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009193 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009194 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009195 return -EXDEV;
9196
Li Zefan33345d012011-04-20 10:31:50 +08009197 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9198 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009199 return -ENOTEMPTY;
9200
Chris Mason39279cc2007-06-12 06:35:45 -04009201 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009202 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009203 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009204
9205
9206 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009207 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009208 new_dentry->d_name.name,
9209 new_dentry->d_name.len);
9210
9211 if (ret) {
9212 if (ret == -EEXIST) {
9213 /* we shouldn't get
9214 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309215 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009216 return ret;
9217 }
9218 } else {
9219 /* maybe -EOVERFLOW */
9220 return ret;
9221 }
9222 }
9223 ret = 0;
9224
Chris Mason5a3f23d2009-03-31 13:27:11 -04009225 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009226 * we're using rename to replace one file with another. Start IO on it
9227 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009228 */
Chris Mason8d875f92014-08-12 10:47:42 -07009229 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009230 filemap_flush(old_inode->i_mapping);
9231
Yan, Zheng76dda932009-09-21 16:00:26 -04009232 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009233 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009234 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009235 /*
9236 * We want to reserve the absolute worst case amount of items. So if
9237 * both inodes are subvols and we need to unlink them then that would
9238 * require 4 item modifications, but if they are both normal inodes it
9239 * would require 5 item modifications, so we'll assume their normal
9240 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9241 * should cover the worst case number of items we'll modify.
9242 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04009243 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009244 if (IS_ERR(trans)) {
9245 ret = PTR_ERR(trans);
9246 goto out_notrans;
9247 }
Chris Mason5f39d392007-10-15 16:14:19 -04009248
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009249 if (dest != root)
9250 btrfs_record_root_in_trans(trans, dest);
9251
Yan, Zhenga5719522009-09-24 09:17:31 -04009252 ret = btrfs_set_inode_index(new_dir, &index);
9253 if (ret)
9254 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009255
Miao Xie67de1172013-12-26 13:07:06 +08009256 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009257 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009258 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009259 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009260 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009261 ret = btrfs_insert_inode_ref(trans, dest,
9262 new_dentry->d_name.name,
9263 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009264 old_ino,
9265 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009266 if (ret)
9267 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009268 /*
9269 * this is an ugly little race, but the rename is required
9270 * to make sure that if we crash, the inode is either at the
9271 * old name or the new one. pinning the log transaction lets
9272 * us make sure we don't allow a log commit to come in after
9273 * we unlink the name but before we add the new name back in.
9274 */
9275 btrfs_pin_log_trans(root);
9276 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009277
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009278 inode_inc_iversion(old_dir);
9279 inode_inc_iversion(new_dir);
9280 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009281 old_dir->i_ctime = old_dir->i_mtime = ctime;
9282 new_dir->i_ctime = new_dir->i_mtime = ctime;
9283 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04009284
Chris Mason12fcfd22009-03-24 10:24:20 -04009285 if (old_dentry->d_parent != new_dentry->d_parent)
9286 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9287
Li Zefan33345d012011-04-20 10:31:50 +08009288 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009289 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9290 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9291 old_dentry->d_name.name,
9292 old_dentry->d_name.len);
9293 } else {
Al Viro92986792011-03-04 17:14:37 +00009294 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009295 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009296 old_dentry->d_name.name,
9297 old_dentry->d_name.len);
9298 if (!ret)
9299 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009300 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009301 if (ret) {
9302 btrfs_abort_transaction(trans, root, ret);
9303 goto out_fail;
9304 }
Chris Mason39279cc2007-06-12 06:35:45 -04009305
9306 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009307 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009308 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08009309 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009310 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9311 root_objectid = BTRFS_I(new_inode)->location.objectid;
9312 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9313 root_objectid,
9314 new_dentry->d_name.name,
9315 new_dentry->d_name.len);
9316 BUG_ON(new_inode->i_nlink == 0);
9317 } else {
9318 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009319 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009320 new_dentry->d_name.name,
9321 new_dentry->d_name.len);
9322 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009323 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009324 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009325 if (ret) {
9326 btrfs_abort_transaction(trans, root, ret);
9327 goto out_fail;
9328 }
Chris Mason39279cc2007-06-12 06:35:45 -04009329 }
Josef Bacikaec74772008-07-24 12:12:38 -04009330
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009331 ret = btrfs_add_link(trans, new_dir, old_inode,
9332 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009333 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009334 if (ret) {
9335 btrfs_abort_transaction(trans, root, ret);
9336 goto out_fail;
9337 }
Chris Mason39279cc2007-06-12 06:35:45 -04009338
Miao Xie67de1172013-12-26 13:07:06 +08009339 if (old_inode->i_nlink == 1)
9340 BTRFS_I(old_inode)->dir_index = index;
9341
Li Zefan33345d012011-04-20 10:31:50 +08009342 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04009343 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00009344 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009345 btrfs_end_log_trans(root);
9346 }
Chris Mason39279cc2007-06-12 06:35:45 -04009347out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009348 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009349out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009350 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009351 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009352
Chris Mason39279cc2007-06-12 06:35:45 -04009353 return ret;
9354}
9355
Miklos Szeredi80ace852014-07-23 15:15:32 +02009356static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9357 struct inode *new_dir, struct dentry *new_dentry,
9358 unsigned int flags)
9359{
9360 if (flags & ~RENAME_NOREPLACE)
9361 return -EINVAL;
9362
9363 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
9364}
9365
Miao Xie8ccf6f192012-10-25 09:28:04 +00009366static void btrfs_run_delalloc_work(struct btrfs_work *work)
9367{
9368 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009369 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009370
9371 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9372 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009373 inode = delalloc_work->inode;
9374 if (delalloc_work->wait) {
9375 btrfs_wait_ordered_range(inode, 0, (u64)-1);
9376 } else {
9377 filemap_flush(inode->i_mapping);
9378 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9379 &BTRFS_I(inode)->runtime_flags))
9380 filemap_flush(inode->i_mapping);
9381 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00009382
9383 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009384 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009385 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009386 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009387 complete(&delalloc_work->completion);
9388}
9389
9390struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
9391 int wait, int delay_iput)
9392{
9393 struct btrfs_delalloc_work *work;
9394
9395 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
9396 if (!work)
9397 return NULL;
9398
9399 init_completion(&work->completion);
9400 INIT_LIST_HEAD(&work->list);
9401 work->inode = inode;
9402 work->wait = wait;
9403 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009404 WARN_ON_ONCE(!inode);
9405 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9406 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009407
9408 return work;
9409}
9410
9411void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9412{
9413 wait_for_completion(&work->completion);
9414 kmem_cache_free(btrfs_delalloc_work_cachep, work);
9415}
9416
Chris Masond352ac62008-09-29 15:18:18 -04009417/*
9418 * some fairly slow code that needs optimization. This walks the list
9419 * of all the inodes with pending delalloc and forces them to disk.
9420 */
Miao Xie6c255e62014-03-06 13:55:01 +08009421static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9422 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009423{
Chris Masonea8c2812008-08-04 23:17:27 -04009424 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009425 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009426 struct btrfs_delalloc_work *work, *next;
9427 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009428 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009429 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009430
Miao Xie8ccf6f192012-10-25 09:28:04 +00009431 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009432 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009433
Miao Xie573bfb72014-03-06 13:55:03 +08009434 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009435 spin_lock(&root->delalloc_lock);
9436 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009437 while (!list_empty(&splice)) {
9438 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009439 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009440
Miao Xieeb73c1b2013-05-15 07:48:22 +00009441 list_move_tail(&binode->delalloc_inodes,
9442 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009443 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009444 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009445 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009446 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009447 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009448 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009449
9450 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009451 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009452 if (delay_iput)
9453 btrfs_add_delayed_iput(inode);
9454 else
9455 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009456 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009457 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009458 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009459 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009460 btrfs_queue_work(root->fs_info->flush_workers,
9461 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009462 ret++;
9463 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009464 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009465 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009466 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009467 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009468 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009469
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009470out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009471 list_for_each_entry_safe(work, next, &works, list) {
9472 list_del_init(&work->list);
9473 btrfs_wait_and_free_delalloc_work(work);
9474 }
9475
Miao Xieeb73c1b2013-05-15 07:48:22 +00009476 if (!list_empty_careful(&splice)) {
9477 spin_lock(&root->delalloc_lock);
9478 list_splice_tail(&splice, &root->delalloc_inodes);
9479 spin_unlock(&root->delalloc_lock);
9480 }
Miao Xie573bfb72014-03-06 13:55:03 +08009481 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009482 return ret;
9483}
9484
9485int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9486{
9487 int ret;
9488
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009489 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009490 return -EROFS;
9491
Miao Xie6c255e62014-03-06 13:55:01 +08009492 ret = __start_delalloc_inodes(root, delay_iput, -1);
9493 if (ret > 0)
9494 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009495 /*
9496 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009497 * we have to make sure the IO is actually started and that
9498 * ordered extents get created before we return
9499 */
9500 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009501 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009502 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009503 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009504 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9505 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009506 }
9507 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009508 return ret;
9509}
9510
Miao Xie6c255e62014-03-06 13:55:01 +08009511int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9512 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009513{
9514 struct btrfs_root *root;
9515 struct list_head splice;
9516 int ret;
9517
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009518 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009519 return -EROFS;
9520
9521 INIT_LIST_HEAD(&splice);
9522
Miao Xie573bfb72014-03-06 13:55:03 +08009523 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009524 spin_lock(&fs_info->delalloc_root_lock);
9525 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009526 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009527 root = list_first_entry(&splice, struct btrfs_root,
9528 delalloc_root);
9529 root = btrfs_grab_fs_root(root);
9530 BUG_ON(!root);
9531 list_move_tail(&root->delalloc_root,
9532 &fs_info->delalloc_roots);
9533 spin_unlock(&fs_info->delalloc_root_lock);
9534
Miao Xie6c255e62014-03-06 13:55:01 +08009535 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009536 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009537 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009538 goto out;
9539
Miao Xie6c255e62014-03-06 13:55:01 +08009540 if (nr != -1) {
9541 nr -= ret;
9542 WARN_ON(nr < 0);
9543 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009544 spin_lock(&fs_info->delalloc_root_lock);
9545 }
9546 spin_unlock(&fs_info->delalloc_root_lock);
9547
Miao Xie6c255e62014-03-06 13:55:01 +08009548 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009549 atomic_inc(&fs_info->async_submit_draining);
9550 while (atomic_read(&fs_info->nr_async_submits) ||
9551 atomic_read(&fs_info->async_delalloc_pages)) {
9552 wait_event(fs_info->async_submit_wait,
9553 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9554 atomic_read(&fs_info->async_delalloc_pages) == 0));
9555 }
9556 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009557out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009558 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009559 spin_lock(&fs_info->delalloc_root_lock);
9560 list_splice_tail(&splice, &fs_info->delalloc_roots);
9561 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009562 }
Miao Xie573bfb72014-03-06 13:55:03 +08009563 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009564 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009565}
9566
Chris Mason39279cc2007-06-12 06:35:45 -04009567static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9568 const char *symname)
9569{
9570 struct btrfs_trans_handle *trans;
9571 struct btrfs_root *root = BTRFS_I(dir)->root;
9572 struct btrfs_path *path;
9573 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009574 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009575 int err;
9576 int drop_inode = 0;
9577 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309578 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009579 int name_len;
9580 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009581 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009582 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009583 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009584
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009585 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009586 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9587 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009588
Josef Bacik9ed74f22009-09-11 16:12:44 -04009589 /*
9590 * 2 items for inode item and ref
9591 * 2 items for dir items
9592 * 1 item for xattr if selinux is on
9593 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009594 trans = btrfs_start_transaction(root, 5);
9595 if (IS_ERR(trans))
9596 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009597
Li Zefan581bb052011-04-20 10:06:11 +08009598 err = btrfs_find_free_ino(root, &objectid);
9599 if (err)
9600 goto out_unlock;
9601
Josef Bacikaec74772008-07-24 12:12:38 -04009602 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009603 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04009604 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009605 if (IS_ERR(inode)) {
9606 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009607 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009608 }
Chris Mason39279cc2007-06-12 06:35:45 -04009609
Casey Schauflerad19db72011-12-15 10:09:07 -05009610 /*
9611 * If the active LSM wants to access the inode during
9612 * d_instantiate it needs these. Smack checks to see
9613 * if the filesystem supports xattrs by looking at the
9614 * ops vector.
9615 */
9616 inode->i_fop = &btrfs_file_operations;
9617 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009618 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009619 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9620
9621 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9622 if (err)
9623 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009624
Josef Bacika1b075d2010-11-19 20:36:11 +00009625 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009626 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009627 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009628
9629 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009630 if (!path) {
9631 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009632 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009633 }
Li Zefan33345d012011-04-20 10:31:50 +08009634 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009635 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009636 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009637 datasize = btrfs_file_extent_calc_inline_size(name_len);
9638 err = btrfs_insert_empty_item(trans, root, path, &key,
9639 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009640 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009641 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009642 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009643 }
Chris Mason5f39d392007-10-15 16:14:19 -04009644 leaf = path->nodes[0];
9645 ei = btrfs_item_ptr(leaf, path->slots[0],
9646 struct btrfs_file_extent_item);
9647 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9648 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009649 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009650 btrfs_set_file_extent_encryption(leaf, ei, 0);
9651 btrfs_set_file_extent_compression(leaf, ei, 0);
9652 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9653 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9654
Chris Mason39279cc2007-06-12 06:35:45 -04009655 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009656 write_extent_buffer(leaf, symname, ptr, name_len);
9657 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009658 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009659
Chris Mason39279cc2007-06-12 06:35:45 -04009660 inode->i_op = &btrfs_symlink_inode_operations;
9661 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -04009662 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009663 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009664 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009665 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009666 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009667 goto out_unlock_inode;
9668 }
9669
9670 unlock_new_inode(inode);
9671 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009672
9673out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009674 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009675 if (drop_inode) {
9676 inode_dec_link_count(inode);
9677 iput(inode);
9678 }
Liu Bob53d3f52012-11-14 14:34:34 +00009679 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009680 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009681
9682out_unlock_inode:
9683 drop_inode = 1;
9684 unlock_new_inode(inode);
9685 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009686}
Chris Mason16432982008-04-10 10:23:21 -04009687
Josef Bacik0af3d002010-06-21 14:48:16 -04009688static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9689 u64 start, u64 num_bytes, u64 min_size,
9690 loff_t actual_len, u64 *alloc_hint,
9691 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009692{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009693 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9694 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009695 struct btrfs_root *root = BTRFS_I(inode)->root;
9696 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009697 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009698 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009699 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009700 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009701 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009702
Josef Bacik0af3d002010-06-21 14:48:16 -04009703 if (trans)
9704 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009705 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009706 if (own_trans) {
9707 trans = btrfs_start_transaction(root, 3);
9708 if (IS_ERR(trans)) {
9709 ret = PTR_ERR(trans);
9710 break;
9711 }
Yan Zhengd899e052008-10-30 14:25:28 -04009712 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009713
Chris Mason154ea282013-03-05 11:11:26 -05009714 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9715 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009716 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009717 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009718 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009719 if (own_trans)
9720 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009721 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009722 }
9723
Yan Zhengd899e052008-10-30 14:25:28 -04009724 ret = insert_reserved_file_extent(trans, inode,
9725 cur_offset, ins.objectid,
9726 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009727 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009728 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009729 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009730 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009731 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009732 btrfs_abort_transaction(trans, root, ret);
9733 if (own_trans)
9734 btrfs_end_transaction(trans, root);
9735 break;
9736 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009737
Chris Masona1ed8352009-09-11 12:27:37 -04009738 btrfs_drop_extent_cache(inode, cur_offset,
9739 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009740
Josef Bacik5dc562c2012-08-17 13:14:17 -04009741 em = alloc_extent_map();
9742 if (!em) {
9743 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9744 &BTRFS_I(inode)->runtime_flags);
9745 goto next;
9746 }
9747
9748 em->start = cur_offset;
9749 em->orig_start = cur_offset;
9750 em->len = ins.offset;
9751 em->block_start = ins.objectid;
9752 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009753 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009754 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009755 em->bdev = root->fs_info->fs_devices->latest_bdev;
9756 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9757 em->generation = trans->transid;
9758
9759 while (1) {
9760 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009761 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009762 write_unlock(&em_tree->lock);
9763 if (ret != -EEXIST)
9764 break;
9765 btrfs_drop_extent_cache(inode, cur_offset,
9766 cur_offset + ins.offset - 1,
9767 0);
9768 }
9769 free_extent_map(em);
9770next:
Yan Zhengd899e052008-10-30 14:25:28 -04009771 num_bytes -= ins.offset;
9772 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009773 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009774
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009775 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009776 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009777 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009778 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009779 (actual_len > inode->i_size) &&
9780 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009781 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009782 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009783 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009784 i_size = cur_offset;
9785 i_size_write(inode, i_size);
9786 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009787 }
9788
Yan Zhengd899e052008-10-30 14:25:28 -04009789 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009790
9791 if (ret) {
9792 btrfs_abort_transaction(trans, root, ret);
9793 if (own_trans)
9794 btrfs_end_transaction(trans, root);
9795 break;
9796 }
Yan Zhengd899e052008-10-30 14:25:28 -04009797
Josef Bacik0af3d002010-06-21 14:48:16 -04009798 if (own_trans)
9799 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009800 }
Yan Zhengd899e052008-10-30 14:25:28 -04009801 return ret;
9802}
9803
Josef Bacik0af3d002010-06-21 14:48:16 -04009804int btrfs_prealloc_file_range(struct inode *inode, int mode,
9805 u64 start, u64 num_bytes, u64 min_size,
9806 loff_t actual_len, u64 *alloc_hint)
9807{
9808 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9809 min_size, actual_len, alloc_hint,
9810 NULL);
9811}
9812
9813int btrfs_prealloc_file_range_trans(struct inode *inode,
9814 struct btrfs_trans_handle *trans, int mode,
9815 u64 start, u64 num_bytes, u64 min_size,
9816 loff_t actual_len, u64 *alloc_hint)
9817{
9818 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9819 min_size, actual_len, alloc_hint, trans);
9820}
9821
Chris Masone6dcd2d2008-07-17 12:53:50 -04009822static int btrfs_set_page_dirty(struct page *page)
9823{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009824 return __set_page_dirty_nobuffers(page);
9825}
9826
Al Viro10556cb22011-06-20 19:28:19 -04009827static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009828{
Li Zefanb83cc962010-12-20 16:04:08 +08009829 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009830 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009831
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009832 if (mask & MAY_WRITE &&
9833 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9834 if (btrfs_root_readonly(root))
9835 return -EROFS;
9836 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9837 return -EACCES;
9838 }
Al Viro2830ba72011-06-20 19:16:29 -04009839 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009840}
Chris Mason39279cc2007-06-12 06:35:45 -04009841
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009842static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9843{
9844 struct btrfs_trans_handle *trans;
9845 struct btrfs_root *root = BTRFS_I(dir)->root;
9846 struct inode *inode = NULL;
9847 u64 objectid;
9848 u64 index;
9849 int ret = 0;
9850
9851 /*
9852 * 5 units required for adding orphan entry
9853 */
9854 trans = btrfs_start_transaction(root, 5);
9855 if (IS_ERR(trans))
9856 return PTR_ERR(trans);
9857
9858 ret = btrfs_find_free_ino(root, &objectid);
9859 if (ret)
9860 goto out;
9861
9862 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9863 btrfs_ino(dir), objectid, mode, &index);
9864 if (IS_ERR(inode)) {
9865 ret = PTR_ERR(inode);
9866 inode = NULL;
9867 goto out;
9868 }
9869
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009870 inode->i_fop = &btrfs_file_operations;
9871 inode->i_op = &btrfs_file_inode_operations;
9872
9873 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009874 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9875
Chris Masonb0d5d102014-09-08 13:08:51 -07009876 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9877 if (ret)
9878 goto out_inode;
9879
9880 ret = btrfs_update_inode(trans, root, inode);
9881 if (ret)
9882 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009883 ret = btrfs_orphan_add(trans, inode);
9884 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009885 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009886
Filipe Manana5762b5c2014-08-01 00:10:32 +01009887 /*
9888 * We set number of links to 0 in btrfs_new_inode(), and here we set
9889 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9890 * through:
9891 *
9892 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9893 */
9894 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009895 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009896 d_tmpfile(dentry, inode);
9897 mark_inode_dirty(inode);
9898
9899out:
9900 btrfs_end_transaction(trans, root);
9901 if (ret)
9902 iput(inode);
9903 btrfs_balance_delayed_items(root);
9904 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009905 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009906
9907out_inode:
9908 unlock_new_inode(inode);
9909 goto out;
9910
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009911}
9912
Filipe Mananab38ef712014-11-13 17:01:45 +00009913/* Inspired by filemap_check_errors() */
9914int btrfs_inode_check_errors(struct inode *inode)
9915{
9916 int ret = 0;
9917
9918 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
9919 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
9920 ret = -ENOSPC;
9921 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
9922 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
9923 ret = -EIO;
9924
9925 return ret;
9926}
9927
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009928static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009929 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009930 .lookup = btrfs_lookup,
9931 .create = btrfs_create,
9932 .unlink = btrfs_unlink,
9933 .link = btrfs_link,
9934 .mkdir = btrfs_mkdir,
9935 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009936 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009937 .symlink = btrfs_symlink,
9938 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009939 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009940 .setxattr = btrfs_setxattr,
9941 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009942 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009943 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009944 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009945 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009946 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009947 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009948 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009949};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009950static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009951 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009952 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009953 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009954 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009955 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009956};
Yan, Zheng76dda932009-09-21 16:00:26 -04009957
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009958static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009959 .llseek = generic_file_llseek,
9960 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009961 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009962 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009963#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009964 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009965#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009966 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009967 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009968};
9969
Chris Masond1310b22008-01-24 16:13:08 -05009970static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009971 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009972 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009973 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009974 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009975 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009976 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009977 .set_bit_hook = btrfs_set_bit_hook,
9978 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009979 .merge_extent_hook = btrfs_merge_extent_hook,
9980 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009981};
9982
Chris Mason35054392009-01-21 13:11:13 -05009983/*
9984 * btrfs doesn't support the bmap operation because swapfiles
9985 * use bmap to make a mapping of extents in the file. They assume
9986 * these extents won't change over the life of the file and they
9987 * use the bmap result to do IO directly to the drive.
9988 *
9989 * the btrfs bmap call would return logical addresses that aren't
9990 * suitable for IO and they also will change frequently as COW
9991 * operations happen. So, swapfile + btrfs == corruption.
9992 *
9993 * For now we're avoiding this by dropping bmap.
9994 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009995static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009996 .readpage = btrfs_readpage,
9997 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04009998 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009999 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010000 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010001 .invalidatepage = btrfs_invalidatepage,
10002 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010003 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010004 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010005};
10006
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010007static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010008 .readpage = btrfs_readpage,
10009 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010010 .invalidatepage = btrfs_invalidatepage,
10011 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010012};
10013
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010014static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010015 .getattr = btrfs_getattr,
10016 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010017 .setxattr = btrfs_setxattr,
10018 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010019 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010020 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -050010021 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010022 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010023 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010024 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010025 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010026};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010027static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010028 .getattr = btrfs_getattr,
10029 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010030 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010031 .setxattr = btrfs_setxattr,
10032 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -040010033 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010034 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010035 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010036 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010037 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010038};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010039static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010040 .readlink = generic_readlink,
10041 .follow_link = page_follow_link_light,
10042 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +000010043 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010044 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010045 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010046 .setxattr = btrfs_setxattr,
10047 .getxattr = btrfs_getxattr,
10048 .listxattr = btrfs_listxattr,
10049 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010050 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010051};
Yan, Zheng76dda932009-09-21 16:00:26 -040010052
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010053const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010054 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010055 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010056};