blob: 611b66d73e80ba0e5f97b4415595d20f8ae35e1a [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
Josef Bacik66657b32012-08-01 15:36:24 -04001099 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001100 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001101 waitqueue_active(&root->fs_info->async_submit_wait))
1102 wake_up(&root->fs_info->async_submit_wait);
1103
Chris Masond3977122009-01-05 21:25:51 -05001104 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001105 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001106}
Chris Masonc8b97812008-10-29 14:49:59 -04001107
Chris Mason771ed682008-11-06 22:02:51 -05001108static noinline void async_cow_free(struct btrfs_work *work)
1109{
1110 struct async_cow *async_cow;
1111 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001112 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001113 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001114 kfree(async_cow);
1115}
1116
1117static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1118 u64 start, u64 end, int *page_started,
1119 unsigned long *nr_written)
1120{
1121 struct async_cow *async_cow;
1122 struct btrfs_root *root = BTRFS_I(inode)->root;
1123 unsigned long nr_pages;
1124 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001125 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001126
Chris Masona3429ab2009-10-08 12:30:20 -04001127 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1128 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001129 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001130 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001131 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001132 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001133 async_cow->root = root;
1134 async_cow->locked_page = locked_page;
1135 async_cow->start = start;
1136
Wang Shilongf79707b2014-07-17 11:44:09 +08001137 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1138 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001139 cur_end = end;
1140 else
1141 cur_end = min(end, start + 512 * 1024 - 1);
1142
1143 async_cow->end = cur_end;
1144 INIT_LIST_HEAD(&async_cow->extents);
1145
Liu Bo9e0af232014-08-15 23:36:53 +08001146 btrfs_init_work(&async_cow->work,
1147 btrfs_delalloc_helper,
1148 async_cow_start, async_cow_submit,
1149 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001150
Chris Mason771ed682008-11-06 22:02:51 -05001151 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1152 PAGE_CACHE_SHIFT;
1153 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1154
Qu Wenruoafe3d242014-02-28 10:46:07 +08001155 btrfs_queue_work(root->fs_info->delalloc_workers,
1156 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001157
1158 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1159 wait_event(root->fs_info->async_submit_wait,
1160 (atomic_read(&root->fs_info->async_delalloc_pages) <
1161 limit));
1162 }
1163
Chris Masond3977122009-01-05 21:25:51 -05001164 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001165 atomic_read(&root->fs_info->async_delalloc_pages)) {
1166 wait_event(root->fs_info->async_submit_wait,
1167 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1168 0));
1169 }
1170
1171 *nr_written += nr_pages;
1172 start = cur_end + 1;
1173 }
1174 *page_started = 1;
1175 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001176}
1177
Chris Masond3977122009-01-05 21:25:51 -05001178static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001179 u64 bytenr, u64 num_bytes)
1180{
1181 int ret;
1182 struct btrfs_ordered_sum *sums;
1183 LIST_HEAD(list);
1184
Yan Zheng07d400a2009-01-06 11:42:00 -05001185 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001186 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001187 if (ret == 0 && list_empty(&list))
1188 return 0;
1189
1190 while (!list_empty(&list)) {
1191 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1192 list_del(&sums->list);
1193 kfree(sums);
1194 }
1195 return 1;
1196}
1197
Chris Masond352ac62008-09-29 15:18:18 -04001198/*
1199 * when nowcow writeback call back. This checks for snapshots or COW copies
1200 * of the extents that exist in the file, and COWs the file as required.
1201 *
1202 * If no cow copies or snapshots exist, we write directly to the existing
1203 * blocks on disk
1204 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001205static noinline int run_delalloc_nocow(struct inode *inode,
1206 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001207 u64 start, u64 end, int *page_started, int force,
1208 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001209{
Chris Masonbe20aa92007-12-17 20:14:01 -05001210 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001211 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001212 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001213 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001214 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001215 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001216 u64 cow_start;
1217 u64 cur_offset;
1218 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001219 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001220 u64 disk_bytenr;
1221 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001222 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001223 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001224 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001225 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001226 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001227 int nocow;
1228 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001229 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001230 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001231
1232 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001233 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001234 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1235 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001236 EXTENT_DO_ACCOUNTING |
1237 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001238 PAGE_CLEAR_DIRTY |
1239 PAGE_SET_WRITEBACK |
1240 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001241 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001242 }
Li Zefan82d59022011-04-20 10:33:24 +08001243
Liu Bo83eea1f2012-07-10 05:28:39 -06001244 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001245
1246 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001247 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001248 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001249 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001250
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001251 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001252 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1253 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001254 EXTENT_DO_ACCOUNTING |
1255 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001256 PAGE_CLEAR_DIRTY |
1257 PAGE_SET_WRITEBACK |
1258 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001259 btrfs_free_path(path);
1260 return PTR_ERR(trans);
1261 }
1262
Josef Bacik74b21072011-04-13 12:02:53 -04001263 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001264
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 cow_start = (u64)-1;
1266 cur_offset = start;
1267 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001268 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001270 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001271 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1273 leaf = path->nodes[0];
1274 btrfs_item_key_to_cpu(leaf, &found_key,
1275 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001276 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001277 found_key.type == BTRFS_EXTENT_DATA_KEY)
1278 path->slots[0]--;
1279 }
1280 check_prev = 0;
1281next_slot:
1282 leaf = path->nodes[0];
1283 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1284 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001285 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001286 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 if (ret > 0)
1288 break;
1289 leaf = path->nodes[0];
1290 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001291
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 nocow = 0;
1293 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001294 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001296
Li Zefan33345d012011-04-20 10:31:50 +08001297 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001298 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1299 found_key.offset > end)
1300 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001301
Yan Zheng80ff3852008-10-30 14:20:02 -04001302 if (found_key.offset > cur_offset) {
1303 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001304 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 goto out_check;
1306 }
Chris Masonc31f8832008-01-08 15:46:31 -05001307
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 fi = btrfs_item_ptr(leaf, path->slots[0],
1309 struct btrfs_file_extent_item);
1310 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001311
Josef Bacikcc95bef2013-04-04 14:31:27 -04001312 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001313 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1314 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001315 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001316 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 extent_end = found_key.offset +
1318 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001319 disk_num_bytes =
1320 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 if (extent_end <= start) {
1322 path->slots[0]++;
1323 goto next_slot;
1324 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001325 if (disk_bytenr == 0)
1326 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001327 if (btrfs_file_extent_compression(leaf, fi) ||
1328 btrfs_file_extent_encryption(leaf, fi) ||
1329 btrfs_file_extent_other_encoding(leaf, fi))
1330 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001331 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1332 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001333 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001334 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001335 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001336 found_key.offset -
1337 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001338 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001339 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001340 disk_bytenr += cur_offset - found_key.offset;
1341 num_bytes = min(end + 1, extent_end) - cur_offset;
1342 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001343 * if there are pending snapshots for this root,
1344 * we fall into common COW way.
1345 */
1346 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001347 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001348 if (!err)
1349 goto out_check;
1350 }
1351 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001352 * force cow if csum exists in the range.
1353 * this ensure that csum for a given extent are
1354 * either valid or do not exist.
1355 */
1356 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1357 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 nocow = 1;
1359 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1360 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001361 btrfs_file_extent_inline_len(leaf,
1362 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 extent_end = ALIGN(extent_end, root->sectorsize);
1364 } else {
1365 BUG_ON(1);
1366 }
1367out_check:
1368 if (extent_end <= start) {
1369 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001370 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001371 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001372 goto next_slot;
1373 }
1374 if (!nocow) {
1375 if (cow_start == (u64)-1)
1376 cow_start = cur_offset;
1377 cur_offset = extent_end;
1378 if (cur_offset > end)
1379 break;
1380 path->slots[0]++;
1381 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001382 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001383
David Sterbab3b4aa72011-04-21 01:20:15 +02001384 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001385 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001386 ret = cow_file_range(inode, locked_page,
1387 cow_start, found_key.offset - 1,
1388 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001389 if (ret) {
1390 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001391 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001392 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001393 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001394 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001395 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001396
Yan Zhengd899e052008-10-30 14:25:28 -04001397 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1398 struct extent_map *em;
1399 struct extent_map_tree *em_tree;
1400 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001401 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001402 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001403 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001404 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001405 em->len = num_bytes;
1406 em->block_len = num_bytes;
1407 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001408 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001409 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001410 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001411 em->mod_start = em->start;
1412 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001413 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001414 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001415 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001416 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001417 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001418 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001419 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001420 if (ret != -EEXIST) {
1421 free_extent_map(em);
1422 break;
1423 }
1424 btrfs_drop_extent_cache(inode, em->start,
1425 em->start + em->len - 1, 0);
1426 }
1427 type = BTRFS_ORDERED_PREALLOC;
1428 } else {
1429 type = BTRFS_ORDERED_NOCOW;
1430 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001431
1432 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001433 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001434 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001435
Yan, Zhengefa56462010-05-16 10:49:59 -04001436 if (root->root_key.objectid ==
1437 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1438 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1439 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001440 if (ret) {
1441 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001442 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001443 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001444 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001445 }
1446
Josef Bacikc2790a22013-07-29 11:20:47 -04001447 extent_clear_unlock_delalloc(inode, cur_offset,
1448 cur_offset + num_bytes - 1,
1449 locked_page, EXTENT_LOCKED |
1450 EXTENT_DELALLOC, PAGE_UNLOCK |
1451 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001452 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001453 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001454 cur_offset = extent_end;
1455 if (cur_offset > end)
1456 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001457 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001458 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001459
Josef Bacik17ca04a2012-05-31 15:58:55 -04001460 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001461 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001462 cur_offset = end;
1463 }
1464
Yan Zheng80ff3852008-10-30 14:20:02 -04001465 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001466 ret = cow_file_range(inode, locked_page, cow_start, end,
1467 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001468 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001469 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001470 }
1471
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001472error:
Miao Xiea698d0752012-09-20 01:51:59 -06001473 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001474 if (!ret)
1475 ret = err;
1476
Josef Bacik17ca04a2012-05-31 15:58:55 -04001477 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001478 extent_clear_unlock_delalloc(inode, cur_offset, end,
1479 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001480 EXTENT_DELALLOC | EXTENT_DEFRAG |
1481 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1482 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001483 PAGE_SET_WRITEBACK |
1484 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001485 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001486 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001487}
1488
Wang Shilong47059d92014-07-03 18:22:07 +08001489static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1490{
1491
1492 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1493 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1494 return 0;
1495
1496 /*
1497 * @defrag_bytes is a hint value, no spinlock held here,
1498 * if is not zero, it means the file is defragging.
1499 * Force cow if given extent needs to be defragged.
1500 */
1501 if (BTRFS_I(inode)->defrag_bytes &&
1502 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1503 EXTENT_DEFRAG, 0, NULL))
1504 return 1;
1505
1506 return 0;
1507}
1508
Chris Masond352ac62008-09-29 15:18:18 -04001509/*
1510 * extent_io.c call back to do delayed allocation processing
1511 */
Chris Masonc8b97812008-10-29 14:49:59 -04001512static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001513 u64 start, u64 end, int *page_started,
1514 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001515{
Chris Masonbe20aa92007-12-17 20:14:01 -05001516 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001517 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001518
Wang Shilong47059d92014-07-03 18:22:07 +08001519 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001520 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001521 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001522 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001523 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001524 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001525 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001526 ret = cow_file_range(inode, locked_page, start, end,
1527 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001528 } else {
1529 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1530 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001531 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001532 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001533 }
Chris Masonb888db22007-08-27 16:49:44 -04001534 return ret;
1535}
1536
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001537static void btrfs_split_extent_hook(struct inode *inode,
1538 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001539{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001540 u64 size;
1541
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001542 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001543 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001544 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001545
Josef Bacikdcab6a32015-02-11 15:08:59 -05001546 size = orig->end - orig->start + 1;
1547 if (size > BTRFS_MAX_EXTENT_SIZE) {
1548 u64 num_extents;
1549 u64 new_size;
1550
1551 /*
Josef Bacikba117212015-03-13 15:01:24 -04001552 * See the explanation in btrfs_merge_extent_hook, the same
1553 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001554 */
1555 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001556 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001557 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001558 new_size = split - orig->start;
1559 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1560 BTRFS_MAX_EXTENT_SIZE);
1561 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1562 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001563 return;
1564 }
1565
Josef Bacik9e0baf62011-07-15 15:16:44 +00001566 spin_lock(&BTRFS_I(inode)->lock);
1567 BTRFS_I(inode)->outstanding_extents++;
1568 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001569}
1570
1571/*
1572 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1573 * extents so we can keep track of new extents that are just merged onto old
1574 * extents, such as when we are doing sequential writes, so we can properly
1575 * account for the metadata space we'll need.
1576 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001577static void btrfs_merge_extent_hook(struct inode *inode,
1578 struct extent_state *new,
1579 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001580{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001581 u64 new_size, old_size;
1582 u64 num_extents;
1583
Josef Bacik9ed74f22009-09-11 16:12:44 -04001584 /* not delalloc, ignore it */
1585 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001586 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001587
Josef Bacik8461a3d2015-03-13 15:12:08 -04001588 if (new->start > other->start)
1589 new_size = new->end - other->start + 1;
1590 else
1591 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001592
1593 /* we're not bigger than the max, unreserve the space and go */
1594 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1595 spin_lock(&BTRFS_I(inode)->lock);
1596 BTRFS_I(inode)->outstanding_extents--;
1597 spin_unlock(&BTRFS_I(inode)->lock);
1598 return;
1599 }
1600
1601 /*
Josef Bacikba117212015-03-13 15:01:24 -04001602 * We have to add up either side to figure out how many extents were
1603 * accounted for before we merged into one big extent. If the number of
1604 * extents we accounted for is <= the amount we need for the new range
1605 * then we can return, otherwise drop. Think of it like this
1606 *
1607 * [ 4k][MAX_SIZE]
1608 *
1609 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1610 * need 2 outstanding extents, on one side we have 1 and the other side
1611 * we have 1 so they are == and we can return. But in this case
1612 *
1613 * [MAX_SIZE+4k][MAX_SIZE+4k]
1614 *
1615 * Each range on their own accounts for 2 extents, but merged together
1616 * they are only 3 extents worth of accounting, so we need to drop in
1617 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001618 */
Josef Bacikba117212015-03-13 15:01:24 -04001619 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001620 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1621 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001622 old_size = new->end - new->start + 1;
1623 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1624 BTRFS_MAX_EXTENT_SIZE);
1625
Josef Bacikdcab6a32015-02-11 15:08:59 -05001626 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001627 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001628 return;
1629
Josef Bacik9e0baf62011-07-15 15:16:44 +00001630 spin_lock(&BTRFS_I(inode)->lock);
1631 BTRFS_I(inode)->outstanding_extents--;
1632 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001633}
1634
Miao Xieeb73c1b2013-05-15 07:48:22 +00001635static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1636 struct inode *inode)
1637{
1638 spin_lock(&root->delalloc_lock);
1639 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1640 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1641 &root->delalloc_inodes);
1642 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1643 &BTRFS_I(inode)->runtime_flags);
1644 root->nr_delalloc_inodes++;
1645 if (root->nr_delalloc_inodes == 1) {
1646 spin_lock(&root->fs_info->delalloc_root_lock);
1647 BUG_ON(!list_empty(&root->delalloc_root));
1648 list_add_tail(&root->delalloc_root,
1649 &root->fs_info->delalloc_roots);
1650 spin_unlock(&root->fs_info->delalloc_root_lock);
1651 }
1652 }
1653 spin_unlock(&root->delalloc_lock);
1654}
1655
1656static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1657 struct inode *inode)
1658{
1659 spin_lock(&root->delalloc_lock);
1660 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1661 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1662 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1663 &BTRFS_I(inode)->runtime_flags);
1664 root->nr_delalloc_inodes--;
1665 if (!root->nr_delalloc_inodes) {
1666 spin_lock(&root->fs_info->delalloc_root_lock);
1667 BUG_ON(list_empty(&root->delalloc_root));
1668 list_del_init(&root->delalloc_root);
1669 spin_unlock(&root->fs_info->delalloc_root_lock);
1670 }
1671 }
1672 spin_unlock(&root->delalloc_lock);
1673}
1674
Chris Masond352ac62008-09-29 15:18:18 -04001675/*
1676 * extent_io.c set_bit_hook, used to track delayed allocation
1677 * bytes in this file, and to maintain the list of inodes that
1678 * have pending delalloc work to be done.
1679 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001680static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001681 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001682{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001683
Wang Shilong47059d92014-07-03 18:22:07 +08001684 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1685 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001686 /*
1687 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001688 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001689 * bit, which is only set or cleared with irqs on
1690 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001691 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001692 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001693 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001694 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001695
Josef Bacik9e0baf62011-07-15 15:16:44 +00001696 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001697 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001698 } else {
1699 spin_lock(&BTRFS_I(inode)->lock);
1700 BTRFS_I(inode)->outstanding_extents++;
1701 spin_unlock(&BTRFS_I(inode)->lock);
1702 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001703
Josef Bacik6a3891c2015-03-16 17:38:52 -04001704 /* For sanity tests */
1705 if (btrfs_test_is_dummy_root(root))
1706 return;
1707
Miao Xie963d6782013-01-29 10:10:51 +00001708 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1709 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001710 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001711 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001712 if (*bits & EXTENT_DEFRAG)
1713 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001714 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001715 &BTRFS_I(inode)->runtime_flags))
1716 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001717 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001718 }
Chris Mason291d6732008-01-29 15:55:23 -05001719}
1720
Chris Masond352ac62008-09-29 15:18:18 -04001721/*
1722 * extent_io.c clear_bit_hook, see set_bit_hook for why
1723 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001724static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001725 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001726 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001727{
Wang Shilong47059d92014-07-03 18:22:07 +08001728 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001729 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1730 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001731
1732 spin_lock(&BTRFS_I(inode)->lock);
1733 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1734 BTRFS_I(inode)->defrag_bytes -= len;
1735 spin_unlock(&BTRFS_I(inode)->lock);
1736
Chris Mason75eff682008-12-15 15:54:40 -05001737 /*
1738 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001739 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001740 * bit, which is only set or cleared with irqs on
1741 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001742 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001743 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001744 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001745
Josef Bacik9e0baf62011-07-15 15:16:44 +00001746 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001747 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001748 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1749 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001750 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001751 spin_unlock(&BTRFS_I(inode)->lock);
1752 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001753
Josef Bacikb6d08f02013-09-27 14:57:43 -04001754 /*
1755 * We don't reserve metadata space for space cache inodes so we
1756 * don't need to call dellalloc_release_metadata if there is an
1757 * error.
1758 */
1759 if (*bits & EXTENT_DO_ACCOUNTING &&
1760 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001761 btrfs_delalloc_release_metadata(inode, len);
1762
Josef Bacik6a3891c2015-03-16 17:38:52 -04001763 /* For sanity tests. */
1764 if (btrfs_test_is_dummy_root(root))
1765 return;
1766
Josef Bacik0cb59c92010-07-02 12:14:14 -04001767 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001768 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001769 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001770
Miao Xie963d6782013-01-29 10:10:51 +00001771 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1772 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001773 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001774 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001775 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001776 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001777 &BTRFS_I(inode)->runtime_flags))
1778 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001779 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001780 }
Chris Mason291d6732008-01-29 15:55:23 -05001781}
1782
Chris Masond352ac62008-09-29 15:18:18 -04001783/*
1784 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1785 * we don't create bios that span stripes or chunks
1786 */
David Woodhouse64a16702009-07-15 23:29:37 +01001787int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001788 size_t size, struct bio *bio,
1789 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001790{
1791 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001792 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001793 u64 length = 0;
1794 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001795 int ret;
1796
Chris Mason771ed682008-11-06 22:02:51 -05001797 if (bio_flags & EXTENT_BIO_COMPRESSED)
1798 return 0;
1799
Kent Overstreet4f024f32013-10-11 15:44:27 -07001800 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001801 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001802 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001803 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001804 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001805 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001806 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001807 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001808 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001809}
1810
Chris Masond352ac62008-09-29 15:18:18 -04001811/*
1812 * in order to insert checksums into the metadata in large chunks,
1813 * we wait until bio submission time. All the pages in the bio are
1814 * checksummed and sums are attached onto the ordered extent record.
1815 *
1816 * At IO completion time the cums attached on the ordered extent record
1817 * are inserted into the btree
1818 */
Chris Masond3977122009-01-05 21:25:51 -05001819static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1820 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001821 unsigned long bio_flags,
1822 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001823{
Chris Mason065631f2008-02-20 12:07:25 -05001824 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001825 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001826
Chris Masond20f7042008-12-08 16:58:54 -05001827 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001828 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001829 return 0;
1830}
Chris Masone0156402008-04-16 11:15:20 -04001831
Chris Mason4a69a412008-11-06 22:03:00 -05001832/*
1833 * in order to insert checksums into the metadata in large chunks,
1834 * we wait until bio submission time. All the pages in the bio are
1835 * checksummed and sums are attached onto the ordered extent record.
1836 *
1837 * At IO completion time the cums attached on the ordered extent record
1838 * are inserted into the btree
1839 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001840static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001841 int mirror_num, unsigned long bio_flags,
1842 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001843{
1844 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001845 int ret;
1846
1847 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001848 if (ret) {
1849 bio->bi_error = ret;
1850 bio_endio(bio);
1851 }
Stefan Behrens61891922012-11-05 18:51:52 +01001852 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001853}
1854
Chris Masond352ac62008-09-29 15:18:18 -04001855/*
Chris Masoncad321a2008-12-17 14:51:42 -05001856 * extent_io.c submission hook. This does the right thing for csum calculation
1857 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001858 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001859static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001860 int mirror_num, unsigned long bio_flags,
1861 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001862{
1863 struct btrfs_root *root = BTRFS_I(inode)->root;
1864 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001865 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001866 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001867 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001868
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001869 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001870
Liu Bo83eea1f2012-07-10 05:28:39 -06001871 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001872 metadata = 2;
1873
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001874 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001875 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1876 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001877 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001878
Chris Masond20f7042008-12-08 16:58:54 -05001879 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001880 ret = btrfs_submit_compressed_read(inode, bio,
1881 mirror_num,
1882 bio_flags);
1883 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001884 } else if (!skip_sum) {
1885 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1886 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001887 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001888 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001889 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001890 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001891 /* csum items have already been cloned */
1892 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1893 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001894 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001895 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001896 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001897 bio_flags, bio_offset,
1898 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001899 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001900 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001901 } else if (!skip_sum) {
1902 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1903 if (ret)
1904 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001905 }
1906
Chris Mason0b86a832008-03-24 15:01:56 -04001907mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001908 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1909
1910out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001911 if (ret < 0) {
1912 bio->bi_error = ret;
1913 bio_endio(bio);
1914 }
Stefan Behrens61891922012-11-05 18:51:52 +01001915 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001916}
Chris Mason6885f302008-02-20 16:11:05 -05001917
Chris Masond352ac62008-09-29 15:18:18 -04001918/*
1919 * given a list of ordered sums record them in the inode. This happens
1920 * at IO completion time based on sums calculated at bio submission time.
1921 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001922static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001923 struct inode *inode, u64 file_offset,
1924 struct list_head *list)
1925{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001926 struct btrfs_ordered_sum *sum;
1927
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001928 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001929 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001930 btrfs_csum_file_blocks(trans,
1931 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001932 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001933 }
1934 return 0;
1935}
1936
Josef Bacik2ac55d42010-02-03 19:33:23 +00001937int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1938 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001939{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001940 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001941 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001942 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001943}
1944
Chris Masond352ac62008-09-29 15:18:18 -04001945/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001946struct btrfs_writepage_fixup {
1947 struct page *page;
1948 struct btrfs_work work;
1949};
1950
Christoph Hellwigb2950862008-12-02 09:54:17 -05001951static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001952{
1953 struct btrfs_writepage_fixup *fixup;
1954 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001955 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001956 struct page *page;
1957 struct inode *inode;
1958 u64 page_start;
1959 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001960 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001961
1962 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1963 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001964again:
Chris Mason247e7432008-07-17 12:53:51 -04001965 lock_page(page);
1966 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1967 ClearPageChecked(page);
1968 goto out_page;
1969 }
1970
1971 inode = page->mapping->host;
1972 page_start = page_offset(page);
1973 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1974
Josef Bacik2ac55d42010-02-03 19:33:23 +00001975 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001976 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001977
1978 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001979 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001980 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001981
1982 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1983 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001984 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1985 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001986 unlock_page(page);
1987 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001988 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001989 goto again;
1990 }
Chris Mason247e7432008-07-17 12:53:51 -04001991
Jeff Mahoney87826df2012-02-15 16:23:57 +01001992 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1993 if (ret) {
1994 mapping_set_error(page->mapping, ret);
1995 end_extent_writepage(page, ret, page_start, page_end);
1996 ClearPageChecked(page);
1997 goto out;
1998 }
1999
Josef Bacik2ac55d42010-02-03 19:33:23 +00002000 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002001 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002002 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002003out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002004 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2005 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002006out_page:
2007 unlock_page(page);
2008 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002009 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002010}
2011
2012/*
2013 * There are a few paths in the higher layers of the kernel that directly
2014 * set the page dirty bit without asking the filesystem if it is a
2015 * good idea. This causes problems because we want to make sure COW
2016 * properly happens and the data=ordered rules are followed.
2017 *
Chris Masonc8b97812008-10-29 14:49:59 -04002018 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002019 * hasn't been properly setup for IO. We kick off an async process
2020 * to fix it up. The async helper will wait for ordered extents, set
2021 * the delalloc bit and make it safe to write the page.
2022 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002023static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002024{
2025 struct inode *inode = page->mapping->host;
2026 struct btrfs_writepage_fixup *fixup;
2027 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002028
Chris Mason8b62b722009-09-02 16:53:46 -04002029 /* this page is properly in the ordered list */
2030 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002031 return 0;
2032
2033 if (PageChecked(page))
2034 return -EAGAIN;
2035
2036 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2037 if (!fixup)
2038 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002039
Chris Mason247e7432008-07-17 12:53:51 -04002040 SetPageChecked(page);
2041 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002042 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2043 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002044 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002045 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002046 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002047}
2048
Yan Zhengd899e052008-10-30 14:25:28 -04002049static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2050 struct inode *inode, u64 file_pos,
2051 u64 disk_bytenr, u64 disk_num_bytes,
2052 u64 num_bytes, u64 ram_bytes,
2053 u8 compression, u8 encryption,
2054 u16 other_encoding, int extent_type)
2055{
2056 struct btrfs_root *root = BTRFS_I(inode)->root;
2057 struct btrfs_file_extent_item *fi;
2058 struct btrfs_path *path;
2059 struct extent_buffer *leaf;
2060 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002061 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002062 int ret;
2063
2064 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002065 if (!path)
2066 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002067
Chris Masona1ed8352009-09-11 12:27:37 -04002068 /*
2069 * we may be replacing one extent in the tree with another.
2070 * The new extent is pinned in the extent map, and we don't want
2071 * to drop it from the cache until it is completely in the btree.
2072 *
2073 * So, tell btrfs_drop_extents to leave this extent in the cache.
2074 * the caller is expected to unpin it and allow it to be merged
2075 * with the others.
2076 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002077 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2078 file_pos + num_bytes, NULL, 0,
2079 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002080 if (ret)
2081 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002082
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002083 if (!extent_inserted) {
2084 ins.objectid = btrfs_ino(inode);
2085 ins.offset = file_pos;
2086 ins.type = BTRFS_EXTENT_DATA_KEY;
2087
2088 path->leave_spinning = 1;
2089 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2090 sizeof(*fi));
2091 if (ret)
2092 goto out;
2093 }
Yan Zhengd899e052008-10-30 14:25:28 -04002094 leaf = path->nodes[0];
2095 fi = btrfs_item_ptr(leaf, path->slots[0],
2096 struct btrfs_file_extent_item);
2097 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2098 btrfs_set_file_extent_type(leaf, fi, extent_type);
2099 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2100 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2101 btrfs_set_file_extent_offset(leaf, fi, 0);
2102 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2103 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2104 btrfs_set_file_extent_compression(leaf, fi, compression);
2105 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2106 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002107
Yan Zhengd899e052008-10-30 14:25:28 -04002108 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002109 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002110
2111 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002112
2113 ins.objectid = disk_bytenr;
2114 ins.offset = disk_num_bytes;
2115 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002116 ret = btrfs_alloc_reserved_file_extent(trans, root,
2117 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002118 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002119out:
Yan Zhengd899e052008-10-30 14:25:28 -04002120 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002121
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002122 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002123}
2124
Liu Bo38c227d2013-01-29 03:18:40 +00002125/* snapshot-aware defrag */
2126struct sa_defrag_extent_backref {
2127 struct rb_node node;
2128 struct old_sa_defrag_extent *old;
2129 u64 root_id;
2130 u64 inum;
2131 u64 file_pos;
2132 u64 extent_offset;
2133 u64 num_bytes;
2134 u64 generation;
2135};
2136
2137struct old_sa_defrag_extent {
2138 struct list_head list;
2139 struct new_sa_defrag_extent *new;
2140
2141 u64 extent_offset;
2142 u64 bytenr;
2143 u64 offset;
2144 u64 len;
2145 int count;
2146};
2147
2148struct new_sa_defrag_extent {
2149 struct rb_root root;
2150 struct list_head head;
2151 struct btrfs_path *path;
2152 struct inode *inode;
2153 u64 file_pos;
2154 u64 len;
2155 u64 bytenr;
2156 u64 disk_len;
2157 u8 compress_type;
2158};
2159
2160static int backref_comp(struct sa_defrag_extent_backref *b1,
2161 struct sa_defrag_extent_backref *b2)
2162{
2163 if (b1->root_id < b2->root_id)
2164 return -1;
2165 else if (b1->root_id > b2->root_id)
2166 return 1;
2167
2168 if (b1->inum < b2->inum)
2169 return -1;
2170 else if (b1->inum > b2->inum)
2171 return 1;
2172
2173 if (b1->file_pos < b2->file_pos)
2174 return -1;
2175 else if (b1->file_pos > b2->file_pos)
2176 return 1;
2177
2178 /*
2179 * [------------------------------] ===> (a range of space)
2180 * |<--->| |<---->| =============> (fs/file tree A)
2181 * |<---------------------------->| ===> (fs/file tree B)
2182 *
2183 * A range of space can refer to two file extents in one tree while
2184 * refer to only one file extent in another tree.
2185 *
2186 * So we may process a disk offset more than one time(two extents in A)
2187 * and locate at the same extent(one extent in B), then insert two same
2188 * backrefs(both refer to the extent in B).
2189 */
2190 return 0;
2191}
2192
2193static void backref_insert(struct rb_root *root,
2194 struct sa_defrag_extent_backref *backref)
2195{
2196 struct rb_node **p = &root->rb_node;
2197 struct rb_node *parent = NULL;
2198 struct sa_defrag_extent_backref *entry;
2199 int ret;
2200
2201 while (*p) {
2202 parent = *p;
2203 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2204
2205 ret = backref_comp(backref, entry);
2206 if (ret < 0)
2207 p = &(*p)->rb_left;
2208 else
2209 p = &(*p)->rb_right;
2210 }
2211
2212 rb_link_node(&backref->node, parent, p);
2213 rb_insert_color(&backref->node, root);
2214}
2215
2216/*
2217 * Note the backref might has changed, and in this case we just return 0.
2218 */
2219static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2220 void *ctx)
2221{
2222 struct btrfs_file_extent_item *extent;
2223 struct btrfs_fs_info *fs_info;
2224 struct old_sa_defrag_extent *old = ctx;
2225 struct new_sa_defrag_extent *new = old->new;
2226 struct btrfs_path *path = new->path;
2227 struct btrfs_key key;
2228 struct btrfs_root *root;
2229 struct sa_defrag_extent_backref *backref;
2230 struct extent_buffer *leaf;
2231 struct inode *inode = new->inode;
2232 int slot;
2233 int ret;
2234 u64 extent_offset;
2235 u64 num_bytes;
2236
2237 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2238 inum == btrfs_ino(inode))
2239 return 0;
2240
2241 key.objectid = root_id;
2242 key.type = BTRFS_ROOT_ITEM_KEY;
2243 key.offset = (u64)-1;
2244
2245 fs_info = BTRFS_I(inode)->root->fs_info;
2246 root = btrfs_read_fs_root_no_name(fs_info, &key);
2247 if (IS_ERR(root)) {
2248 if (PTR_ERR(root) == -ENOENT)
2249 return 0;
2250 WARN_ON(1);
2251 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2252 inum, offset, root_id);
2253 return PTR_ERR(root);
2254 }
2255
2256 key.objectid = inum;
2257 key.type = BTRFS_EXTENT_DATA_KEY;
2258 if (offset > (u64)-1 << 32)
2259 key.offset = 0;
2260 else
2261 key.offset = offset;
2262
2263 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302264 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002265 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002266 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002267
2268 while (1) {
2269 cond_resched();
2270
2271 leaf = path->nodes[0];
2272 slot = path->slots[0];
2273
2274 if (slot >= btrfs_header_nritems(leaf)) {
2275 ret = btrfs_next_leaf(root, path);
2276 if (ret < 0) {
2277 goto out;
2278 } else if (ret > 0) {
2279 ret = 0;
2280 goto out;
2281 }
2282 continue;
2283 }
2284
2285 path->slots[0]++;
2286
2287 btrfs_item_key_to_cpu(leaf, &key, slot);
2288
2289 if (key.objectid > inum)
2290 goto out;
2291
2292 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2293 continue;
2294
2295 extent = btrfs_item_ptr(leaf, slot,
2296 struct btrfs_file_extent_item);
2297
2298 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2299 continue;
2300
Liu Boe68afa42013-07-01 22:13:26 +08002301 /*
2302 * 'offset' refers to the exact key.offset,
2303 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2304 * (key.offset - extent_offset).
2305 */
2306 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002307 continue;
2308
Liu Boe68afa42013-07-01 22:13:26 +08002309 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002310 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002311
Liu Bo38c227d2013-01-29 03:18:40 +00002312 if (extent_offset >= old->extent_offset + old->offset +
2313 old->len || extent_offset + num_bytes <=
2314 old->extent_offset + old->offset)
2315 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002316 break;
2317 }
2318
2319 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2320 if (!backref) {
2321 ret = -ENOENT;
2322 goto out;
2323 }
2324
2325 backref->root_id = root_id;
2326 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002327 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002328 backref->num_bytes = num_bytes;
2329 backref->extent_offset = extent_offset;
2330 backref->generation = btrfs_file_extent_generation(leaf, extent);
2331 backref->old = old;
2332 backref_insert(&new->root, backref);
2333 old->count++;
2334out:
2335 btrfs_release_path(path);
2336 WARN_ON(ret);
2337 return ret;
2338}
2339
2340static noinline bool record_extent_backrefs(struct btrfs_path *path,
2341 struct new_sa_defrag_extent *new)
2342{
2343 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2344 struct old_sa_defrag_extent *old, *tmp;
2345 int ret;
2346
2347 new->path = path;
2348
2349 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002350 ret = iterate_inodes_from_logical(old->bytenr +
2351 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002352 path, record_one_backref,
2353 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002354 if (ret < 0 && ret != -ENOENT)
2355 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002356
2357 /* no backref to be processed for this extent */
2358 if (!old->count) {
2359 list_del(&old->list);
2360 kfree(old);
2361 }
2362 }
2363
2364 if (list_empty(&new->head))
2365 return false;
2366
2367 return true;
2368}
2369
2370static int relink_is_mergable(struct extent_buffer *leaf,
2371 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002372 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002373{
Liu Bo116e0022013-08-02 16:30:40 +08002374 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002375 return 0;
2376
2377 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2378 return 0;
2379
Liu Bo116e0022013-08-02 16:30:40 +08002380 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2381 return 0;
2382
2383 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002384 btrfs_file_extent_other_encoding(leaf, fi))
2385 return 0;
2386
2387 return 1;
2388}
2389
2390/*
2391 * Note the backref might has changed, and in this case we just return 0.
2392 */
2393static noinline int relink_extent_backref(struct btrfs_path *path,
2394 struct sa_defrag_extent_backref *prev,
2395 struct sa_defrag_extent_backref *backref)
2396{
2397 struct btrfs_file_extent_item *extent;
2398 struct btrfs_file_extent_item *item;
2399 struct btrfs_ordered_extent *ordered;
2400 struct btrfs_trans_handle *trans;
2401 struct btrfs_fs_info *fs_info;
2402 struct btrfs_root *root;
2403 struct btrfs_key key;
2404 struct extent_buffer *leaf;
2405 struct old_sa_defrag_extent *old = backref->old;
2406 struct new_sa_defrag_extent *new = old->new;
2407 struct inode *src_inode = new->inode;
2408 struct inode *inode;
2409 struct extent_state *cached = NULL;
2410 int ret = 0;
2411 u64 start;
2412 u64 len;
2413 u64 lock_start;
2414 u64 lock_end;
2415 bool merge = false;
2416 int index;
2417
2418 if (prev && prev->root_id == backref->root_id &&
2419 prev->inum == backref->inum &&
2420 prev->file_pos + prev->num_bytes == backref->file_pos)
2421 merge = true;
2422
2423 /* step 1: get root */
2424 key.objectid = backref->root_id;
2425 key.type = BTRFS_ROOT_ITEM_KEY;
2426 key.offset = (u64)-1;
2427
2428 fs_info = BTRFS_I(src_inode)->root->fs_info;
2429 index = srcu_read_lock(&fs_info->subvol_srcu);
2430
2431 root = btrfs_read_fs_root_no_name(fs_info, &key);
2432 if (IS_ERR(root)) {
2433 srcu_read_unlock(&fs_info->subvol_srcu, index);
2434 if (PTR_ERR(root) == -ENOENT)
2435 return 0;
2436 return PTR_ERR(root);
2437 }
Liu Bo38c227d2013-01-29 03:18:40 +00002438
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002439 if (btrfs_root_readonly(root)) {
2440 srcu_read_unlock(&fs_info->subvol_srcu, index);
2441 return 0;
2442 }
2443
Liu Bo38c227d2013-01-29 03:18:40 +00002444 /* step 2: get inode */
2445 key.objectid = backref->inum;
2446 key.type = BTRFS_INODE_ITEM_KEY;
2447 key.offset = 0;
2448
2449 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2450 if (IS_ERR(inode)) {
2451 srcu_read_unlock(&fs_info->subvol_srcu, index);
2452 return 0;
2453 }
2454
2455 srcu_read_unlock(&fs_info->subvol_srcu, index);
2456
2457 /* step 3: relink backref */
2458 lock_start = backref->file_pos;
2459 lock_end = backref->file_pos + backref->num_bytes - 1;
2460 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2461 0, &cached);
2462
2463 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2464 if (ordered) {
2465 btrfs_put_ordered_extent(ordered);
2466 goto out_unlock;
2467 }
2468
2469 trans = btrfs_join_transaction(root);
2470 if (IS_ERR(trans)) {
2471 ret = PTR_ERR(trans);
2472 goto out_unlock;
2473 }
2474
2475 key.objectid = backref->inum;
2476 key.type = BTRFS_EXTENT_DATA_KEY;
2477 key.offset = backref->file_pos;
2478
2479 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2480 if (ret < 0) {
2481 goto out_free_path;
2482 } else if (ret > 0) {
2483 ret = 0;
2484 goto out_free_path;
2485 }
2486
2487 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2488 struct btrfs_file_extent_item);
2489
2490 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2491 backref->generation)
2492 goto out_free_path;
2493
2494 btrfs_release_path(path);
2495
2496 start = backref->file_pos;
2497 if (backref->extent_offset < old->extent_offset + old->offset)
2498 start += old->extent_offset + old->offset -
2499 backref->extent_offset;
2500
2501 len = min(backref->extent_offset + backref->num_bytes,
2502 old->extent_offset + old->offset + old->len);
2503 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2504
2505 ret = btrfs_drop_extents(trans, root, inode, start,
2506 start + len, 1);
2507 if (ret)
2508 goto out_free_path;
2509again:
2510 key.objectid = btrfs_ino(inode);
2511 key.type = BTRFS_EXTENT_DATA_KEY;
2512 key.offset = start;
2513
Liu Boa09a0a72013-03-11 09:20:58 +00002514 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002515 if (merge) {
2516 struct btrfs_file_extent_item *fi;
2517 u64 extent_len;
2518 struct btrfs_key found_key;
2519
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002520 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002521 if (ret < 0)
2522 goto out_free_path;
2523
2524 path->slots[0]--;
2525 leaf = path->nodes[0];
2526 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2527
2528 fi = btrfs_item_ptr(leaf, path->slots[0],
2529 struct btrfs_file_extent_item);
2530 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2531
Liu Bo116e0022013-08-02 16:30:40 +08002532 if (extent_len + found_key.offset == start &&
2533 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002534 btrfs_set_file_extent_num_bytes(leaf, fi,
2535 extent_len + len);
2536 btrfs_mark_buffer_dirty(leaf);
2537 inode_add_bytes(inode, len);
2538
2539 ret = 1;
2540 goto out_free_path;
2541 } else {
2542 merge = false;
2543 btrfs_release_path(path);
2544 goto again;
2545 }
2546 }
2547
2548 ret = btrfs_insert_empty_item(trans, root, path, &key,
2549 sizeof(*extent));
2550 if (ret) {
2551 btrfs_abort_transaction(trans, root, ret);
2552 goto out_free_path;
2553 }
2554
2555 leaf = path->nodes[0];
2556 item = btrfs_item_ptr(leaf, path->slots[0],
2557 struct btrfs_file_extent_item);
2558 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2559 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2560 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2561 btrfs_set_file_extent_num_bytes(leaf, item, len);
2562 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2563 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2564 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2565 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2566 btrfs_set_file_extent_encryption(leaf, item, 0);
2567 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2568
2569 btrfs_mark_buffer_dirty(leaf);
2570 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002571 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002572
2573 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2574 new->disk_len, 0,
2575 backref->root_id, backref->inum,
2576 new->file_pos, 0); /* start - extent_offset */
2577 if (ret) {
2578 btrfs_abort_transaction(trans, root, ret);
2579 goto out_free_path;
2580 }
2581
2582 ret = 1;
2583out_free_path:
2584 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002585 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002586 btrfs_end_transaction(trans, root);
2587out_unlock:
2588 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2589 &cached, GFP_NOFS);
2590 iput(inode);
2591 return ret;
2592}
2593
Liu Bo6f519562013-10-29 10:45:05 +08002594static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2595{
2596 struct old_sa_defrag_extent *old, *tmp;
2597
2598 if (!new)
2599 return;
2600
2601 list_for_each_entry_safe(old, tmp, &new->head, list) {
2602 list_del(&old->list);
2603 kfree(old);
2604 }
2605 kfree(new);
2606}
2607
Liu Bo38c227d2013-01-29 03:18:40 +00002608static void relink_file_extents(struct new_sa_defrag_extent *new)
2609{
2610 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002611 struct sa_defrag_extent_backref *backref;
2612 struct sa_defrag_extent_backref *prev = NULL;
2613 struct inode *inode;
2614 struct btrfs_root *root;
2615 struct rb_node *node;
2616 int ret;
2617
2618 inode = new->inode;
2619 root = BTRFS_I(inode)->root;
2620
2621 path = btrfs_alloc_path();
2622 if (!path)
2623 return;
2624
2625 if (!record_extent_backrefs(path, new)) {
2626 btrfs_free_path(path);
2627 goto out;
2628 }
2629 btrfs_release_path(path);
2630
2631 while (1) {
2632 node = rb_first(&new->root);
2633 if (!node)
2634 break;
2635 rb_erase(node, &new->root);
2636
2637 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2638
2639 ret = relink_extent_backref(path, prev, backref);
2640 WARN_ON(ret < 0);
2641
2642 kfree(prev);
2643
2644 if (ret == 1)
2645 prev = backref;
2646 else
2647 prev = NULL;
2648 cond_resched();
2649 }
2650 kfree(prev);
2651
2652 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002653out:
Liu Bo6f519562013-10-29 10:45:05 +08002654 free_sa_defrag_extent(new);
2655
Liu Bo38c227d2013-01-29 03:18:40 +00002656 atomic_dec(&root->fs_info->defrag_running);
2657 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002658}
2659
2660static struct new_sa_defrag_extent *
2661record_old_file_extents(struct inode *inode,
2662 struct btrfs_ordered_extent *ordered)
2663{
2664 struct btrfs_root *root = BTRFS_I(inode)->root;
2665 struct btrfs_path *path;
2666 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002667 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002668 struct new_sa_defrag_extent *new;
2669 int ret;
2670
2671 new = kmalloc(sizeof(*new), GFP_NOFS);
2672 if (!new)
2673 return NULL;
2674
2675 new->inode = inode;
2676 new->file_pos = ordered->file_offset;
2677 new->len = ordered->len;
2678 new->bytenr = ordered->start;
2679 new->disk_len = ordered->disk_len;
2680 new->compress_type = ordered->compress_type;
2681 new->root = RB_ROOT;
2682 INIT_LIST_HEAD(&new->head);
2683
2684 path = btrfs_alloc_path();
2685 if (!path)
2686 goto out_kfree;
2687
2688 key.objectid = btrfs_ino(inode);
2689 key.type = BTRFS_EXTENT_DATA_KEY;
2690 key.offset = new->file_pos;
2691
2692 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2693 if (ret < 0)
2694 goto out_free_path;
2695 if (ret > 0 && path->slots[0] > 0)
2696 path->slots[0]--;
2697
2698 /* find out all the old extents for the file range */
2699 while (1) {
2700 struct btrfs_file_extent_item *extent;
2701 struct extent_buffer *l;
2702 int slot;
2703 u64 num_bytes;
2704 u64 offset;
2705 u64 end;
2706 u64 disk_bytenr;
2707 u64 extent_offset;
2708
2709 l = path->nodes[0];
2710 slot = path->slots[0];
2711
2712 if (slot >= btrfs_header_nritems(l)) {
2713 ret = btrfs_next_leaf(root, path);
2714 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002715 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002716 else if (ret > 0)
2717 break;
2718 continue;
2719 }
2720
2721 btrfs_item_key_to_cpu(l, &key, slot);
2722
2723 if (key.objectid != btrfs_ino(inode))
2724 break;
2725 if (key.type != BTRFS_EXTENT_DATA_KEY)
2726 break;
2727 if (key.offset >= new->file_pos + new->len)
2728 break;
2729
2730 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2731
2732 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2733 if (key.offset + num_bytes < new->file_pos)
2734 goto next;
2735
2736 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2737 if (!disk_bytenr)
2738 goto next;
2739
2740 extent_offset = btrfs_file_extent_offset(l, extent);
2741
2742 old = kmalloc(sizeof(*old), GFP_NOFS);
2743 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002744 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002745
2746 offset = max(new->file_pos, key.offset);
2747 end = min(new->file_pos + new->len, key.offset + num_bytes);
2748
2749 old->bytenr = disk_bytenr;
2750 old->extent_offset = extent_offset;
2751 old->offset = offset - key.offset;
2752 old->len = end - offset;
2753 old->new = new;
2754 old->count = 0;
2755 list_add_tail(&old->list, &new->head);
2756next:
2757 path->slots[0]++;
2758 cond_resched();
2759 }
2760
2761 btrfs_free_path(path);
2762 atomic_inc(&root->fs_info->defrag_running);
2763
2764 return new;
2765
Liu Bo38c227d2013-01-29 03:18:40 +00002766out_free_path:
2767 btrfs_free_path(path);
2768out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002769 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002770 return NULL;
2771}
2772
Miao Xiee570fd22014-06-19 10:42:50 +08002773static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2774 u64 start, u64 len)
2775{
2776 struct btrfs_block_group_cache *cache;
2777
2778 cache = btrfs_lookup_block_group(root->fs_info, start);
2779 ASSERT(cache);
2780
2781 spin_lock(&cache->lock);
2782 cache->delalloc_bytes -= len;
2783 spin_unlock(&cache->lock);
2784
2785 btrfs_put_block_group(cache);
2786}
2787
Chris Masond352ac62008-09-29 15:18:18 -04002788/* as ordered data IO finishes, this gets called so we can finish
2789 * an ordered extent if the range of bytes in the file it covers are
2790 * fully written.
2791 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002792static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002793{
Josef Bacik5fd02042012-05-02 14:00:54 -04002794 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002795 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002796 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002797 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002798 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002799 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002800 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002801 int ret = 0;
2802 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002803 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002804 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002805
Liu Bo83eea1f2012-07-10 05:28:39 -06002806 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002807
Josef Bacik5fd02042012-05-02 14:00:54 -04002808 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2809 ret = -EIO;
2810 goto out;
2811 }
2812
Miao Xief6124962014-09-12 18:44:04 +08002813 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2814 ordered_extent->file_offset +
2815 ordered_extent->len - 1);
2816
Josef Bacik77cef2e2013-08-29 13:57:21 -04002817 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2818 truncated = true;
2819 logical_len = ordered_extent->truncated_len;
2820 /* Truncated the entire extent, don't bother adding */
2821 if (!logical_len)
2822 goto out;
2823 }
2824
Yan, Zhengc2167752009-11-12 09:34:21 +00002825 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002826 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002827 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2828 if (nolock)
2829 trans = btrfs_join_transaction_nolock(root);
2830 else
2831 trans = btrfs_join_transaction(root);
2832 if (IS_ERR(trans)) {
2833 ret = PTR_ERR(trans);
2834 trans = NULL;
2835 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002836 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002837 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2838 ret = btrfs_update_inode_fallback(trans, root, inode);
2839 if (ret) /* -ENOMEM or corruption */
2840 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002841 goto out;
2842 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002843
Josef Bacik2ac55d42010-02-03 19:33:23 +00002844 lock_extent_bits(io_tree, ordered_extent->file_offset,
2845 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002846 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002847
Liu Bo38c227d2013-01-29 03:18:40 +00002848 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2849 ordered_extent->file_offset + ordered_extent->len - 1,
2850 EXTENT_DEFRAG, 1, cached_state);
2851 if (ret) {
2852 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002853 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002854 /* the inode is shared */
2855 new = record_old_file_extents(inode, ordered_extent);
2856
2857 clear_extent_bit(io_tree, ordered_extent->file_offset,
2858 ordered_extent->file_offset + ordered_extent->len - 1,
2859 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2860 }
2861
Josef Bacik0cb59c92010-07-02 12:14:14 -04002862 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002863 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002864 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002865 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002866 if (IS_ERR(trans)) {
2867 ret = PTR_ERR(trans);
2868 trans = NULL;
2869 goto out_unlock;
2870 }
Chris Masona79b7d42014-05-22 16:18:52 -07002871
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002872 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002873
Chris Masonc8b97812008-10-29 14:49:59 -04002874 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002875 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002876 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002877 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002878 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002879 ordered_extent->file_offset,
2880 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002881 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002882 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002883 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002884 ret = insert_reserved_file_extent(trans, inode,
2885 ordered_extent->file_offset,
2886 ordered_extent->start,
2887 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002888 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002889 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002890 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002891 if (!ret)
2892 btrfs_release_delalloc_bytes(root,
2893 ordered_extent->start,
2894 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002895 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002896 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2897 ordered_extent->file_offset, ordered_extent->len,
2898 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002899 if (ret < 0) {
2900 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002901 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002902 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002903
Chris Masone6dcd2d2008-07-17 12:53:50 -04002904 add_pending_csums(trans, inode, ordered_extent->file_offset,
2905 &ordered_extent->list);
2906
Josef Bacik6c760c02012-11-09 10:53:21 -05002907 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2908 ret = btrfs_update_inode_fallback(trans, root, inode);
2909 if (ret) { /* -ENOMEM or corruption */
2910 btrfs_abort_transaction(trans, root, ret);
2911 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002912 }
2913 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002914out_unlock:
2915 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2916 ordered_extent->file_offset +
2917 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002918out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002919 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002920 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002921 if (trans)
2922 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002923
Josef Bacik77cef2e2013-08-29 13:57:21 -04002924 if (ret || truncated) {
2925 u64 start, end;
2926
2927 if (truncated)
2928 start = ordered_extent->file_offset + logical_len;
2929 else
2930 start = ordered_extent->file_offset;
2931 end = ordered_extent->file_offset + ordered_extent->len - 1;
2932 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2933
2934 /* Drop the cache for the part of the extent we didn't write. */
2935 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002936
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002937 /*
2938 * If the ordered extent had an IOERR or something else went
2939 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002940 * back to the allocator. We only free the extent in the
2941 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002942 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002943 if ((ret || !logical_len) &&
2944 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002945 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2946 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002947 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002948 }
2949
2950
Josef Bacik5fd02042012-05-02 14:00:54 -04002951 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002952 * This needs to be done to make sure anybody waiting knows we are done
2953 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002954 */
2955 btrfs_remove_ordered_extent(inode, ordered_extent);
2956
Liu Bo38c227d2013-01-29 03:18:40 +00002957 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002958 if (new) {
2959 if (ret) {
2960 free_sa_defrag_extent(new);
2961 atomic_dec(&root->fs_info->defrag_running);
2962 } else {
2963 relink_file_extents(new);
2964 }
2965 }
Liu Bo38c227d2013-01-29 03:18:40 +00002966
Chris Masone6dcd2d2008-07-17 12:53:50 -04002967 /* once for us */
2968 btrfs_put_ordered_extent(ordered_extent);
2969 /* once for the tree */
2970 btrfs_put_ordered_extent(ordered_extent);
2971
Josef Bacik5fd02042012-05-02 14:00:54 -04002972 return ret;
2973}
2974
2975static void finish_ordered_fn(struct btrfs_work *work)
2976{
2977 struct btrfs_ordered_extent *ordered_extent;
2978 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2979 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002980}
2981
Christoph Hellwigb2950862008-12-02 09:54:17 -05002982static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002983 struct extent_state *state, int uptodate)
2984{
Josef Bacik5fd02042012-05-02 14:00:54 -04002985 struct inode *inode = page->mapping->host;
2986 struct btrfs_root *root = BTRFS_I(inode)->root;
2987 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002988 struct btrfs_workqueue *wq;
2989 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002990
liubo1abe9b82011-03-24 11:18:59 +00002991 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2992
Chris Mason8b62b722009-09-02 16:53:46 -04002993 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002994 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2995 end - start + 1, uptodate))
2996 return 0;
2997
Liu Bo9e0af232014-08-15 23:36:53 +08002998 if (btrfs_is_free_space_inode(inode)) {
2999 wq = root->fs_info->endio_freespace_worker;
3000 func = btrfs_freespace_write_helper;
3001 } else {
3002 wq = root->fs_info->endio_write_workers;
3003 func = btrfs_endio_write_helper;
3004 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003005
Liu Bo9e0af232014-08-15 23:36:53 +08003006 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3007 NULL);
3008 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003009
3010 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003011}
3012
Miao Xiedc380ae2014-09-12 18:43:55 +08003013static int __readpage_endio_check(struct inode *inode,
3014 struct btrfs_io_bio *io_bio,
3015 int icsum, struct page *page,
3016 int pgoff, u64 start, size_t len)
3017{
3018 char *kaddr;
3019 u32 csum_expected;
3020 u32 csum = ~(u32)0;
3021 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
3022 DEFAULT_RATELIMIT_BURST);
3023
3024 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3025
3026 kaddr = kmap_atomic(page);
3027 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3028 btrfs_csum_final(csum, (char *)&csum);
3029 if (csum != csum_expected)
3030 goto zeroit;
3031
3032 kunmap_atomic(kaddr);
3033 return 0;
3034zeroit:
3035 if (__ratelimit(&_rs))
David Sterbaf0954c62014-12-19 18:38:44 +01003036 btrfs_warn(BTRFS_I(inode)->root->fs_info,
Miao Xiedc380ae2014-09-12 18:43:55 +08003037 "csum failed ino %llu off %llu csum %u expected csum %u",
3038 btrfs_ino(inode), start, csum, csum_expected);
3039 memset(kaddr + pgoff, 1, len);
3040 flush_dcache_page(page);
3041 kunmap_atomic(kaddr);
3042 if (csum_expected == 0)
3043 return 0;
3044 return -EIO;
3045}
3046
Chris Masond352ac62008-09-29 15:18:18 -04003047/*
Chris Masond352ac62008-09-29 15:18:18 -04003048 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003049 * if there's a match, we allow the bio to finish. If not, the code in
3050 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003051 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003052static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3053 u64 phy_offset, struct page *page,
3054 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003055{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003056 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003057 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003058 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003059 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003060
Chris Masond20f7042008-12-08 16:58:54 -05003061 if (PageChecked(page)) {
3062 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003063 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003064 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003065
3066 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003067 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003068
Yan Zheng17d217f2008-12-12 10:03:38 -05003069 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003070 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05003071 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
3072 GFP_NOFS);
3073 return 0;
3074 }
3075
Miao Xiefacc8a222013-07-25 19:22:34 +08003076 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003077 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3078 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003079}
Chris Masonb888db22007-08-27 16:49:44 -04003080
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003081struct delayed_iput {
3082 struct list_head list;
3083 struct inode *inode;
3084};
3085
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003086/* JDM: If this is fs-wide, why can't we add a pointer to
3087 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003088void btrfs_add_delayed_iput(struct inode *inode)
3089{
3090 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3091 struct delayed_iput *delayed;
3092
3093 if (atomic_add_unless(&inode->i_count, -1, 1))
3094 return;
3095
3096 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3097 delayed->inode = inode;
3098
3099 spin_lock(&fs_info->delayed_iput_lock);
3100 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3101 spin_unlock(&fs_info->delayed_iput_lock);
3102}
3103
3104void btrfs_run_delayed_iputs(struct btrfs_root *root)
3105{
3106 LIST_HEAD(list);
3107 struct btrfs_fs_info *fs_info = root->fs_info;
3108 struct delayed_iput *delayed;
3109 int empty;
3110
3111 spin_lock(&fs_info->delayed_iput_lock);
3112 empty = list_empty(&fs_info->delayed_iputs);
3113 spin_unlock(&fs_info->delayed_iput_lock);
3114 if (empty)
3115 return;
3116
Zhao Leid7c15172015-02-26 10:49:20 +08003117 down_read(&fs_info->delayed_iput_sem);
3118
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003119 spin_lock(&fs_info->delayed_iput_lock);
3120 list_splice_init(&fs_info->delayed_iputs, &list);
3121 spin_unlock(&fs_info->delayed_iput_lock);
3122
3123 while (!list_empty(&list)) {
3124 delayed = list_entry(list.next, struct delayed_iput, list);
3125 list_del(&delayed->list);
3126 iput(delayed->inode);
3127 kfree(delayed);
3128 }
Zhao Leid7c15172015-02-26 10:49:20 +08003129
3130 up_read(&root->fs_info->delayed_iput_sem);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003131}
3132
Yan, Zhengd68fc572010-05-16 10:49:58 -04003133/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003134 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003135 * files in the subvolume, it removes orphan item and frees block_rsv
3136 * structure.
3137 */
3138void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3139 struct btrfs_root *root)
3140{
Josef Bacik90290e12011-12-02 15:44:12 -05003141 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003142 int ret;
3143
Josef Bacik8a35d952012-05-23 14:26:42 -04003144 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003145 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3146 return;
3147
Josef Bacik90290e12011-12-02 15:44:12 -05003148 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003149 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003150 spin_unlock(&root->orphan_lock);
3151 return;
3152 }
3153
3154 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3155 spin_unlock(&root->orphan_lock);
3156 return;
3157 }
3158
3159 block_rsv = root->orphan_block_rsv;
3160 root->orphan_block_rsv = NULL;
3161 spin_unlock(&root->orphan_lock);
3162
Miao Xie27cdeb72014-04-02 19:51:05 +08003163 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003164 btrfs_root_refs(&root->root_item) > 0) {
3165 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3166 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003167 if (ret)
3168 btrfs_abort_transaction(trans, root, ret);
3169 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003170 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3171 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003172 }
3173
Josef Bacik90290e12011-12-02 15:44:12 -05003174 if (block_rsv) {
3175 WARN_ON(block_rsv->size > 0);
3176 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003177 }
3178}
3179
3180/*
Josef Bacik7b128762008-07-24 12:17:14 -04003181 * This creates an orphan entry for the given inode in case something goes
3182 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003183 *
3184 * NOTE: caller of this function should reserve 5 units of metadata for
3185 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003186 */
3187int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3188{
3189 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003190 struct btrfs_block_rsv *block_rsv = NULL;
3191 int reserve = 0;
3192 int insert = 0;
3193 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003194
Yan, Zhengd68fc572010-05-16 10:49:58 -04003195 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003196 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003197 if (!block_rsv)
3198 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003199 }
3200
Yan, Zhengd68fc572010-05-16 10:49:58 -04003201 spin_lock(&root->orphan_lock);
3202 if (!root->orphan_block_rsv) {
3203 root->orphan_block_rsv = block_rsv;
3204 } else if (block_rsv) {
3205 btrfs_free_block_rsv(root, block_rsv);
3206 block_rsv = NULL;
3207 }
Josef Bacik7b128762008-07-24 12:17:14 -04003208
Josef Bacik8a35d952012-05-23 14:26:42 -04003209 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3210 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003211#if 0
3212 /*
3213 * For proper ENOSPC handling, we should do orphan
3214 * cleanup when mounting. But this introduces backward
3215 * compatibility issue.
3216 */
3217 if (!xchg(&root->orphan_item_inserted, 1))
3218 insert = 2;
3219 else
3220 insert = 1;
3221#endif
3222 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003223 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003224 }
Josef Bacik7b128762008-07-24 12:17:14 -04003225
Josef Bacik72ac3c02012-05-23 14:13:11 -04003226 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3227 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003228 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003229 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003230
Yan, Zhengd68fc572010-05-16 10:49:58 -04003231 /* grab metadata reservation from transaction handle */
3232 if (reserve) {
3233 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003234 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003235 }
3236
3237 /* insert an orphan item to track this unlinked/truncated file */
3238 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003239 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003240 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003241 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003242 if (reserve) {
3243 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3244 &BTRFS_I(inode)->runtime_flags);
3245 btrfs_orphan_release_metadata(inode);
3246 }
3247 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003248 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3249 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003250 btrfs_abort_transaction(trans, root, ret);
3251 return ret;
3252 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003253 }
3254 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003255 }
3256
3257 /* insert an orphan item to track subvolume contains orphan files */
3258 if (insert >= 2) {
3259 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3260 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003261 if (ret && ret != -EEXIST) {
3262 btrfs_abort_transaction(trans, root, ret);
3263 return ret;
3264 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003265 }
3266 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003267}
3268
3269/*
3270 * We have done the truncate/delete so we can go ahead and remove the orphan
3271 * item for this particular inode.
3272 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003273static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3274 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003275{
3276 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277 int delete_item = 0;
3278 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003279 int ret = 0;
3280
Yan, Zhengd68fc572010-05-16 10:49:58 -04003281 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003282 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3283 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003284 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003285
Josef Bacik72ac3c02012-05-23 14:13:11 -04003286 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3287 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003288 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003289 spin_unlock(&root->orphan_lock);
3290
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003291 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003292 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003293 if (trans)
3294 ret = btrfs_del_orphan_item(trans, root,
3295 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003296 }
Josef Bacik7b128762008-07-24 12:17:14 -04003297
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003298 if (release_rsv)
3299 btrfs_orphan_release_metadata(inode);
3300
Josef Bacik4ef31a42013-08-13 14:10:08 -04003301 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003302}
3303
3304/*
3305 * this cleans up any orphans that may be left on the list from the last use
3306 * of this root.
3307 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003308int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003309{
3310 struct btrfs_path *path;
3311 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003312 struct btrfs_key key, found_key;
3313 struct btrfs_trans_handle *trans;
3314 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003315 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003316 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3317
Yan, Zhengd68fc572010-05-16 10:49:58 -04003318 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003319 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003320
3321 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003322 if (!path) {
3323 ret = -ENOMEM;
3324 goto out;
3325 }
Josef Bacik7b128762008-07-24 12:17:14 -04003326 path->reada = -1;
3327
3328 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003329 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003330 key.offset = (u64)-1;
3331
Josef Bacik7b128762008-07-24 12:17:14 -04003332 while (1) {
3333 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003334 if (ret < 0)
3335 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003336
3337 /*
3338 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003339 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003340 * find the key and see if we have stuff that matches
3341 */
3342 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003343 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003344 if (path->slots[0] == 0)
3345 break;
3346 path->slots[0]--;
3347 }
3348
3349 /* pull out the item */
3350 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003351 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3352
3353 /* make sure the item matches what we want */
3354 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3355 break;
David Sterba962a2982014-06-04 18:41:45 +02003356 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003357 break;
3358
3359 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003360 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003361
3362 /*
3363 * this is where we are basically btrfs_lookup, without the
3364 * crossing root thing. we store the inode number in the
3365 * offset of the orphan item.
3366 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003367
3368 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003369 btrfs_err(root->fs_info,
3370 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003371 ret = -EINVAL;
3372 goto out;
3373 }
3374
3375 last_objectid = found_key.offset;
3376
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003377 found_key.objectid = found_key.offset;
3378 found_key.type = BTRFS_INODE_ITEM_KEY;
3379 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003380 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303381 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003382 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003383 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003384
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003385 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3386 struct btrfs_root *dead_root;
3387 struct btrfs_fs_info *fs_info = root->fs_info;
3388 int is_dead_root = 0;
3389
3390 /*
3391 * this is an orphan in the tree root. Currently these
3392 * could come from 2 sources:
3393 * a) a snapshot deletion in progress
3394 * b) a free space cache inode
3395 * We need to distinguish those two, as the snapshot
3396 * orphan must not get deleted.
3397 * find_dead_roots already ran before us, so if this
3398 * is a snapshot deletion, we should find the root
3399 * in the dead_roots list
3400 */
3401 spin_lock(&fs_info->trans_lock);
3402 list_for_each_entry(dead_root, &fs_info->dead_roots,
3403 root_list) {
3404 if (dead_root->root_key.objectid ==
3405 found_key.objectid) {
3406 is_dead_root = 1;
3407 break;
3408 }
3409 }
3410 spin_unlock(&fs_info->trans_lock);
3411 if (is_dead_root) {
3412 /* prevent this orphan from being found again */
3413 key.offset = found_key.objectid - 1;
3414 continue;
3415 }
3416 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003417 /*
3418 * Inode is already gone but the orphan item is still there,
3419 * kill the orphan item.
3420 */
3421 if (ret == -ESTALE) {
3422 trans = btrfs_start_transaction(root, 1);
3423 if (IS_ERR(trans)) {
3424 ret = PTR_ERR(trans);
3425 goto out;
3426 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003427 btrfs_debug(root->fs_info, "auto deleting %Lu",
3428 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003429 ret = btrfs_del_orphan_item(trans, root,
3430 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003431 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003432 if (ret)
3433 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003434 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003435 }
Josef Bacik7b128762008-07-24 12:17:14 -04003436
Josef Bacik7b128762008-07-24 12:17:14 -04003437 /*
3438 * add this inode to the orphan list so btrfs_orphan_del does
3439 * the proper thing when we hit it
3440 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003441 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3442 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003443 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003444
Josef Bacik7b128762008-07-24 12:17:14 -04003445 /* if we have links, this was a truncate, lets do that */
3446 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303447 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003448 iput(inode);
3449 continue;
3450 }
Josef Bacik7b128762008-07-24 12:17:14 -04003451 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003452
3453 /* 1 for the orphan item deletion. */
3454 trans = btrfs_start_transaction(root, 1);
3455 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003456 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003457 ret = PTR_ERR(trans);
3458 goto out;
3459 }
3460 ret = btrfs_orphan_add(trans, inode);
3461 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003462 if (ret) {
3463 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003464 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003465 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003466
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003467 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003468 if (ret)
3469 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003470 } else {
3471 nr_unlink++;
3472 }
3473
3474 /* this will do delete_inode and everything for us */
3475 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003476 if (ret)
3477 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003478 }
Miao Xie3254c872011-11-10 20:45:05 -05003479 /* release the path since we're done with it */
3480 btrfs_release_path(path);
3481
Yan, Zhengd68fc572010-05-16 10:49:58 -04003482 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3483
3484 if (root->orphan_block_rsv)
3485 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3486 (u64)-1);
3487
Miao Xie27cdeb72014-04-02 19:51:05 +08003488 if (root->orphan_block_rsv ||
3489 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003490 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003491 if (!IS_ERR(trans))
3492 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003493 }
Josef Bacik7b128762008-07-24 12:17:14 -04003494
3495 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003496 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003497 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003498 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003499
3500out:
3501 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003502 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003503 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003504 btrfs_free_path(path);
3505 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003506}
3507
Chris Masond352ac62008-09-29 15:18:18 -04003508/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003509 * very simple check to peek ahead in the leaf looking for xattrs. If we
3510 * don't find any xattrs, we know there can't be any acls.
3511 *
3512 * slot is the slot the inode is in, objectid is the objectid of the inode
3513 */
3514static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003515 int slot, u64 objectid,
3516 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003517{
3518 u32 nritems = btrfs_header_nritems(leaf);
3519 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003520 static u64 xattr_access = 0;
3521 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003522 int scanned = 0;
3523
Josef Bacikf23b5a52013-06-19 10:16:26 -04003524 if (!xattr_access) {
3525 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3526 strlen(POSIX_ACL_XATTR_ACCESS));
3527 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3528 strlen(POSIX_ACL_XATTR_DEFAULT));
3529 }
3530
Chris Mason46a53cc2009-04-27 11:47:50 -04003531 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003532 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003533 while (slot < nritems) {
3534 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3535
3536 /* we found a different objectid, there must not be acls */
3537 if (found_key.objectid != objectid)
3538 return 0;
3539
3540 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003541 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003542 if (*first_xattr_slot == -1)
3543 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003544 if (found_key.offset == xattr_access ||
3545 found_key.offset == xattr_default)
3546 return 1;
3547 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003548
3549 /*
3550 * we found a key greater than an xattr key, there can't
3551 * be any acls later on
3552 */
3553 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3554 return 0;
3555
3556 slot++;
3557 scanned++;
3558
3559 /*
3560 * it goes inode, inode backrefs, xattrs, extents,
3561 * so if there are a ton of hard links to an inode there can
3562 * be a lot of backrefs. Don't waste time searching too hard,
3563 * this is just an optimization
3564 */
3565 if (scanned >= 8)
3566 break;
3567 }
3568 /* we hit the end of the leaf before we found an xattr or
3569 * something larger than an xattr. We have to assume the inode
3570 * has acls
3571 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003572 if (*first_xattr_slot == -1)
3573 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003574 return 1;
3575}
3576
3577/*
Chris Masond352ac62008-09-29 15:18:18 -04003578 * read an inode from the btree into the in-memory inode
3579 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003580static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003581{
3582 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003583 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003584 struct btrfs_inode_item *inode_item;
3585 struct btrfs_root *root = BTRFS_I(inode)->root;
3586 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003587 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003588 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003589 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003590 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003591 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003592 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003593
3594 ret = btrfs_fill_inode(inode, &rdev);
3595 if (!ret)
3596 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003597
3598 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003599 if (!path)
3600 goto make_bad;
3601
Chris Mason39279cc2007-06-12 06:35:45 -04003602 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003603
Chris Mason39279cc2007-06-12 06:35:45 -04003604 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003605 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003606 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003607
Chris Mason5f39d392007-10-15 16:14:19 -04003608 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003609
3610 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003611 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003612
Chris Mason5f39d392007-10-15 16:14:19 -04003613 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3614 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003615 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003616 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003617 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3618 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003619 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003620
David Sterbaa937b972014-12-12 17:39:12 +01003621 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3622 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003623
David Sterbaa937b972014-12-12 17:39:12 +01003624 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3625 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003626
David Sterbaa937b972014-12-12 17:39:12 +01003627 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3628 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003629
chandan r9cc97d62012-07-04 12:48:07 +05303630 BTRFS_I(inode)->i_otime.tv_sec =
3631 btrfs_timespec_sec(leaf, &inode_item->otime);
3632 BTRFS_I(inode)->i_otime.tv_nsec =
3633 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003634
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003635 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003636 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003637 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3638
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003639 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003640 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003641 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003642 rdev = btrfs_inode_rdev(leaf, inode_item);
3643
Josef Bacikaec74772008-07-24 12:12:38 -04003644 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003645 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003646
3647cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003648 /*
3649 * If we were modified in the current generation and evicted from memory
3650 * and then re-read we need to do a full sync since we don't have any
3651 * idea about which extents were modified before we were evicted from
3652 * cache.
3653 *
3654 * This is required for both inode re-read from disk and delayed inode
3655 * in delayed_nodes_tree.
3656 */
3657 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3658 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3659 &BTRFS_I(inode)->runtime_flags);
3660
Filipe Mananabde6c242015-07-24 00:00:19 +01003661 /*
3662 * We don't persist the id of the transaction where an unlink operation
3663 * against the inode was last made. So here we assume the inode might
3664 * have been evicted, and therefore the exact value of last_unlink_trans
3665 * lost, and set it to last_trans to avoid metadata inconsistencies
3666 * between the inode and its parent if the inode is fsync'ed and the log
3667 * replayed. For example, in the scenario:
3668 *
3669 * touch mydir/foo
3670 * ln mydir/foo mydir/bar
3671 * sync
3672 * unlink mydir/bar
3673 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3674 * xfs_io -c fsync mydir/foo
3675 * <power failure>
3676 * mount fs, triggers fsync log replay
3677 *
3678 * We must make sure that when we fsync our inode foo we also log its
3679 * parent inode, otherwise after log replay the parent still has the
3680 * dentry with the "bar" name but our inode foo has a link count of 1
3681 * and doesn't have an inode ref with the name "bar" anymore.
3682 *
3683 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3684 * but it guarantees correctness at the expense of ocassional full
3685 * transaction commits on fsync if our inode is a directory, or if our
3686 * inode is not a directory, logging its parent unnecessarily.
3687 */
3688 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3689
Miao Xie67de1172013-12-26 13:07:06 +08003690 path->slots[0]++;
3691 if (inode->i_nlink != 1 ||
3692 path->slots[0] >= btrfs_header_nritems(leaf))
3693 goto cache_acl;
3694
3695 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3696 if (location.objectid != btrfs_ino(inode))
3697 goto cache_acl;
3698
3699 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3700 if (location.type == BTRFS_INODE_REF_KEY) {
3701 struct btrfs_inode_ref *ref;
3702
3703 ref = (struct btrfs_inode_ref *)ptr;
3704 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3705 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3706 struct btrfs_inode_extref *extref;
3707
3708 extref = (struct btrfs_inode_extref *)ptr;
3709 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3710 extref);
3711 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003712cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003713 /*
3714 * try to precache a NULL acl entry for files that don't have
3715 * any xattrs or acls
3716 */
Li Zefan33345d012011-04-20 10:31:50 +08003717 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003718 btrfs_ino(inode), &first_xattr_slot);
3719 if (first_xattr_slot != -1) {
3720 path->slots[0] = first_xattr_slot;
3721 ret = btrfs_load_inode_props(inode, path);
3722 if (ret)
3723 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003724 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003725 btrfs_ino(inode),
3726 root->root_key.objectid, ret);
3727 }
3728 btrfs_free_path(path);
3729
Al Viro72c04902009-06-24 16:58:48 -04003730 if (!maybe_acls)
3731 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003732
Chris Mason39279cc2007-06-12 06:35:45 -04003733 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003734 case S_IFREG:
3735 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003736 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003737 inode->i_fop = &btrfs_file_operations;
3738 inode->i_op = &btrfs_file_inode_operations;
3739 break;
3740 case S_IFDIR:
3741 inode->i_fop = &btrfs_dir_file_operations;
3742 if (root == root->fs_info->tree_root)
3743 inode->i_op = &btrfs_dir_ro_inode_operations;
3744 else
3745 inode->i_op = &btrfs_dir_inode_operations;
3746 break;
3747 case S_IFLNK:
3748 inode->i_op = &btrfs_symlink_inode_operations;
3749 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3750 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003751 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003752 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003753 init_special_inode(inode, inode->i_mode, rdev);
3754 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003755 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003756
3757 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003758 return;
3759
3760make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003761 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003762 make_bad_inode(inode);
3763}
3764
Chris Masond352ac62008-09-29 15:18:18 -04003765/*
3766 * given a leaf and an inode, copy the inode fields into the leaf
3767 */
Chris Masone02119d2008-09-05 16:13:11 -04003768static void fill_inode_item(struct btrfs_trans_handle *trans,
3769 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003770 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003771 struct inode *inode)
3772{
Liu Bo51fab692012-12-27 09:01:21 +00003773 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003774
Liu Bo51fab692012-12-27 09:01:21 +00003775 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003776
Liu Bo51fab692012-12-27 09:01:21 +00003777 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3778 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3779 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3780 &token);
3781 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3782 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003783
David Sterbaa937b972014-12-12 17:39:12 +01003784 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003785 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003786 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003787 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003788
David Sterbaa937b972014-12-12 17:39:12 +01003789 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003790 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003791 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003792 inode->i_mtime.tv_nsec, &token);
3793
David Sterbaa937b972014-12-12 17:39:12 +01003794 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003795 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003796 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003797 inode->i_ctime.tv_nsec, &token);
3798
chandan r9cc97d62012-07-04 12:48:07 +05303799 btrfs_set_token_timespec_sec(leaf, &item->otime,
3800 BTRFS_I(inode)->i_otime.tv_sec, &token);
3801 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3802 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3803
Liu Bo51fab692012-12-27 09:01:21 +00003804 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3805 &token);
3806 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3807 &token);
3808 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3809 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3810 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3811 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3812 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003813}
3814
Chris Masond352ac62008-09-29 15:18:18 -04003815/*
3816 * copy everything in the in-memory inode into the btree.
3817 */
Chris Mason21151332011-11-10 20:39:08 -05003818static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003819 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003820{
3821 struct btrfs_inode_item *inode_item;
3822 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003823 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003824 int ret;
3825
3826 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003827 if (!path)
3828 return -ENOMEM;
3829
Chris Masonb9473432009-03-13 11:00:37 -04003830 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003831 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3832 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003833 if (ret) {
3834 if (ret > 0)
3835 ret = -ENOENT;
3836 goto failed;
3837 }
3838
Chris Mason5f39d392007-10-15 16:14:19 -04003839 leaf = path->nodes[0];
3840 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003841 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003842
Chris Masone02119d2008-09-05 16:13:11 -04003843 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003844 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003845 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003846 ret = 0;
3847failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003848 btrfs_free_path(path);
3849 return ret;
3850}
3851
Chris Masond352ac62008-09-29 15:18:18 -04003852/*
Chris Mason21151332011-11-10 20:39:08 -05003853 * copy everything in the in-memory inode into the btree.
3854 */
3855noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3856 struct btrfs_root *root, struct inode *inode)
3857{
3858 int ret;
3859
3860 /*
3861 * If the inode is a free space inode, we can deadlock during commit
3862 * if we put it into the delayed code.
3863 *
3864 * The data relocation inode should also be directly updated
3865 * without delay
3866 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003867 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003868 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3869 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003870 btrfs_update_root_times(trans, root);
3871
Chris Mason21151332011-11-10 20:39:08 -05003872 ret = btrfs_delayed_update_inode(trans, root, inode);
3873 if (!ret)
3874 btrfs_set_inode_last_trans(trans, inode);
3875 return ret;
3876 }
3877
3878 return btrfs_update_inode_item(trans, root, inode);
3879}
3880
Josef Bacikbe6aef62012-10-22 15:43:12 -04003881noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3882 struct btrfs_root *root,
3883 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003884{
3885 int ret;
3886
3887 ret = btrfs_update_inode(trans, root, inode);
3888 if (ret == -ENOSPC)
3889 return btrfs_update_inode_item(trans, root, inode);
3890 return ret;
3891}
3892
3893/*
Chris Masond352ac62008-09-29 15:18:18 -04003894 * unlink helper that gets used here in inode.c and in the tree logging
3895 * recovery code. It remove a link in a directory with a given name, and
3896 * also drops the back refs in the inode to the directory
3897 */
Al Viro92986792011-03-04 17:14:37 +00003898static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3899 struct btrfs_root *root,
3900 struct inode *dir, struct inode *inode,
3901 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003902{
3903 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003904 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003905 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003906 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003907 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003908 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003909 u64 ino = btrfs_ino(inode);
3910 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003911
3912 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003913 if (!path) {
3914 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003915 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003916 }
3917
Chris Masonb9473432009-03-13 11:00:37 -04003918 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003919 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003920 name, name_len, -1);
3921 if (IS_ERR(di)) {
3922 ret = PTR_ERR(di);
3923 goto err;
3924 }
3925 if (!di) {
3926 ret = -ENOENT;
3927 goto err;
3928 }
Chris Mason5f39d392007-10-15 16:14:19 -04003929 leaf = path->nodes[0];
3930 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003931 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003932 if (ret)
3933 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003934 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003935
Miao Xie67de1172013-12-26 13:07:06 +08003936 /*
3937 * If we don't have dir index, we have to get it by looking up
3938 * the inode ref, since we get the inode ref, remove it directly,
3939 * it is unnecessary to do delayed deletion.
3940 *
3941 * But if we have dir index, needn't search inode ref to get it.
3942 * Since the inode ref is close to the inode item, it is better
3943 * that we delay to delete it, and just do this deletion when
3944 * we update the inode item.
3945 */
3946 if (BTRFS_I(inode)->dir_index) {
3947 ret = btrfs_delayed_delete_inode_ref(inode);
3948 if (!ret) {
3949 index = BTRFS_I(inode)->dir_index;
3950 goto skip_backref;
3951 }
3952 }
3953
Li Zefan33345d012011-04-20 10:31:50 +08003954 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3955 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003956 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003957 btrfs_info(root->fs_info,
3958 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003959 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003960 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003961 goto err;
3962 }
Miao Xie67de1172013-12-26 13:07:06 +08003963skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003964 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003965 if (ret) {
3966 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003967 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003968 }
Chris Mason39279cc2007-06-12 06:35:45 -04003969
Chris Masone02119d2008-09-05 16:13:11 -04003970 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003971 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003972 if (ret != 0 && ret != -ENOENT) {
3973 btrfs_abort_transaction(trans, root, ret);
3974 goto err;
3975 }
Chris Masone02119d2008-09-05 16:13:11 -04003976
3977 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3978 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003979 if (ret == -ENOENT)
3980 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003981 else if (ret)
3982 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003983err:
3984 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003985 if (ret)
3986 goto out;
3987
3988 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003989 inode_inc_iversion(inode);
3990 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003991 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003992 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003993out:
Chris Mason39279cc2007-06-12 06:35:45 -04003994 return ret;
3995}
3996
Al Viro92986792011-03-04 17:14:37 +00003997int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3998 struct btrfs_root *root,
3999 struct inode *dir, struct inode *inode,
4000 const char *name, int name_len)
4001{
4002 int ret;
4003 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4004 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004005 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004006 ret = btrfs_update_inode(trans, root, inode);
4007 }
4008 return ret;
4009}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004010
4011/*
4012 * helper to start transaction for unlink and rmdir.
4013 *
Josef Bacikd52be812013-05-29 14:54:47 -04004014 * unlink and rmdir are special in btrfs, they do not always free space, so
4015 * if we cannot make our reservations the normal way try and see if there is
4016 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4017 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004018 */
Josef Bacikd52be812013-05-29 14:54:47 -04004019static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004020{
4021 struct btrfs_trans_handle *trans;
4022 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004023 int ret;
4024
Josef Bacike70bea52011-10-11 14:18:24 -04004025 /*
4026 * 1 for the possible orphan item
4027 * 1 for the dir item
4028 * 1 for the dir index
4029 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004030 * 1 for the inode
4031 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04004032 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004033 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
4034 return trans;
4035
Josef Bacikd52be812013-05-29 14:54:47 -04004036 if (PTR_ERR(trans) == -ENOSPC) {
4037 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004038
Josef Bacikd52be812013-05-29 14:54:47 -04004039 trans = btrfs_start_transaction(root, 0);
4040 if (IS_ERR(trans))
4041 return trans;
4042 ret = btrfs_cond_migrate_bytes(root->fs_info,
4043 &root->fs_info->trans_block_rsv,
4044 num_bytes, 5);
4045 if (ret) {
4046 btrfs_end_transaction(trans, root);
4047 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004048 }
Josef Bacik5a77d762011-11-01 14:32:23 -04004049 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04004050 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004051 }
Josef Bacikd52be812013-05-29 14:54:47 -04004052 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004053}
4054
Chris Mason39279cc2007-06-12 06:35:45 -04004055static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4056{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004057 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004058 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004059 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004060 int ret;
4061
Josef Bacikd52be812013-05-29 14:54:47 -04004062 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004063 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004064 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004065
David Howells2b0143b2015-03-17 22:25:59 +00004066 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004067
David Howells2b0143b2015-03-17 22:25:59 +00004068 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004069 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004070 if (ret)
4071 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004072
Yan, Zhenga22285a2010-05-16 10:48:46 -04004073 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004074 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004075 if (ret)
4076 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004077 }
Josef Bacik7b128762008-07-24 12:17:14 -04004078
Tsutomu Itohb5324022011-07-19 07:27:20 +00004079out:
Josef Bacikd52be812013-05-29 14:54:47 -04004080 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004081 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004082 return ret;
4083}
4084
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004085int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4086 struct btrfs_root *root,
4087 struct inode *dir, u64 objectid,
4088 const char *name, int name_len)
4089{
4090 struct btrfs_path *path;
4091 struct extent_buffer *leaf;
4092 struct btrfs_dir_item *di;
4093 struct btrfs_key key;
4094 u64 index;
4095 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004096 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004097
4098 path = btrfs_alloc_path();
4099 if (!path)
4100 return -ENOMEM;
4101
Li Zefan33345d012011-04-20 10:31:50 +08004102 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004103 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004104 if (IS_ERR_OR_NULL(di)) {
4105 if (!di)
4106 ret = -ENOENT;
4107 else
4108 ret = PTR_ERR(di);
4109 goto out;
4110 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004111
4112 leaf = path->nodes[0];
4113 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4114 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4115 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004116 if (ret) {
4117 btrfs_abort_transaction(trans, root, ret);
4118 goto out;
4119 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004120 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004121
4122 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4123 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004124 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004125 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004126 if (ret != -ENOENT) {
4127 btrfs_abort_transaction(trans, root, ret);
4128 goto out;
4129 }
Li Zefan33345d012011-04-20 10:31:50 +08004130 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004131 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004132 if (IS_ERR_OR_NULL(di)) {
4133 if (!di)
4134 ret = -ENOENT;
4135 else
4136 ret = PTR_ERR(di);
4137 btrfs_abort_transaction(trans, root, ret);
4138 goto out;
4139 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004140
4141 leaf = path->nodes[0];
4142 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004143 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004144 index = key.offset;
4145 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004146 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004147
Miao Xie16cdcec2011-04-22 18:12:22 +08004148 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004149 if (ret) {
4150 btrfs_abort_transaction(trans, root, ret);
4151 goto out;
4152 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004153
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004154 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004155 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004156 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004157 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004158 if (ret)
4159 btrfs_abort_transaction(trans, root, ret);
4160out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004161 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004162 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004163}
4164
Chris Mason39279cc2007-06-12 06:35:45 -04004165static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4166{
David Howells2b0143b2015-03-17 22:25:59 +00004167 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004168 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004169 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004170 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004171
David Sterbab3ae2442012-09-13 16:04:34 -06004172 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004173 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004174 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4175 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004176
Josef Bacikd52be812013-05-29 14:54:47 -04004177 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004178 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004179 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004180
Li Zefan33345d012011-04-20 10:31:50 +08004181 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004182 err = btrfs_unlink_subvol(trans, root, dir,
4183 BTRFS_I(inode)->location.objectid,
4184 dentry->d_name.name,
4185 dentry->d_name.len);
4186 goto out;
4187 }
4188
Josef Bacik7b128762008-07-24 12:17:14 -04004189 err = btrfs_orphan_add(trans, inode);
4190 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004191 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004192
Chris Mason39279cc2007-06-12 06:35:45 -04004193 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004194 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004195 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004196 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004197 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004198out:
Josef Bacikd52be812013-05-29 14:54:47 -04004199 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004200 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004201
Chris Mason39279cc2007-06-12 06:35:45 -04004202 return err;
4203}
4204
Chris Mason28f75a02015-02-04 06:59:29 -08004205static int truncate_space_check(struct btrfs_trans_handle *trans,
4206 struct btrfs_root *root,
4207 u64 bytes_deleted)
4208{
4209 int ret;
4210
4211 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
4212 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4213 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4214 if (!ret)
4215 trans->bytes_reserved += bytes_deleted;
4216 return ret;
4217
4218}
4219
Chris Mason323ac952008-10-01 19:05:46 -04004220/*
Chris Mason39279cc2007-06-12 06:35:45 -04004221 * this can truncate away extent items, csum items and directory items.
4222 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004223 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004224 *
4225 * csum items that cross the new i_size are truncated to the new size
4226 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004227 *
4228 * min_type is the minimum key type to truncate down to. If set to 0, this
4229 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004230 */
Yan, Zheng80825102009-11-12 09:35:36 +00004231int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4232 struct btrfs_root *root,
4233 struct inode *inode,
4234 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004235{
Chris Mason39279cc2007-06-12 06:35:45 -04004236 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004237 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004238 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004239 struct btrfs_key key;
4240 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004241 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004242 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004243 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004244 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004245 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004246 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004247 int found_extent;
4248 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004249 int pending_del_nr = 0;
4250 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004251 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004252 int ret;
4253 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004254 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004255 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004256 bool be_nice = 0;
4257 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004258 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004259
4260 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004261
Chris Mason28ed1342014-12-17 09:41:04 -08004262 /*
4263 * for non-free space inodes and ref cows, we want to back off from
4264 * time to time
4265 */
4266 if (!btrfs_is_free_space_inode(inode) &&
4267 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4268 be_nice = 1;
4269
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004270 path = btrfs_alloc_path();
4271 if (!path)
4272 return -ENOMEM;
4273 path->reada = -1;
4274
Josef Bacik5dc562c2012-08-17 13:14:17 -04004275 /*
4276 * We want to drop from the next block forward in case this new size is
4277 * not block aligned since we will be keeping the last block of the
4278 * extent just the way it is.
4279 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004280 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4281 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004282 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4283 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004284
Miao Xie16cdcec2011-04-22 18:12:22 +08004285 /*
4286 * This function is also used to drop the items in the log tree before
4287 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4288 * it is used to drop the loged items. So we shouldn't kill the delayed
4289 * items.
4290 */
4291 if (min_type == 0 && root == BTRFS_I(inode)->root)
4292 btrfs_kill_delayed_inode_items(inode);
4293
Li Zefan33345d012011-04-20 10:31:50 +08004294 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004295 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004296 key.type = (u8)-1;
4297
Chris Mason85e21ba2008-01-29 15:11:36 -05004298search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004299 /*
4300 * with a 16K leaf size and 128MB extents, you can actually queue
4301 * up a huge file in a single leaf. Most of the time that
4302 * bytes_deleted is > 0, it will be huge by the time we get here
4303 */
4304 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
4305 if (btrfs_should_end_transaction(trans, root)) {
4306 err = -EAGAIN;
4307 goto error;
4308 }
4309 }
4310
4311
Chris Masonb9473432009-03-13 11:00:37 -04004312 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004313 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004314 if (ret < 0) {
4315 err = ret;
4316 goto out;
4317 }
Chris Masond3977122009-01-05 21:25:51 -05004318
Chris Mason85e21ba2008-01-29 15:11:36 -05004319 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004320 /* there are no items in the tree for us to truncate, we're
4321 * done
4322 */
Yan, Zheng80825102009-11-12 09:35:36 +00004323 if (path->slots[0] == 0)
4324 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004325 path->slots[0]--;
4326 }
4327
Chris Masond3977122009-01-05 21:25:51 -05004328 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004329 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004330 leaf = path->nodes[0];
4331 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004332 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004333
Li Zefan33345d012011-04-20 10:31:50 +08004334 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004335 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004336
Chris Mason85e21ba2008-01-29 15:11:36 -05004337 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004338 break;
4339
Chris Mason5f39d392007-10-15 16:14:19 -04004340 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004341 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004342 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004343 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004344 extent_type = btrfs_file_extent_type(leaf, fi);
4345 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004346 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004347 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004348 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004349 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004350 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004351 }
Yan008630c2007-11-07 13:31:09 -05004352 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004353 }
Yan, Zheng80825102009-11-12 09:35:36 +00004354 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004355 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004356 } else {
4357 if (item_end < new_size)
4358 break;
4359 if (found_key.offset >= new_size)
4360 del_item = 1;
4361 else
4362 del_item = 0;
4363 }
Chris Mason39279cc2007-06-12 06:35:45 -04004364 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004365 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004366 if (found_type != BTRFS_EXTENT_DATA_KEY)
4367 goto delete;
4368
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004369 if (del_item)
4370 last_size = found_key.offset;
4371 else
4372 last_size = new_size;
4373
Chris Mason179e29e2007-11-01 11:28:41 -04004374 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004375 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004376 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004377 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004378 u64 orig_num_bytes =
4379 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004380 extent_num_bytes = ALIGN(new_size -
4381 found_key.offset,
4382 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004383 btrfs_set_file_extent_num_bytes(leaf, fi,
4384 extent_num_bytes);
4385 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004386 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004387 if (test_bit(BTRFS_ROOT_REF_COWS,
4388 &root->state) &&
4389 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004390 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004391 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004392 } else {
Chris Masondb945352007-10-15 16:15:53 -04004393 extent_num_bytes =
4394 btrfs_file_extent_disk_num_bytes(leaf,
4395 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004396 extent_offset = found_key.offset -
4397 btrfs_file_extent_offset(leaf, fi);
4398
Chris Mason39279cc2007-06-12 06:35:45 -04004399 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004400 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004401 if (extent_start != 0) {
4402 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004403 if (test_bit(BTRFS_ROOT_REF_COWS,
4404 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004405 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004406 }
4407 }
Chris Mason90692182008-02-08 13:49:28 -05004408 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004409 /*
4410 * we can't truncate inline items that have had
4411 * special encodings
4412 */
4413 if (!del_item &&
4414 btrfs_file_extent_compression(leaf, fi) == 0 &&
4415 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4416 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004417 u32 size = new_size - found_key.offset;
4418
Miao Xie27cdeb72014-04-02 19:51:05 +08004419 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004420 inode_sub_bytes(inode, item_end + 1 -
4421 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004422
4423 /*
4424 * update the ram bytes to properly reflect
4425 * the new size of our item
4426 */
4427 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004428 size =
4429 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004430 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004431 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4432 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004433 inode_sub_bytes(inode, item_end + 1 -
4434 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004435 }
Chris Mason39279cc2007-06-12 06:35:45 -04004436 }
Chris Mason179e29e2007-11-01 11:28:41 -04004437delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004438 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004439 if (!pending_del_nr) {
4440 /* no pending yet, add ourselves */
4441 pending_del_slot = path->slots[0];
4442 pending_del_nr = 1;
4443 } else if (pending_del_nr &&
4444 path->slots[0] + 1 == pending_del_slot) {
4445 /* hop on the pending chunk */
4446 pending_del_nr++;
4447 pending_del_slot = path->slots[0];
4448 } else {
Chris Masond3977122009-01-05 21:25:51 -05004449 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004450 }
Chris Mason39279cc2007-06-12 06:35:45 -04004451 } else {
4452 break;
4453 }
Chris Mason28f75a02015-02-04 06:59:29 -08004454 should_throttle = 0;
4455
Miao Xie27cdeb72014-04-02 19:51:05 +08004456 if (found_extent &&
4457 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4458 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004459 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004460 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004461 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004462 extent_num_bytes, 0,
4463 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004464 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004465 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004466 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004467 btrfs_async_run_delayed_refs(root,
4468 trans->delayed_ref_updates * 2, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004469 if (be_nice) {
4470 if (truncate_space_check(trans, root,
4471 extent_num_bytes)) {
4472 should_end = 1;
4473 }
4474 if (btrfs_should_throttle_delayed_refs(trans,
4475 root)) {
4476 should_throttle = 1;
4477 }
4478 }
Chris Mason39279cc2007-06-12 06:35:45 -04004479 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004480
Yan, Zheng80825102009-11-12 09:35:36 +00004481 if (found_type == BTRFS_INODE_ITEM_KEY)
4482 break;
4483
4484 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004485 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004486 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004487 if (pending_del_nr) {
4488 ret = btrfs_del_items(trans, root, path,
4489 pending_del_slot,
4490 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004491 if (ret) {
4492 btrfs_abort_transaction(trans,
4493 root, ret);
4494 goto error;
4495 }
Yan, Zheng80825102009-11-12 09:35:36 +00004496 pending_del_nr = 0;
4497 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004498 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004499 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004500 unsigned long updates = trans->delayed_ref_updates;
4501 if (updates) {
4502 trans->delayed_ref_updates = 0;
4503 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4504 if (ret && !err)
4505 err = ret;
4506 }
4507 }
Chris Mason28f75a02015-02-04 06:59:29 -08004508 /*
4509 * if we failed to refill our space rsv, bail out
4510 * and let the transaction restart
4511 */
4512 if (should_end) {
4513 err = -EAGAIN;
4514 goto error;
4515 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004516 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004517 } else {
4518 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004519 }
Chris Mason39279cc2007-06-12 06:35:45 -04004520 }
Yan, Zheng80825102009-11-12 09:35:36 +00004521out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004522 if (pending_del_nr) {
4523 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4524 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004525 if (ret)
4526 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004527 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004528error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004529 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004530 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004531
Chris Mason39279cc2007-06-12 06:35:45 -04004532 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004533
Chris Mason28f75a02015-02-04 06:59:29 -08004534 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
Chris Mason28ed1342014-12-17 09:41:04 -08004535 unsigned long updates = trans->delayed_ref_updates;
4536 if (updates) {
4537 trans->delayed_ref_updates = 0;
4538 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4539 if (ret && !err)
4540 err = ret;
4541 }
4542 }
Yan, Zheng80825102009-11-12 09:35:36 +00004543 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004544}
4545
Chris Masona52d9a82007-08-27 16:49:44 -04004546/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004547 * btrfs_truncate_page - read, zero a chunk and write a page
4548 * @inode - inode that we're zeroing
4549 * @from - the offset to start zeroing
4550 * @len - the length to zero, 0 to zero the entire range respective to the
4551 * offset
4552 * @front - zero up to the offset instead of from the offset on
4553 *
4554 * This will find the page for the "from" offset and cow the page and zero the
4555 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004556 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004557int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4558 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004559{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004560 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004561 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004562 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4563 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004564 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004565 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004566 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004567 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4568 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4569 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004570 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004571 int ret = 0;
4572 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004573 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004574
Josef Bacik2aaa6652012-08-29 14:27:18 -04004575 if ((offset & (blocksize - 1)) == 0 &&
4576 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004577 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004578 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004579 if (ret)
4580 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004581
Chris Mason211c17f2008-05-15 09:13:45 -04004582again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004583 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004584 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004585 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004586 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004587 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004588 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004589
4590 page_start = page_offset(page);
4591 page_end = page_start + PAGE_CACHE_SIZE - 1;
4592
Chris Masona52d9a82007-08-27 16:49:44 -04004593 if (!PageUptodate(page)) {
4594 ret = btrfs_readpage(NULL, page);
4595 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004596 if (page->mapping != mapping) {
4597 unlock_page(page);
4598 page_cache_release(page);
4599 goto again;
4600 }
Chris Masona52d9a82007-08-27 16:49:44 -04004601 if (!PageUptodate(page)) {
4602 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004603 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004604 }
4605 }
Chris Mason211c17f2008-05-15 09:13:45 -04004606 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004607
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004608 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004609 set_page_extent_mapped(page);
4610
4611 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4612 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004613 unlock_extent_cached(io_tree, page_start, page_end,
4614 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004615 unlock_page(page);
4616 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004617 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004618 btrfs_put_ordered_extent(ordered);
4619 goto again;
4620 }
4621
Josef Bacik2ac55d42010-02-03 19:33:23 +00004622 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004623 EXTENT_DIRTY | EXTENT_DELALLOC |
4624 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004625 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004626
Josef Bacik2ac55d42010-02-03 19:33:23 +00004627 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4628 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004629 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004630 unlock_extent_cached(io_tree, page_start, page_end,
4631 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004632 goto out_unlock;
4633 }
4634
Chris Masone6dcd2d2008-07-17 12:53:50 -04004635 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004636 if (!len)
4637 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004638 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004639 if (front)
4640 memset(kaddr, 0, offset);
4641 else
4642 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004643 flush_dcache_page(page);
4644 kunmap(page);
4645 }
Chris Mason247e7432008-07-17 12:53:51 -04004646 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004647 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004648 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4649 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004650
Chris Mason89642222008-07-24 09:41:53 -04004651out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004652 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004653 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004654 unlock_page(page);
4655 page_cache_release(page);
4656out:
4657 return ret;
4658}
4659
Josef Bacik16e75492013-10-22 12:18:51 -04004660static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4661 u64 offset, u64 len)
4662{
4663 struct btrfs_trans_handle *trans;
4664 int ret;
4665
4666 /*
4667 * Still need to make sure the inode looks like it's been updated so
4668 * that any holes get logged if we fsync.
4669 */
4670 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4671 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4672 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4673 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4674 return 0;
4675 }
4676
4677 /*
4678 * 1 - for the one we're dropping
4679 * 1 - for the one we're adding
4680 * 1 - for updating the inode.
4681 */
4682 trans = btrfs_start_transaction(root, 3);
4683 if (IS_ERR(trans))
4684 return PTR_ERR(trans);
4685
4686 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4687 if (ret) {
4688 btrfs_abort_transaction(trans, root, ret);
4689 btrfs_end_transaction(trans, root);
4690 return ret;
4691 }
4692
4693 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4694 0, 0, len, 0, len, 0, 0, 0);
4695 if (ret)
4696 btrfs_abort_transaction(trans, root, ret);
4697 else
4698 btrfs_update_inode(trans, root, inode);
4699 btrfs_end_transaction(trans, root);
4700 return ret;
4701}
4702
Josef Bacik695a0d02011-03-04 15:46:53 -05004703/*
4704 * This function puts in dummy file extents for the area we're creating a hole
4705 * for. So if we are truncating this file to a larger size we need to insert
4706 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4707 * the range between oldsize and size
4708 */
Josef Bacika41ad392011-01-31 15:30:16 -05004709int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004710{
Yan Zheng9036c102008-10-30 14:19:41 -04004711 struct btrfs_root *root = BTRFS_I(inode)->root;
4712 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004713 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004714 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004715 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004716 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4717 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004718 u64 last_byte;
4719 u64 cur_offset;
4720 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004721 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004722
Josef Bacika71754f2013-06-17 17:14:39 -04004723 /*
4724 * If our size started in the middle of a page we need to zero out the
4725 * rest of the page before we expand the i_size, otherwise we could
4726 * expose stale data.
4727 */
4728 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4729 if (err)
4730 return err;
4731
Yan Zheng9036c102008-10-30 14:19:41 -04004732 if (size <= hole_start)
4733 return 0;
4734
Yan Zheng9036c102008-10-30 14:19:41 -04004735 while (1) {
4736 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004737
Josef Bacik2ac55d42010-02-03 19:33:23 +00004738 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004739 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004740 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4741 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004742 if (!ordered)
4743 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004744 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4745 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004746 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004747 btrfs_put_ordered_extent(ordered);
4748 }
4749
Yan Zheng9036c102008-10-30 14:19:41 -04004750 cur_offset = hole_start;
4751 while (1) {
4752 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4753 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004754 if (IS_ERR(em)) {
4755 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004756 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004757 break;
4758 }
Yan Zheng9036c102008-10-30 14:19:41 -04004759 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004760 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004761 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004762 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004763 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004764
Josef Bacik16e75492013-10-22 12:18:51 -04004765 err = maybe_insert_hole(root, inode, cur_offset,
4766 hole_size);
4767 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004768 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004769 btrfs_drop_extent_cache(inode, cur_offset,
4770 cur_offset + hole_size - 1, 0);
4771 hole_em = alloc_extent_map();
4772 if (!hole_em) {
4773 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4774 &BTRFS_I(inode)->runtime_flags);
4775 goto next;
4776 }
4777 hole_em->start = cur_offset;
4778 hole_em->len = hole_size;
4779 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004780
Josef Bacik5dc562c2012-08-17 13:14:17 -04004781 hole_em->block_start = EXTENT_MAP_HOLE;
4782 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004783 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004784 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004785 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4786 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004787 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004788
4789 while (1) {
4790 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004791 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004792 write_unlock(&em_tree->lock);
4793 if (err != -EEXIST)
4794 break;
4795 btrfs_drop_extent_cache(inode, cur_offset,
4796 cur_offset +
4797 hole_size - 1, 0);
4798 }
4799 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004800 }
Josef Bacik16e75492013-10-22 12:18:51 -04004801next:
Yan Zheng9036c102008-10-30 14:19:41 -04004802 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004803 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004804 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004805 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004806 break;
4807 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004808 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004809 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4810 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004811 return err;
4812}
4813
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004814static int wait_snapshoting_atomic_t(atomic_t *a)
4815{
4816 schedule();
4817 return 0;
4818}
4819
4820static void wait_for_snapshot_creation(struct btrfs_root *root)
4821{
4822 while (true) {
4823 int ret;
4824
4825 ret = btrfs_start_write_no_snapshoting(root);
4826 if (ret)
4827 break;
4828 wait_on_atomic_t(&root->will_be_snapshoted,
4829 wait_snapshoting_atomic_t,
4830 TASK_UNINTERRUPTIBLE);
4831 }
4832}
4833
Eric Sandeen3972f262013-01-12 02:57:22 +00004834static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004835{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004836 struct btrfs_root *root = BTRFS_I(inode)->root;
4837 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004838 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004839 loff_t newsize = attr->ia_size;
4840 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004841 int ret;
4842
Eric Sandeen3972f262013-01-12 02:57:22 +00004843 /*
4844 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4845 * special case where we need to update the times despite not having
4846 * these flags set. For all other operations the VFS set these flags
4847 * explicitly if it wants a timestamp update.
4848 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004849 if (newsize != oldsize) {
4850 inode_inc_iversion(inode);
4851 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4852 inode->i_ctime = inode->i_mtime =
4853 current_fs_time(inode->i_sb);
4854 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004855
Josef Bacika41ad392011-01-31 15:30:16 -05004856 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004857 truncate_pagecache(inode, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004858 /*
4859 * Don't do an expanding truncate while snapshoting is ongoing.
4860 * This is to ensure the snapshot captures a fully consistent
4861 * state of this file - if the snapshot captures this expanding
4862 * truncation, it must capture all writes that happened before
4863 * this truncation.
4864 */
4865 wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004866 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004867 if (ret) {
4868 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004869 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004870 }
Yan, Zheng80825102009-11-12 09:35:36 +00004871
Miao Xief4a2f4c2011-12-14 20:12:01 -05004872 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004873 if (IS_ERR(trans)) {
4874 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004875 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004876 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004877
4878 i_size_write(inode, newsize);
4879 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4880 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004881 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004882 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004883 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004884
Josef Bacika41ad392011-01-31 15:30:16 -05004885 /*
4886 * We're truncating a file that used to have good data down to
4887 * zero. Make sure it gets into the ordered flush list so that
4888 * any new writes get down to disk quickly.
4889 */
4890 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004891 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4892 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004893
Josef Bacikf3fe8202013-01-07 17:03:21 -05004894 /*
4895 * 1 for the orphan item we're going to add
4896 * 1 for the orphan item deletion.
4897 */
4898 trans = btrfs_start_transaction(root, 2);
4899 if (IS_ERR(trans))
4900 return PTR_ERR(trans);
4901
4902 /*
4903 * We need to do this in case we fail at _any_ point during the
4904 * actual truncate. Once we do the truncate_setsize we could
4905 * invalidate pages which forces any outstanding ordered io to
4906 * be instantly completed which will give us extents that need
4907 * to be truncated. If we fail to get an orphan inode down we
4908 * could have left over extents that were never meant to live,
4909 * so we need to garuntee from this point on that everything
4910 * will be consistent.
4911 */
4912 ret = btrfs_orphan_add(trans, inode);
4913 btrfs_end_transaction(trans, root);
4914 if (ret)
4915 return ret;
4916
Josef Bacika41ad392011-01-31 15:30:16 -05004917 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4918 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004919
4920 /* Disable nonlocked read DIO to avoid the end less truncate */
4921 btrfs_inode_block_unlocked_dio(inode);
4922 inode_dio_wait(inode);
4923 btrfs_inode_resume_unlocked_dio(inode);
4924
Josef Bacika41ad392011-01-31 15:30:16 -05004925 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004926 if (ret && inode->i_nlink) {
4927 int err;
4928
4929 /*
4930 * failed to truncate, disk_i_size is only adjusted down
4931 * as we remove extents, so it should represent the true
4932 * size of the inode, so reset the in memory size and
4933 * delete our orphan entry.
4934 */
4935 trans = btrfs_join_transaction(root);
4936 if (IS_ERR(trans)) {
4937 btrfs_orphan_del(NULL, inode);
4938 return ret;
4939 }
4940 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4941 err = btrfs_orphan_del(trans, inode);
4942 if (err)
4943 btrfs_abort_transaction(trans, root, err);
4944 btrfs_end_transaction(trans, root);
4945 }
Yan, Zheng80825102009-11-12 09:35:36 +00004946 }
4947
Josef Bacika41ad392011-01-31 15:30:16 -05004948 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004949}
4950
Chris Mason39279cc2007-06-12 06:35:45 -04004951static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4952{
David Howells2b0143b2015-03-17 22:25:59 +00004953 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004954 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004955 int err;
4956
Li Zefanb83cc962010-12-20 16:04:08 +08004957 if (btrfs_root_readonly(root))
4958 return -EROFS;
4959
Chris Mason39279cc2007-06-12 06:35:45 -04004960 err = inode_change_ok(inode, attr);
4961 if (err)
4962 return err;
4963
Chris Mason5a3f23d2009-03-31 13:27:11 -04004964 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004965 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004966 if (err)
4967 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004968 }
Yan Zheng9036c102008-10-30 14:19:41 -04004969
Christoph Hellwig10257742010-06-04 11:30:02 +02004970 if (attr->ia_valid) {
4971 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004972 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004973 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004974
Josef Bacik22c44fe2011-11-30 10:45:38 -05004975 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004976 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004977 }
4978
Chris Mason39279cc2007-06-12 06:35:45 -04004979 return err;
4980}
Chris Mason61295eb2008-01-14 16:24:38 -05004981
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004982/*
4983 * While truncating the inode pages during eviction, we get the VFS calling
4984 * btrfs_invalidatepage() against each page of the inode. This is slow because
4985 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4986 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4987 * extent_state structures over and over, wasting lots of time.
4988 *
4989 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4990 * those expensive operations on a per page basis and do only the ordered io
4991 * finishing, while we release here the extent_map and extent_state structures,
4992 * without the excessive merging and splitting.
4993 */
4994static void evict_inode_truncate_pages(struct inode *inode)
4995{
4996 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4997 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4998 struct rb_node *node;
4999
5000 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005001 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005002
5003 write_lock(&map_tree->lock);
5004 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5005 struct extent_map *em;
5006
5007 node = rb_first(&map_tree->map);
5008 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005009 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5010 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005011 remove_extent_mapping(map_tree, em);
5012 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005013 if (need_resched()) {
5014 write_unlock(&map_tree->lock);
5015 cond_resched();
5016 write_lock(&map_tree->lock);
5017 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005018 }
5019 write_unlock(&map_tree->lock);
5020
Filipe Manana6ca07092015-05-26 00:55:42 +01005021 /*
5022 * Keep looping until we have no more ranges in the io tree.
5023 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005024 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5025 * still in progress (unlocked the pages in the bio but did not yet
5026 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005027 * ranges can still be locked and eviction started because before
5028 * submitting those bios, which are executed by a separate task (work
5029 * queue kthread), inode references (inode->i_count) were not taken
5030 * (which would be dropped in the end io callback of each bio).
5031 * Therefore here we effectively end up waiting for those bios and
5032 * anyone else holding locked ranges without having bumped the inode's
5033 * reference count - if we don't do it, when they access the inode's
5034 * io_tree to unlock a range it may be too late, leading to an
5035 * use-after-free issue.
5036 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005037 spin_lock(&io_tree->lock);
5038 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5039 struct extent_state *state;
5040 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005041 u64 start;
5042 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005043
5044 node = rb_first(&io_tree->state);
5045 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005046 start = state->start;
5047 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005048 spin_unlock(&io_tree->lock);
5049
Filipe Manana6ca07092015-05-26 00:55:42 +01005050 lock_extent_bits(io_tree, start, end, 0, &cached_state);
5051 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005052 EXTENT_LOCKED | EXTENT_DIRTY |
5053 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5054 EXTENT_DEFRAG, 1, 1,
5055 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005056
Filipe Manana7064dd52014-08-08 02:47:05 +01005057 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005058 spin_lock(&io_tree->lock);
5059 }
5060 spin_unlock(&io_tree->lock);
5061}
5062
Al Virobd555972010-06-07 11:35:40 -04005063void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005064{
5065 struct btrfs_trans_handle *trans;
5066 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005067 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005068 int steal_from_global = 0;
Josef Bacik07127182011-08-19 10:29:59 -04005069 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04005070 int ret;
5071
liubo1abe9b82011-03-24 11:18:59 +00005072 trace_btrfs_inode_evict(inode);
5073
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005074 evict_inode_truncate_pages(inode);
5075
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005076 if (inode->i_nlink &&
5077 ((btrfs_root_refs(&root->root_item) != 0 &&
5078 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5079 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005080 goto no_delete;
5081
Chris Mason39279cc2007-06-12 06:35:45 -04005082 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005083 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005084 goto no_delete;
5085 }
Al Virobd555972010-06-07 11:35:40 -04005086 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005087 if (!special_file(inode->i_mode))
5088 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005089
Miao Xief6124962014-09-12 18:44:04 +08005090 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5091
Yan, Zhengc71bf092009-11-12 09:34:40 +00005092 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06005093 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005094 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005095 goto no_delete;
5096 }
5097
Yan, Zheng76dda932009-09-21 16:00:26 -04005098 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005099 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5100 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005101 goto no_delete;
5102 }
5103
Miao Xie0e8c36a2012-12-19 06:59:51 +00005104 ret = btrfs_commit_inode_delayed_inode(inode);
5105 if (ret) {
5106 btrfs_orphan_del(NULL, inode);
5107 goto no_delete;
5108 }
5109
Miao Xie66d8f3d2012-09-06 04:02:28 -06005110 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005111 if (!rsv) {
5112 btrfs_orphan_del(NULL, inode);
5113 goto no_delete;
5114 }
Josef Bacik4a338542011-08-29 11:01:31 -04005115 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005116 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005117 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005118
Chris Masondbe674a2008-07-17 12:54:05 -04005119 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005120
Josef Bacik4289a662011-08-05 13:22:24 -04005121 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005122 * This is a bit simpler than btrfs_truncate since we've already
5123 * reserved our space for our orphan item in the unlink, so we just
5124 * need to reserve some slack space in case we add bytes and update
5125 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005126 */
Yan, Zheng80825102009-11-12 09:35:36 +00005127 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005128 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5129 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005130
Josef Bacik726c35f2011-09-26 15:46:06 -04005131 /*
5132 * Try and steal from the global reserve since we will
5133 * likely not use this space anyway, we want to try as
5134 * hard as possible to get this to work.
5135 */
5136 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005137 steal_from_global++;
5138 else
5139 steal_from_global = 0;
5140 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005141
Josef Bacik3bce8762015-02-24 12:35:51 -08005142 /*
5143 * steal_from_global == 0: we reserved stuff, hooray!
5144 * steal_from_global == 1: we didn't reserve stuff, boo!
5145 * steal_from_global == 2: we've committed, still not a lot of
5146 * room but maybe we'll have room in the global reserve this
5147 * time.
5148 * steal_from_global == 3: abandon all hope!
5149 */
5150 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005151 btrfs_warn(root->fs_info,
5152 "Could not get space for a delete, will truncate on mount %d",
5153 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005154 btrfs_orphan_del(NULL, inode);
5155 btrfs_free_block_rsv(root, rsv);
5156 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005157 }
5158
Miao Xie0e8c36a2012-12-19 06:59:51 +00005159 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005160 if (IS_ERR(trans)) {
5161 btrfs_orphan_del(NULL, inode);
5162 btrfs_free_block_rsv(root, rsv);
5163 goto no_delete;
5164 }
5165
Josef Bacik3bce8762015-02-24 12:35:51 -08005166 /*
5167 * We can't just steal from the global reserve, we need tomake
5168 * sure there is room to do it, if not we need to commit and try
5169 * again.
5170 */
5171 if (steal_from_global) {
5172 if (!btrfs_check_space_for_delayed_refs(trans, root))
5173 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5174 min_size);
5175 else
5176 ret = -ENOSPC;
5177 }
5178
5179 /*
5180 * Couldn't steal from the global reserve, we have too much
5181 * pending stuff built up, commit the transaction and try it
5182 * again.
5183 */
5184 if (ret) {
5185 ret = btrfs_commit_transaction(trans, root);
5186 if (ret) {
5187 btrfs_orphan_del(NULL, inode);
5188 btrfs_free_block_rsv(root, rsv);
5189 goto no_delete;
5190 }
5191 continue;
5192 } else {
5193 steal_from_global = 0;
5194 }
5195
Josef Bacik4289a662011-08-05 13:22:24 -04005196 trans->block_rsv = rsv;
5197
Yan, Zhengd68fc572010-05-16 10:49:58 -04005198 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005199 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005200 break;
5201
Miao Xie8407aa42012-09-07 01:43:32 -06005202 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005203 btrfs_end_transaction(trans, root);
5204 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005205 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005206 }
5207
Josef Bacik4289a662011-08-05 13:22:24 -04005208 btrfs_free_block_rsv(root, rsv);
5209
Josef Bacik4ef31a42013-08-13 14:10:08 -04005210 /*
5211 * Errors here aren't a big deal, it just means we leave orphan items
5212 * in the tree. They will be cleaned up on the next mount.
5213 */
Yan, Zheng80825102009-11-12 09:35:36 +00005214 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005215 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005216 btrfs_orphan_del(trans, inode);
5217 } else {
5218 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005219 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005220
Josef Bacik4289a662011-08-05 13:22:24 -04005221 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005222 if (!(root == root->fs_info->tree_root ||
5223 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005224 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005225
Chris Mason54aa1f42007-06-22 14:16:25 -04005226 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005227 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005228no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005229 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005230 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005231 return;
Chris Mason39279cc2007-06-12 06:35:45 -04005232}
5233
5234/*
5235 * this returns the key found in the dir entry in the location pointer.
5236 * If no dir entries were found, location->objectid is 0.
5237 */
5238static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5239 struct btrfs_key *location)
5240{
5241 const char *name = dentry->d_name.name;
5242 int namelen = dentry->d_name.len;
5243 struct btrfs_dir_item *di;
5244 struct btrfs_path *path;
5245 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005246 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005247
5248 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005249 if (!path)
5250 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005251
Li Zefan33345d012011-04-20 10:31:50 +08005252 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005253 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005254 if (IS_ERR(di))
5255 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005256
David Sterbac7040052011-04-19 18:00:01 +02005257 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005258 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005259
Chris Mason5f39d392007-10-15 16:14:19 -04005260 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005261out:
Chris Mason39279cc2007-06-12 06:35:45 -04005262 btrfs_free_path(path);
5263 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005264out_err:
5265 location->objectid = 0;
5266 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005267}
5268
5269/*
5270 * when we hit a tree root in a directory, the btrfs part of the inode
5271 * needs to be changed to reflect the root directory of the tree root. This
5272 * is kind of like crossing a mount point.
5273 */
5274static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005275 struct inode *dir,
5276 struct dentry *dentry,
5277 struct btrfs_key *location,
5278 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005279{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005280 struct btrfs_path *path;
5281 struct btrfs_root *new_root;
5282 struct btrfs_root_ref *ref;
5283 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005284 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005285 int ret;
5286 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005287
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005288 path = btrfs_alloc_path();
5289 if (!path) {
5290 err = -ENOMEM;
5291 goto out;
5292 }
Chris Mason39279cc2007-06-12 06:35:45 -04005293
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005294 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005295 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5296 key.type = BTRFS_ROOT_REF_KEY;
5297 key.offset = location->objectid;
5298
5299 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5300 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005301 if (ret) {
5302 if (ret < 0)
5303 err = ret;
5304 goto out;
5305 }
Chris Mason39279cc2007-06-12 06:35:45 -04005306
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005307 leaf = path->nodes[0];
5308 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005309 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005310 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5311 goto out;
5312
5313 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5314 (unsigned long)(ref + 1),
5315 dentry->d_name.len);
5316 if (ret)
5317 goto out;
5318
David Sterbab3b4aa72011-04-21 01:20:15 +02005319 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005320
5321 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5322 if (IS_ERR(new_root)) {
5323 err = PTR_ERR(new_root);
5324 goto out;
5325 }
5326
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005327 *sub_root = new_root;
5328 location->objectid = btrfs_root_dirid(&new_root->root_item);
5329 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005330 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005331 err = 0;
5332out:
5333 btrfs_free_path(path);
5334 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005335}
5336
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005337static void inode_tree_add(struct inode *inode)
5338{
5339 struct btrfs_root *root = BTRFS_I(inode)->root;
5340 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005341 struct rb_node **p;
5342 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005343 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005344 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005345
Al Viro1d3382cb2010-10-23 15:19:20 -04005346 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005347 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005348 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005349 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005350 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005351 while (*p) {
5352 parent = *p;
5353 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5354
Li Zefan33345d012011-04-20 10:31:50 +08005355 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005356 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005357 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005358 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005359 else {
5360 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005361 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005362 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005363 RB_CLEAR_NODE(parent);
5364 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005365 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005366 }
5367 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005368 rb_link_node(new, parent, p);
5369 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005370 spin_unlock(&root->inode_lock);
5371}
5372
5373static void inode_tree_del(struct inode *inode)
5374{
5375 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005376 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005377
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005378 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005379 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005380 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005381 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005382 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005383 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005384 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005385
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005386 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005387 synchronize_srcu(&root->fs_info->subvol_srcu);
5388 spin_lock(&root->inode_lock);
5389 empty = RB_EMPTY_ROOT(&root->inode_tree);
5390 spin_unlock(&root->inode_lock);
5391 if (empty)
5392 btrfs_add_dead_root(root);
5393 }
5394}
5395
Jeff Mahoney143bede2012-03-01 14:56:26 +01005396void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005397{
5398 struct rb_node *node;
5399 struct rb_node *prev;
5400 struct btrfs_inode *entry;
5401 struct inode *inode;
5402 u64 objectid = 0;
5403
Liu Bo7813b3d2014-02-10 17:37:25 +08005404 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5405 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005406
5407 spin_lock(&root->inode_lock);
5408again:
5409 node = root->inode_tree.rb_node;
5410 prev = NULL;
5411 while (node) {
5412 prev = node;
5413 entry = rb_entry(node, struct btrfs_inode, rb_node);
5414
Li Zefan33345d012011-04-20 10:31:50 +08005415 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005416 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005417 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005418 node = node->rb_right;
5419 else
5420 break;
5421 }
5422 if (!node) {
5423 while (prev) {
5424 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005425 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005426 node = prev;
5427 break;
5428 }
5429 prev = rb_next(prev);
5430 }
5431 }
5432 while (node) {
5433 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005434 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005435 inode = igrab(&entry->vfs_inode);
5436 if (inode) {
5437 spin_unlock(&root->inode_lock);
5438 if (atomic_read(&inode->i_count) > 1)
5439 d_prune_aliases(inode);
5440 /*
Al Viro45321ac2010-06-07 13:43:19 -04005441 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005442 * the inode cache when its usage count
5443 * hits zero.
5444 */
5445 iput(inode);
5446 cond_resched();
5447 spin_lock(&root->inode_lock);
5448 goto again;
5449 }
5450
5451 if (cond_resched_lock(&root->inode_lock))
5452 goto again;
5453
5454 node = rb_next(node);
5455 }
5456 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005457}
5458
Chris Masone02119d2008-09-05 16:13:11 -04005459static int btrfs_init_locked_inode(struct inode *inode, void *p)
5460{
5461 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005462 inode->i_ino = args->location->objectid;
5463 memcpy(&BTRFS_I(inode)->location, args->location,
5464 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005465 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005466 return 0;
5467}
5468
5469static int btrfs_find_actor(struct inode *inode, void *opaque)
5470{
5471 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005472 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005473 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005474}
5475
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005476static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005477 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005478 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005479{
5480 struct inode *inode;
5481 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005482 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005483
Chris Mason90d3e592014-01-09 17:28:00 -08005484 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005485 args.root = root;
5486
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005487 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005488 btrfs_init_locked_inode,
5489 (void *)&args);
5490 return inode;
5491}
5492
Balaji Rao1a54ef82008-07-21 02:01:04 +05305493/* Get an inode object given its location and corresponding root.
5494 * Returns in *is_new if the inode was read from disk
5495 */
5496struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005497 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305498{
5499 struct inode *inode;
5500
Chris Mason90d3e592014-01-09 17:28:00 -08005501 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305502 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005503 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305504
5505 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305506 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005507 if (!is_bad_inode(inode)) {
5508 inode_tree_add(inode);
5509 unlock_new_inode(inode);
5510 if (new)
5511 *new = 1;
5512 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005513 unlock_new_inode(inode);
5514 iput(inode);
5515 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005516 }
5517 }
5518
Balaji Rao1a54ef82008-07-21 02:01:04 +05305519 return inode;
5520}
5521
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005522static struct inode *new_simple_dir(struct super_block *s,
5523 struct btrfs_key *key,
5524 struct btrfs_root *root)
5525{
5526 struct inode *inode = new_inode(s);
5527
5528 if (!inode)
5529 return ERR_PTR(-ENOMEM);
5530
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005531 BTRFS_I(inode)->root = root;
5532 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005533 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005534
5535 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005536 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005537 inode->i_fop = &simple_dir_operations;
5538 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
chandan r9cc97d62012-07-04 12:48:07 +05305539 inode->i_mtime = CURRENT_TIME;
5540 inode->i_atime = inode->i_mtime;
5541 inode->i_ctime = inode->i_mtime;
5542 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005543
5544 return inode;
5545}
5546
Chris Mason3de45862008-11-17 21:02:50 -05005547struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005548{
Chris Masond3977122009-01-05 21:25:51 -05005549 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005550 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005551 struct btrfs_root *sub_root = root;
5552 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005553 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005554 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005555
5556 if (dentry->d_name.len > BTRFS_NAME_LEN)
5557 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005558
Jeff Layton39e3c952012-11-28 11:30:53 -05005559 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005560 if (ret < 0)
5561 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005562
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005563 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005564 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005565
5566 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005567 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005568 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005569 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005570
5571 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5572
Yan, Zheng76dda932009-09-21 16:00:26 -04005573 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005574 ret = fixup_tree_root_location(root, dir, dentry,
5575 &location, &sub_root);
5576 if (ret < 0) {
5577 if (ret != -ENOENT)
5578 inode = ERR_PTR(ret);
5579 else
5580 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5581 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005582 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005583 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005584 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5585
Julia Lawall34d19ba2011-01-24 19:55:19 +00005586 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005587 down_read(&root->fs_info->cleanup_work_sem);
5588 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005589 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005590 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005591 if (ret) {
5592 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005593 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005594 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005595 }
5596
Chris Mason3de45862008-11-17 21:02:50 -05005597 return inode;
5598}
5599
Nick Pigginfe15ce42011-01-07 17:49:23 +11005600static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005601{
5602 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005603 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005604
Li Zefan848cce02012-02-21 17:04:28 +08005605 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005606 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005607
Li Zefan848cce02012-02-21 17:04:28 +08005608 if (inode) {
5609 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005610 if (btrfs_root_refs(&root->root_item) == 0)
5611 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005612
5613 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5614 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005615 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005616 return 0;
5617}
5618
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005619static void btrfs_dentry_release(struct dentry *dentry)
5620{
Daeseok Youn944a4512014-04-14 15:37:02 +09005621 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005622}
5623
Chris Mason3de45862008-11-17 21:02:50 -05005624static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005625 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005626{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005627 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005628
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005629 inode = btrfs_lookup_dentry(dir, dentry);
5630 if (IS_ERR(inode)) {
5631 if (PTR_ERR(inode) == -ENOENT)
5632 inode = NULL;
5633 else
5634 return ERR_CAST(inode);
5635 }
5636
Al Viro41d28bc2014-10-12 22:24:21 -04005637 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005638}
5639
Miao Xie16cdcec2011-04-22 18:12:22 +08005640unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005641 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5642};
5643
Al Viro9cdda8d2013-05-22 16:48:09 -04005644static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005645{
Al Viro9cdda8d2013-05-22 16:48:09 -04005646 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005647 struct btrfs_root *root = BTRFS_I(inode)->root;
5648 struct btrfs_item *item;
5649 struct btrfs_dir_item *di;
5650 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005651 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005652 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005653 struct list_head ins_list;
5654 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005655 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005656 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005657 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005658 unsigned char d_type;
5659 int over = 0;
5660 u32 di_cur;
5661 u32 di_total;
5662 u32 di_len;
5663 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005664 char tmp_name[32];
5665 char *name_ptr;
5666 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005667 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005668
5669 /* FIXME, use a real flag for deciding about the key type */
5670 if (root->fs_info->tree_root == root)
5671 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005672
Al Viro9cdda8d2013-05-22 16:48:09 -04005673 if (!dir_emit_dots(file, ctx))
5674 return 0;
5675
David Woodhouse49593bf2008-08-17 17:08:36 +01005676 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005677 if (!path)
5678 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005679
Josef Bacik026fd312011-05-13 10:32:11 -04005680 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005681
Miao Xie16cdcec2011-04-22 18:12:22 +08005682 if (key_type == BTRFS_DIR_INDEX_KEY) {
5683 INIT_LIST_HEAD(&ins_list);
5684 INIT_LIST_HEAD(&del_list);
5685 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5686 }
5687
David Sterba962a2982014-06-04 18:41:45 +02005688 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005689 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005690 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005691
Chris Mason39279cc2007-06-12 06:35:45 -04005692 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5693 if (ret < 0)
5694 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005695
5696 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005697 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005698 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005699 if (slot >= btrfs_header_nritems(leaf)) {
5700 ret = btrfs_next_leaf(root, path);
5701 if (ret < 0)
5702 goto err;
5703 else if (ret > 0)
5704 break;
5705 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005706 }
Chris Mason3de45862008-11-17 21:02:50 -05005707
Ross Kirkdd3cc162013-09-16 15:58:09 +01005708 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005709 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5710
5711 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005712 break;
David Sterba962a2982014-06-04 18:41:45 +02005713 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005714 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005715 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005716 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005717 if (key_type == BTRFS_DIR_INDEX_KEY &&
5718 btrfs_should_delete_dir_index(&del_list,
5719 found_key.offset))
5720 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005721
Al Viro9cdda8d2013-05-22 16:48:09 -04005722 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005723 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005724
Chris Mason39279cc2007-06-12 06:35:45 -04005725 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5726 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005727 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005728
5729 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005730 struct btrfs_key location;
5731
Josef Bacik22a94d42011-03-16 16:47:17 -04005732 if (verify_dir_item(root, leaf, di))
5733 break;
5734
Chris Mason5f39d392007-10-15 16:14:19 -04005735 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005736 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005737 name_ptr = tmp_name;
5738 } else {
5739 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005740 if (!name_ptr) {
5741 ret = -ENOMEM;
5742 goto err;
5743 }
Chris Mason5f39d392007-10-15 16:14:19 -04005744 }
5745 read_extent_buffer(leaf, name_ptr,
5746 (unsigned long)(di + 1), name_len);
5747
5748 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5749 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005750
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005751
Chris Mason3de45862008-11-17 21:02:50 -05005752 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005753 * skip it.
5754 *
5755 * In contrast to old kernels, we insert the snapshot's
5756 * dir item and dir index after it has been created, so
5757 * we won't find a reference to our own snapshot. We
5758 * still keep the following code for backward
5759 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005760 */
5761 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5762 location.objectid == root->root_key.objectid) {
5763 over = 0;
5764 goto skip;
5765 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005766 over = !dir_emit(ctx, name_ptr, name_len,
5767 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005768
Chris Mason3de45862008-11-17 21:02:50 -05005769skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005770 if (name_ptr != tmp_name)
5771 kfree(name_ptr);
5772
Chris Mason39279cc2007-06-12 06:35:45 -04005773 if (over)
5774 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005775 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005776 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005777 di_cur += di_len;
5778 di = (struct btrfs_dir_item *)((char *)di + di_len);
5779 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005780next:
5781 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005782 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005783
Miao Xie16cdcec2011-04-22 18:12:22 +08005784 if (key_type == BTRFS_DIR_INDEX_KEY) {
5785 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005786 ctx->pos++;
5787 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005788 if (ret)
5789 goto nopos;
5790 }
5791
David Woodhouse49593bf2008-08-17 17:08:36 +01005792 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005793 ctx->pos++;
5794
5795 /*
5796 * Stop new entries from being returned after we return the last
5797 * entry.
5798 *
5799 * New directory entries are assigned a strictly increasing
5800 * offset. This means that new entries created during readdir
5801 * are *guaranteed* to be seen in the future by that readdir.
5802 * This has broken buggy programs which operate on names as
5803 * they're returned by readdir. Until we re-use freed offsets
5804 * we have this hack to stop new entries from being returned
5805 * under the assumption that they'll never reach this huge
5806 * offset.
5807 *
5808 * This is being careful not to overflow 32bit loff_t unless the
5809 * last entry requires it because doing so has broken 32bit apps
5810 * in the past.
5811 */
5812 if (key_type == BTRFS_DIR_INDEX_KEY) {
5813 if (ctx->pos >= INT_MAX)
5814 ctx->pos = LLONG_MAX;
5815 else
5816 ctx->pos = INT_MAX;
5817 }
Chris Mason39279cc2007-06-12 06:35:45 -04005818nopos:
5819 ret = 0;
5820err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005821 if (key_type == BTRFS_DIR_INDEX_KEY)
5822 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005823 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005824 return ret;
5825}
5826
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005827int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005828{
5829 struct btrfs_root *root = BTRFS_I(inode)->root;
5830 struct btrfs_trans_handle *trans;
5831 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005832 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005833
Josef Bacik72ac3c02012-05-23 14:13:11 -04005834 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005835 return 0;
5836
Liu Bo83eea1f2012-07-10 05:28:39 -06005837 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005838 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005839
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005840 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005841 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005842 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005843 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005844 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005845 if (IS_ERR(trans))
5846 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005847 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005848 }
5849 return ret;
5850}
5851
5852/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005853 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005854 * inode changes. But, it is most likely to find the inode in cache.
5855 * FIXME, needs more benchmarking...there are no reasons other than performance
5856 * to keep or drop this code.
5857 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005858static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005859{
5860 struct btrfs_root *root = BTRFS_I(inode)->root;
5861 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005862 int ret;
5863
Josef Bacik72ac3c02012-05-23 14:13:11 -04005864 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005865 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005866
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005867 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005868 if (IS_ERR(trans))
5869 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005870
5871 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005872 if (ret && ret == -ENOSPC) {
5873 /* whoops, lets try again with the full transaction */
5874 btrfs_end_transaction(trans, root);
5875 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005876 if (IS_ERR(trans))
5877 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005878
Chris Mason94b60442010-05-26 11:02:00 -04005879 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005880 }
Chris Mason39279cc2007-06-12 06:35:45 -04005881 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005882 if (BTRFS_I(inode)->delayed_node)
5883 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005884
5885 return ret;
5886}
5887
5888/*
5889 * This is a copy of file_update_time. We need this so we can return error on
5890 * ENOSPC for updating the inode in the case of file write and mmap writes.
5891 */
Josef Bacike41f9412012-03-26 09:46:47 -04005892static int btrfs_update_time(struct inode *inode, struct timespec *now,
5893 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005894{
Alexander Block2bc5565282012-06-15 09:49:33 +02005895 struct btrfs_root *root = BTRFS_I(inode)->root;
5896
5897 if (btrfs_root_readonly(root))
5898 return -EROFS;
5899
Josef Bacike41f9412012-03-26 09:46:47 -04005900 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005901 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005902 if (flags & S_CTIME)
5903 inode->i_ctime = *now;
5904 if (flags & S_MTIME)
5905 inode->i_mtime = *now;
5906 if (flags & S_ATIME)
5907 inode->i_atime = *now;
5908 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005909}
5910
Chris Masond352ac62008-09-29 15:18:18 -04005911/*
5912 * find the highest existing sequence number in a directory
5913 * and then set the in-memory index_cnt variable to reflect
5914 * free sequence numbers
5915 */
Josef Bacikaec74772008-07-24 12:12:38 -04005916static int btrfs_set_inode_index_count(struct inode *inode)
5917{
5918 struct btrfs_root *root = BTRFS_I(inode)->root;
5919 struct btrfs_key key, found_key;
5920 struct btrfs_path *path;
5921 struct extent_buffer *leaf;
5922 int ret;
5923
Li Zefan33345d012011-04-20 10:31:50 +08005924 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005925 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005926 key.offset = (u64)-1;
5927
5928 path = btrfs_alloc_path();
5929 if (!path)
5930 return -ENOMEM;
5931
5932 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5933 if (ret < 0)
5934 goto out;
5935 /* FIXME: we should be able to handle this */
5936 if (ret == 0)
5937 goto out;
5938 ret = 0;
5939
5940 /*
5941 * MAGIC NUMBER EXPLANATION:
5942 * since we search a directory based on f_pos we have to start at 2
5943 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5944 * else has to start at 2
5945 */
5946 if (path->slots[0] == 0) {
5947 BTRFS_I(inode)->index_cnt = 2;
5948 goto out;
5949 }
5950
5951 path->slots[0]--;
5952
5953 leaf = path->nodes[0];
5954 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5955
Li Zefan33345d012011-04-20 10:31:50 +08005956 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005957 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005958 BTRFS_I(inode)->index_cnt = 2;
5959 goto out;
5960 }
5961
5962 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5963out:
5964 btrfs_free_path(path);
5965 return ret;
5966}
5967
Chris Masond352ac62008-09-29 15:18:18 -04005968/*
5969 * helper to find a free sequence number in a given directory. This current
5970 * code is very simple, later versions will do smarter things in the btree
5971 */
Chris Mason3de45862008-11-17 21:02:50 -05005972int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005973{
5974 int ret = 0;
5975
5976 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005977 ret = btrfs_inode_delayed_dir_index_count(dir);
5978 if (ret) {
5979 ret = btrfs_set_inode_index_count(dir);
5980 if (ret)
5981 return ret;
5982 }
Josef Bacikaec74772008-07-24 12:12:38 -04005983 }
5984
Chris Mason00e4e6b2008-08-05 11:18:09 -04005985 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005986 BTRFS_I(dir)->index_cnt++;
5987
5988 return ret;
5989}
5990
Chris Masonb0d5d102014-09-08 13:08:51 -07005991static int btrfs_insert_inode_locked(struct inode *inode)
5992{
5993 struct btrfs_iget_args args;
5994 args.location = &BTRFS_I(inode)->location;
5995 args.root = BTRFS_I(inode)->root;
5996
5997 return insert_inode_locked4(inode,
5998 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5999 btrfs_find_actor, &args);
6000}
6001
Chris Mason39279cc2007-06-12 06:35:45 -04006002static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6003 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006004 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006005 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006006 u64 ref_objectid, u64 objectid,
6007 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006008{
6009 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006010 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006011 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006012 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006013 struct btrfs_inode_ref *ref;
6014 struct btrfs_key key[2];
6015 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006016 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006017 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006018 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006019
Chris Mason5f39d392007-10-15 16:14:19 -04006020 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006021 if (!path)
6022 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006023
Chris Mason39279cc2007-06-12 06:35:45 -04006024 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006025 if (!inode) {
6026 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006027 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006028 }
Chris Mason39279cc2007-06-12 06:35:45 -04006029
Li Zefan581bb052011-04-20 10:06:11 +08006030 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006031 * O_TMPFILE, set link count to 0, so that after this point,
6032 * we fill in an inode item with the correct link count.
6033 */
6034 if (!name)
6035 set_nlink(inode, 0);
6036
6037 /*
Li Zefan581bb052011-04-20 10:06:11 +08006038 * we have to initialize this early, so we can reclaim the inode
6039 * number if we fail afterwards in this function.
6040 */
6041 inode->i_ino = objectid;
6042
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006043 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006044 trace_btrfs_inode_request(dir);
6045
Chris Mason3de45862008-11-17 21:02:50 -05006046 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006047 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006048 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006049 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006050 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006051 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006052 } else if (dir) {
6053 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006054 }
6055 /*
6056 * index_cnt is ignored for everything but a dir,
6057 * btrfs_get_inode_index_count has an explanation for the magic
6058 * number
6059 */
6060 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006061 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006062 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006063 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006064 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006065
Josef Bacik5dc562c2012-08-17 13:14:17 -04006066 /*
6067 * We could have gotten an inode number from somebody who was fsynced
6068 * and then removed in this same transaction, so let's just set full
6069 * sync since it will be a full sync anyway and this will blow away the
6070 * old info in the log.
6071 */
6072 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6073
Chris Mason9c583092008-01-29 15:15:18 -05006074 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006075 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006076 key[0].offset = 0;
6077
Chris Mason9c583092008-01-29 15:15:18 -05006078 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006079
6080 if (name) {
6081 /*
6082 * Start new inodes with an inode_ref. This is slightly more
6083 * efficient for small numbers of hard links since they will
6084 * be packed into one item. Extended refs will kick in if we
6085 * add more hard links than can fit in the ref item.
6086 */
6087 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006088 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006089 key[1].offset = ref_objectid;
6090
6091 sizes[1] = name_len + sizeof(*ref);
6092 }
Chris Mason9c583092008-01-29 15:15:18 -05006093
Chris Masonb0d5d102014-09-08 13:08:51 -07006094 location = &BTRFS_I(inode)->location;
6095 location->objectid = objectid;
6096 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006097 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006098
6099 ret = btrfs_insert_inode_locked(inode);
6100 if (ret < 0)
6101 goto fail;
6102
Chris Masonb9473432009-03-13 11:00:37 -04006103 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006104 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006105 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006106 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006107
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006108 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006109 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306110
6111 inode->i_mtime = CURRENT_TIME;
6112 inode->i_atime = inode->i_mtime;
6113 inode->i_ctime = inode->i_mtime;
6114 BTRFS_I(inode)->i_otime = inode->i_mtime;
6115
Chris Mason5f39d392007-10-15 16:14:19 -04006116 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6117 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006118 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6119 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006120 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006121
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006122 if (name) {
6123 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6124 struct btrfs_inode_ref);
6125 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6126 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6127 ptr = (unsigned long)(ref + 1);
6128 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6129 }
Chris Mason9c583092008-01-29 15:15:18 -05006130
Chris Mason5f39d392007-10-15 16:14:19 -04006131 btrfs_mark_buffer_dirty(path->nodes[0]);
6132 btrfs_free_path(path);
6133
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006134 btrfs_inherit_iflags(inode, dir);
6135
Al Viro569254b2011-07-24 17:08:40 -04006136 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04006137 if (btrfs_test_opt(root, NODATASUM))
6138 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06006139 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006140 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6141 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006142 }
6143
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006144 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006145
6146 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006147 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006148
Alexander Block8ea05e32012-07-25 17:35:53 +02006149 btrfs_update_root_times(trans, root);
6150
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006151 ret = btrfs_inode_inherit_props(trans, inode, dir);
6152 if (ret)
6153 btrfs_err(root->fs_info,
6154 "error inheriting props for ino %llu (root %llu): %d",
6155 btrfs_ino(inode), root->root_key.objectid, ret);
6156
Chris Mason39279cc2007-06-12 06:35:45 -04006157 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006158
6159fail_unlock:
6160 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006161fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006162 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006163 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006164 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006165 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006166 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006167}
6168
6169static inline u8 btrfs_inode_type(struct inode *inode)
6170{
6171 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6172}
6173
Chris Masond352ac62008-09-29 15:18:18 -04006174/*
6175 * utility function to add 'inode' into 'parent_inode' with
6176 * a give name and a given sequence number.
6177 * if 'add_backref' is true, also insert a backref from the
6178 * inode to the parent directory.
6179 */
Chris Masone02119d2008-09-05 16:13:11 -04006180int btrfs_add_link(struct btrfs_trans_handle *trans,
6181 struct inode *parent_inode, struct inode *inode,
6182 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006183{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006184 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006185 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006186 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006187 u64 ino = btrfs_ino(inode);
6188 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006189
Li Zefan33345d012011-04-20 10:31:50 +08006190 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006191 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6192 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006193 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006194 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006195 key.offset = 0;
6196 }
Chris Mason39279cc2007-06-12 06:35:45 -04006197
Li Zefan33345d012011-04-20 10:31:50 +08006198 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006199 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6200 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006201 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006202 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006203 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6204 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006205 }
6206
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006207 /* Nothing to clean up yet */
6208 if (ret)
6209 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006210
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006211 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6212 parent_inode, &key,
6213 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006214 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006215 goto fail_dir_item;
6216 else if (ret) {
6217 btrfs_abort_transaction(trans, root, ret);
6218 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006219 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006220
6221 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6222 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006223 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006224 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
6225 ret = btrfs_update_inode(trans, root, parent_inode);
6226 if (ret)
6227 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006228 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006229
6230fail_dir_item:
6231 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6232 u64 local_index;
6233 int err;
6234 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6235 key.objectid, root->root_key.objectid,
6236 parent_ino, &local_index, name, name_len);
6237
6238 } else if (add_backref) {
6239 u64 local_index;
6240 int err;
6241
6242 err = btrfs_del_inode_ref(trans, root, name, name_len,
6243 ino, parent_ino, &local_index);
6244 }
6245 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006246}
6247
6248static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006249 struct inode *dir, struct dentry *dentry,
6250 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006251{
Josef Bacika1b075d2010-11-19 20:36:11 +00006252 int err = btrfs_add_link(trans, dir, inode,
6253 dentry->d_name.name, dentry->d_name.len,
6254 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006255 if (err > 0)
6256 err = -EEXIST;
6257 return err;
6258}
6259
Josef Bacik618e21d2007-07-11 10:18:17 -04006260static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006261 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006262{
6263 struct btrfs_trans_handle *trans;
6264 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006265 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006266 int err;
6267 int drop_inode = 0;
6268 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006269 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006270
6271 if (!new_valid_dev(rdev))
6272 return -EINVAL;
6273
Josef Bacik9ed74f22009-09-11 16:12:44 -04006274 /*
6275 * 2 for inode item and ref
6276 * 2 for dir items
6277 * 1 for xattr if selinux is on
6278 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006279 trans = btrfs_start_transaction(root, 5);
6280 if (IS_ERR(trans))
6281 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006282
Li Zefan581bb052011-04-20 10:06:11 +08006283 err = btrfs_find_free_ino(root, &objectid);
6284 if (err)
6285 goto out_unlock;
6286
Josef Bacikaec74772008-07-24 12:12:38 -04006287 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006288 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006289 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006290 if (IS_ERR(inode)) {
6291 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006292 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006293 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006294
Casey Schauflerad19db72011-12-15 10:09:07 -05006295 /*
6296 * If the active LSM wants to access the inode during
6297 * d_instantiate it needs these. Smack checks to see
6298 * if the filesystem supports xattrs by looking at the
6299 * ops vector.
6300 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006301 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006302 init_special_inode(inode, inode->i_mode, rdev);
6303
6304 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006305 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006306 goto out_unlock_inode;
6307
6308 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6309 if (err) {
6310 goto out_unlock_inode;
6311 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006312 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006313 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006314 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006315 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006316
Josef Bacik618e21d2007-07-11 10:18:17 -04006317out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006318 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006319 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006320 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006321 if (drop_inode) {
6322 inode_dec_link_count(inode);
6323 iput(inode);
6324 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006325 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006326
6327out_unlock_inode:
6328 drop_inode = 1;
6329 unlock_new_inode(inode);
6330 goto out_unlock;
6331
Josef Bacik618e21d2007-07-11 10:18:17 -04006332}
6333
Chris Mason39279cc2007-06-12 06:35:45 -04006334static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006335 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006336{
6337 struct btrfs_trans_handle *trans;
6338 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006339 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006340 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006341 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006342 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006343 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006344
Josef Bacik9ed74f22009-09-11 16:12:44 -04006345 /*
6346 * 2 for inode item and ref
6347 * 2 for dir items
6348 * 1 for xattr if selinux is on
6349 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006350 trans = btrfs_start_transaction(root, 5);
6351 if (IS_ERR(trans))
6352 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006353
Li Zefan581bb052011-04-20 10:06:11 +08006354 err = btrfs_find_free_ino(root, &objectid);
6355 if (err)
6356 goto out_unlock;
6357
Josef Bacikaec74772008-07-24 12:12:38 -04006358 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006359 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006360 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006361 if (IS_ERR(inode)) {
6362 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006363 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006364 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006365 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006366 /*
6367 * If the active LSM wants to access the inode during
6368 * d_instantiate it needs these. Smack checks to see
6369 * if the filesystem supports xattrs by looking at the
6370 * ops vector.
6371 */
6372 inode->i_fop = &btrfs_file_operations;
6373 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006374 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006375
6376 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6377 if (err)
6378 goto out_unlock_inode;
6379
6380 err = btrfs_update_inode(trans, root, inode);
6381 if (err)
6382 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006383
Josef Bacika1b075d2010-11-19 20:36:11 +00006384 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006385 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006386 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006387
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006388 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006389 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006390 d_instantiate(dentry, inode);
6391
Chris Mason39279cc2007-06-12 06:35:45 -04006392out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006393 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006394 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006395 inode_dec_link_count(inode);
6396 iput(inode);
6397 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006398 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006399 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006400 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006401
6402out_unlock_inode:
6403 unlock_new_inode(inode);
6404 goto out_unlock;
6405
Chris Mason39279cc2007-06-12 06:35:45 -04006406}
6407
6408static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6409 struct dentry *dentry)
6410{
6411 struct btrfs_trans_handle *trans;
6412 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006413 struct inode *inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006414 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006415 int err;
6416 int drop_inode = 0;
6417
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006418 /* do not allow sys_link's with other subvols of the same device */
6419 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006420 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006421
Mark Fashehf1863732012-08-08 11:32:27 -07006422 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006423 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006424
Chris Mason3de45862008-11-17 21:02:50 -05006425 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006426 if (err)
6427 goto fail;
6428
Yan, Zhenga22285a2010-05-16 10:48:46 -04006429 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006430 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006431 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006432 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006433 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006434 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006435 if (IS_ERR(trans)) {
6436 err = PTR_ERR(trans);
6437 goto fail;
6438 }
Chris Mason5f39d392007-10-15 16:14:19 -04006439
Miao Xie67de1172013-12-26 13:07:06 +08006440 /* There are several dir indexes for this inode, clear the cache. */
6441 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006442 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006443 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006444 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006445 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006446 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006447
Josef Bacika1b075d2010-11-19 20:36:11 +00006448 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006449
Yan, Zhenga5719522009-09-24 09:17:31 -04006450 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006451 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006452 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006453 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006454 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006455 if (err)
6456 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006457 if (inode->i_nlink == 1) {
6458 /*
6459 * If new hard link count is 1, it's a file created
6460 * with open(2) O_TMPFILE flag.
6461 */
6462 err = btrfs_orphan_del(trans, inode);
6463 if (err)
6464 goto fail;
6465 }
Al Viro08c422c2011-12-23 07:58:13 -05006466 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006467 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006468 }
Chris Mason39279cc2007-06-12 06:35:45 -04006469
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006470 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006471 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006472fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006473 if (drop_inode) {
6474 inode_dec_link_count(inode);
6475 iput(inode);
6476 }
Liu Bob53d3f52012-11-14 14:34:34 +00006477 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006478 return err;
6479}
6480
Al Viro18bb1db2011-07-26 01:41:39 -04006481static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006482{
Chris Masonb9d86662008-05-02 16:13:49 -04006483 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006484 struct btrfs_trans_handle *trans;
6485 struct btrfs_root *root = BTRFS_I(dir)->root;
6486 int err = 0;
6487 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006488 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006489 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006490
Josef Bacik9ed74f22009-09-11 16:12:44 -04006491 /*
6492 * 2 items for inode and ref
6493 * 2 items for dir items
6494 * 1 for xattr if selinux is on
6495 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006496 trans = btrfs_start_transaction(root, 5);
6497 if (IS_ERR(trans))
6498 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006499
Li Zefan581bb052011-04-20 10:06:11 +08006500 err = btrfs_find_free_ino(root, &objectid);
6501 if (err)
6502 goto out_fail;
6503
Josef Bacikaec74772008-07-24 12:12:38 -04006504 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006505 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006506 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006507 if (IS_ERR(inode)) {
6508 err = PTR_ERR(inode);
6509 goto out_fail;
6510 }
Chris Mason5f39d392007-10-15 16:14:19 -04006511
Chris Mason39279cc2007-06-12 06:35:45 -04006512 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006513 /* these must be set before we unlock the inode */
6514 inode->i_op = &btrfs_dir_inode_operations;
6515 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006516
Eric Paris2a7dba32011-02-01 11:05:39 -05006517 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006518 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006519 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006520
Chris Masondbe674a2008-07-17 12:54:05 -04006521 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006522 err = btrfs_update_inode(trans, root, inode);
6523 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006524 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006525
Josef Bacika1b075d2010-11-19 20:36:11 +00006526 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6527 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006528 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006529 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006530
Chris Mason39279cc2007-06-12 06:35:45 -04006531 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006532 /*
6533 * mkdir is special. We're unlocking after we call d_instantiate
6534 * to avoid a race with nfsd calling d_instantiate.
6535 */
6536 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006537 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006538
6539out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006540 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006541 if (drop_on_err) {
6542 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006543 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006544 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006545 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006546 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006547 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006548
6549out_fail_inode:
6550 unlock_new_inode(inode);
6551 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006552}
6553
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006554/* Find next extent map of a given extent map, caller needs to ensure locks */
6555static struct extent_map *next_extent_map(struct extent_map *em)
6556{
6557 struct rb_node *next;
6558
6559 next = rb_next(&em->rb_node);
6560 if (!next)
6561 return NULL;
6562 return container_of(next, struct extent_map, rb_node);
6563}
6564
6565static struct extent_map *prev_extent_map(struct extent_map *em)
6566{
6567 struct rb_node *prev;
6568
6569 prev = rb_prev(&em->rb_node);
6570 if (!prev)
6571 return NULL;
6572 return container_of(prev, struct extent_map, rb_node);
6573}
6574
Chris Masond352ac62008-09-29 15:18:18 -04006575/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006576 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006577 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006578 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006579 */
Chris Mason3b951512008-04-17 11:29:12 -04006580static int merge_extent_mapping(struct extent_map_tree *em_tree,
6581 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006582 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006583 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006584{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006585 struct extent_map *prev;
6586 struct extent_map *next;
6587 u64 start;
6588 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006589 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006590
Chris Masone6dcd2d2008-07-17 12:53:50 -04006591 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006592
6593 if (existing->start > map_start) {
6594 next = existing;
6595 prev = prev_extent_map(next);
6596 } else {
6597 prev = existing;
6598 next = next_extent_map(prev);
6599 }
6600
6601 start = prev ? extent_map_end(prev) : em->start;
6602 start = max_t(u64, start, em->start);
6603 end = next ? next->start : extent_map_end(em);
6604 end = min_t(u64, end, extent_map_end(em));
6605 start_diff = start - em->start;
6606 em->start = start;
6607 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006608 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6609 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006610 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006611 em->block_len -= start_diff;
6612 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006613 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006614}
6615
Chris Masonc8b97812008-10-29 14:49:59 -04006616static noinline int uncompress_inline(struct btrfs_path *path,
6617 struct inode *inode, struct page *page,
6618 size_t pg_offset, u64 extent_offset,
6619 struct btrfs_file_extent_item *item)
6620{
6621 int ret;
6622 struct extent_buffer *leaf = path->nodes[0];
6623 char *tmp;
6624 size_t max_size;
6625 unsigned long inline_size;
6626 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006627 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006628
6629 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006630 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006631 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6632 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006633 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006634 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006635 if (!tmp)
6636 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006637 ptr = btrfs_file_extent_inline_start(item);
6638
6639 read_extent_buffer(leaf, tmp, ptr, inline_size);
6640
Chris Mason5b050f02008-11-11 09:34:41 -05006641 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006642 ret = btrfs_decompress(compress_type, tmp, page,
6643 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006644 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006645 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006646}
6647
Chris Masond352ac62008-09-29 15:18:18 -04006648/*
6649 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006650 * the ugly parts come from merging extents from the disk with the in-ram
6651 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006652 * where the in-ram extents might be locked pending data=ordered completion.
6653 *
6654 * This also copies inline extents directly into the page.
6655 */
Chris Masond3977122009-01-05 21:25:51 -05006656
Chris Masona52d9a82007-08-27 16:49:44 -04006657struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006658 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006659 int create)
6660{
6661 int ret;
6662 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006663 u64 extent_start = 0;
6664 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006665 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006666 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006667 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006668 struct btrfs_root *root = BTRFS_I(inode)->root;
6669 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006670 struct extent_buffer *leaf;
6671 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006672 struct extent_map *em = NULL;
6673 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006674 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006675 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006676 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006677
Chris Masona52d9a82007-08-27 16:49:44 -04006678again:
Chris Mason890871b2009-09-02 16:24:52 -04006679 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006680 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006681 if (em)
6682 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006683 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006684
Chris Masona52d9a82007-08-27 16:49:44 -04006685 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006686 if (em->start > start || em->start + em->len <= start)
6687 free_extent_map(em);
6688 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006689 free_extent_map(em);
6690 else
6691 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006692 }
David Sterba172ddd62011-04-21 00:48:27 +02006693 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006694 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006695 err = -ENOMEM;
6696 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006697 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006698 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006699 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006700 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006701 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006702 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006703
6704 if (!path) {
6705 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006706 if (!path) {
6707 err = -ENOMEM;
6708 goto out;
6709 }
6710 /*
6711 * Chances are we'll be called again, so go ahead and do
6712 * readahead
6713 */
6714 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006715 }
6716
Chris Mason179e29e2007-11-01 11:28:41 -04006717 ret = btrfs_lookup_file_extent(trans, root, path,
6718 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006719 if (ret < 0) {
6720 err = ret;
6721 goto out;
6722 }
6723
6724 if (ret != 0) {
6725 if (path->slots[0] == 0)
6726 goto not_found;
6727 path->slots[0]--;
6728 }
6729
Chris Mason5f39d392007-10-15 16:14:19 -04006730 leaf = path->nodes[0];
6731 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006732 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006733 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006734 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006735 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006736 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006737 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006738 /*
6739 * If we backup past the first extent we want to move forward
6740 * and see if there is an extent in front of us, otherwise we'll
6741 * say there is a hole for our whole search range which can
6742 * cause problems.
6743 */
6744 extent_end = start;
6745 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006746 }
6747
Chris Mason5f39d392007-10-15 16:14:19 -04006748 found_type = btrfs_file_extent_type(leaf, item);
6749 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006750 if (found_type == BTRFS_FILE_EXTENT_REG ||
6751 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006752 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006753 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006754 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6755 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006756 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006757 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006758 }
Josef Bacik25a50342013-10-14 12:08:38 -04006759next:
Yan Zheng9036c102008-10-30 14:19:41 -04006760 if (start >= extent_end) {
6761 path->slots[0]++;
6762 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6763 ret = btrfs_next_leaf(root, path);
6764 if (ret < 0) {
6765 err = ret;
6766 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006767 }
Yan Zheng9036c102008-10-30 14:19:41 -04006768 if (ret > 0)
6769 goto not_found;
6770 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006771 }
Yan Zheng9036c102008-10-30 14:19:41 -04006772 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6773 if (found_key.objectid != objectid ||
6774 found_key.type != BTRFS_EXTENT_DATA_KEY)
6775 goto not_found;
6776 if (start + len <= found_key.offset)
6777 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006778 if (start > found_key.offset)
6779 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006780 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006781 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006782 em->len = found_key.offset - start;
6783 goto not_found_em;
6784 }
6785
Filipe Manana7ffbb592014-06-09 03:48:05 +01006786 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6787
Yan Zhengd899e052008-10-30 14:25:28 -04006788 if (found_type == BTRFS_FILE_EXTENT_REG ||
6789 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006790 goto insert;
6791 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006792 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006793 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006794 size_t size;
6795 size_t extent_offset;
6796 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006797
Filipe Manana7ffbb592014-06-09 03:48:05 +01006798 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006799 goto out;
Yan689f9342007-10-29 11:41:07 -04006800
Chris Mason514ac8a2014-01-03 21:07:00 -08006801 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006802 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006803 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006804 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006805 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006806 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006807 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006808 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006809 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006810 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006811 if (btrfs_file_extent_compression(leaf, item) !=
6812 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006813 ret = uncompress_inline(path, inode, page,
6814 pg_offset,
6815 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006816 if (ret) {
6817 err = ret;
6818 goto out;
6819 }
Chris Masonc8b97812008-10-29 14:49:59 -04006820 } else {
6821 map = kmap(page);
6822 read_extent_buffer(leaf, map + pg_offset, ptr,
6823 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006824 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6825 memset(map + pg_offset + copy_size, 0,
6826 PAGE_CACHE_SIZE - pg_offset -
6827 copy_size);
6828 }
Chris Masonc8b97812008-10-29 14:49:59 -04006829 kunmap(page);
6830 }
Chris Mason179e29e2007-11-01 11:28:41 -04006831 flush_dcache_page(page);
6832 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006833 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006834 if (!trans) {
6835 kunmap(page);
6836 free_extent_map(em);
6837 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006838
David Sterbab3b4aa72011-04-21 01:20:15 +02006839 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006840 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006841
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006842 if (IS_ERR(trans))
6843 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006844 goto again;
6845 }
Chris Masonc8b97812008-10-29 14:49:59 -04006846 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006847 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006848 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006849 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006850 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006851 }
Chris Masond1310b22008-01-24 16:13:08 -05006852 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006853 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006854 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006855 }
6856not_found:
6857 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006858 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006859 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006860not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006861 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006862 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006863insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006864 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006865 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006866 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006867 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006868 err = -EIO;
6869 goto out;
6870 }
Chris Masond1310b22008-01-24 16:13:08 -05006871
6872 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006873 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006874 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006875 /* it is possible that someone inserted the extent into the tree
6876 * while we had the lock dropped. It is also possible that
6877 * an overlapping map exists in the tree
6878 */
Chris Masona52d9a82007-08-27 16:49:44 -04006879 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006880 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006881
6882 ret = 0;
6883
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006884 existing = search_extent_mapping(em_tree, start, len);
6885 /*
6886 * existing will always be non-NULL, since there must be
6887 * extent causing the -EEXIST.
6888 */
6889 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006890 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006891 /*
6892 * The existing extent map is the one nearest to
6893 * the [start, start + len) range which overlaps
6894 */
6895 err = merge_extent_mapping(em_tree, existing,
6896 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006897 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006898 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006899 free_extent_map(em);
6900 em = NULL;
6901 }
6902 } else {
6903 free_extent_map(em);
6904 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006905 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006906 }
Chris Masona52d9a82007-08-27 16:49:44 -04006907 }
Chris Mason890871b2009-09-02 16:24:52 -04006908 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006909out:
liubo1abe9b82011-03-24 11:18:59 +00006910
Steven Rostedt4cd85872013-11-14 22:57:29 -05006911 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006912
Tsutomu Itoh527afb42015-08-19 14:55:00 +09006913 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006914 if (trans) {
6915 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006916 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006917 err = ret;
6918 }
Chris Masona52d9a82007-08-27 16:49:44 -04006919 if (err) {
6920 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006921 return ERR_PTR(err);
6922 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006923 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006924 return em;
6925}
6926
Chris Masonec29ed52011-02-23 16:23:20 -05006927struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6928 size_t pg_offset, u64 start, u64 len,
6929 int create)
6930{
6931 struct extent_map *em;
6932 struct extent_map *hole_em = NULL;
6933 u64 range_start = start;
6934 u64 end;
6935 u64 found;
6936 u64 found_end;
6937 int err = 0;
6938
6939 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6940 if (IS_ERR(em))
6941 return em;
6942 if (em) {
6943 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006944 * if our em maps to
6945 * - a hole or
6946 * - a pre-alloc extent,
6947 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006948 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006949 if (em->block_start != EXTENT_MAP_HOLE &&
6950 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006951 return em;
6952 else
6953 hole_em = em;
6954 }
6955
6956 /* check to see if we've wrapped (len == -1 or similar) */
6957 end = start + len;
6958 if (end < start)
6959 end = (u64)-1;
6960 else
6961 end -= 1;
6962
6963 em = NULL;
6964
6965 /* ok, we didn't find anything, lets look for delalloc */
6966 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6967 end, len, EXTENT_DELALLOC, 1);
6968 found_end = range_start + found;
6969 if (found_end < range_start)
6970 found_end = (u64)-1;
6971
6972 /*
6973 * we didn't find anything useful, return
6974 * the original results from get_extent()
6975 */
6976 if (range_start > end || found_end <= start) {
6977 em = hole_em;
6978 hole_em = NULL;
6979 goto out;
6980 }
6981
6982 /* adjust the range_start to make sure it doesn't
6983 * go backwards from the start they passed in
6984 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306985 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006986 found = found_end - range_start;
6987
6988 if (found > 0) {
6989 u64 hole_start = start;
6990 u64 hole_len = len;
6991
David Sterba172ddd62011-04-21 00:48:27 +02006992 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006993 if (!em) {
6994 err = -ENOMEM;
6995 goto out;
6996 }
6997 /*
6998 * when btrfs_get_extent can't find anything it
6999 * returns one huge hole
7000 *
7001 * make sure what it found really fits our range, and
7002 * adjust to make sure it is based on the start from
7003 * the caller
7004 */
7005 if (hole_em) {
7006 u64 calc_end = extent_map_end(hole_em);
7007
7008 if (calc_end <= start || (hole_em->start > end)) {
7009 free_extent_map(hole_em);
7010 hole_em = NULL;
7011 } else {
7012 hole_start = max(hole_em->start, start);
7013 hole_len = calc_end - hole_start;
7014 }
7015 }
7016 em->bdev = NULL;
7017 if (hole_em && range_start > hole_start) {
7018 /* our hole starts before our delalloc, so we
7019 * have to return just the parts of the hole
7020 * that go until the delalloc starts
7021 */
7022 em->len = min(hole_len,
7023 range_start - hole_start);
7024 em->start = hole_start;
7025 em->orig_start = hole_start;
7026 /*
7027 * don't adjust block start at all,
7028 * it is fixed at EXTENT_MAP_HOLE
7029 */
7030 em->block_start = hole_em->block_start;
7031 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007032 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7033 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007034 } else {
7035 em->start = range_start;
7036 em->len = found;
7037 em->orig_start = range_start;
7038 em->block_start = EXTENT_MAP_DELALLOC;
7039 em->block_len = found;
7040 }
7041 } else if (hole_em) {
7042 return hole_em;
7043 }
7044out:
7045
7046 free_extent_map(hole_em);
7047 if (err) {
7048 free_extent_map(em);
7049 return ERR_PTR(err);
7050 }
7051 return em;
7052}
7053
Josef Bacik4b46fce2010-05-23 11:00:55 -04007054static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7055 u64 start, u64 len)
7056{
7057 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007058 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007059 struct btrfs_key ins;
7060 u64 alloc_hint;
7061 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007062
Josef Bacik4b46fce2010-05-23 11:00:55 -04007063 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04007064 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007065 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007066 if (ret)
7067 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007068
Josef Bacik70c8a912012-10-11 16:54:30 -04007069 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007070 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04007071 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08007072 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007073 return em;
7074 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007075
7076 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
7077 ins.offset, ins.offset, 0);
7078 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08007079 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007080 free_extent_map(em);
7081 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007082 }
Josef Bacik00361582013-08-14 14:02:47 -04007083
Josef Bacik4b46fce2010-05-23 11:00:55 -04007084 return em;
7085}
7086
Chris Mason46bfbb52010-05-26 11:04:10 -04007087/*
7088 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7089 * block must be cow'd
7090 */
Josef Bacik00361582013-08-14 14:02:47 -04007091noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007092 u64 *orig_start, u64 *orig_block_len,
7093 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007094{
Josef Bacik00361582013-08-14 14:02:47 -04007095 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007096 struct btrfs_path *path;
7097 int ret;
7098 struct extent_buffer *leaf;
7099 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007100 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007101 struct btrfs_file_extent_item *fi;
7102 struct btrfs_key key;
7103 u64 disk_bytenr;
7104 u64 backref_offset;
7105 u64 extent_end;
7106 u64 num_bytes;
7107 int slot;
7108 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007109 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007110
Chris Mason46bfbb52010-05-26 11:04:10 -04007111 path = btrfs_alloc_path();
7112 if (!path)
7113 return -ENOMEM;
7114
Josef Bacik00361582013-08-14 14:02:47 -04007115 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007116 offset, 0);
7117 if (ret < 0)
7118 goto out;
7119
7120 slot = path->slots[0];
7121 if (ret == 1) {
7122 if (slot == 0) {
7123 /* can't find the item, must cow */
7124 ret = 0;
7125 goto out;
7126 }
7127 slot--;
7128 }
7129 ret = 0;
7130 leaf = path->nodes[0];
7131 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007132 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007133 key.type != BTRFS_EXTENT_DATA_KEY) {
7134 /* not our file or wrong item type, must cow */
7135 goto out;
7136 }
7137
7138 if (key.offset > offset) {
7139 /* Wrong offset, must cow */
7140 goto out;
7141 }
7142
7143 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7144 found_type = btrfs_file_extent_type(leaf, fi);
7145 if (found_type != BTRFS_FILE_EXTENT_REG &&
7146 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7147 /* not a regular extent, must cow */
7148 goto out;
7149 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007150
7151 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7152 goto out;
7153
Miao Xiee77751a2013-12-27 21:11:50 +08007154 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7155 if (extent_end <= offset)
7156 goto out;
7157
Chris Mason46bfbb52010-05-26 11:04:10 -04007158 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007159 if (disk_bytenr == 0)
7160 goto out;
7161
7162 if (btrfs_file_extent_compression(leaf, fi) ||
7163 btrfs_file_extent_encryption(leaf, fi) ||
7164 btrfs_file_extent_other_encoding(leaf, fi))
7165 goto out;
7166
Chris Mason46bfbb52010-05-26 11:04:10 -04007167 backref_offset = btrfs_file_extent_offset(leaf, fi);
7168
Josef Bacik7ee9e442013-06-21 16:37:03 -04007169 if (orig_start) {
7170 *orig_start = key.offset - backref_offset;
7171 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7172 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7173 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007174
Chris Mason46bfbb52010-05-26 11:04:10 -04007175 if (btrfs_extent_readonly(root, disk_bytenr))
7176 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007177
7178 num_bytes = min(offset + *len, extent_end) - offset;
7179 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7180 u64 range_end;
7181
7182 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7183 ret = test_range_bit(io_tree, offset, range_end,
7184 EXTENT_DELALLOC, 0, NULL);
7185 if (ret) {
7186 ret = -EAGAIN;
7187 goto out;
7188 }
7189 }
7190
Josef Bacik1bda19e2013-10-18 12:10:36 -04007191 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007192
7193 /*
7194 * look for other files referencing this extent, if we
7195 * find any we must cow
7196 */
Josef Bacik00361582013-08-14 14:02:47 -04007197 trans = btrfs_join_transaction(root);
7198 if (IS_ERR(trans)) {
7199 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007200 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007201 }
7202
7203 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7204 key.offset - backref_offset, disk_bytenr);
7205 btrfs_end_transaction(trans, root);
7206 if (ret) {
7207 ret = 0;
7208 goto out;
7209 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007210
7211 /*
7212 * adjust disk_bytenr and num_bytes to cover just the bytes
7213 * in this extent we are about to write. If there
7214 * are any csums in that range we have to cow in order
7215 * to keep the csums correct
7216 */
7217 disk_bytenr += backref_offset;
7218 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007219 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7220 goto out;
7221 /*
7222 * all of the above have passed, it is safe to overwrite this extent
7223 * without cow
7224 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007225 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007226 ret = 1;
7227out:
7228 btrfs_free_path(path);
7229 return ret;
7230}
7231
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007232bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7233{
7234 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7235 int found = false;
7236 void **pagep = NULL;
7237 struct page *page = NULL;
7238 int start_idx;
7239 int end_idx;
7240
7241 start_idx = start >> PAGE_CACHE_SHIFT;
7242
7243 /*
7244 * end is the last byte in the last page. end == start is legal
7245 */
7246 end_idx = end >> PAGE_CACHE_SHIFT;
7247
7248 rcu_read_lock();
7249
7250 /* Most of the code in this while loop is lifted from
7251 * find_get_page. It's been modified to begin searching from a
7252 * page and return just the first page found in that range. If the
7253 * found idx is less than or equal to the end idx then we know that
7254 * a page exists. If no pages are found or if those pages are
7255 * outside of the range then we're fine (yay!) */
7256 while (page == NULL &&
7257 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7258 page = radix_tree_deref_slot(pagep);
7259 if (unlikely(!page))
7260 break;
7261
7262 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007263 if (radix_tree_deref_retry(page)) {
7264 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007265 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007266 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007267 /*
7268 * Otherwise, shmem/tmpfs must be storing a swap entry
7269 * here as an exceptional entry: so return it without
7270 * attempting to raise page count.
7271 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007272 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007273 break; /* TODO: Is this relevant for this use case? */
7274 }
7275
Filipe Manana91405152014-06-05 13:22:24 +01007276 if (!page_cache_get_speculative(page)) {
7277 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007278 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007279 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007280
7281 /*
7282 * Has the page moved?
7283 * This is part of the lockless pagecache protocol. See
7284 * include/linux/pagemap.h for details.
7285 */
7286 if (unlikely(page != *pagep)) {
7287 page_cache_release(page);
7288 page = NULL;
7289 }
7290 }
7291
7292 if (page) {
7293 if (page->index <= end_idx)
7294 found = true;
7295 page_cache_release(page);
7296 }
7297
7298 rcu_read_unlock();
7299 return found;
7300}
7301
Josef Bacikeb838e72012-07-31 16:28:48 -04007302static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7303 struct extent_state **cached_state, int writing)
7304{
7305 struct btrfs_ordered_extent *ordered;
7306 int ret = 0;
7307
7308 while (1) {
7309 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7310 0, cached_state);
7311 /*
7312 * We're concerned with the entire range that we're going to be
7313 * doing DIO to, so we need to make sure theres no ordered
7314 * extents in this range.
7315 */
7316 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7317 lockend - lockstart + 1);
7318
7319 /*
7320 * We need to make sure there are no buffered pages in this
7321 * range either, we could have raced between the invalidate in
7322 * generic_file_direct_write and locking the extent. The
7323 * invalidate needs to happen so that reads after a write do not
7324 * get stale data.
7325 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007326 if (!ordered &&
7327 (!writing ||
7328 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007329 break;
7330
7331 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7332 cached_state, GFP_NOFS);
7333
7334 if (ordered) {
7335 btrfs_start_ordered_extent(inode, ordered, 1);
7336 btrfs_put_ordered_extent(ordered);
7337 } else {
7338 /* Screw you mmap */
Filipe Manana728404d2014-10-10 09:43:11 +01007339 ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
Filipe Manana075bdbd2014-10-09 21:18:55 +01007340 if (ret)
7341 break;
7342 ret = filemap_fdatawait_range(inode->i_mapping,
7343 lockstart,
7344 lockend);
Josef Bacikeb838e72012-07-31 16:28:48 -04007345 if (ret)
7346 break;
7347
7348 /*
7349 * If we found a page that couldn't be invalidated just
7350 * fall back to buffered.
7351 */
7352 ret = invalidate_inode_pages2_range(inode->i_mapping,
7353 lockstart >> PAGE_CACHE_SHIFT,
7354 lockend >> PAGE_CACHE_SHIFT);
7355 if (ret)
7356 break;
7357 }
7358
7359 cond_resched();
7360 }
7361
7362 return ret;
7363}
7364
Josef Bacik69ffb542012-09-11 15:40:07 -04007365static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7366 u64 len, u64 orig_start,
7367 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007368 u64 orig_block_len, u64 ram_bytes,
7369 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007370{
7371 struct extent_map_tree *em_tree;
7372 struct extent_map *em;
7373 struct btrfs_root *root = BTRFS_I(inode)->root;
7374 int ret;
7375
7376 em_tree = &BTRFS_I(inode)->extent_tree;
7377 em = alloc_extent_map();
7378 if (!em)
7379 return ERR_PTR(-ENOMEM);
7380
7381 em->start = start;
7382 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007383 em->mod_start = start;
7384 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007385 em->len = len;
7386 em->block_len = block_len;
7387 em->block_start = block_start;
7388 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007389 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007390 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007391 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007392 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7393 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007394 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007395
7396 do {
7397 btrfs_drop_extent_cache(inode, em->start,
7398 em->start + em->len - 1, 0);
7399 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007400 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007401 write_unlock(&em_tree->lock);
7402 } while (ret == -EEXIST);
7403
7404 if (ret) {
7405 free_extent_map(em);
7406 return ERR_PTR(ret);
7407 }
7408
7409 return em;
7410}
7411
chandan50745b02015-08-28 21:10:13 +05307412struct btrfs_dio_data {
7413 u64 outstanding_extents;
7414 u64 reserve;
7415};
Josef Bacik69ffb542012-09-11 15:40:07 -04007416
Josef Bacik4b46fce2010-05-23 11:00:55 -04007417static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7418 struct buffer_head *bh_result, int create)
7419{
7420 struct extent_map *em;
7421 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007422 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307423 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007424 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007425 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007426 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007427 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007428 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007429
Miao Xie172a5042013-02-21 02:48:22 -07007430 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007431 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007432 else
Josef Bacikc3298612012-08-03 16:49:19 -04007433 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007434
Josef Bacikc3298612012-08-03 16:49:19 -04007435 lockstart = start;
7436 lockend = start + len - 1;
7437
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007438 if (current->journal_info) {
7439 /*
7440 * Need to pull our outstanding extents and set journal_info to NULL so
7441 * that anything that needs to check if there's a transction doesn't get
7442 * confused.
7443 */
chandan50745b02015-08-28 21:10:13 +05307444 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007445 current->journal_info = NULL;
7446 }
7447
Josef Bacikeb838e72012-07-31 16:28:48 -04007448 /*
7449 * If this errors out it's because we couldn't invalidate pagecache for
7450 * this range and we need to fallback to buffered.
7451 */
7452 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7453 return -ENOTBLK;
7454
Josef Bacik4b46fce2010-05-23 11:00:55 -04007455 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007456 if (IS_ERR(em)) {
7457 ret = PTR_ERR(em);
7458 goto unlock_err;
7459 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007460
7461 /*
7462 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7463 * io. INLINE is special, and we could probably kludge it in here, but
7464 * it's still buffered so for safety lets just fall back to the generic
7465 * buffered path.
7466 *
7467 * For COMPRESSED we _have_ to read the entire extent in so we can
7468 * decompress it, so there will be buffering required no matter what we
7469 * do, so go ahead and fallback to buffered.
7470 *
7471 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7472 * to buffered IO. Don't blame me, this is the price we pay for using
7473 * the generic code.
7474 */
7475 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7476 em->block_start == EXTENT_MAP_INLINE) {
7477 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007478 ret = -ENOTBLK;
7479 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007480 }
7481
7482 /* Just a good old fashioned hole, return */
7483 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7484 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7485 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007486 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007487 }
7488
7489 /*
7490 * We don't allocate a new extent in the following cases
7491 *
7492 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7493 * existing extent.
7494 * 2) The extent is marked as PREALLOC. We're good to go here and can
7495 * just use the extent.
7496 *
7497 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007498 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007499 len = min(len, em->len - (start - em->start));
7500 lockstart = start + len;
7501 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007502 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007503
7504 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7505 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7506 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007507 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007508 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007509
7510 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7511 type = BTRFS_ORDERED_PREALLOC;
7512 else
7513 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007514 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007515 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007516
Josef Bacik00361582013-08-14 14:02:47 -04007517 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007518 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007519 if (type == BTRFS_ORDERED_PREALLOC) {
7520 free_extent_map(em);
7521 em = create_pinned_em(inode, start, len,
7522 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007523 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007524 orig_block_len,
7525 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007526 if (IS_ERR(em)) {
7527 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007528 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007529 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007530 }
7531
Chris Mason46bfbb52010-05-26 11:04:10 -04007532 ret = btrfs_add_ordered_extent_dio(inode, start,
7533 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007534 if (ret) {
7535 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007536 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007537 }
7538 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007539 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007540 }
Josef Bacik00361582013-08-14 14:02:47 -04007541
Chris Mason46bfbb52010-05-26 11:04:10 -04007542 /*
7543 * this will cow the extent, reset the len in case we changed
7544 * it above
7545 */
7546 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007547 free_extent_map(em);
7548 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007549 if (IS_ERR(em)) {
7550 ret = PTR_ERR(em);
7551 goto unlock_err;
7552 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007553 len = min(len, em->len - (start - em->start));
7554unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007555 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7556 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007557 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007558 bh_result->b_bdev = em->bdev;
7559 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007560 if (create) {
7561 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7562 set_buffer_new(bh_result);
7563
7564 /*
7565 * Need to update the i_size under the extent lock so buffered
7566 * readers will get the updated i_size when we unlock.
7567 */
7568 if (start + len > i_size_read(inode))
7569 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007570
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007571 /*
7572 * If we have an outstanding_extents count still set then we're
7573 * within our reservation, otherwise we need to adjust our inode
7574 * counter appropriately.
7575 */
chandan50745b02015-08-28 21:10:13 +05307576 if (dio_data->outstanding_extents) {
7577 (dio_data->outstanding_extents)--;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007578 } else {
Josef Bacik3e05bde2015-02-11 15:08:57 -05007579 spin_lock(&BTRFS_I(inode)->lock);
7580 BTRFS_I(inode)->outstanding_extents++;
7581 spin_unlock(&BTRFS_I(inode)->lock);
7582 }
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007583
Josef Bacik32667892015-02-11 15:08:58 -05007584 btrfs_free_reserved_data_space(inode, len);
chandan50745b02015-08-28 21:10:13 +05307585 WARN_ON(dio_data->reserve < len);
7586 dio_data->reserve -= len;
7587 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007588 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007589
Josef Bacikeb838e72012-07-31 16:28:48 -04007590 /*
7591 * In the case of write we need to clear and unlock the entire range,
7592 * in the case of read we need to unlock only the end area that we
7593 * aren't using if there is any left over space.
7594 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007595 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007596 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7597 lockend, unlock_bits, 1, 0,
7598 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007599 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007600 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007601 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007602
Josef Bacik4b46fce2010-05-23 11:00:55 -04007603 free_extent_map(em);
7604
7605 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007606
7607unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007608 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7609 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
chandan50745b02015-08-28 21:10:13 +05307610 if (dio_data)
7611 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007612 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007613}
7614
Miao Xie8b110e32014-09-12 18:44:03 +08007615static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7616 int rw, int mirror_num)
7617{
7618 struct btrfs_root *root = BTRFS_I(inode)->root;
7619 int ret;
7620
7621 BUG_ON(rw & REQ_WRITE);
7622
7623 bio_get(bio);
7624
7625 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7626 BTRFS_WQ_ENDIO_DIO_REPAIR);
7627 if (ret)
7628 goto err;
7629
7630 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7631err:
7632 bio_put(bio);
7633 return ret;
7634}
7635
7636static int btrfs_check_dio_repairable(struct inode *inode,
7637 struct bio *failed_bio,
7638 struct io_failure_record *failrec,
7639 int failed_mirror)
7640{
7641 int num_copies;
7642
7643 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7644 failrec->logical, failrec->len);
7645 if (num_copies == 1) {
7646 /*
7647 * we only have a single copy of the data, so don't bother with
7648 * all the retry and error correction code that follows. no
7649 * matter what the error is, it is very likely to persist.
7650 */
7651 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7652 num_copies, failrec->this_mirror, failed_mirror);
7653 return 0;
7654 }
7655
7656 failrec->failed_mirror = failed_mirror;
7657 failrec->this_mirror++;
7658 if (failrec->this_mirror == failed_mirror)
7659 failrec->this_mirror++;
7660
7661 if (failrec->this_mirror > num_copies) {
7662 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7663 num_copies, failrec->this_mirror, failed_mirror);
7664 return 0;
7665 }
7666
7667 return 1;
7668}
7669
7670static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7671 struct page *page, u64 start, u64 end,
7672 int failed_mirror, bio_end_io_t *repair_endio,
7673 void *repair_arg)
7674{
7675 struct io_failure_record *failrec;
7676 struct bio *bio;
7677 int isector;
7678 int read_mode;
7679 int ret;
7680
7681 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7682
7683 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7684 if (ret)
7685 return ret;
7686
7687 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7688 failed_mirror);
7689 if (!ret) {
7690 free_io_failure(inode, failrec);
7691 return -EIO;
7692 }
7693
7694 if (failed_bio->bi_vcnt > 1)
7695 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7696 else
7697 read_mode = READ_SYNC;
7698
7699 isector = start - btrfs_io_bio(failed_bio)->logical;
7700 isector >>= inode->i_sb->s_blocksize_bits;
7701 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7702 0, isector, repair_endio, repair_arg);
7703 if (!bio) {
7704 free_io_failure(inode, failrec);
7705 return -EIO;
7706 }
7707
7708 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7709 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7710 read_mode, failrec->this_mirror, failrec->in_validation);
7711
7712 ret = submit_dio_repair_bio(inode, bio, read_mode,
7713 failrec->this_mirror);
7714 if (ret) {
7715 free_io_failure(inode, failrec);
7716 bio_put(bio);
7717 }
7718
7719 return ret;
7720}
7721
7722struct btrfs_retry_complete {
7723 struct completion done;
7724 struct inode *inode;
7725 u64 start;
7726 int uptodate;
7727};
7728
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007729static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007730{
7731 struct btrfs_retry_complete *done = bio->bi_private;
7732 struct bio_vec *bvec;
7733 int i;
7734
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007735 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007736 goto end;
7737
7738 done->uptodate = 1;
7739 bio_for_each_segment_all(bvec, bio, i)
7740 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7741end:
7742 complete(&done->done);
7743 bio_put(bio);
7744}
7745
7746static int __btrfs_correct_data_nocsum(struct inode *inode,
7747 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007748{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007749 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007750 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007751 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007752 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007753 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007754
Miao Xiec1dc0892014-09-12 18:43:56 +08007755 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007756 done.inode = inode;
7757
7758 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7759try_again:
7760 done.uptodate = 0;
7761 done.start = start;
7762 init_completion(&done.done);
7763
7764 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7765 start + bvec->bv_len - 1,
7766 io_bio->mirror_num,
7767 btrfs_retry_endio_nocsum, &done);
7768 if (ret)
7769 return ret;
7770
7771 wait_for_completion(&done.done);
7772
7773 if (!done.uptodate) {
7774 /* We might have another mirror, so try again */
7775 goto try_again;
7776 }
7777
7778 start += bvec->bv_len;
7779 }
7780
7781 return 0;
7782}
7783
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007784static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007785{
7786 struct btrfs_retry_complete *done = bio->bi_private;
7787 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7788 struct bio_vec *bvec;
7789 int uptodate;
7790 int ret;
7791 int i;
7792
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007793 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007794 goto end;
7795
7796 uptodate = 1;
7797 bio_for_each_segment_all(bvec, bio, i) {
7798 ret = __readpage_endio_check(done->inode, io_bio, i,
7799 bvec->bv_page, 0,
7800 done->start, bvec->bv_len);
7801 if (!ret)
7802 clean_io_failure(done->inode, done->start,
7803 bvec->bv_page, 0);
7804 else
7805 uptodate = 0;
7806 }
7807
7808 done->uptodate = uptodate;
7809end:
7810 complete(&done->done);
7811 bio_put(bio);
7812}
7813
7814static int __btrfs_subio_endio_read(struct inode *inode,
7815 struct btrfs_io_bio *io_bio, int err)
7816{
7817 struct bio_vec *bvec;
7818 struct btrfs_retry_complete done;
7819 u64 start;
7820 u64 offset = 0;
7821 int i;
7822 int ret;
7823
7824 err = 0;
7825 start = io_bio->logical;
7826 done.inode = inode;
7827
Miao Xiec1dc0892014-09-12 18:43:56 +08007828 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007829 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7830 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007831 if (likely(!ret))
7832 goto next;
7833try_again:
7834 done.uptodate = 0;
7835 done.start = start;
7836 init_completion(&done.done);
7837
7838 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7839 start + bvec->bv_len - 1,
7840 io_bio->mirror_num,
7841 btrfs_retry_endio, &done);
7842 if (ret) {
7843 err = ret;
7844 goto next;
7845 }
7846
7847 wait_for_completion(&done.done);
7848
7849 if (!done.uptodate) {
7850 /* We might have another mirror, so try again */
7851 goto try_again;
7852 }
7853next:
7854 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007855 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007856 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007857
7858 return err;
7859}
7860
Miao Xie8b110e32014-09-12 18:44:03 +08007861static int btrfs_subio_endio_read(struct inode *inode,
7862 struct btrfs_io_bio *io_bio, int err)
7863{
7864 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7865
7866 if (skip_csum) {
7867 if (unlikely(err))
7868 return __btrfs_correct_data_nocsum(inode, io_bio);
7869 else
7870 return 0;
7871 } else {
7872 return __btrfs_subio_endio_read(inode, io_bio, err);
7873 }
7874}
7875
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007876static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08007877{
7878 struct btrfs_dio_private *dip = bio->bi_private;
7879 struct inode *inode = dip->inode;
7880 struct bio *dio_bio;
7881 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007882 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08007883
Miao Xie8b110e32014-09-12 18:44:03 +08007884 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7885 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007886
Josef Bacik4b46fce2010-05-23 11:00:55 -04007887 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007888 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007889 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007890
Josef Bacik4b46fce2010-05-23 11:00:55 -04007891 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007892
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007893 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08007894
7895 if (io_bio->end_io)
7896 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007897 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007898}
7899
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007900static void btrfs_endio_direct_write(struct bio *bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007901{
7902 struct btrfs_dio_private *dip = bio->bi_private;
7903 struct inode *inode = dip->inode;
7904 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007905 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007906 u64 ordered_offset = dip->logical_offset;
7907 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007908 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007909 int ret;
7910
Chris Mason163cf092010-11-28 19:56:33 -05007911again:
7912 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7913 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007914 ordered_bytes,
7915 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007916 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007917 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007918
Liu Bo9e0af232014-08-15 23:36:53 +08007919 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7920 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007921 btrfs_queue_work(root->fs_info->endio_write_workers,
7922 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007923out_test:
7924 /*
7925 * our bio might span multiple ordered extents. If we haven't
7926 * completed the accounting for the whole dio, go back and try again
7927 */
7928 if (ordered_offset < dip->logical_offset + dip->bytes) {
7929 ordered_bytes = dip->logical_offset + dip->bytes -
7930 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007931 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007932 goto again;
7933 }
Chris Mason9be33952013-05-17 18:30:14 -04007934 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007935
Josef Bacik4b46fce2010-05-23 11:00:55 -04007936 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007937
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007938 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04007939 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007940}
7941
Chris Masoneaf25d92010-05-25 09:48:28 -04007942static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7943 struct bio *bio, int mirror_num,
7944 unsigned long bio_flags, u64 offset)
7945{
7946 int ret;
7947 struct btrfs_root *root = BTRFS_I(inode)->root;
7948 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007949 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007950 return 0;
7951}
7952
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007953static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007954{
7955 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007956 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00007957
Miao Xie8b110e32014-09-12 18:44:03 +08007958 if (err)
7959 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
7960 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7961 btrfs_ino(dip->inode), bio->bi_rw,
7962 (unsigned long long)bio->bi_iter.bi_sector,
7963 bio->bi_iter.bi_size, err);
7964
7965 if (dip->subio_endio)
7966 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007967
7968 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007969 dip->errors = 1;
7970
7971 /*
7972 * before atomic variable goto zero, we must make sure
7973 * dip->errors is perceived to be set.
7974 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007975 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007976 }
7977
7978 /* if there are more bios still pending for this dio, just exit */
7979 if (!atomic_dec_and_test(&dip->pending_bios))
7980 goto out;
7981
Chris Mason9be33952013-05-17 18:30:14 -04007982 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007983 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007984 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007985 dip->dio_bio->bi_error = 0;
7986 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007987 }
7988out:
7989 bio_put(bio);
7990}
7991
7992static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7993 u64 first_sector, gfp_t gfp_flags)
7994{
Chris Masonda2f0f72015-07-02 13:57:22 -07007995 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07007996 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07007997 if (bio)
7998 bio_associate_current(bio);
7999 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008000}
8001
Miao Xiec1dc0892014-09-12 18:43:56 +08008002static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
8003 struct inode *inode,
8004 struct btrfs_dio_private *dip,
8005 struct bio *bio,
8006 u64 file_offset)
8007{
8008 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8009 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8010 int ret;
8011
8012 /*
8013 * We load all the csum data we need when we submit
8014 * the first bio to reduce the csum tree search and
8015 * contention.
8016 */
8017 if (dip->logical_offset == file_offset) {
8018 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
8019 file_offset);
8020 if (ret)
8021 return ret;
8022 }
8023
8024 if (bio == dip->orig_bio)
8025 return 0;
8026
8027 file_offset -= dip->logical_offset;
8028 file_offset >>= inode->i_sb->s_blocksize_bits;
8029 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8030
8031 return 0;
8032}
8033
Miao Xiee65e1532010-11-22 03:04:43 +00008034static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8035 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008036 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008037{
Miao Xiefacc8a222013-07-25 19:22:34 +08008038 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00008039 int write = rw & REQ_WRITE;
8040 struct btrfs_root *root = BTRFS_I(inode)->root;
8041 int ret;
8042
Josef Bacikb812ce22012-11-16 13:56:32 -05008043 if (async_submit)
8044 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8045
Miao Xiee65e1532010-11-22 03:04:43 +00008046 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008047
8048 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02008049 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8050 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008051 if (ret)
8052 goto err;
8053 }
Miao Xiee65e1532010-11-22 03:04:43 +00008054
Josef Bacik1ae39932011-04-06 14:41:34 -04008055 if (skip_sum)
8056 goto map;
8057
8058 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008059 ret = btrfs_wq_submit_bio(root->fs_info,
8060 inode, rw, bio, 0, 0,
8061 file_offset,
8062 __btrfs_submit_bio_start_direct_io,
8063 __btrfs_submit_bio_done);
8064 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008065 } else if (write) {
8066 /*
8067 * If we aren't doing async submit, calculate the csum of the
8068 * bio now.
8069 */
8070 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8071 if (ret)
8072 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008073 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008074 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8075 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008076 if (ret)
8077 goto err;
8078 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008079map:
8080 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008081err:
8082 bio_put(bio);
8083 return ret;
8084}
8085
8086static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8087 int skip_sum)
8088{
8089 struct inode *inode = dip->inode;
8090 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008091 struct bio *bio;
8092 struct bio *orig_bio = dip->orig_bio;
8093 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008094 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008095 u64 file_offset = dip->logical_offset;
8096 u64 submit_len = 0;
8097 u64 map_length;
8098 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08008099 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04008100 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008101
Kent Overstreet4f024f32013-10-11 15:44:27 -07008102 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008103 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008104 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008105 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008106 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008107
Kent Overstreet4f024f32013-10-11 15:44:27 -07008108 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008109 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008110 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008111 goto submit;
8112 }
8113
David Woodhouse53b381b2013-01-29 18:40:14 -05008114 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008115 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008116 async_submit = 0;
8117 else
8118 async_submit = 1;
8119
Josef Bacik02f57c72011-04-06 14:25:44 -04008120 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8121 if (!bio)
8122 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008123
Josef Bacik02f57c72011-04-06 14:25:44 -04008124 bio->bi_private = dip;
8125 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008126 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008127 atomic_inc(&dip->pending_bios);
8128
Miao Xiee65e1532010-11-22 03:04:43 +00008129 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02008130 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00008131 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02008132 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00008133 /*
8134 * inc the count before we submit the bio so
8135 * we know the end IO handler won't happen before
8136 * we inc the count. Otherwise, the dip might get freed
8137 * before we're done setting it up
8138 */
8139 atomic_inc(&dip->pending_bios);
8140 ret = __btrfs_submit_dio_bio(bio, inode, rw,
8141 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008142 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008143 if (ret) {
8144 bio_put(bio);
8145 atomic_dec(&dip->pending_bios);
8146 goto out_err;
8147 }
8148
Miao Xiee65e1532010-11-22 03:04:43 +00008149 start_sector += submit_len >> 9;
8150 file_offset += submit_len;
8151
8152 submit_len = 0;
8153 nr_pages = 0;
8154
8155 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8156 start_sector, GFP_NOFS);
8157 if (!bio)
8158 goto out_err;
8159 bio->bi_private = dip;
8160 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008161 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008162
Kent Overstreet4f024f32013-10-11 15:44:27 -07008163 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008164 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008165 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008166 &map_length, NULL, 0);
8167 if (ret) {
8168 bio_put(bio);
8169 goto out_err;
8170 }
8171 } else {
8172 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308173 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00008174 bvec++;
8175 }
8176 }
8177
Josef Bacik02f57c72011-04-06 14:25:44 -04008178submit:
Miao Xiee65e1532010-11-22 03:04:43 +00008179 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008180 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008181 if (!ret)
8182 return 0;
8183
8184 bio_put(bio);
8185out_err:
8186 dip->errors = 1;
8187 /*
8188 * before atomic variable goto zero, we must
8189 * make sure dip->errors is perceived to be set.
8190 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008191 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008192 if (atomic_dec_and_test(&dip->pending_bios))
8193 bio_io_error(dip->orig_bio);
8194
8195 /* bio_end_io() will handle error, so we needn't return it */
8196 return 0;
8197}
8198
Chris Mason9be33952013-05-17 18:30:14 -04008199static void btrfs_submit_direct(int rw, struct bio *dio_bio,
8200 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008201{
Filipe Manana61de7182015-07-01 12:13:10 +01008202 struct btrfs_dio_private *dip = NULL;
8203 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008204 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008205 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02008206 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008207 int ret = 0;
8208
8209 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8210
Chris Mason9be33952013-05-17 18:30:14 -04008211 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008212 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008213 ret = -ENOMEM;
8214 goto free_ordered;
8215 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008216
Miao Xiec1dc0892014-09-12 18:43:56 +08008217 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008218 if (!dip) {
8219 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008220 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008221 }
8222
8223 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008224 dip->inode = inode;
8225 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008226 dip->bytes = dio_bio->bi_iter.bi_size;
8227 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008228 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008229 dip->orig_bio = io_bio;
8230 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008231 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008232 btrfs_bio = btrfs_io_bio(io_bio);
8233 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008234
Miao Xiec1dc0892014-09-12 18:43:56 +08008235 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008236 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008237 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008238 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008239 dip->subio_endio = btrfs_subio_endio_read;
8240 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008241
Miao Xiee65e1532010-11-22 03:04:43 +00008242 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8243 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008244 return;
Chris Mason9be33952013-05-17 18:30:14 -04008245
Miao Xie23ea8e52014-09-12 18:43:54 +08008246 if (btrfs_bio->end_io)
8247 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008248
Josef Bacik4b46fce2010-05-23 11:00:55 -04008249free_ordered:
8250 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008251 * If we arrived here it means either we failed to submit the dip
8252 * or we either failed to clone the dio_bio or failed to allocate the
8253 * dip. If we cloned the dio_bio and allocated the dip, we can just
8254 * call bio_endio against our io_bio so that we get proper resource
8255 * cleanup if we fail to submit the dip, otherwise, we must do the
8256 * same as btrfs_endio_direct_[write|read] because we can't call these
8257 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008258 */
Filipe Manana61de7182015-07-01 12:13:10 +01008259 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008260 io_bio->bi_error = -EIO;
8261 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008262 /*
8263 * The end io callbacks free our dip, do the final put on io_bio
8264 * and all the cleanup and final put for dio_bio (through
8265 * dio_end_io()).
8266 */
8267 dip = NULL;
8268 io_bio = NULL;
8269 } else {
8270 if (write) {
8271 struct btrfs_ordered_extent *ordered;
8272
8273 ordered = btrfs_lookup_ordered_extent(inode,
8274 file_offset);
8275 set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
8276 /*
8277 * Decrements our ref on the ordered extent and removes
8278 * the ordered extent from the inode's ordered tree,
8279 * doing all the proper resource cleanup such as for the
8280 * reserved space and waking up any waiters for this
8281 * ordered extent (through btrfs_remove_ordered_extent).
8282 */
8283 btrfs_finish_ordered_io(ordered);
8284 } else {
8285 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8286 file_offset + dio_bio->bi_iter.bi_size - 1);
8287 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008288 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008289 /*
8290 * Releases and cleans up our dio_bio, no need to bio_put()
8291 * nor bio_endio()/bio_io_error() against dio_bio.
8292 */
8293 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008294 }
Filipe Manana61de7182015-07-01 12:13:10 +01008295 if (io_bio)
8296 bio_put(io_bio);
8297 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008298}
8299
Omar Sandoval6f673762015-03-16 04:33:52 -07008300static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008301 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008302{
8303 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008304 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008305 unsigned blocksize_mask = root->sectorsize - 1;
8306 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008307
8308 if (offset & blocksize_mask)
8309 goto out;
8310
Al Viro28060d52014-03-22 05:15:17 -04008311 if (iov_iter_alignment(iter) & blocksize_mask)
8312 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008313
Al Viro28060d52014-03-22 05:15:17 -04008314 /* If this is a write we don't need to check anymore */
Omar Sandoval6f673762015-03-16 04:33:52 -07008315 if (iov_iter_rw(iter) == WRITE)
Al Viro28060d52014-03-22 05:15:17 -04008316 return 0;
8317 /*
8318 * Check to make sure we don't have duplicate iov_base's in this
8319 * iovec, if so return EINVAL, otherwise we'll get csum errors
8320 * when reading back.
8321 */
8322 for (seg = 0; seg < iter->nr_segs; seg++) {
8323 for (i = seg + 1; i < iter->nr_segs; i++) {
8324 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008325 goto out;
8326 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008327 }
8328 retval = 0;
8329out:
8330 return retval;
8331}
Josef Bacikeb838e72012-07-31 16:28:48 -04008332
Omar Sandoval22c61862015-03-16 04:33:53 -07008333static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
8334 loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04008335{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008336 struct file *file = iocb->ki_filp;
8337 struct inode *inode = file->f_mapping->host;
chandan50745b02015-08-28 21:10:13 +05308338 struct btrfs_root *root = BTRFS_I(inode)->root;
8339 struct btrfs_dio_data dio_data = { 0 };
Miao Xie09348562013-02-07 10:12:07 +00008340 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008341 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008342 bool wakeup = true;
8343 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008344 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008345
Omar Sandoval6f673762015-03-16 04:33:52 -07008346 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008347 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008348
Jens Axboefe0f07d2015-04-15 17:05:48 -06008349 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008350 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008351
Josef Bacik0e267c42013-07-02 10:38:02 -04008352 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008353 * The generic stuff only does filemap_write_and_wait_range, which
8354 * isn't enough if we've written compressed pages to this area, so
8355 * we need to flush the dirty pages again to make absolutely sure
8356 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008357 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008358 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008359 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8360 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008361 filemap_fdatawrite_range(inode->i_mapping, offset,
8362 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008363
Omar Sandoval6f673762015-03-16 04:33:52 -07008364 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008365 /*
8366 * If the write DIO is beyond the EOF, we need update
8367 * the isize, but it is protected by i_mutex. So we can
8368 * not unlock the i_mutex at this case.
8369 */
8370 if (offset + count <= inode->i_size) {
8371 mutex_unlock(&inode->i_mutex);
8372 relock = true;
8373 }
Miao Xie09348562013-02-07 10:12:07 +00008374 ret = btrfs_delalloc_reserve_space(inode, count);
8375 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008376 goto out;
chandan50745b02015-08-28 21:10:13 +05308377 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008378 BTRFS_MAX_EXTENT_SIZE - 1,
8379 BTRFS_MAX_EXTENT_SIZE);
8380
8381 /*
8382 * We need to know how many extents we reserved so that we can
8383 * do the accounting properly if we go over the number we
8384 * originally calculated. Abuse current->journal_info for this.
8385 */
chandan50745b02015-08-28 21:10:13 +05308386 dio_data.reserve = round_up(count, root->sectorsize);
8387 current->journal_info = &dio_data;
David Sterbaee39b432014-09-30 01:33:33 +02008388 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8389 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008390 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008391 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8392 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008393 }
8394
Omar Sandoval17f8c842015-03-16 04:33:50 -07008395 ret = __blockdev_direct_IO(iocb, inode,
8396 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
8397 iter, offset, btrfs_get_blocks_direct, NULL,
8398 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008399 if (iov_iter_rw(iter) == WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008400 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008401 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308402 if (dio_data.reserve)
8403 btrfs_delalloc_release_space(inode,
8404 dio_data.reserve);
Liu Boddba1bf2015-06-17 16:59:58 +08008405 } else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00008406 btrfs_delalloc_release_space(inode,
8407 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008408 }
Miao Xie38851cc2013-02-08 07:04:11 +00008409out:
Miao Xie2e60a512013-02-08 07:01:08 +00008410 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008411 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008412 if (relock)
8413 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008414
8415 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008416}
8417
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008418#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8419
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008420static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8421 __u64 start, __u64 len)
8422{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008423 int ret;
8424
8425 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8426 if (ret)
8427 return ret;
8428
Chris Masonec29ed52011-02-23 16:23:20 -05008429 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008430}
8431
Chris Mason9ebefb182007-06-15 13:50:00 -04008432int btrfs_readpage(struct file *file, struct page *page)
8433{
Chris Masond1310b22008-01-24 16:13:08 -05008434 struct extent_io_tree *tree;
8435 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008436 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008437}
Chris Mason1832a6d2007-12-21 16:27:21 -05008438
Chris Mason39279cc2007-06-12 06:35:45 -04008439static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8440{
Chris Masond1310b22008-01-24 16:13:08 -05008441 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04008442
8443
8444 if (current->flags & PF_MEMALLOC) {
8445 redirty_page_for_writepage(wbc, page);
8446 unlock_page(page);
8447 return 0;
8448 }
Chris Masond1310b22008-01-24 16:13:08 -05008449 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008450 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8451}
Chris Mason39279cc2007-06-12 06:35:45 -04008452
Eric Sandeen48a3b632013-04-25 20:41:01 +00008453static int btrfs_writepages(struct address_space *mapping,
8454 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008455{
Chris Masond1310b22008-01-24 16:13:08 -05008456 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008457
Chris Masond1310b22008-01-24 16:13:08 -05008458 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008459 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8460}
8461
Chris Mason3ab2fb52007-11-08 10:59:22 -05008462static int
8463btrfs_readpages(struct file *file, struct address_space *mapping,
8464 struct list_head *pages, unsigned nr_pages)
8465{
Chris Masond1310b22008-01-24 16:13:08 -05008466 struct extent_io_tree *tree;
8467 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008468 return extent_readpages(tree, mapping, pages, nr_pages,
8469 btrfs_get_extent);
8470}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008471static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008472{
Chris Masond1310b22008-01-24 16:13:08 -05008473 struct extent_io_tree *tree;
8474 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008475 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008476
Chris Masond1310b22008-01-24 16:13:08 -05008477 tree = &BTRFS_I(page->mapping->host)->io_tree;
8478 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008479 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008480 if (ret == 1) {
8481 ClearPagePrivate(page);
8482 set_page_private(page, 0);
8483 page_cache_release(page);
8484 }
8485 return ret;
8486}
Chris Mason39279cc2007-06-12 06:35:45 -04008487
Chris Masone6dcd2d2008-07-17 12:53:50 -04008488static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8489{
Chris Mason98509cf2008-09-11 15:51:43 -04008490 if (PageWriteback(page) || PageDirty(page))
8491 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008492 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008493}
8494
Lukas Czernerd47992f2013-05-21 23:17:23 -04008495static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8496 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008497{
Josef Bacik5fd02042012-05-02 14:00:54 -04008498 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008499 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008500 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008501 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008502 u64 page_start = page_offset(page);
8503 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008504 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008505
Chris Mason8b62b722009-09-02 16:53:46 -04008506 /*
8507 * we have the page locked, so new writeback can't start,
8508 * and the dirty bit won't be cleared while we are here.
8509 *
8510 * Wait for IO on this page so that we can safely clear
8511 * the PagePrivate2 bit and do ordered accounting
8512 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008513 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008514
Josef Bacik5fd02042012-05-02 14:00:54 -04008515 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008516 if (offset) {
8517 btrfs_releasepage(page, GFP_NOFS);
8518 return;
8519 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008520
8521 if (!inode_evicting)
8522 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8523 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008524 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008525 /*
8526 * IO on this page will never be started, so we need
8527 * to account for any ordered extents now
8528 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008529 if (!inode_evicting)
8530 clear_extent_bit(tree, page_start, page_end,
8531 EXTENT_DIRTY | EXTENT_DELALLOC |
8532 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8533 EXTENT_DEFRAG, 1, 0, &cached_state,
8534 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008535 /*
8536 * whoever cleared the private bit is responsible
8537 * for the finish_ordered_io
8538 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008539 if (TestClearPagePrivate2(page)) {
8540 struct btrfs_ordered_inode_tree *tree;
8541 u64 new_len;
8542
8543 tree = &BTRFS_I(inode)->ordered_tree;
8544
8545 spin_lock_irq(&tree->lock);
8546 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8547 new_len = page_start - ordered->file_offset;
8548 if (new_len < ordered->truncated_len)
8549 ordered->truncated_len = new_len;
8550 spin_unlock_irq(&tree->lock);
8551
8552 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8553 page_start,
8554 PAGE_CACHE_SIZE, 1))
8555 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008556 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008557 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008558 if (!inode_evicting) {
8559 cached_state = NULL;
8560 lock_extent_bits(tree, page_start, page_end, 0,
8561 &cached_state);
8562 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008563 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008564
8565 if (!inode_evicting) {
8566 clear_extent_bit(tree, page_start, page_end,
8567 EXTENT_LOCKED | EXTENT_DIRTY |
8568 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8569 EXTENT_DEFRAG, 1, 1,
8570 &cached_state, GFP_NOFS);
8571
8572 __btrfs_releasepage(page, GFP_NOFS);
8573 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008574
Chris Mason4a096752008-07-21 10:29:44 -04008575 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008576 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008577 ClearPagePrivate(page);
8578 set_page_private(page, 0);
8579 page_cache_release(page);
8580 }
Chris Mason39279cc2007-06-12 06:35:45 -04008581}
8582
Chris Mason9ebefb182007-06-15 13:50:00 -04008583/*
8584 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8585 * called from a page fault handler when a page is first dirtied. Hence we must
8586 * be careful to check for EOF conditions here. We set the page up correctly
8587 * for a written page which means we get ENOSPC checking when writing into
8588 * holes and correct delalloc and unwritten extent mapping on filesystems that
8589 * support these features.
8590 *
8591 * We are not allowed to take the i_mutex here so we have to play games to
8592 * protect against truncate races as the page could now be beyond EOF. Because
8593 * vmtruncate() writes the inode size before removing pages, once we have the
8594 * page lock we can determine safely if the page is beyond EOF. If it is not
8595 * beyond EOF, then the page is guaranteed safe against truncation until we
8596 * unlock the page.
8597 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008598int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008599{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008600 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008601 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008602 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008603 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8604 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008605 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008606 char *kaddr;
8607 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008608 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008609 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008610 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008611 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008612 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008613
Jan Karab2b5ef52012-06-12 16:20:45 +02008614 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04008615 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008616 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008617 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008618 reserved = 1;
8619 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008620 if (ret) {
8621 if (ret == -ENOMEM)
8622 ret = VM_FAULT_OOM;
8623 else /* -ENOSPC, -EIO, etc */
8624 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008625 if (reserved)
8626 goto out;
8627 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008628 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008629
Nick Piggin56a76f82009-03-31 15:23:23 -07008630 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008631again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008632 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008633 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008634 page_start = page_offset(page);
8635 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04008636
Chris Mason9ebefb182007-06-15 13:50:00 -04008637 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008638 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008639 /* page got truncated out from underneath us */
8640 goto out_unlock;
8641 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008642 wait_on_page_writeback(page);
8643
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008644 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008645 set_page_extent_mapped(page);
8646
Chris Masoneb84ae02008-07-17 13:53:27 -04008647 /*
8648 * we can't set the delalloc bits if there are pending ordered
8649 * extents. Drop our locks and wait for them to finish
8650 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008651 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8652 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008653 unlock_extent_cached(io_tree, page_start, page_end,
8654 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008655 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008656 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008657 btrfs_put_ordered_extent(ordered);
8658 goto again;
8659 }
8660
Josef Bacikfbf19082009-10-01 17:10:23 -04008661 /*
8662 * XXX - page_mkwrite gets called every time the page is dirtied, even
8663 * if it was already dirty, so for space accounting reasons we need to
8664 * clear any delalloc bits for the range we are fixing to save. There
8665 * is probably a better way to do this, but for now keep consistent with
8666 * prepare_pages in the normal write path.
8667 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008668 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008669 EXTENT_DIRTY | EXTENT_DELALLOC |
8670 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008671 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008672
Josef Bacik2ac55d42010-02-03 19:33:23 +00008673 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8674 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008675 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008676 unlock_extent_cached(io_tree, page_start, page_end,
8677 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008678 ret = VM_FAULT_SIGBUS;
8679 goto out_unlock;
8680 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008681 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008682
8683 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008684 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008685 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008686 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008687 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008688
Chris Masone6dcd2d2008-07-17 12:53:50 -04008689 if (zero_start != PAGE_CACHE_SIZE) {
8690 kaddr = kmap(page);
8691 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8692 flush_dcache_page(page);
8693 kunmap(page);
8694 }
Chris Mason247e7432008-07-17 12:53:51 -04008695 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008696 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008697 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008698
Chris Mason257c62e2009-10-13 13:21:08 -04008699 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8700 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008701 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008702
Josef Bacik2ac55d42010-02-03 19:33:23 +00008703 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008704
8705out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008706 if (!ret) {
8707 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008708 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008709 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008710 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008711out:
Josef Bacikec39e182012-01-12 19:10:12 -05008712 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008713out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008714 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008715 return ret;
8716}
8717
Josef Bacika41ad392011-01-31 15:30:16 -05008718static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008719{
8720 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008721 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008722 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008723 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008724 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008725 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008726 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008727
Josef Bacik0ef8b722013-10-25 16:13:35 -04008728 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8729 (u64)-1);
8730 if (ret)
8731 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008732
Josef Bacikfcb80c22011-05-03 10:40:22 -04008733 /*
8734 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8735 * 3 things going on here
8736 *
8737 * 1) We need to reserve space for our orphan item and the space to
8738 * delete our orphan item. Lord knows we don't want to have a dangling
8739 * orphan item because we didn't reserve space to remove it.
8740 *
8741 * 2) We need to reserve space to update our inode.
8742 *
8743 * 3) We need to have something to cache all the space that is going to
8744 * be free'd up by the truncate operation, but also have some slack
8745 * space reserved in case it uses space during the truncate (thank you
8746 * very much snapshotting).
8747 *
8748 * And we need these to all be seperate. The fact is we can use alot of
8749 * space doing the truncate, and we have no earthly idea how much space
8750 * we will use, so we need the truncate reservation to be seperate so it
8751 * doesn't end up using space reserved for updating the inode or
8752 * removing the orphan item. We also need to be able to stop the
8753 * transaction and start a new one, which means we need to be able to
8754 * update the inode several times, and we have no idea of knowing how
8755 * many times that will be, so we can't just reserve 1 item for the
8756 * entirety of the opration, so that has to be done seperately as well.
8757 * Then there is the orphan item, which does indeed need to be held on
8758 * to for the whole operation, and we need nobody to touch this reserved
8759 * space except the orphan code.
8760 *
8761 * So that leaves us with
8762 *
8763 * 1) root->orphan_block_rsv - for the orphan deletion.
8764 * 2) rsv - for the truncate reservation, which we will steal from the
8765 * transaction reservation.
8766 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8767 * updating the inode.
8768 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008769 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008770 if (!rsv)
8771 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008772 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008773 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008774
Josef Bacik907cbce2011-08-08 13:46:15 -04008775 /*
Josef Bacik07127182011-08-19 10:29:59 -04008776 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008777 * 1 for updating the inode.
8778 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008779 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008780 if (IS_ERR(trans)) {
8781 err = PTR_ERR(trans);
8782 goto out;
8783 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008784
Josef Bacik907cbce2011-08-08 13:46:15 -04008785 /* Migrate the slack space for the truncate to our reserve */
8786 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8787 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008788 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008789
Chris Mason5a3f23d2009-03-31 13:27:11 -04008790 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008791 * So if we truncate and then write and fsync we normally would just
8792 * write the extents that changed, which is a problem if we need to
8793 * first truncate that entire inode. So set this flag so we write out
8794 * all of the extents in the inode to the sync log so we're completely
8795 * safe.
8796 */
8797 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008798 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008799
Yan, Zheng80825102009-11-12 09:35:36 +00008800 while (1) {
8801 ret = btrfs_truncate_inode_items(trans, root, inode,
8802 inode->i_size,
8803 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08008804 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05008805 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008806 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008807 }
Chris Mason39279cc2007-06-12 06:35:45 -04008808
Josef Bacikfcb80c22011-05-03 10:40:22 -04008809 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008810 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008811 if (ret) {
8812 err = ret;
8813 break;
8814 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008815
Yan, Zheng80825102009-11-12 09:35:36 +00008816 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008817 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008818
8819 trans = btrfs_start_transaction(root, 2);
8820 if (IS_ERR(trans)) {
8821 ret = err = PTR_ERR(trans);
8822 trans = NULL;
8823 break;
8824 }
8825
8826 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8827 rsv, min_size);
8828 BUG_ON(ret); /* shouldn't happen */
8829 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008830 }
8831
8832 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008833 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008834 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008835 if (ret)
8836 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008837 }
8838
Chris Mason917c16b2011-11-08 14:49:59 -05008839 if (trans) {
8840 trans->block_rsv = &root->fs_info->trans_block_rsv;
8841 ret = btrfs_update_inode(trans, root, inode);
8842 if (ret && !err)
8843 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008844
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008845 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008846 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008847 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008848
8849out:
8850 btrfs_free_block_rsv(root, rsv);
8851
Josef Bacik3893e332011-01-31 16:03:11 -05008852 if (ret && !err)
8853 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008854
Josef Bacik3893e332011-01-31 16:03:11 -05008855 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008856}
8857
Sven Wegener3b963622008-06-09 21:57:42 -04008858/*
Chris Masond352ac62008-09-29 15:18:18 -04008859 * create a new subvolume directory/inode (helper for the ioctl).
8860 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008861int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008862 struct btrfs_root *new_root,
8863 struct btrfs_root *parent_root,
8864 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008865{
Chris Mason39279cc2007-06-12 06:35:45 -04008866 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008867 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008868 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008869
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008870 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8871 new_dirid, new_dirid,
8872 S_IFDIR | (~current_umask() & S_IRWXUGO),
8873 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008874 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008875 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008876 inode->i_op = &btrfs_dir_inode_operations;
8877 inode->i_fop = &btrfs_dir_file_operations;
8878
Miklos Szeredibfe86842011-10-28 14:13:29 +02008879 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008880 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008881 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008882
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008883 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8884 if (err)
8885 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008886 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008887 new_root->root_key.objectid, err);
8888
Yan, Zheng76dda932009-09-21 16:00:26 -04008889 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008890
Yan, Zheng76dda932009-09-21 16:00:26 -04008891 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008892 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008893}
8894
Chris Mason39279cc2007-06-12 06:35:45 -04008895struct inode *btrfs_alloc_inode(struct super_block *sb)
8896{
8897 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008898 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008899
8900 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8901 if (!ei)
8902 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008903
8904 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008905 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008906 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008907 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008908 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008909 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008910 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008911 ei->disk_i_size = 0;
8912 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008913 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008914 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008915 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008916 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008917 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008918
Josef Bacik9e0baf62011-07-15 15:16:44 +00008919 spin_lock_init(&ei->lock);
8920 ei->outstanding_extents = 0;
8921 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008922
Josef Bacik72ac3c02012-05-23 14:13:11 -04008923 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008924 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008925
Miao Xie16cdcec2011-04-22 18:12:22 +08008926 ei->delayed_node = NULL;
8927
chandan r9cc97d62012-07-04 12:48:07 +05308928 ei->i_otime.tv_sec = 0;
8929 ei->i_otime.tv_nsec = 0;
8930
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008931 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008932 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008933 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8934 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008935 ei->io_tree.track_uptodate = 1;
8936 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008937 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008938 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008939 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008940 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008941 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008942 RB_CLEAR_NODE(&ei->rb_node);
8943
8944 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008945}
8946
Josef Bacikaaedb552013-10-11 14:44:09 -04008947#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8948void btrfs_test_destroy_inode(struct inode *inode)
8949{
8950 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8951 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8952}
8953#endif
8954
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008955static void btrfs_i_callback(struct rcu_head *head)
8956{
8957 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008958 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8959}
8960
Chris Mason39279cc2007-06-12 06:35:45 -04008961void btrfs_destroy_inode(struct inode *inode)
8962{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008963 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008964 struct btrfs_root *root = BTRFS_I(inode)->root;
8965
Al Virob3d9b7a2012-06-09 13:51:19 -04008966 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008967 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008968 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8969 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008970 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8971 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008972 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008973
Chris Mason5a3f23d2009-03-31 13:27:11 -04008974 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008975 * This can happen where we create an inode, but somebody else also
8976 * created the same inode and we need to destroy the one we already
8977 * created.
8978 */
8979 if (!root)
8980 goto free;
8981
Josef Bacik8a35d952012-05-23 14:26:42 -04008982 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8983 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008984 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008985 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008986 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008987 }
Josef Bacik7b128762008-07-24 12:17:14 -04008988
Chris Masond3977122009-01-05 21:25:51 -05008989 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008990 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8991 if (!ordered)
8992 break;
8993 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008994 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008995 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008996 btrfs_remove_ordered_extent(inode, ordered);
8997 btrfs_put_ordered_extent(ordered);
8998 btrfs_put_ordered_extent(ordered);
8999 }
9000 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009001 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009002 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009003free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009004 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009005}
9006
Al Viro45321ac2010-06-07 13:43:19 -04009007int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009008{
9009 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009010
Naohiro Aota6379ef92013-06-06 09:56:34 +00009011 if (root == NULL)
9012 return 1;
9013
Liu Bofa6ac872013-02-20 14:10:23 +00009014 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009015 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009016 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009017 else
Al Viro45321ac2010-06-07 13:43:19 -04009018 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009019}
9020
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009021static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009022{
9023 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9024
9025 inode_init_once(&ei->vfs_inode);
9026}
9027
9028void btrfs_destroy_cachep(void)
9029{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009030 /*
9031 * Make sure all delayed rcu free inodes are flushed before we
9032 * destroy cache.
9033 */
9034 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04009035 if (btrfs_inode_cachep)
9036 kmem_cache_destroy(btrfs_inode_cachep);
9037 if (btrfs_trans_handle_cachep)
9038 kmem_cache_destroy(btrfs_trans_handle_cachep);
9039 if (btrfs_transaction_cachep)
9040 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009041 if (btrfs_path_cachep)
9042 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05009043 if (btrfs_free_space_cachep)
9044 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009045 if (btrfs_delalloc_work_cachep)
9046 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009047}
9048
9049int btrfs_init_cachep(void)
9050{
David Sterba837e1972012-09-07 03:00:48 -06009051 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009052 sizeof(struct btrfs_inode), 0,
9053 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009054 if (!btrfs_inode_cachep)
9055 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009056
David Sterba837e1972012-09-07 03:00:48 -06009057 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009058 sizeof(struct btrfs_trans_handle), 0,
9059 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009060 if (!btrfs_trans_handle_cachep)
9061 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009062
David Sterba837e1972012-09-07 03:00:48 -06009063 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009064 sizeof(struct btrfs_transaction), 0,
9065 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009066 if (!btrfs_transaction_cachep)
9067 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009068
David Sterba837e1972012-09-07 03:00:48 -06009069 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009070 sizeof(struct btrfs_path), 0,
9071 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009072 if (!btrfs_path_cachep)
9073 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009074
David Sterba837e1972012-09-07 03:00:48 -06009075 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009076 sizeof(struct btrfs_free_space), 0,
9077 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
9078 if (!btrfs_free_space_cachep)
9079 goto fail;
9080
Miao Xie8ccf6f192012-10-25 09:28:04 +00009081 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
9082 sizeof(struct btrfs_delalloc_work), 0,
9083 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
9084 NULL);
9085 if (!btrfs_delalloc_work_cachep)
9086 goto fail;
9087
Chris Mason39279cc2007-06-12 06:35:45 -04009088 return 0;
9089fail:
9090 btrfs_destroy_cachep();
9091 return -ENOMEM;
9092}
9093
9094static int btrfs_getattr(struct vfsmount *mnt,
9095 struct dentry *dentry, struct kstat *stat)
9096{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009097 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009098 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009099 u32 blocksize = inode->i_sb->s_blocksize;
9100
Chris Mason39279cc2007-06-12 06:35:45 -04009101 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009102 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05009103 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009104
9105 spin_lock(&BTRFS_I(inode)->lock);
9106 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9107 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009108 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009109 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009110 return 0;
9111}
9112
Chris Masond3977122009-01-05 21:25:51 -05009113static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9114 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009115{
9116 struct btrfs_trans_handle *trans;
9117 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009118 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009119 struct inode *new_inode = d_inode(new_dentry);
9120 struct inode *old_inode = d_inode(old_dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04009121 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009122 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009123 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009124 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009125 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009126
Li Zefan33345d012011-04-20 10:31:50 +08009127 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009128 return -EPERM;
9129
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009130 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009131 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009132 return -EXDEV;
9133
Li Zefan33345d012011-04-20 10:31:50 +08009134 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9135 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009136 return -ENOTEMPTY;
9137
Chris Mason39279cc2007-06-12 06:35:45 -04009138 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009139 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009140 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009141
9142
9143 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009144 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009145 new_dentry->d_name.name,
9146 new_dentry->d_name.len);
9147
9148 if (ret) {
9149 if (ret == -EEXIST) {
9150 /* we shouldn't get
9151 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309152 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009153 return ret;
9154 }
9155 } else {
9156 /* maybe -EOVERFLOW */
9157 return ret;
9158 }
9159 }
9160 ret = 0;
9161
Chris Mason5a3f23d2009-03-31 13:27:11 -04009162 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009163 * we're using rename to replace one file with another. Start IO on it
9164 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009165 */
Chris Mason8d875f92014-08-12 10:47:42 -07009166 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009167 filemap_flush(old_inode->i_mapping);
9168
Yan, Zheng76dda932009-09-21 16:00:26 -04009169 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009170 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009171 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009172 /*
9173 * We want to reserve the absolute worst case amount of items. So if
9174 * both inodes are subvols and we need to unlink them then that would
9175 * require 4 item modifications, but if they are both normal inodes it
9176 * would require 5 item modifications, so we'll assume their normal
9177 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9178 * should cover the worst case number of items we'll modify.
9179 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04009180 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009181 if (IS_ERR(trans)) {
9182 ret = PTR_ERR(trans);
9183 goto out_notrans;
9184 }
Chris Mason5f39d392007-10-15 16:14:19 -04009185
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009186 if (dest != root)
9187 btrfs_record_root_in_trans(trans, dest);
9188
Yan, Zhenga5719522009-09-24 09:17:31 -04009189 ret = btrfs_set_inode_index(new_dir, &index);
9190 if (ret)
9191 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009192
Miao Xie67de1172013-12-26 13:07:06 +08009193 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009194 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009195 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009196 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009197 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009198 ret = btrfs_insert_inode_ref(trans, dest,
9199 new_dentry->d_name.name,
9200 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009201 old_ino,
9202 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009203 if (ret)
9204 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009205 /*
9206 * this is an ugly little race, but the rename is required
9207 * to make sure that if we crash, the inode is either at the
9208 * old name or the new one. pinning the log transaction lets
9209 * us make sure we don't allow a log commit to come in after
9210 * we unlink the name but before we add the new name back in.
9211 */
9212 btrfs_pin_log_trans(root);
9213 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009214
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009215 inode_inc_iversion(old_dir);
9216 inode_inc_iversion(new_dir);
9217 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009218 old_dir->i_ctime = old_dir->i_mtime = ctime;
9219 new_dir->i_ctime = new_dir->i_mtime = ctime;
9220 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04009221
Chris Mason12fcfd22009-03-24 10:24:20 -04009222 if (old_dentry->d_parent != new_dentry->d_parent)
9223 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9224
Li Zefan33345d012011-04-20 10:31:50 +08009225 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009226 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9227 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9228 old_dentry->d_name.name,
9229 old_dentry->d_name.len);
9230 } else {
Al Viro92986792011-03-04 17:14:37 +00009231 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009232 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009233 old_dentry->d_name.name,
9234 old_dentry->d_name.len);
9235 if (!ret)
9236 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009237 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009238 if (ret) {
9239 btrfs_abort_transaction(trans, root, ret);
9240 goto out_fail;
9241 }
Chris Mason39279cc2007-06-12 06:35:45 -04009242
9243 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009244 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009245 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08009246 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009247 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9248 root_objectid = BTRFS_I(new_inode)->location.objectid;
9249 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9250 root_objectid,
9251 new_dentry->d_name.name,
9252 new_dentry->d_name.len);
9253 BUG_ON(new_inode->i_nlink == 0);
9254 } else {
9255 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009256 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009257 new_dentry->d_name.name,
9258 new_dentry->d_name.len);
9259 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009260 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009261 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009262 if (ret) {
9263 btrfs_abort_transaction(trans, root, ret);
9264 goto out_fail;
9265 }
Chris Mason39279cc2007-06-12 06:35:45 -04009266 }
Josef Bacikaec74772008-07-24 12:12:38 -04009267
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009268 ret = btrfs_add_link(trans, new_dir, old_inode,
9269 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009270 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009271 if (ret) {
9272 btrfs_abort_transaction(trans, root, ret);
9273 goto out_fail;
9274 }
Chris Mason39279cc2007-06-12 06:35:45 -04009275
Miao Xie67de1172013-12-26 13:07:06 +08009276 if (old_inode->i_nlink == 1)
9277 BTRFS_I(old_inode)->dir_index = index;
9278
Li Zefan33345d012011-04-20 10:31:50 +08009279 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04009280 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00009281 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009282 btrfs_end_log_trans(root);
9283 }
Chris Mason39279cc2007-06-12 06:35:45 -04009284out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009285 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009286out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009287 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009288 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009289
Chris Mason39279cc2007-06-12 06:35:45 -04009290 return ret;
9291}
9292
Miklos Szeredi80ace852014-07-23 15:15:32 +02009293static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9294 struct inode *new_dir, struct dentry *new_dentry,
9295 unsigned int flags)
9296{
9297 if (flags & ~RENAME_NOREPLACE)
9298 return -EINVAL;
9299
9300 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
9301}
9302
Miao Xie8ccf6f192012-10-25 09:28:04 +00009303static void btrfs_run_delalloc_work(struct btrfs_work *work)
9304{
9305 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009306 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009307
9308 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9309 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009310 inode = delalloc_work->inode;
9311 if (delalloc_work->wait) {
9312 btrfs_wait_ordered_range(inode, 0, (u64)-1);
9313 } else {
9314 filemap_flush(inode->i_mapping);
9315 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9316 &BTRFS_I(inode)->runtime_flags))
9317 filemap_flush(inode->i_mapping);
9318 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00009319
9320 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009321 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009322 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009323 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009324 complete(&delalloc_work->completion);
9325}
9326
9327struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
9328 int wait, int delay_iput)
9329{
9330 struct btrfs_delalloc_work *work;
9331
9332 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
9333 if (!work)
9334 return NULL;
9335
9336 init_completion(&work->completion);
9337 INIT_LIST_HEAD(&work->list);
9338 work->inode = inode;
9339 work->wait = wait;
9340 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009341 WARN_ON_ONCE(!inode);
9342 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9343 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009344
9345 return work;
9346}
9347
9348void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9349{
9350 wait_for_completion(&work->completion);
9351 kmem_cache_free(btrfs_delalloc_work_cachep, work);
9352}
9353
Chris Masond352ac62008-09-29 15:18:18 -04009354/*
9355 * some fairly slow code that needs optimization. This walks the list
9356 * of all the inodes with pending delalloc and forces them to disk.
9357 */
Miao Xie6c255e62014-03-06 13:55:01 +08009358static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9359 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009360{
Chris Masonea8c2812008-08-04 23:17:27 -04009361 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009362 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009363 struct btrfs_delalloc_work *work, *next;
9364 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009365 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009366 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009367
Miao Xie8ccf6f192012-10-25 09:28:04 +00009368 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009369 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009370
Miao Xie573bfb72014-03-06 13:55:03 +08009371 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009372 spin_lock(&root->delalloc_lock);
9373 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009374 while (!list_empty(&splice)) {
9375 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009376 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009377
Miao Xieeb73c1b2013-05-15 07:48:22 +00009378 list_move_tail(&binode->delalloc_inodes,
9379 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009380 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009381 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009382 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009383 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009384 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009385 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009386
9387 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009388 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009389 if (delay_iput)
9390 btrfs_add_delayed_iput(inode);
9391 else
9392 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009393 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009394 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009395 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009396 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009397 btrfs_queue_work(root->fs_info->flush_workers,
9398 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009399 ret++;
9400 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009401 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009402 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009403 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009404 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009405 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009406
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009407out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009408 list_for_each_entry_safe(work, next, &works, list) {
9409 list_del_init(&work->list);
9410 btrfs_wait_and_free_delalloc_work(work);
9411 }
9412
Miao Xieeb73c1b2013-05-15 07:48:22 +00009413 if (!list_empty_careful(&splice)) {
9414 spin_lock(&root->delalloc_lock);
9415 list_splice_tail(&splice, &root->delalloc_inodes);
9416 spin_unlock(&root->delalloc_lock);
9417 }
Miao Xie573bfb72014-03-06 13:55:03 +08009418 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009419 return ret;
9420}
9421
9422int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9423{
9424 int ret;
9425
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009426 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009427 return -EROFS;
9428
Miao Xie6c255e62014-03-06 13:55:01 +08009429 ret = __start_delalloc_inodes(root, delay_iput, -1);
9430 if (ret > 0)
9431 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009432 /*
9433 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009434 * we have to make sure the IO is actually started and that
9435 * ordered extents get created before we return
9436 */
9437 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009438 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009439 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009440 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009441 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9442 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009443 }
9444 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009445 return ret;
9446}
9447
Miao Xie6c255e62014-03-06 13:55:01 +08009448int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9449 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009450{
9451 struct btrfs_root *root;
9452 struct list_head splice;
9453 int ret;
9454
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009455 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009456 return -EROFS;
9457
9458 INIT_LIST_HEAD(&splice);
9459
Miao Xie573bfb72014-03-06 13:55:03 +08009460 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009461 spin_lock(&fs_info->delalloc_root_lock);
9462 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009463 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009464 root = list_first_entry(&splice, struct btrfs_root,
9465 delalloc_root);
9466 root = btrfs_grab_fs_root(root);
9467 BUG_ON(!root);
9468 list_move_tail(&root->delalloc_root,
9469 &fs_info->delalloc_roots);
9470 spin_unlock(&fs_info->delalloc_root_lock);
9471
Miao Xie6c255e62014-03-06 13:55:01 +08009472 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009473 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009474 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009475 goto out;
9476
Miao Xie6c255e62014-03-06 13:55:01 +08009477 if (nr != -1) {
9478 nr -= ret;
9479 WARN_ON(nr < 0);
9480 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009481 spin_lock(&fs_info->delalloc_root_lock);
9482 }
9483 spin_unlock(&fs_info->delalloc_root_lock);
9484
Miao Xie6c255e62014-03-06 13:55:01 +08009485 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009486 atomic_inc(&fs_info->async_submit_draining);
9487 while (atomic_read(&fs_info->nr_async_submits) ||
9488 atomic_read(&fs_info->async_delalloc_pages)) {
9489 wait_event(fs_info->async_submit_wait,
9490 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9491 atomic_read(&fs_info->async_delalloc_pages) == 0));
9492 }
9493 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009494out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009495 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009496 spin_lock(&fs_info->delalloc_root_lock);
9497 list_splice_tail(&splice, &fs_info->delalloc_roots);
9498 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009499 }
Miao Xie573bfb72014-03-06 13:55:03 +08009500 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009501 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009502}
9503
Chris Mason39279cc2007-06-12 06:35:45 -04009504static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9505 const char *symname)
9506{
9507 struct btrfs_trans_handle *trans;
9508 struct btrfs_root *root = BTRFS_I(dir)->root;
9509 struct btrfs_path *path;
9510 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009511 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009512 int err;
9513 int drop_inode = 0;
9514 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309515 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009516 int name_len;
9517 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009518 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009519 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009520 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009521
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009522 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009523 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9524 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009525
Josef Bacik9ed74f22009-09-11 16:12:44 -04009526 /*
9527 * 2 items for inode item and ref
9528 * 2 items for dir items
9529 * 1 item for xattr if selinux is on
9530 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009531 trans = btrfs_start_transaction(root, 5);
9532 if (IS_ERR(trans))
9533 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009534
Li Zefan581bb052011-04-20 10:06:11 +08009535 err = btrfs_find_free_ino(root, &objectid);
9536 if (err)
9537 goto out_unlock;
9538
Josef Bacikaec74772008-07-24 12:12:38 -04009539 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009540 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04009541 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009542 if (IS_ERR(inode)) {
9543 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009544 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009545 }
Chris Mason39279cc2007-06-12 06:35:45 -04009546
Casey Schauflerad19db72011-12-15 10:09:07 -05009547 /*
9548 * If the active LSM wants to access the inode during
9549 * d_instantiate it needs these. Smack checks to see
9550 * if the filesystem supports xattrs by looking at the
9551 * ops vector.
9552 */
9553 inode->i_fop = &btrfs_file_operations;
9554 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009555 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009556 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9557
9558 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9559 if (err)
9560 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009561
Josef Bacika1b075d2010-11-19 20:36:11 +00009562 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009563 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009564 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009565
9566 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009567 if (!path) {
9568 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009569 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009570 }
Li Zefan33345d012011-04-20 10:31:50 +08009571 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009572 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009573 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009574 datasize = btrfs_file_extent_calc_inline_size(name_len);
9575 err = btrfs_insert_empty_item(trans, root, path, &key,
9576 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009577 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009578 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009579 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009580 }
Chris Mason5f39d392007-10-15 16:14:19 -04009581 leaf = path->nodes[0];
9582 ei = btrfs_item_ptr(leaf, path->slots[0],
9583 struct btrfs_file_extent_item);
9584 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9585 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009586 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009587 btrfs_set_file_extent_encryption(leaf, ei, 0);
9588 btrfs_set_file_extent_compression(leaf, ei, 0);
9589 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9590 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9591
Chris Mason39279cc2007-06-12 06:35:45 -04009592 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009593 write_extent_buffer(leaf, symname, ptr, name_len);
9594 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009595 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009596
Chris Mason39279cc2007-06-12 06:35:45 -04009597 inode->i_op = &btrfs_symlink_inode_operations;
9598 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -04009599 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009600 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009601 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009602 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009603 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009604 goto out_unlock_inode;
9605 }
9606
9607 unlock_new_inode(inode);
9608 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009609
9610out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009611 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009612 if (drop_inode) {
9613 inode_dec_link_count(inode);
9614 iput(inode);
9615 }
Liu Bob53d3f52012-11-14 14:34:34 +00009616 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009617 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009618
9619out_unlock_inode:
9620 drop_inode = 1;
9621 unlock_new_inode(inode);
9622 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009623}
Chris Mason16432982008-04-10 10:23:21 -04009624
Josef Bacik0af3d002010-06-21 14:48:16 -04009625static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9626 u64 start, u64 num_bytes, u64 min_size,
9627 loff_t actual_len, u64 *alloc_hint,
9628 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009629{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009630 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9631 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009632 struct btrfs_root *root = BTRFS_I(inode)->root;
9633 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009634 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009635 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009636 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009637 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009638 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009639
Josef Bacik0af3d002010-06-21 14:48:16 -04009640 if (trans)
9641 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009642 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009643 if (own_trans) {
9644 trans = btrfs_start_transaction(root, 3);
9645 if (IS_ERR(trans)) {
9646 ret = PTR_ERR(trans);
9647 break;
9648 }
Yan Zhengd899e052008-10-30 14:25:28 -04009649 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009650
Chris Mason154ea282013-03-05 11:11:26 -05009651 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9652 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009653 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009654 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009655 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009656 if (own_trans)
9657 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009658 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009659 }
9660
Yan Zhengd899e052008-10-30 14:25:28 -04009661 ret = insert_reserved_file_extent(trans, inode,
9662 cur_offset, ins.objectid,
9663 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009664 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009665 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009666 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009667 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009668 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009669 btrfs_abort_transaction(trans, root, ret);
9670 if (own_trans)
9671 btrfs_end_transaction(trans, root);
9672 break;
9673 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009674
Chris Masona1ed8352009-09-11 12:27:37 -04009675 btrfs_drop_extent_cache(inode, cur_offset,
9676 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009677
Josef Bacik5dc562c2012-08-17 13:14:17 -04009678 em = alloc_extent_map();
9679 if (!em) {
9680 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9681 &BTRFS_I(inode)->runtime_flags);
9682 goto next;
9683 }
9684
9685 em->start = cur_offset;
9686 em->orig_start = cur_offset;
9687 em->len = ins.offset;
9688 em->block_start = ins.objectid;
9689 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009690 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009691 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009692 em->bdev = root->fs_info->fs_devices->latest_bdev;
9693 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9694 em->generation = trans->transid;
9695
9696 while (1) {
9697 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009698 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009699 write_unlock(&em_tree->lock);
9700 if (ret != -EEXIST)
9701 break;
9702 btrfs_drop_extent_cache(inode, cur_offset,
9703 cur_offset + ins.offset - 1,
9704 0);
9705 }
9706 free_extent_map(em);
9707next:
Yan Zhengd899e052008-10-30 14:25:28 -04009708 num_bytes -= ins.offset;
9709 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009710 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009711
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009712 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009713 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009714 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009715 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009716 (actual_len > inode->i_size) &&
9717 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009718 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009719 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009720 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009721 i_size = cur_offset;
9722 i_size_write(inode, i_size);
9723 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009724 }
9725
Yan Zhengd899e052008-10-30 14:25:28 -04009726 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009727
9728 if (ret) {
9729 btrfs_abort_transaction(trans, root, ret);
9730 if (own_trans)
9731 btrfs_end_transaction(trans, root);
9732 break;
9733 }
Yan Zhengd899e052008-10-30 14:25:28 -04009734
Josef Bacik0af3d002010-06-21 14:48:16 -04009735 if (own_trans)
9736 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009737 }
Yan Zhengd899e052008-10-30 14:25:28 -04009738 return ret;
9739}
9740
Josef Bacik0af3d002010-06-21 14:48:16 -04009741int btrfs_prealloc_file_range(struct inode *inode, int mode,
9742 u64 start, u64 num_bytes, u64 min_size,
9743 loff_t actual_len, u64 *alloc_hint)
9744{
9745 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9746 min_size, actual_len, alloc_hint,
9747 NULL);
9748}
9749
9750int btrfs_prealloc_file_range_trans(struct inode *inode,
9751 struct btrfs_trans_handle *trans, int mode,
9752 u64 start, u64 num_bytes, u64 min_size,
9753 loff_t actual_len, u64 *alloc_hint)
9754{
9755 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9756 min_size, actual_len, alloc_hint, trans);
9757}
9758
Chris Masone6dcd2d2008-07-17 12:53:50 -04009759static int btrfs_set_page_dirty(struct page *page)
9760{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009761 return __set_page_dirty_nobuffers(page);
9762}
9763
Al Viro10556cb22011-06-20 19:28:19 -04009764static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009765{
Li Zefanb83cc962010-12-20 16:04:08 +08009766 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009767 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009768
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009769 if (mask & MAY_WRITE &&
9770 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9771 if (btrfs_root_readonly(root))
9772 return -EROFS;
9773 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9774 return -EACCES;
9775 }
Al Viro2830ba72011-06-20 19:16:29 -04009776 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009777}
Chris Mason39279cc2007-06-12 06:35:45 -04009778
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009779static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9780{
9781 struct btrfs_trans_handle *trans;
9782 struct btrfs_root *root = BTRFS_I(dir)->root;
9783 struct inode *inode = NULL;
9784 u64 objectid;
9785 u64 index;
9786 int ret = 0;
9787
9788 /*
9789 * 5 units required for adding orphan entry
9790 */
9791 trans = btrfs_start_transaction(root, 5);
9792 if (IS_ERR(trans))
9793 return PTR_ERR(trans);
9794
9795 ret = btrfs_find_free_ino(root, &objectid);
9796 if (ret)
9797 goto out;
9798
9799 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9800 btrfs_ino(dir), objectid, mode, &index);
9801 if (IS_ERR(inode)) {
9802 ret = PTR_ERR(inode);
9803 inode = NULL;
9804 goto out;
9805 }
9806
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009807 inode->i_fop = &btrfs_file_operations;
9808 inode->i_op = &btrfs_file_inode_operations;
9809
9810 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009811 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9812
Chris Masonb0d5d102014-09-08 13:08:51 -07009813 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9814 if (ret)
9815 goto out_inode;
9816
9817 ret = btrfs_update_inode(trans, root, inode);
9818 if (ret)
9819 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009820 ret = btrfs_orphan_add(trans, inode);
9821 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009822 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009823
Filipe Manana5762b5c2014-08-01 00:10:32 +01009824 /*
9825 * We set number of links to 0 in btrfs_new_inode(), and here we set
9826 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9827 * through:
9828 *
9829 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9830 */
9831 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009832 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009833 d_tmpfile(dentry, inode);
9834 mark_inode_dirty(inode);
9835
9836out:
9837 btrfs_end_transaction(trans, root);
9838 if (ret)
9839 iput(inode);
9840 btrfs_balance_delayed_items(root);
9841 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009842 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009843
9844out_inode:
9845 unlock_new_inode(inode);
9846 goto out;
9847
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009848}
9849
Filipe Mananab38ef712014-11-13 17:01:45 +00009850/* Inspired by filemap_check_errors() */
9851int btrfs_inode_check_errors(struct inode *inode)
9852{
9853 int ret = 0;
9854
9855 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
9856 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
9857 ret = -ENOSPC;
9858 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
9859 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
9860 ret = -EIO;
9861
9862 return ret;
9863}
9864
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009865static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009866 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009867 .lookup = btrfs_lookup,
9868 .create = btrfs_create,
9869 .unlink = btrfs_unlink,
9870 .link = btrfs_link,
9871 .mkdir = btrfs_mkdir,
9872 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009873 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009874 .symlink = btrfs_symlink,
9875 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009876 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009877 .setxattr = btrfs_setxattr,
9878 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009879 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009880 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009881 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009882 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009883 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009884 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009885 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009886};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009887static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009888 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009889 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009890 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009891 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009892 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009893};
Yan, Zheng76dda932009-09-21 16:00:26 -04009894
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009895static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009896 .llseek = generic_file_llseek,
9897 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009898 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009899 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009900#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009901 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009902#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009903 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009904 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009905};
9906
Chris Masond1310b22008-01-24 16:13:08 -05009907static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009908 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009909 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009910 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009911 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009912 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009913 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009914 .set_bit_hook = btrfs_set_bit_hook,
9915 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009916 .merge_extent_hook = btrfs_merge_extent_hook,
9917 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009918};
9919
Chris Mason35054392009-01-21 13:11:13 -05009920/*
9921 * btrfs doesn't support the bmap operation because swapfiles
9922 * use bmap to make a mapping of extents in the file. They assume
9923 * these extents won't change over the life of the file and they
9924 * use the bmap result to do IO directly to the drive.
9925 *
9926 * the btrfs bmap call would return logical addresses that aren't
9927 * suitable for IO and they also will change frequently as COW
9928 * operations happen. So, swapfile + btrfs == corruption.
9929 *
9930 * For now we're avoiding this by dropping bmap.
9931 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009932static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009933 .readpage = btrfs_readpage,
9934 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04009935 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009936 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009937 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009938 .invalidatepage = btrfs_invalidatepage,
9939 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009940 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009941 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009942};
9943
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009944static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009945 .readpage = btrfs_readpage,
9946 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009947 .invalidatepage = btrfs_invalidatepage,
9948 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009949};
9950
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009951static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009952 .getattr = btrfs_getattr,
9953 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009954 .setxattr = btrfs_setxattr,
9955 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009956 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009957 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009958 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009959 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009960 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009961 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009962 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009963};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009964static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009965 .getattr = btrfs_getattr,
9966 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009967 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009968 .setxattr = btrfs_setxattr,
9969 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009970 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009971 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009972 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009973 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009974 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009975};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009976static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009977 .readlink = generic_readlink,
9978 .follow_link = page_follow_link_light,
9979 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009980 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009981 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009982 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009983 .setxattr = btrfs_setxattr,
9984 .getxattr = btrfs_getxattr,
9985 .listxattr = btrfs_listxattr,
9986 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009987 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009988};
Yan, Zheng76dda932009-09-21 16:00:26 -04009989
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009990const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009991 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009992 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009993};