blob: e3fe137fb826f4a946233c008a8c5610989fe6b5 [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>
Kent Overstreeta27bb332013-05-07 16:19:08 -070035#include <linux/aio.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050037#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040038#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040039#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020041#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050042#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000043#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050044#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040045#include <linux/posix_acl_xattr.h>
Guenter Roeck4a3d1ca2015-03-13 01:58:46 -070046#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040052#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040053#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040054#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020055#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040056#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050057#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050058#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080059#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000060#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040061#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000062#include "props.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 }
756
Yan, Zhengc2167752009-11-12 09:34:21 +0000757 /*
758 * here we're doing allocation and writeback of the
759 * compressed pages
760 */
761 btrfs_drop_extent_cache(inode, async_extent->start,
762 async_extent->start +
763 async_extent->ram_size - 1, 0);
764
David Sterba172ddd62011-04-21 00:48:27 +0200765 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000766 if (!em) {
767 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500768 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000769 }
Chris Mason771ed682008-11-06 22:02:51 -0500770 em->start = async_extent->start;
771 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500772 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400773 em->mod_start = em->start;
774 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500775
776 em->block_start = ins.objectid;
777 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500778 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400779 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500780 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800781 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500782 set_bit(EXTENT_FLAG_PINNED, &em->flags);
783 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400784 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500785
Chris Masond3977122009-01-05 21:25:51 -0500786 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400787 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400788 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400789 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500790 if (ret != -EEXIST) {
791 free_extent_map(em);
792 break;
793 }
794 btrfs_drop_extent_cache(inode, async_extent->start,
795 async_extent->start +
796 async_extent->ram_size - 1, 0);
797 }
798
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500799 if (ret)
800 goto out_free_reserve;
801
Li Zefan261507a02010-12-17 14:21:50 +0800802 ret = btrfs_add_ordered_extent_compress(inode,
803 async_extent->start,
804 ins.objectid,
805 async_extent->ram_size,
806 ins.offset,
807 BTRFS_ORDERED_COMPRESSED,
808 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100809 if (ret) {
810 btrfs_drop_extent_cache(inode, async_extent->start,
811 async_extent->start +
812 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500813 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100814 }
Chris Mason771ed682008-11-06 22:02:51 -0500815
Chris Mason771ed682008-11-06 22:02:51 -0500816 /*
817 * clear dirty, set writeback and unlock the pages.
818 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400819 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400820 async_extent->start +
821 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400822 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
823 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400824 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500825 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500826 async_extent->start,
827 async_extent->ram_size,
828 ins.objectid,
829 ins.offset, async_extent->pages,
830 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100831 if (ret) {
832 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
833 struct page *p = async_extent->pages[0];
834 const u64 start = async_extent->start;
835 const u64 end = start + async_extent->ram_size - 1;
836
837 p->mapping = inode->i_mapping;
838 tree->ops->writepage_end_io_hook(p, start, end,
839 NULL, 0);
840 p->mapping = NULL;
841 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
842 PAGE_END_WRITEBACK |
843 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100844 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100845 }
Chris Mason771ed682008-11-06 22:02:51 -0500846 alloc_hint = ins.objectid + ins.offset;
847 kfree(async_extent);
848 cond_resched();
849 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100850 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500851out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800852 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100853out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400854 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500855 async_extent->start +
856 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400857 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400858 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
859 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100860 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
861 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100862 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100863 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500864 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500865}
866
Josef Bacik4b46fce2010-05-23 11:00:55 -0400867static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
868 u64 num_bytes)
869{
870 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
871 struct extent_map *em;
872 u64 alloc_hint = 0;
873
874 read_lock(&em_tree->lock);
875 em = search_extent_mapping(em_tree, start, num_bytes);
876 if (em) {
877 /*
878 * if block start isn't an actual block number then find the
879 * first block in this inode and use that as a hint. If that
880 * block is also bogus then just don't worry about it.
881 */
882 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
883 free_extent_map(em);
884 em = search_extent_mapping(em_tree, 0, 0);
885 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
886 alloc_hint = em->block_start;
887 if (em)
888 free_extent_map(em);
889 } else {
890 alloc_hint = em->block_start;
891 free_extent_map(em);
892 }
893 }
894 read_unlock(&em_tree->lock);
895
896 return alloc_hint;
897}
898
Chris Mason771ed682008-11-06 22:02:51 -0500899/*
900 * when extent_io.c finds a delayed allocation range in the file,
901 * the call backs end up in this code. The basic idea is to
902 * allocate extents on disk for the range, and create ordered data structs
903 * in ram to track those extents.
904 *
905 * locked_page is the page that writepage had locked already. We use
906 * it to make sure we don't do extra locks or unlocks.
907 *
908 * *page_started is set to one if we unlock locked_page and do everything
909 * required to start IO on it. It may be clean and already done with
910 * IO when we return.
911 */
Josef Bacik00361582013-08-14 14:02:47 -0400912static noinline int cow_file_range(struct inode *inode,
913 struct page *locked_page,
914 u64 start, u64 end, int *page_started,
915 unsigned long *nr_written,
916 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500917{
Josef Bacik00361582013-08-14 14:02:47 -0400918 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500919 u64 alloc_hint = 0;
920 u64 num_bytes;
921 unsigned long ram_size;
922 u64 disk_num_bytes;
923 u64 cur_alloc_size;
924 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500925 struct btrfs_key ins;
926 struct extent_map *em;
927 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
928 int ret = 0;
929
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400930 if (btrfs_is_free_space_inode(inode)) {
931 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500932 ret = -EINVAL;
933 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400934 }
Chris Mason771ed682008-11-06 22:02:51 -0500935
Qu Wenruofda28322013-02-26 08:10:22 +0000936 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500937 num_bytes = max(blocksize, num_bytes);
938 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500939
Chris Mason4cb53002011-05-24 15:35:30 -0400940 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400941 if (num_bytes < 64 * 1024 &&
942 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400943 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400944
Chris Mason771ed682008-11-06 22:02:51 -0500945 if (start == 0) {
946 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400947 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
948 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500949 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400950 extent_clear_unlock_delalloc(inode, start, end, NULL,
951 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400952 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400953 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
954 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000955
Chris Mason771ed682008-11-06 22:02:51 -0500956 *nr_written = *nr_written +
957 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
958 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500959 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100960 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100961 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500962 }
963 }
Chris Masonc8b97812008-10-29 14:49:59 -0400964
965 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200966 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400967
Josef Bacik4b46fce2010-05-23 11:00:55 -0400968 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400969 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400970
Chris Masond3977122009-01-05 21:25:51 -0500971 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400972 unsigned long op;
973
Josef Bacik287a0ab2010-03-19 18:07:23 +0000974 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400975 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500976 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800977 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400978 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100979 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500980
David Sterba172ddd62011-04-21 00:48:27 +0200981 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000982 if (!em) {
983 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000984 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000985 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400986 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500987 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500988 ram_size = ins.offset;
989 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400990 em->mod_start = em->start;
991 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400992
Chris Masone6dcd2d2008-07-17 12:53:50 -0400993 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400994 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500995 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400996 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400997 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400998 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400999 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001000
Chris Masond3977122009-01-05 21:25:51 -05001001 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001002 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001003 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001004 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001005 if (ret != -EEXIST) {
1006 free_extent_map(em);
1007 break;
1008 }
1009 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001010 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001011 }
Liu Boace68ba2013-04-22 10:53:47 +00001012 if (ret)
1013 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001014
Chris Mason98d20f62008-04-14 09:46:10 -04001015 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001016 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001017 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001018 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001019 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001020
Yan Zheng17d217f2008-12-12 10:03:38 -05001021 if (root->root_key.objectid ==
1022 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1023 ret = btrfs_reloc_clone_csums(inode, start,
1024 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001025 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001026 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001027 }
1028
Chris Masond3977122009-01-05 21:25:51 -05001029 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001030 break;
Chris Masond3977122009-01-05 21:25:51 -05001031
Chris Masonc8b97812008-10-29 14:49:59 -04001032 /* we're not doing compressed IO, don't unlock the first
1033 * page (which the caller expects to stay locked), don't
1034 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001035 *
1036 * Do set the Private2 bit so we know this page was properly
1037 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001038 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001039 op = unlock ? PAGE_UNLOCK : 0;
1040 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001041
Josef Bacikc2790a22013-07-29 11:20:47 -04001042 extent_clear_unlock_delalloc(inode, start,
1043 start + ram_size - 1, locked_page,
1044 EXTENT_LOCKED | EXTENT_DELALLOC,
1045 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001046 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001047 num_bytes -= cur_alloc_size;
1048 alloc_hint = ins.objectid + ins.offset;
1049 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001050 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001051out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001052 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001053
Filipe Mananad9f85962014-08-25 10:43:00 +01001054out_drop_extent_cache:
1055 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001056out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001057 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001058out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001059 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001060 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1061 EXTENT_DELALLOC | EXTENT_DEFRAG,
1062 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1063 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001064 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001065}
Chris Masonc8b97812008-10-29 14:49:59 -04001066
Chris Mason771ed682008-11-06 22:02:51 -05001067/*
1068 * work queue call back to started compression on a file and pages
1069 */
1070static noinline void async_cow_start(struct btrfs_work *work)
1071{
1072 struct async_cow *async_cow;
1073 int num_added = 0;
1074 async_cow = container_of(work, struct async_cow, work);
1075
1076 compress_file_range(async_cow->inode, async_cow->locked_page,
1077 async_cow->start, async_cow->end, async_cow,
1078 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001079 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001080 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001081 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001082 }
Chris Mason771ed682008-11-06 22:02:51 -05001083}
1084
1085/*
1086 * work queue call back to submit previously compressed pages
1087 */
1088static noinline void async_cow_submit(struct btrfs_work *work)
1089{
1090 struct async_cow *async_cow;
1091 struct btrfs_root *root;
1092 unsigned long nr_pages;
1093
1094 async_cow = container_of(work, struct async_cow, work);
1095
1096 root = async_cow->root;
1097 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1098 PAGE_CACHE_SHIFT;
1099
Josef Bacik66657b32012-08-01 15:36:24 -04001100 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001101 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001102 waitqueue_active(&root->fs_info->async_submit_wait))
1103 wake_up(&root->fs_info->async_submit_wait);
1104
Chris Masond3977122009-01-05 21:25:51 -05001105 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001106 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001107}
Chris Masonc8b97812008-10-29 14:49:59 -04001108
Chris Mason771ed682008-11-06 22:02:51 -05001109static noinline void async_cow_free(struct btrfs_work *work)
1110{
1111 struct async_cow *async_cow;
1112 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001113 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001114 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001115 kfree(async_cow);
1116}
1117
1118static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1119 u64 start, u64 end, int *page_started,
1120 unsigned long *nr_written)
1121{
1122 struct async_cow *async_cow;
1123 struct btrfs_root *root = BTRFS_I(inode)->root;
1124 unsigned long nr_pages;
1125 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001126 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001127
Chris Masona3429ab2009-10-08 12:30:20 -04001128 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1129 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001130 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001131 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001132 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001133 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001134 async_cow->root = root;
1135 async_cow->locked_page = locked_page;
1136 async_cow->start = start;
1137
Wang Shilongf79707b2014-07-17 11:44:09 +08001138 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1139 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001140 cur_end = end;
1141 else
1142 cur_end = min(end, start + 512 * 1024 - 1);
1143
1144 async_cow->end = cur_end;
1145 INIT_LIST_HEAD(&async_cow->extents);
1146
Liu Bo9e0af232014-08-15 23:36:53 +08001147 btrfs_init_work(&async_cow->work,
1148 btrfs_delalloc_helper,
1149 async_cow_start, async_cow_submit,
1150 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001151
Chris Mason771ed682008-11-06 22:02:51 -05001152 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1153 PAGE_CACHE_SHIFT;
1154 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1155
Qu Wenruoafe3d242014-02-28 10:46:07 +08001156 btrfs_queue_work(root->fs_info->delalloc_workers,
1157 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001158
1159 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1160 wait_event(root->fs_info->async_submit_wait,
1161 (atomic_read(&root->fs_info->async_delalloc_pages) <
1162 limit));
1163 }
1164
Chris Masond3977122009-01-05 21:25:51 -05001165 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001166 atomic_read(&root->fs_info->async_delalloc_pages)) {
1167 wait_event(root->fs_info->async_submit_wait,
1168 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1169 0));
1170 }
1171
1172 *nr_written += nr_pages;
1173 start = cur_end + 1;
1174 }
1175 *page_started = 1;
1176 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001177}
1178
Chris Masond3977122009-01-05 21:25:51 -05001179static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001180 u64 bytenr, u64 num_bytes)
1181{
1182 int ret;
1183 struct btrfs_ordered_sum *sums;
1184 LIST_HEAD(list);
1185
Yan Zheng07d400a2009-01-06 11:42:00 -05001186 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001187 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001188 if (ret == 0 && list_empty(&list))
1189 return 0;
1190
1191 while (!list_empty(&list)) {
1192 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1193 list_del(&sums->list);
1194 kfree(sums);
1195 }
1196 return 1;
1197}
1198
Chris Masond352ac62008-09-29 15:18:18 -04001199/*
1200 * when nowcow writeback call back. This checks for snapshots or COW copies
1201 * of the extents that exist in the file, and COWs the file as required.
1202 *
1203 * If no cow copies or snapshots exist, we write directly to the existing
1204 * blocks on disk
1205 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001206static noinline int run_delalloc_nocow(struct inode *inode,
1207 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001208 u64 start, u64 end, int *page_started, int force,
1209 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001210{
Chris Masonbe20aa92007-12-17 20:14:01 -05001211 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001212 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001213 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001214 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001215 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001216 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001217 u64 cow_start;
1218 u64 cur_offset;
1219 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001220 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001221 u64 disk_bytenr;
1222 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001223 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001224 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001225 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001226 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001227 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 int nocow;
1229 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001230 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001231 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001232
1233 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001234 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001235 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1236 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001237 EXTENT_DO_ACCOUNTING |
1238 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001239 PAGE_CLEAR_DIRTY |
1240 PAGE_SET_WRITEBACK |
1241 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001242 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001243 }
Li Zefan82d59022011-04-20 10:33:24 +08001244
Liu Bo83eea1f2012-07-10 05:28:39 -06001245 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001246
1247 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001248 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001249 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001250 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001251
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001252 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001253 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1254 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001255 EXTENT_DO_ACCOUNTING |
1256 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001257 PAGE_CLEAR_DIRTY |
1258 PAGE_SET_WRITEBACK |
1259 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001260 btrfs_free_path(path);
1261 return PTR_ERR(trans);
1262 }
1263
Josef Bacik74b21072011-04-13 12:02:53 -04001264 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001265
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 cow_start = (u64)-1;
1267 cur_offset = start;
1268 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001269 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001271 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001272 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001273 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1274 leaf = path->nodes[0];
1275 btrfs_item_key_to_cpu(leaf, &found_key,
1276 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001277 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 found_key.type == BTRFS_EXTENT_DATA_KEY)
1279 path->slots[0]--;
1280 }
1281 check_prev = 0;
1282next_slot:
1283 leaf = path->nodes[0];
1284 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1285 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001286 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001287 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001288 if (ret > 0)
1289 break;
1290 leaf = path->nodes[0];
1291 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001292
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 nocow = 0;
1294 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001295 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001297
Li Zefan33345d012011-04-20 10:31:50 +08001298 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001299 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1300 found_key.offset > end)
1301 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001302
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 if (found_key.offset > cur_offset) {
1304 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001305 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001306 goto out_check;
1307 }
Chris Masonc31f8832008-01-08 15:46:31 -05001308
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 fi = btrfs_item_ptr(leaf, path->slots[0],
1310 struct btrfs_file_extent_item);
1311 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001312
Josef Bacikcc95bef2013-04-04 14:31:27 -04001313 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001314 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1315 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001317 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 extent_end = found_key.offset +
1319 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001320 disk_num_bytes =
1321 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001322 if (extent_end <= start) {
1323 path->slots[0]++;
1324 goto next_slot;
1325 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001326 if (disk_bytenr == 0)
1327 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001328 if (btrfs_file_extent_compression(leaf, fi) ||
1329 btrfs_file_extent_encryption(leaf, fi) ||
1330 btrfs_file_extent_other_encoding(leaf, fi))
1331 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001332 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1333 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001334 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001335 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001336 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001337 found_key.offset -
1338 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001339 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001340 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001341 disk_bytenr += cur_offset - found_key.offset;
1342 num_bytes = min(end + 1, extent_end) - cur_offset;
1343 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001344 * if there are pending snapshots for this root,
1345 * we fall into common COW way.
1346 */
1347 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001348 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001349 if (!err)
1350 goto out_check;
1351 }
1352 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001353 * force cow if csum exists in the range.
1354 * this ensure that csum for a given extent are
1355 * either valid or do not exist.
1356 */
1357 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1358 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 nocow = 1;
1360 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1361 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001362 btrfs_file_extent_inline_len(leaf,
1363 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 extent_end = ALIGN(extent_end, root->sectorsize);
1365 } else {
1366 BUG_ON(1);
1367 }
1368out_check:
1369 if (extent_end <= start) {
1370 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001371 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001372 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 goto next_slot;
1374 }
1375 if (!nocow) {
1376 if (cow_start == (u64)-1)
1377 cow_start = cur_offset;
1378 cur_offset = extent_end;
1379 if (cur_offset > end)
1380 break;
1381 path->slots[0]++;
1382 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001383 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001384
David Sterbab3b4aa72011-04-21 01:20:15 +02001385 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001386 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001387 ret = cow_file_range(inode, locked_page,
1388 cow_start, found_key.offset - 1,
1389 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001390 if (ret) {
1391 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001392 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001393 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001394 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001395 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001396 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001397
Yan Zhengd899e052008-10-30 14:25:28 -04001398 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1399 struct extent_map *em;
1400 struct extent_map_tree *em_tree;
1401 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001402 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001403 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001404 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001405 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001406 em->len = num_bytes;
1407 em->block_len = num_bytes;
1408 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001409 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001410 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001411 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001412 em->mod_start = em->start;
1413 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001414 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001415 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001416 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001417 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001418 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001419 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001420 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001421 if (ret != -EEXIST) {
1422 free_extent_map(em);
1423 break;
1424 }
1425 btrfs_drop_extent_cache(inode, em->start,
1426 em->start + em->len - 1, 0);
1427 }
1428 type = BTRFS_ORDERED_PREALLOC;
1429 } else {
1430 type = BTRFS_ORDERED_NOCOW;
1431 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001432
1433 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001434 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001435 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001436
Yan, Zhengefa56462010-05-16 10:49:59 -04001437 if (root->root_key.objectid ==
1438 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1439 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1440 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001441 if (ret) {
1442 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001443 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001444 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001445 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001446 }
1447
Josef Bacikc2790a22013-07-29 11:20:47 -04001448 extent_clear_unlock_delalloc(inode, cur_offset,
1449 cur_offset + num_bytes - 1,
1450 locked_page, EXTENT_LOCKED |
1451 EXTENT_DELALLOC, PAGE_UNLOCK |
1452 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001453 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001454 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001455 cur_offset = extent_end;
1456 if (cur_offset > end)
1457 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001458 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001459 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001460
Josef Bacik17ca04a2012-05-31 15:58:55 -04001461 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001462 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001463 cur_offset = end;
1464 }
1465
Yan Zheng80ff3852008-10-30 14:20:02 -04001466 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001467 ret = cow_file_range(inode, locked_page, cow_start, end,
1468 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001469 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001470 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001471 }
1472
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001473error:
Miao Xiea698d0752012-09-20 01:51:59 -06001474 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001475 if (!ret)
1476 ret = err;
1477
Josef Bacik17ca04a2012-05-31 15:58:55 -04001478 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001479 extent_clear_unlock_delalloc(inode, cur_offset, end,
1480 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001481 EXTENT_DELALLOC | EXTENT_DEFRAG |
1482 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1483 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001484 PAGE_SET_WRITEBACK |
1485 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001486 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001487 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001488}
1489
Wang Shilong47059d92014-07-03 18:22:07 +08001490static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1491{
1492
1493 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1494 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1495 return 0;
1496
1497 /*
1498 * @defrag_bytes is a hint value, no spinlock held here,
1499 * if is not zero, it means the file is defragging.
1500 * Force cow if given extent needs to be defragged.
1501 */
1502 if (BTRFS_I(inode)->defrag_bytes &&
1503 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1504 EXTENT_DEFRAG, 0, NULL))
1505 return 1;
1506
1507 return 0;
1508}
1509
Chris Masond352ac62008-09-29 15:18:18 -04001510/*
1511 * extent_io.c call back to do delayed allocation processing
1512 */
Chris Masonc8b97812008-10-29 14:49:59 -04001513static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001514 u64 start, u64 end, int *page_started,
1515 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001516{
Chris Masonbe20aa92007-12-17 20:14:01 -05001517 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001518 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001519
Wang Shilong47059d92014-07-03 18:22:07 +08001520 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001521 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001522 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001523 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001524 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001525 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001526 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001527 ret = cow_file_range(inode, locked_page, start, end,
1528 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001529 } else {
1530 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1531 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001532 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001533 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001534 }
Chris Masonb888db22007-08-27 16:49:44 -04001535 return ret;
1536}
1537
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001538static void btrfs_split_extent_hook(struct inode *inode,
1539 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001540{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001541 u64 size;
1542
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001543 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001544 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001545 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001546
Josef Bacikdcab6a32015-02-11 15:08:59 -05001547 size = orig->end - orig->start + 1;
1548 if (size > BTRFS_MAX_EXTENT_SIZE) {
1549 u64 num_extents;
1550 u64 new_size;
1551
1552 /*
Josef Bacikba117212015-03-13 15:01:24 -04001553 * See the explanation in btrfs_merge_extent_hook, the same
1554 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001555 */
1556 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001557 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001558 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001559 new_size = split - orig->start;
1560 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1561 BTRFS_MAX_EXTENT_SIZE);
1562 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1563 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001564 return;
1565 }
1566
Josef Bacik9e0baf62011-07-15 15:16:44 +00001567 spin_lock(&BTRFS_I(inode)->lock);
1568 BTRFS_I(inode)->outstanding_extents++;
1569 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001570}
1571
1572/*
1573 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1574 * extents so we can keep track of new extents that are just merged onto old
1575 * extents, such as when we are doing sequential writes, so we can properly
1576 * account for the metadata space we'll need.
1577 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001578static void btrfs_merge_extent_hook(struct inode *inode,
1579 struct extent_state *new,
1580 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001581{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001582 u64 new_size, old_size;
1583 u64 num_extents;
1584
Josef Bacik9ed74f22009-09-11 16:12:44 -04001585 /* not delalloc, ignore it */
1586 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001587 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001588
Josef Bacik8461a3d2015-03-13 15:12:08 -04001589 if (new->start > other->start)
1590 new_size = new->end - other->start + 1;
1591 else
1592 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001593
1594 /* we're not bigger than the max, unreserve the space and go */
1595 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1596 spin_lock(&BTRFS_I(inode)->lock);
1597 BTRFS_I(inode)->outstanding_extents--;
1598 spin_unlock(&BTRFS_I(inode)->lock);
1599 return;
1600 }
1601
1602 /*
Josef Bacikba117212015-03-13 15:01:24 -04001603 * We have to add up either side to figure out how many extents were
1604 * accounted for before we merged into one big extent. If the number of
1605 * extents we accounted for is <= the amount we need for the new range
1606 * then we can return, otherwise drop. Think of it like this
1607 *
1608 * [ 4k][MAX_SIZE]
1609 *
1610 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1611 * need 2 outstanding extents, on one side we have 1 and the other side
1612 * we have 1 so they are == and we can return. But in this case
1613 *
1614 * [MAX_SIZE+4k][MAX_SIZE+4k]
1615 *
1616 * Each range on their own accounts for 2 extents, but merged together
1617 * they are only 3 extents worth of accounting, so we need to drop in
1618 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001619 */
Josef Bacikba117212015-03-13 15:01:24 -04001620 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1622 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001623 old_size = new->end - new->start + 1;
1624 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1625 BTRFS_MAX_EXTENT_SIZE);
1626
Josef Bacikdcab6a32015-02-11 15:08:59 -05001627 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001628 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001629 return;
1630
Josef Bacik9e0baf62011-07-15 15:16:44 +00001631 spin_lock(&BTRFS_I(inode)->lock);
1632 BTRFS_I(inode)->outstanding_extents--;
1633 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001634}
1635
Miao Xieeb73c1b2013-05-15 07:48:22 +00001636static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1637 struct inode *inode)
1638{
1639 spin_lock(&root->delalloc_lock);
1640 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1641 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1642 &root->delalloc_inodes);
1643 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1644 &BTRFS_I(inode)->runtime_flags);
1645 root->nr_delalloc_inodes++;
1646 if (root->nr_delalloc_inodes == 1) {
1647 spin_lock(&root->fs_info->delalloc_root_lock);
1648 BUG_ON(!list_empty(&root->delalloc_root));
1649 list_add_tail(&root->delalloc_root,
1650 &root->fs_info->delalloc_roots);
1651 spin_unlock(&root->fs_info->delalloc_root_lock);
1652 }
1653 }
1654 spin_unlock(&root->delalloc_lock);
1655}
1656
1657static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1658 struct inode *inode)
1659{
1660 spin_lock(&root->delalloc_lock);
1661 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1662 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1663 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1664 &BTRFS_I(inode)->runtime_flags);
1665 root->nr_delalloc_inodes--;
1666 if (!root->nr_delalloc_inodes) {
1667 spin_lock(&root->fs_info->delalloc_root_lock);
1668 BUG_ON(list_empty(&root->delalloc_root));
1669 list_del_init(&root->delalloc_root);
1670 spin_unlock(&root->fs_info->delalloc_root_lock);
1671 }
1672 }
1673 spin_unlock(&root->delalloc_lock);
1674}
1675
Chris Masond352ac62008-09-29 15:18:18 -04001676/*
1677 * extent_io.c set_bit_hook, used to track delayed allocation
1678 * bytes in this file, and to maintain the list of inodes that
1679 * have pending delalloc work to be done.
1680 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001681static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001682 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001683{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001684
Wang Shilong47059d92014-07-03 18:22:07 +08001685 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1686 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001687 /*
1688 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001689 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001690 * bit, which is only set or cleared with irqs on
1691 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001692 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001693 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001694 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001695 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001696
Josef Bacik9e0baf62011-07-15 15:16:44 +00001697 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001698 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001699 } else {
1700 spin_lock(&BTRFS_I(inode)->lock);
1701 BTRFS_I(inode)->outstanding_extents++;
1702 spin_unlock(&BTRFS_I(inode)->lock);
1703 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001704
Josef Bacik6a3891c2015-03-16 17:38:52 -04001705 /* For sanity tests */
1706 if (btrfs_test_is_dummy_root(root))
1707 return;
1708
Miao Xie963d6782013-01-29 10:10:51 +00001709 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1710 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001711 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001712 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001713 if (*bits & EXTENT_DEFRAG)
1714 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001715 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001716 &BTRFS_I(inode)->runtime_flags))
1717 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001718 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001719 }
Chris Mason291d6732008-01-29 15:55:23 -05001720}
1721
Chris Masond352ac62008-09-29 15:18:18 -04001722/*
1723 * extent_io.c clear_bit_hook, see set_bit_hook for why
1724 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001725static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001726 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001727 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001728{
Wang Shilong47059d92014-07-03 18:22:07 +08001729 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001730 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1731 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001732
1733 spin_lock(&BTRFS_I(inode)->lock);
1734 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1735 BTRFS_I(inode)->defrag_bytes -= len;
1736 spin_unlock(&BTRFS_I(inode)->lock);
1737
Chris Mason75eff682008-12-15 15:54:40 -05001738 /*
1739 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001740 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001741 * bit, which is only set or cleared with irqs on
1742 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001743 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001744 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001745 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001746
Josef Bacik9e0baf62011-07-15 15:16:44 +00001747 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001748 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001749 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1750 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001751 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001752 spin_unlock(&BTRFS_I(inode)->lock);
1753 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001754
Josef Bacikb6d08f02013-09-27 14:57:43 -04001755 /*
1756 * We don't reserve metadata space for space cache inodes so we
1757 * don't need to call dellalloc_release_metadata if there is an
1758 * error.
1759 */
1760 if (*bits & EXTENT_DO_ACCOUNTING &&
1761 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001762 btrfs_delalloc_release_metadata(inode, len);
1763
Josef Bacik6a3891c2015-03-16 17:38:52 -04001764 /* For sanity tests. */
1765 if (btrfs_test_is_dummy_root(root))
1766 return;
1767
Josef Bacik0cb59c92010-07-02 12:14:14 -04001768 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001769 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001770 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001771
Miao Xie963d6782013-01-29 10:10:51 +00001772 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1773 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001774 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001775 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001776 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001777 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001778 &BTRFS_I(inode)->runtime_flags))
1779 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001780 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001781 }
Chris Mason291d6732008-01-29 15:55:23 -05001782}
1783
Chris Masond352ac62008-09-29 15:18:18 -04001784/*
1785 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1786 * we don't create bios that span stripes or chunks
1787 */
David Woodhouse64a16702009-07-15 23:29:37 +01001788int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001789 size_t size, struct bio *bio,
1790 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001791{
1792 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001793 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001794 u64 length = 0;
1795 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001796 int ret;
1797
Chris Mason771ed682008-11-06 22:02:51 -05001798 if (bio_flags & EXTENT_BIO_COMPRESSED)
1799 return 0;
1800
Kent Overstreet4f024f32013-10-11 15:44:27 -07001801 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001802 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001803 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001804 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001805 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001806 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001807 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001808 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001809 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001810}
1811
Chris Masond352ac62008-09-29 15:18:18 -04001812/*
1813 * in order to insert checksums into the metadata in large chunks,
1814 * we wait until bio submission time. All the pages in the bio are
1815 * checksummed and sums are attached onto the ordered extent record.
1816 *
1817 * At IO completion time the cums attached on the ordered extent record
1818 * are inserted into the btree
1819 */
Chris Masond3977122009-01-05 21:25:51 -05001820static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1821 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001822 unsigned long bio_flags,
1823 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001824{
Chris Mason065631f2008-02-20 12:07:25 -05001825 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001826 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001827
Chris Masond20f7042008-12-08 16:58:54 -05001828 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001829 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001830 return 0;
1831}
Chris Masone0156402008-04-16 11:15:20 -04001832
Chris Mason4a69a412008-11-06 22:03:00 -05001833/*
1834 * in order to insert checksums into the metadata in large chunks,
1835 * we wait until bio submission time. All the pages in the bio are
1836 * checksummed and sums are attached onto the ordered extent record.
1837 *
1838 * At IO completion time the cums attached on the ordered extent record
1839 * are inserted into the btree
1840 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001841static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001842 int mirror_num, unsigned long bio_flags,
1843 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001844{
1845 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001846 int ret;
1847
1848 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1849 if (ret)
1850 bio_endio(bio, ret);
1851 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001852}
1853
Chris Masond352ac62008-09-29 15:18:18 -04001854/*
Chris Masoncad321a2008-12-17 14:51:42 -05001855 * extent_io.c submission hook. This does the right thing for csum calculation
1856 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001857 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001858static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001859 int mirror_num, unsigned long bio_flags,
1860 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001861{
1862 struct btrfs_root *root = BTRFS_I(inode)->root;
1863 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001864 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001865 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001866 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001867
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001868 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001869
Liu Bo83eea1f2012-07-10 05:28:39 -06001870 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001871 metadata = 2;
1872
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001873 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001874 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1875 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001876 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001877
Chris Masond20f7042008-12-08 16:58:54 -05001878 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001879 ret = btrfs_submit_compressed_read(inode, bio,
1880 mirror_num,
1881 bio_flags);
1882 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001883 } else if (!skip_sum) {
1884 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1885 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001886 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001887 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001888 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001889 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001890 /* csum items have already been cloned */
1891 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1892 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001893 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001894 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001895 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001896 bio_flags, bio_offset,
1897 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001898 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001899 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001900 } else if (!skip_sum) {
1901 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1902 if (ret)
1903 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001904 }
1905
Chris Mason0b86a832008-03-24 15:01:56 -04001906mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001907 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1908
1909out:
1910 if (ret < 0)
1911 bio_endio(bio, ret);
1912 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001913}
Chris Mason6885f302008-02-20 16:11:05 -05001914
Chris Masond352ac62008-09-29 15:18:18 -04001915/*
1916 * given a list of ordered sums record them in the inode. This happens
1917 * at IO completion time based on sums calculated at bio submission time.
1918 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001919static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001920 struct inode *inode, u64 file_offset,
1921 struct list_head *list)
1922{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001923 struct btrfs_ordered_sum *sum;
1924
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001925 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001926 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001927 btrfs_csum_file_blocks(trans,
1928 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001929 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001930 }
1931 return 0;
1932}
1933
Josef Bacik2ac55d42010-02-03 19:33:23 +00001934int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1935 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001936{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001937 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001938 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001939 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001940}
1941
Chris Masond352ac62008-09-29 15:18:18 -04001942/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001943struct btrfs_writepage_fixup {
1944 struct page *page;
1945 struct btrfs_work work;
1946};
1947
Christoph Hellwigb2950862008-12-02 09:54:17 -05001948static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001949{
1950 struct btrfs_writepage_fixup *fixup;
1951 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001952 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001953 struct page *page;
1954 struct inode *inode;
1955 u64 page_start;
1956 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001957 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001958
1959 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1960 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001961again:
Chris Mason247e7432008-07-17 12:53:51 -04001962 lock_page(page);
1963 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1964 ClearPageChecked(page);
1965 goto out_page;
1966 }
1967
1968 inode = page->mapping->host;
1969 page_start = page_offset(page);
1970 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1971
Josef Bacik2ac55d42010-02-03 19:33:23 +00001972 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001973 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001974
1975 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001976 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001977 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001978
1979 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1980 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001981 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1982 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001983 unlock_page(page);
1984 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001985 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001986 goto again;
1987 }
Chris Mason247e7432008-07-17 12:53:51 -04001988
Jeff Mahoney87826df2012-02-15 16:23:57 +01001989 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1990 if (ret) {
1991 mapping_set_error(page->mapping, ret);
1992 end_extent_writepage(page, ret, page_start, page_end);
1993 ClearPageChecked(page);
1994 goto out;
1995 }
1996
Josef Bacik2ac55d42010-02-03 19:33:23 +00001997 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001998 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001999 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002000out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002001 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2002 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002003out_page:
2004 unlock_page(page);
2005 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002006 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002007}
2008
2009/*
2010 * There are a few paths in the higher layers of the kernel that directly
2011 * set the page dirty bit without asking the filesystem if it is a
2012 * good idea. This causes problems because we want to make sure COW
2013 * properly happens and the data=ordered rules are followed.
2014 *
Chris Masonc8b97812008-10-29 14:49:59 -04002015 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002016 * hasn't been properly setup for IO. We kick off an async process
2017 * to fix it up. The async helper will wait for ordered extents, set
2018 * the delalloc bit and make it safe to write the page.
2019 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002020static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002021{
2022 struct inode *inode = page->mapping->host;
2023 struct btrfs_writepage_fixup *fixup;
2024 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002025
Chris Mason8b62b722009-09-02 16:53:46 -04002026 /* this page is properly in the ordered list */
2027 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002028 return 0;
2029
2030 if (PageChecked(page))
2031 return -EAGAIN;
2032
2033 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2034 if (!fixup)
2035 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002036
Chris Mason247e7432008-07-17 12:53:51 -04002037 SetPageChecked(page);
2038 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002039 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2040 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002041 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002042 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002043 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002044}
2045
Yan Zhengd899e052008-10-30 14:25:28 -04002046static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2047 struct inode *inode, u64 file_pos,
2048 u64 disk_bytenr, u64 disk_num_bytes,
2049 u64 num_bytes, u64 ram_bytes,
2050 u8 compression, u8 encryption,
2051 u16 other_encoding, int extent_type)
2052{
2053 struct btrfs_root *root = BTRFS_I(inode)->root;
2054 struct btrfs_file_extent_item *fi;
2055 struct btrfs_path *path;
2056 struct extent_buffer *leaf;
2057 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002058 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002059 int ret;
2060
2061 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002062 if (!path)
2063 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002064
Chris Masona1ed8352009-09-11 12:27:37 -04002065 /*
2066 * we may be replacing one extent in the tree with another.
2067 * The new extent is pinned in the extent map, and we don't want
2068 * to drop it from the cache until it is completely in the btree.
2069 *
2070 * So, tell btrfs_drop_extents to leave this extent in the cache.
2071 * the caller is expected to unpin it and allow it to be merged
2072 * with the others.
2073 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002074 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2075 file_pos + num_bytes, NULL, 0,
2076 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002077 if (ret)
2078 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002079
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002080 if (!extent_inserted) {
2081 ins.objectid = btrfs_ino(inode);
2082 ins.offset = file_pos;
2083 ins.type = BTRFS_EXTENT_DATA_KEY;
2084
2085 path->leave_spinning = 1;
2086 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2087 sizeof(*fi));
2088 if (ret)
2089 goto out;
2090 }
Yan Zhengd899e052008-10-30 14:25:28 -04002091 leaf = path->nodes[0];
2092 fi = btrfs_item_ptr(leaf, path->slots[0],
2093 struct btrfs_file_extent_item);
2094 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2095 btrfs_set_file_extent_type(leaf, fi, extent_type);
2096 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2097 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2098 btrfs_set_file_extent_offset(leaf, fi, 0);
2099 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2100 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2101 btrfs_set_file_extent_compression(leaf, fi, compression);
2102 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2103 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002104
Yan Zhengd899e052008-10-30 14:25:28 -04002105 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002106 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002107
2108 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002109
2110 ins.objectid = disk_bytenr;
2111 ins.offset = disk_num_bytes;
2112 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002113 ret = btrfs_alloc_reserved_file_extent(trans, root,
2114 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002115 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002116out:
Yan Zhengd899e052008-10-30 14:25:28 -04002117 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002118
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002119 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002120}
2121
Liu Bo38c227d2013-01-29 03:18:40 +00002122/* snapshot-aware defrag */
2123struct sa_defrag_extent_backref {
2124 struct rb_node node;
2125 struct old_sa_defrag_extent *old;
2126 u64 root_id;
2127 u64 inum;
2128 u64 file_pos;
2129 u64 extent_offset;
2130 u64 num_bytes;
2131 u64 generation;
2132};
2133
2134struct old_sa_defrag_extent {
2135 struct list_head list;
2136 struct new_sa_defrag_extent *new;
2137
2138 u64 extent_offset;
2139 u64 bytenr;
2140 u64 offset;
2141 u64 len;
2142 int count;
2143};
2144
2145struct new_sa_defrag_extent {
2146 struct rb_root root;
2147 struct list_head head;
2148 struct btrfs_path *path;
2149 struct inode *inode;
2150 u64 file_pos;
2151 u64 len;
2152 u64 bytenr;
2153 u64 disk_len;
2154 u8 compress_type;
2155};
2156
2157static int backref_comp(struct sa_defrag_extent_backref *b1,
2158 struct sa_defrag_extent_backref *b2)
2159{
2160 if (b1->root_id < b2->root_id)
2161 return -1;
2162 else if (b1->root_id > b2->root_id)
2163 return 1;
2164
2165 if (b1->inum < b2->inum)
2166 return -1;
2167 else if (b1->inum > b2->inum)
2168 return 1;
2169
2170 if (b1->file_pos < b2->file_pos)
2171 return -1;
2172 else if (b1->file_pos > b2->file_pos)
2173 return 1;
2174
2175 /*
2176 * [------------------------------] ===> (a range of space)
2177 * |<--->| |<---->| =============> (fs/file tree A)
2178 * |<---------------------------->| ===> (fs/file tree B)
2179 *
2180 * A range of space can refer to two file extents in one tree while
2181 * refer to only one file extent in another tree.
2182 *
2183 * So we may process a disk offset more than one time(two extents in A)
2184 * and locate at the same extent(one extent in B), then insert two same
2185 * backrefs(both refer to the extent in B).
2186 */
2187 return 0;
2188}
2189
2190static void backref_insert(struct rb_root *root,
2191 struct sa_defrag_extent_backref *backref)
2192{
2193 struct rb_node **p = &root->rb_node;
2194 struct rb_node *parent = NULL;
2195 struct sa_defrag_extent_backref *entry;
2196 int ret;
2197
2198 while (*p) {
2199 parent = *p;
2200 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2201
2202 ret = backref_comp(backref, entry);
2203 if (ret < 0)
2204 p = &(*p)->rb_left;
2205 else
2206 p = &(*p)->rb_right;
2207 }
2208
2209 rb_link_node(&backref->node, parent, p);
2210 rb_insert_color(&backref->node, root);
2211}
2212
2213/*
2214 * Note the backref might has changed, and in this case we just return 0.
2215 */
2216static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2217 void *ctx)
2218{
2219 struct btrfs_file_extent_item *extent;
2220 struct btrfs_fs_info *fs_info;
2221 struct old_sa_defrag_extent *old = ctx;
2222 struct new_sa_defrag_extent *new = old->new;
2223 struct btrfs_path *path = new->path;
2224 struct btrfs_key key;
2225 struct btrfs_root *root;
2226 struct sa_defrag_extent_backref *backref;
2227 struct extent_buffer *leaf;
2228 struct inode *inode = new->inode;
2229 int slot;
2230 int ret;
2231 u64 extent_offset;
2232 u64 num_bytes;
2233
2234 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2235 inum == btrfs_ino(inode))
2236 return 0;
2237
2238 key.objectid = root_id;
2239 key.type = BTRFS_ROOT_ITEM_KEY;
2240 key.offset = (u64)-1;
2241
2242 fs_info = BTRFS_I(inode)->root->fs_info;
2243 root = btrfs_read_fs_root_no_name(fs_info, &key);
2244 if (IS_ERR(root)) {
2245 if (PTR_ERR(root) == -ENOENT)
2246 return 0;
2247 WARN_ON(1);
2248 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2249 inum, offset, root_id);
2250 return PTR_ERR(root);
2251 }
2252
2253 key.objectid = inum;
2254 key.type = BTRFS_EXTENT_DATA_KEY;
2255 if (offset > (u64)-1 << 32)
2256 key.offset = 0;
2257 else
2258 key.offset = offset;
2259
2260 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302261 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002262 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002263 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002264
2265 while (1) {
2266 cond_resched();
2267
2268 leaf = path->nodes[0];
2269 slot = path->slots[0];
2270
2271 if (slot >= btrfs_header_nritems(leaf)) {
2272 ret = btrfs_next_leaf(root, path);
2273 if (ret < 0) {
2274 goto out;
2275 } else if (ret > 0) {
2276 ret = 0;
2277 goto out;
2278 }
2279 continue;
2280 }
2281
2282 path->slots[0]++;
2283
2284 btrfs_item_key_to_cpu(leaf, &key, slot);
2285
2286 if (key.objectid > inum)
2287 goto out;
2288
2289 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2290 continue;
2291
2292 extent = btrfs_item_ptr(leaf, slot,
2293 struct btrfs_file_extent_item);
2294
2295 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2296 continue;
2297
Liu Boe68afa42013-07-01 22:13:26 +08002298 /*
2299 * 'offset' refers to the exact key.offset,
2300 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2301 * (key.offset - extent_offset).
2302 */
2303 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002304 continue;
2305
Liu Boe68afa42013-07-01 22:13:26 +08002306 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002307 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002308
Liu Bo38c227d2013-01-29 03:18:40 +00002309 if (extent_offset >= old->extent_offset + old->offset +
2310 old->len || extent_offset + num_bytes <=
2311 old->extent_offset + old->offset)
2312 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002313 break;
2314 }
2315
2316 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2317 if (!backref) {
2318 ret = -ENOENT;
2319 goto out;
2320 }
2321
2322 backref->root_id = root_id;
2323 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002324 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002325 backref->num_bytes = num_bytes;
2326 backref->extent_offset = extent_offset;
2327 backref->generation = btrfs_file_extent_generation(leaf, extent);
2328 backref->old = old;
2329 backref_insert(&new->root, backref);
2330 old->count++;
2331out:
2332 btrfs_release_path(path);
2333 WARN_ON(ret);
2334 return ret;
2335}
2336
2337static noinline bool record_extent_backrefs(struct btrfs_path *path,
2338 struct new_sa_defrag_extent *new)
2339{
2340 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2341 struct old_sa_defrag_extent *old, *tmp;
2342 int ret;
2343
2344 new->path = path;
2345
2346 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002347 ret = iterate_inodes_from_logical(old->bytenr +
2348 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002349 path, record_one_backref,
2350 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002351 if (ret < 0 && ret != -ENOENT)
2352 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002353
2354 /* no backref to be processed for this extent */
2355 if (!old->count) {
2356 list_del(&old->list);
2357 kfree(old);
2358 }
2359 }
2360
2361 if (list_empty(&new->head))
2362 return false;
2363
2364 return true;
2365}
2366
2367static int relink_is_mergable(struct extent_buffer *leaf,
2368 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002369 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002370{
Liu Bo116e0022013-08-02 16:30:40 +08002371 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002372 return 0;
2373
2374 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2375 return 0;
2376
Liu Bo116e0022013-08-02 16:30:40 +08002377 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2378 return 0;
2379
2380 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002381 btrfs_file_extent_other_encoding(leaf, fi))
2382 return 0;
2383
2384 return 1;
2385}
2386
2387/*
2388 * Note the backref might has changed, and in this case we just return 0.
2389 */
2390static noinline int relink_extent_backref(struct btrfs_path *path,
2391 struct sa_defrag_extent_backref *prev,
2392 struct sa_defrag_extent_backref *backref)
2393{
2394 struct btrfs_file_extent_item *extent;
2395 struct btrfs_file_extent_item *item;
2396 struct btrfs_ordered_extent *ordered;
2397 struct btrfs_trans_handle *trans;
2398 struct btrfs_fs_info *fs_info;
2399 struct btrfs_root *root;
2400 struct btrfs_key key;
2401 struct extent_buffer *leaf;
2402 struct old_sa_defrag_extent *old = backref->old;
2403 struct new_sa_defrag_extent *new = old->new;
2404 struct inode *src_inode = new->inode;
2405 struct inode *inode;
2406 struct extent_state *cached = NULL;
2407 int ret = 0;
2408 u64 start;
2409 u64 len;
2410 u64 lock_start;
2411 u64 lock_end;
2412 bool merge = false;
2413 int index;
2414
2415 if (prev && prev->root_id == backref->root_id &&
2416 prev->inum == backref->inum &&
2417 prev->file_pos + prev->num_bytes == backref->file_pos)
2418 merge = true;
2419
2420 /* step 1: get root */
2421 key.objectid = backref->root_id;
2422 key.type = BTRFS_ROOT_ITEM_KEY;
2423 key.offset = (u64)-1;
2424
2425 fs_info = BTRFS_I(src_inode)->root->fs_info;
2426 index = srcu_read_lock(&fs_info->subvol_srcu);
2427
2428 root = btrfs_read_fs_root_no_name(fs_info, &key);
2429 if (IS_ERR(root)) {
2430 srcu_read_unlock(&fs_info->subvol_srcu, index);
2431 if (PTR_ERR(root) == -ENOENT)
2432 return 0;
2433 return PTR_ERR(root);
2434 }
Liu Bo38c227d2013-01-29 03:18:40 +00002435
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002436 if (btrfs_root_readonly(root)) {
2437 srcu_read_unlock(&fs_info->subvol_srcu, index);
2438 return 0;
2439 }
2440
Liu Bo38c227d2013-01-29 03:18:40 +00002441 /* step 2: get inode */
2442 key.objectid = backref->inum;
2443 key.type = BTRFS_INODE_ITEM_KEY;
2444 key.offset = 0;
2445
2446 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2447 if (IS_ERR(inode)) {
2448 srcu_read_unlock(&fs_info->subvol_srcu, index);
2449 return 0;
2450 }
2451
2452 srcu_read_unlock(&fs_info->subvol_srcu, index);
2453
2454 /* step 3: relink backref */
2455 lock_start = backref->file_pos;
2456 lock_end = backref->file_pos + backref->num_bytes - 1;
2457 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2458 0, &cached);
2459
2460 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2461 if (ordered) {
2462 btrfs_put_ordered_extent(ordered);
2463 goto out_unlock;
2464 }
2465
2466 trans = btrfs_join_transaction(root);
2467 if (IS_ERR(trans)) {
2468 ret = PTR_ERR(trans);
2469 goto out_unlock;
2470 }
2471
2472 key.objectid = backref->inum;
2473 key.type = BTRFS_EXTENT_DATA_KEY;
2474 key.offset = backref->file_pos;
2475
2476 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2477 if (ret < 0) {
2478 goto out_free_path;
2479 } else if (ret > 0) {
2480 ret = 0;
2481 goto out_free_path;
2482 }
2483
2484 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2485 struct btrfs_file_extent_item);
2486
2487 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2488 backref->generation)
2489 goto out_free_path;
2490
2491 btrfs_release_path(path);
2492
2493 start = backref->file_pos;
2494 if (backref->extent_offset < old->extent_offset + old->offset)
2495 start += old->extent_offset + old->offset -
2496 backref->extent_offset;
2497
2498 len = min(backref->extent_offset + backref->num_bytes,
2499 old->extent_offset + old->offset + old->len);
2500 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2501
2502 ret = btrfs_drop_extents(trans, root, inode, start,
2503 start + len, 1);
2504 if (ret)
2505 goto out_free_path;
2506again:
2507 key.objectid = btrfs_ino(inode);
2508 key.type = BTRFS_EXTENT_DATA_KEY;
2509 key.offset = start;
2510
Liu Boa09a0a72013-03-11 09:20:58 +00002511 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002512 if (merge) {
2513 struct btrfs_file_extent_item *fi;
2514 u64 extent_len;
2515 struct btrfs_key found_key;
2516
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002517 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002518 if (ret < 0)
2519 goto out_free_path;
2520
2521 path->slots[0]--;
2522 leaf = path->nodes[0];
2523 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2524
2525 fi = btrfs_item_ptr(leaf, path->slots[0],
2526 struct btrfs_file_extent_item);
2527 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2528
Liu Bo116e0022013-08-02 16:30:40 +08002529 if (extent_len + found_key.offset == start &&
2530 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002531 btrfs_set_file_extent_num_bytes(leaf, fi,
2532 extent_len + len);
2533 btrfs_mark_buffer_dirty(leaf);
2534 inode_add_bytes(inode, len);
2535
2536 ret = 1;
2537 goto out_free_path;
2538 } else {
2539 merge = false;
2540 btrfs_release_path(path);
2541 goto again;
2542 }
2543 }
2544
2545 ret = btrfs_insert_empty_item(trans, root, path, &key,
2546 sizeof(*extent));
2547 if (ret) {
2548 btrfs_abort_transaction(trans, root, ret);
2549 goto out_free_path;
2550 }
2551
2552 leaf = path->nodes[0];
2553 item = btrfs_item_ptr(leaf, path->slots[0],
2554 struct btrfs_file_extent_item);
2555 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2556 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2557 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2558 btrfs_set_file_extent_num_bytes(leaf, item, len);
2559 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2560 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2561 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2562 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2563 btrfs_set_file_extent_encryption(leaf, item, 0);
2564 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2565
2566 btrfs_mark_buffer_dirty(leaf);
2567 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002568 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002569
2570 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2571 new->disk_len, 0,
2572 backref->root_id, backref->inum,
2573 new->file_pos, 0); /* start - extent_offset */
2574 if (ret) {
2575 btrfs_abort_transaction(trans, root, ret);
2576 goto out_free_path;
2577 }
2578
2579 ret = 1;
2580out_free_path:
2581 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002582 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002583 btrfs_end_transaction(trans, root);
2584out_unlock:
2585 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2586 &cached, GFP_NOFS);
2587 iput(inode);
2588 return ret;
2589}
2590
Liu Bo6f519562013-10-29 10:45:05 +08002591static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2592{
2593 struct old_sa_defrag_extent *old, *tmp;
2594
2595 if (!new)
2596 return;
2597
2598 list_for_each_entry_safe(old, tmp, &new->head, list) {
2599 list_del(&old->list);
2600 kfree(old);
2601 }
2602 kfree(new);
2603}
2604
Liu Bo38c227d2013-01-29 03:18:40 +00002605static void relink_file_extents(struct new_sa_defrag_extent *new)
2606{
2607 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002608 struct sa_defrag_extent_backref *backref;
2609 struct sa_defrag_extent_backref *prev = NULL;
2610 struct inode *inode;
2611 struct btrfs_root *root;
2612 struct rb_node *node;
2613 int ret;
2614
2615 inode = new->inode;
2616 root = BTRFS_I(inode)->root;
2617
2618 path = btrfs_alloc_path();
2619 if (!path)
2620 return;
2621
2622 if (!record_extent_backrefs(path, new)) {
2623 btrfs_free_path(path);
2624 goto out;
2625 }
2626 btrfs_release_path(path);
2627
2628 while (1) {
2629 node = rb_first(&new->root);
2630 if (!node)
2631 break;
2632 rb_erase(node, &new->root);
2633
2634 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2635
2636 ret = relink_extent_backref(path, prev, backref);
2637 WARN_ON(ret < 0);
2638
2639 kfree(prev);
2640
2641 if (ret == 1)
2642 prev = backref;
2643 else
2644 prev = NULL;
2645 cond_resched();
2646 }
2647 kfree(prev);
2648
2649 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002650out:
Liu Bo6f519562013-10-29 10:45:05 +08002651 free_sa_defrag_extent(new);
2652
Liu Bo38c227d2013-01-29 03:18:40 +00002653 atomic_dec(&root->fs_info->defrag_running);
2654 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002655}
2656
2657static struct new_sa_defrag_extent *
2658record_old_file_extents(struct inode *inode,
2659 struct btrfs_ordered_extent *ordered)
2660{
2661 struct btrfs_root *root = BTRFS_I(inode)->root;
2662 struct btrfs_path *path;
2663 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002664 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002665 struct new_sa_defrag_extent *new;
2666 int ret;
2667
2668 new = kmalloc(sizeof(*new), GFP_NOFS);
2669 if (!new)
2670 return NULL;
2671
2672 new->inode = inode;
2673 new->file_pos = ordered->file_offset;
2674 new->len = ordered->len;
2675 new->bytenr = ordered->start;
2676 new->disk_len = ordered->disk_len;
2677 new->compress_type = ordered->compress_type;
2678 new->root = RB_ROOT;
2679 INIT_LIST_HEAD(&new->head);
2680
2681 path = btrfs_alloc_path();
2682 if (!path)
2683 goto out_kfree;
2684
2685 key.objectid = btrfs_ino(inode);
2686 key.type = BTRFS_EXTENT_DATA_KEY;
2687 key.offset = new->file_pos;
2688
2689 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2690 if (ret < 0)
2691 goto out_free_path;
2692 if (ret > 0 && path->slots[0] > 0)
2693 path->slots[0]--;
2694
2695 /* find out all the old extents for the file range */
2696 while (1) {
2697 struct btrfs_file_extent_item *extent;
2698 struct extent_buffer *l;
2699 int slot;
2700 u64 num_bytes;
2701 u64 offset;
2702 u64 end;
2703 u64 disk_bytenr;
2704 u64 extent_offset;
2705
2706 l = path->nodes[0];
2707 slot = path->slots[0];
2708
2709 if (slot >= btrfs_header_nritems(l)) {
2710 ret = btrfs_next_leaf(root, path);
2711 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002712 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002713 else if (ret > 0)
2714 break;
2715 continue;
2716 }
2717
2718 btrfs_item_key_to_cpu(l, &key, slot);
2719
2720 if (key.objectid != btrfs_ino(inode))
2721 break;
2722 if (key.type != BTRFS_EXTENT_DATA_KEY)
2723 break;
2724 if (key.offset >= new->file_pos + new->len)
2725 break;
2726
2727 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2728
2729 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2730 if (key.offset + num_bytes < new->file_pos)
2731 goto next;
2732
2733 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2734 if (!disk_bytenr)
2735 goto next;
2736
2737 extent_offset = btrfs_file_extent_offset(l, extent);
2738
2739 old = kmalloc(sizeof(*old), GFP_NOFS);
2740 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002741 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002742
2743 offset = max(new->file_pos, key.offset);
2744 end = min(new->file_pos + new->len, key.offset + num_bytes);
2745
2746 old->bytenr = disk_bytenr;
2747 old->extent_offset = extent_offset;
2748 old->offset = offset - key.offset;
2749 old->len = end - offset;
2750 old->new = new;
2751 old->count = 0;
2752 list_add_tail(&old->list, &new->head);
2753next:
2754 path->slots[0]++;
2755 cond_resched();
2756 }
2757
2758 btrfs_free_path(path);
2759 atomic_inc(&root->fs_info->defrag_running);
2760
2761 return new;
2762
Liu Bo38c227d2013-01-29 03:18:40 +00002763out_free_path:
2764 btrfs_free_path(path);
2765out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002766 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002767 return NULL;
2768}
2769
Miao Xiee570fd22014-06-19 10:42:50 +08002770static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2771 u64 start, u64 len)
2772{
2773 struct btrfs_block_group_cache *cache;
2774
2775 cache = btrfs_lookup_block_group(root->fs_info, start);
2776 ASSERT(cache);
2777
2778 spin_lock(&cache->lock);
2779 cache->delalloc_bytes -= len;
2780 spin_unlock(&cache->lock);
2781
2782 btrfs_put_block_group(cache);
2783}
2784
Chris Masond352ac62008-09-29 15:18:18 -04002785/* as ordered data IO finishes, this gets called so we can finish
2786 * an ordered extent if the range of bytes in the file it covers are
2787 * fully written.
2788 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002789static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002790{
Josef Bacik5fd02042012-05-02 14:00:54 -04002791 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002792 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002793 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002794 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002795 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002796 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002797 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002798 int ret = 0;
2799 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002800 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002801 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002802
Liu Bo83eea1f2012-07-10 05:28:39 -06002803 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002804
Josef Bacik5fd02042012-05-02 14:00:54 -04002805 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2806 ret = -EIO;
2807 goto out;
2808 }
2809
Miao Xief6124962014-09-12 18:44:04 +08002810 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2811 ordered_extent->file_offset +
2812 ordered_extent->len - 1);
2813
Josef Bacik77cef2e2013-08-29 13:57:21 -04002814 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2815 truncated = true;
2816 logical_len = ordered_extent->truncated_len;
2817 /* Truncated the entire extent, don't bother adding */
2818 if (!logical_len)
2819 goto out;
2820 }
2821
Yan, Zhengc2167752009-11-12 09:34:21 +00002822 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002823 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002824 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2825 if (nolock)
2826 trans = btrfs_join_transaction_nolock(root);
2827 else
2828 trans = btrfs_join_transaction(root);
2829 if (IS_ERR(trans)) {
2830 ret = PTR_ERR(trans);
2831 trans = NULL;
2832 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002833 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002834 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2835 ret = btrfs_update_inode_fallback(trans, root, inode);
2836 if (ret) /* -ENOMEM or corruption */
2837 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002838 goto out;
2839 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002840
Josef Bacik2ac55d42010-02-03 19:33:23 +00002841 lock_extent_bits(io_tree, ordered_extent->file_offset,
2842 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002843 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002844
Liu Bo38c227d2013-01-29 03:18:40 +00002845 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2846 ordered_extent->file_offset + ordered_extent->len - 1,
2847 EXTENT_DEFRAG, 1, cached_state);
2848 if (ret) {
2849 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002850 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002851 /* the inode is shared */
2852 new = record_old_file_extents(inode, ordered_extent);
2853
2854 clear_extent_bit(io_tree, ordered_extent->file_offset,
2855 ordered_extent->file_offset + ordered_extent->len - 1,
2856 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2857 }
2858
Josef Bacik0cb59c92010-07-02 12:14:14 -04002859 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002860 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002861 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002862 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002863 if (IS_ERR(trans)) {
2864 ret = PTR_ERR(trans);
2865 trans = NULL;
2866 goto out_unlock;
2867 }
Chris Masona79b7d42014-05-22 16:18:52 -07002868
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002869 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002870
Chris Masonc8b97812008-10-29 14:49:59 -04002871 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002872 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002873 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002874 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002875 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002876 ordered_extent->file_offset,
2877 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002878 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002879 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002880 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002881 ret = insert_reserved_file_extent(trans, inode,
2882 ordered_extent->file_offset,
2883 ordered_extent->start,
2884 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002885 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002886 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002887 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002888 if (!ret)
2889 btrfs_release_delalloc_bytes(root,
2890 ordered_extent->start,
2891 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002892 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002893 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2894 ordered_extent->file_offset, ordered_extent->len,
2895 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002896 if (ret < 0) {
2897 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002898 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002899 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002900
Chris Masone6dcd2d2008-07-17 12:53:50 -04002901 add_pending_csums(trans, inode, ordered_extent->file_offset,
2902 &ordered_extent->list);
2903
Josef Bacik6c760c02012-11-09 10:53:21 -05002904 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2905 ret = btrfs_update_inode_fallback(trans, root, inode);
2906 if (ret) { /* -ENOMEM or corruption */
2907 btrfs_abort_transaction(trans, root, ret);
2908 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002909 }
2910 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002911out_unlock:
2912 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2913 ordered_extent->file_offset +
2914 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002915out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002916 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002917 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002918 if (trans)
2919 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002920
Josef Bacik77cef2e2013-08-29 13:57:21 -04002921 if (ret || truncated) {
2922 u64 start, end;
2923
2924 if (truncated)
2925 start = ordered_extent->file_offset + logical_len;
2926 else
2927 start = ordered_extent->file_offset;
2928 end = ordered_extent->file_offset + ordered_extent->len - 1;
2929 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2930
2931 /* Drop the cache for the part of the extent we didn't write. */
2932 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002933
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002934 /*
2935 * If the ordered extent had an IOERR or something else went
2936 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002937 * back to the allocator. We only free the extent in the
2938 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002939 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002940 if ((ret || !logical_len) &&
2941 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002942 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2943 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002944 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002945 }
2946
2947
Josef Bacik5fd02042012-05-02 14:00:54 -04002948 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002949 * This needs to be done to make sure anybody waiting knows we are done
2950 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002951 */
2952 btrfs_remove_ordered_extent(inode, ordered_extent);
2953
Liu Bo38c227d2013-01-29 03:18:40 +00002954 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002955 if (new) {
2956 if (ret) {
2957 free_sa_defrag_extent(new);
2958 atomic_dec(&root->fs_info->defrag_running);
2959 } else {
2960 relink_file_extents(new);
2961 }
2962 }
Liu Bo38c227d2013-01-29 03:18:40 +00002963
Chris Masone6dcd2d2008-07-17 12:53:50 -04002964 /* once for us */
2965 btrfs_put_ordered_extent(ordered_extent);
2966 /* once for the tree */
2967 btrfs_put_ordered_extent(ordered_extent);
2968
Josef Bacik5fd02042012-05-02 14:00:54 -04002969 return ret;
2970}
2971
2972static void finish_ordered_fn(struct btrfs_work *work)
2973{
2974 struct btrfs_ordered_extent *ordered_extent;
2975 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2976 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002977}
2978
Christoph Hellwigb2950862008-12-02 09:54:17 -05002979static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002980 struct extent_state *state, int uptodate)
2981{
Josef Bacik5fd02042012-05-02 14:00:54 -04002982 struct inode *inode = page->mapping->host;
2983 struct btrfs_root *root = BTRFS_I(inode)->root;
2984 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002985 struct btrfs_workqueue *wq;
2986 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002987
liubo1abe9b82011-03-24 11:18:59 +00002988 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2989
Chris Mason8b62b722009-09-02 16:53:46 -04002990 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002991 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2992 end - start + 1, uptodate))
2993 return 0;
2994
Liu Bo9e0af232014-08-15 23:36:53 +08002995 if (btrfs_is_free_space_inode(inode)) {
2996 wq = root->fs_info->endio_freespace_worker;
2997 func = btrfs_freespace_write_helper;
2998 } else {
2999 wq = root->fs_info->endio_write_workers;
3000 func = btrfs_endio_write_helper;
3001 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003002
Liu Bo9e0af232014-08-15 23:36:53 +08003003 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3004 NULL);
3005 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003006
3007 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003008}
3009
Miao Xiedc380ae2014-09-12 18:43:55 +08003010static int __readpage_endio_check(struct inode *inode,
3011 struct btrfs_io_bio *io_bio,
3012 int icsum, struct page *page,
3013 int pgoff, u64 start, size_t len)
3014{
3015 char *kaddr;
3016 u32 csum_expected;
3017 u32 csum = ~(u32)0;
3018 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
3019 DEFAULT_RATELIMIT_BURST);
3020
3021 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3022
3023 kaddr = kmap_atomic(page);
3024 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3025 btrfs_csum_final(csum, (char *)&csum);
3026 if (csum != csum_expected)
3027 goto zeroit;
3028
3029 kunmap_atomic(kaddr);
3030 return 0;
3031zeroit:
3032 if (__ratelimit(&_rs))
David Sterbaf0954c62014-12-19 18:38:44 +01003033 btrfs_warn(BTRFS_I(inode)->root->fs_info,
Miao Xiedc380ae2014-09-12 18:43:55 +08003034 "csum failed ino %llu off %llu csum %u expected csum %u",
3035 btrfs_ino(inode), start, csum, csum_expected);
3036 memset(kaddr + pgoff, 1, len);
3037 flush_dcache_page(page);
3038 kunmap_atomic(kaddr);
3039 if (csum_expected == 0)
3040 return 0;
3041 return -EIO;
3042}
3043
Chris Masond352ac62008-09-29 15:18:18 -04003044/*
Chris Masond352ac62008-09-29 15:18:18 -04003045 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003046 * if there's a match, we allow the bio to finish. If not, the code in
3047 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003048 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003049static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3050 u64 phy_offset, struct page *page,
3051 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003052{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003053 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003054 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003055 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003056 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003057
Chris Masond20f7042008-12-08 16:58:54 -05003058 if (PageChecked(page)) {
3059 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003060 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003061 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003062
3063 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003064 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003065
Yan Zheng17d217f2008-12-12 10:03:38 -05003066 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003067 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05003068 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
3069 GFP_NOFS);
3070 return 0;
3071 }
3072
Miao Xiefacc8a222013-07-25 19:22:34 +08003073 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003074 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3075 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003076}
Chris Masonb888db22007-08-27 16:49:44 -04003077
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003078struct delayed_iput {
3079 struct list_head list;
3080 struct inode *inode;
3081};
3082
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003083/* JDM: If this is fs-wide, why can't we add a pointer to
3084 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003085void btrfs_add_delayed_iput(struct inode *inode)
3086{
3087 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3088 struct delayed_iput *delayed;
3089
3090 if (atomic_add_unless(&inode->i_count, -1, 1))
3091 return;
3092
3093 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3094 delayed->inode = inode;
3095
3096 spin_lock(&fs_info->delayed_iput_lock);
3097 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3098 spin_unlock(&fs_info->delayed_iput_lock);
3099}
3100
3101void btrfs_run_delayed_iputs(struct btrfs_root *root)
3102{
3103 LIST_HEAD(list);
3104 struct btrfs_fs_info *fs_info = root->fs_info;
3105 struct delayed_iput *delayed;
3106 int empty;
3107
3108 spin_lock(&fs_info->delayed_iput_lock);
3109 empty = list_empty(&fs_info->delayed_iputs);
3110 spin_unlock(&fs_info->delayed_iput_lock);
3111 if (empty)
3112 return;
3113
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003114 spin_lock(&fs_info->delayed_iput_lock);
3115 list_splice_init(&fs_info->delayed_iputs, &list);
3116 spin_unlock(&fs_info->delayed_iput_lock);
3117
3118 while (!list_empty(&list)) {
3119 delayed = list_entry(list.next, struct delayed_iput, list);
3120 list_del(&delayed->list);
3121 iput(delayed->inode);
3122 kfree(delayed);
3123 }
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003124}
3125
Yan, Zhengd68fc572010-05-16 10:49:58 -04003126/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003127 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003128 * files in the subvolume, it removes orphan item and frees block_rsv
3129 * structure.
3130 */
3131void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3132 struct btrfs_root *root)
3133{
Josef Bacik90290e12011-12-02 15:44:12 -05003134 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003135 int ret;
3136
Josef Bacik8a35d952012-05-23 14:26:42 -04003137 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003138 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3139 return;
3140
Josef Bacik90290e12011-12-02 15:44:12 -05003141 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003142 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003143 spin_unlock(&root->orphan_lock);
3144 return;
3145 }
3146
3147 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3148 spin_unlock(&root->orphan_lock);
3149 return;
3150 }
3151
3152 block_rsv = root->orphan_block_rsv;
3153 root->orphan_block_rsv = NULL;
3154 spin_unlock(&root->orphan_lock);
3155
Miao Xie27cdeb72014-04-02 19:51:05 +08003156 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003157 btrfs_root_refs(&root->root_item) > 0) {
3158 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3159 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003160 if (ret)
3161 btrfs_abort_transaction(trans, root, ret);
3162 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003163 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3164 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003165 }
3166
Josef Bacik90290e12011-12-02 15:44:12 -05003167 if (block_rsv) {
3168 WARN_ON(block_rsv->size > 0);
3169 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003170 }
3171}
3172
3173/*
Josef Bacik7b128762008-07-24 12:17:14 -04003174 * This creates an orphan entry for the given inode in case something goes
3175 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003176 *
3177 * NOTE: caller of this function should reserve 5 units of metadata for
3178 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003179 */
3180int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3181{
3182 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003183 struct btrfs_block_rsv *block_rsv = NULL;
3184 int reserve = 0;
3185 int insert = 0;
3186 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003187
Yan, Zhengd68fc572010-05-16 10:49:58 -04003188 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003189 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003190 if (!block_rsv)
3191 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003192 }
3193
Yan, Zhengd68fc572010-05-16 10:49:58 -04003194 spin_lock(&root->orphan_lock);
3195 if (!root->orphan_block_rsv) {
3196 root->orphan_block_rsv = block_rsv;
3197 } else if (block_rsv) {
3198 btrfs_free_block_rsv(root, block_rsv);
3199 block_rsv = NULL;
3200 }
Josef Bacik7b128762008-07-24 12:17:14 -04003201
Josef Bacik8a35d952012-05-23 14:26:42 -04003202 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3203 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003204#if 0
3205 /*
3206 * For proper ENOSPC handling, we should do orphan
3207 * cleanup when mounting. But this introduces backward
3208 * compatibility issue.
3209 */
3210 if (!xchg(&root->orphan_item_inserted, 1))
3211 insert = 2;
3212 else
3213 insert = 1;
3214#endif
3215 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003216 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003217 }
Josef Bacik7b128762008-07-24 12:17:14 -04003218
Josef Bacik72ac3c02012-05-23 14:13:11 -04003219 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3220 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003221 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003222 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003223
Yan, Zhengd68fc572010-05-16 10:49:58 -04003224 /* grab metadata reservation from transaction handle */
3225 if (reserve) {
3226 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003227 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003228 }
3229
3230 /* insert an orphan item to track this unlinked/truncated file */
3231 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003232 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003233 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003234 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003235 if (reserve) {
3236 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3237 &BTRFS_I(inode)->runtime_flags);
3238 btrfs_orphan_release_metadata(inode);
3239 }
3240 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003241 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3242 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003243 btrfs_abort_transaction(trans, root, ret);
3244 return ret;
3245 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003246 }
3247 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003248 }
3249
3250 /* insert an orphan item to track subvolume contains orphan files */
3251 if (insert >= 2) {
3252 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3253 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003254 if (ret && ret != -EEXIST) {
3255 btrfs_abort_transaction(trans, root, ret);
3256 return ret;
3257 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003258 }
3259 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003260}
3261
3262/*
3263 * We have done the truncate/delete so we can go ahead and remove the orphan
3264 * item for this particular inode.
3265 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003266static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3267 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003268{
3269 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003270 int delete_item = 0;
3271 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003272 int ret = 0;
3273
Yan, Zhengd68fc572010-05-16 10:49:58 -04003274 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003275 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3276 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003278
Josef Bacik72ac3c02012-05-23 14:13:11 -04003279 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3280 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003281 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003282 spin_unlock(&root->orphan_lock);
3283
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003284 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003285 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003286 if (trans)
3287 ret = btrfs_del_orphan_item(trans, root,
3288 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003289 }
Josef Bacik7b128762008-07-24 12:17:14 -04003290
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003291 if (release_rsv)
3292 btrfs_orphan_release_metadata(inode);
3293
Josef Bacik4ef31a42013-08-13 14:10:08 -04003294 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003295}
3296
3297/*
3298 * this cleans up any orphans that may be left on the list from the last use
3299 * of this root.
3300 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003301int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003302{
3303 struct btrfs_path *path;
3304 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003305 struct btrfs_key key, found_key;
3306 struct btrfs_trans_handle *trans;
3307 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003308 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003309 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3310
Yan, Zhengd68fc572010-05-16 10:49:58 -04003311 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003312 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003313
3314 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003315 if (!path) {
3316 ret = -ENOMEM;
3317 goto out;
3318 }
Josef Bacik7b128762008-07-24 12:17:14 -04003319 path->reada = -1;
3320
3321 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003322 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003323 key.offset = (u64)-1;
3324
Josef Bacik7b128762008-07-24 12:17:14 -04003325 while (1) {
3326 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003327 if (ret < 0)
3328 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003329
3330 /*
3331 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003332 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003333 * find the key and see if we have stuff that matches
3334 */
3335 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003336 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003337 if (path->slots[0] == 0)
3338 break;
3339 path->slots[0]--;
3340 }
3341
3342 /* pull out the item */
3343 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003344 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3345
3346 /* make sure the item matches what we want */
3347 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3348 break;
David Sterba962a2982014-06-04 18:41:45 +02003349 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003350 break;
3351
3352 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003353 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003354
3355 /*
3356 * this is where we are basically btrfs_lookup, without the
3357 * crossing root thing. we store the inode number in the
3358 * offset of the orphan item.
3359 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003360
3361 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003362 btrfs_err(root->fs_info,
3363 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003364 ret = -EINVAL;
3365 goto out;
3366 }
3367
3368 last_objectid = found_key.offset;
3369
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003370 found_key.objectid = found_key.offset;
3371 found_key.type = BTRFS_INODE_ITEM_KEY;
3372 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003373 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303374 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003375 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003376 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003377
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003378 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3379 struct btrfs_root *dead_root;
3380 struct btrfs_fs_info *fs_info = root->fs_info;
3381 int is_dead_root = 0;
3382
3383 /*
3384 * this is an orphan in the tree root. Currently these
3385 * could come from 2 sources:
3386 * a) a snapshot deletion in progress
3387 * b) a free space cache inode
3388 * We need to distinguish those two, as the snapshot
3389 * orphan must not get deleted.
3390 * find_dead_roots already ran before us, so if this
3391 * is a snapshot deletion, we should find the root
3392 * in the dead_roots list
3393 */
3394 spin_lock(&fs_info->trans_lock);
3395 list_for_each_entry(dead_root, &fs_info->dead_roots,
3396 root_list) {
3397 if (dead_root->root_key.objectid ==
3398 found_key.objectid) {
3399 is_dead_root = 1;
3400 break;
3401 }
3402 }
3403 spin_unlock(&fs_info->trans_lock);
3404 if (is_dead_root) {
3405 /* prevent this orphan from being found again */
3406 key.offset = found_key.objectid - 1;
3407 continue;
3408 }
3409 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003410 /*
3411 * Inode is already gone but the orphan item is still there,
3412 * kill the orphan item.
3413 */
3414 if (ret == -ESTALE) {
3415 trans = btrfs_start_transaction(root, 1);
3416 if (IS_ERR(trans)) {
3417 ret = PTR_ERR(trans);
3418 goto out;
3419 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003420 btrfs_debug(root->fs_info, "auto deleting %Lu",
3421 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003422 ret = btrfs_del_orphan_item(trans, root,
3423 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003424 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003425 if (ret)
3426 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003427 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003428 }
Josef Bacik7b128762008-07-24 12:17:14 -04003429
Josef Bacik7b128762008-07-24 12:17:14 -04003430 /*
3431 * add this inode to the orphan list so btrfs_orphan_del does
3432 * the proper thing when we hit it
3433 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003434 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3435 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003436 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003437
Josef Bacik7b128762008-07-24 12:17:14 -04003438 /* if we have links, this was a truncate, lets do that */
3439 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303440 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003441 iput(inode);
3442 continue;
3443 }
Josef Bacik7b128762008-07-24 12:17:14 -04003444 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003445
3446 /* 1 for the orphan item deletion. */
3447 trans = btrfs_start_transaction(root, 1);
3448 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003449 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003450 ret = PTR_ERR(trans);
3451 goto out;
3452 }
3453 ret = btrfs_orphan_add(trans, inode);
3454 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003455 if (ret) {
3456 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003457 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003458 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003459
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003460 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003461 if (ret)
3462 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003463 } else {
3464 nr_unlink++;
3465 }
3466
3467 /* this will do delete_inode and everything for us */
3468 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003469 if (ret)
3470 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003471 }
Miao Xie3254c872011-11-10 20:45:05 -05003472 /* release the path since we're done with it */
3473 btrfs_release_path(path);
3474
Yan, Zhengd68fc572010-05-16 10:49:58 -04003475 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3476
3477 if (root->orphan_block_rsv)
3478 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3479 (u64)-1);
3480
Miao Xie27cdeb72014-04-02 19:51:05 +08003481 if (root->orphan_block_rsv ||
3482 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003483 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003484 if (!IS_ERR(trans))
3485 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003486 }
Josef Bacik7b128762008-07-24 12:17:14 -04003487
3488 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003489 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003490 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003491 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003492
3493out:
3494 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003495 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003496 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003497 btrfs_free_path(path);
3498 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003499}
3500
Chris Masond352ac62008-09-29 15:18:18 -04003501/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003502 * very simple check to peek ahead in the leaf looking for xattrs. If we
3503 * don't find any xattrs, we know there can't be any acls.
3504 *
3505 * slot is the slot the inode is in, objectid is the objectid of the inode
3506 */
3507static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003508 int slot, u64 objectid,
3509 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003510{
3511 u32 nritems = btrfs_header_nritems(leaf);
3512 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003513 static u64 xattr_access = 0;
3514 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003515 int scanned = 0;
3516
Josef Bacikf23b5a52013-06-19 10:16:26 -04003517 if (!xattr_access) {
3518 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3519 strlen(POSIX_ACL_XATTR_ACCESS));
3520 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3521 strlen(POSIX_ACL_XATTR_DEFAULT));
3522 }
3523
Chris Mason46a53cc2009-04-27 11:47:50 -04003524 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003525 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003526 while (slot < nritems) {
3527 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3528
3529 /* we found a different objectid, there must not be acls */
3530 if (found_key.objectid != objectid)
3531 return 0;
3532
3533 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003534 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003535 if (*first_xattr_slot == -1)
3536 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003537 if (found_key.offset == xattr_access ||
3538 found_key.offset == xattr_default)
3539 return 1;
3540 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003541
3542 /*
3543 * we found a key greater than an xattr key, there can't
3544 * be any acls later on
3545 */
3546 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3547 return 0;
3548
3549 slot++;
3550 scanned++;
3551
3552 /*
3553 * it goes inode, inode backrefs, xattrs, extents,
3554 * so if there are a ton of hard links to an inode there can
3555 * be a lot of backrefs. Don't waste time searching too hard,
3556 * this is just an optimization
3557 */
3558 if (scanned >= 8)
3559 break;
3560 }
3561 /* we hit the end of the leaf before we found an xattr or
3562 * something larger than an xattr. We have to assume the inode
3563 * has acls
3564 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003565 if (*first_xattr_slot == -1)
3566 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003567 return 1;
3568}
3569
3570/*
Chris Masond352ac62008-09-29 15:18:18 -04003571 * read an inode from the btree into the in-memory inode
3572 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003573static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003574{
3575 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003576 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003577 struct btrfs_inode_item *inode_item;
3578 struct btrfs_root *root = BTRFS_I(inode)->root;
3579 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003580 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003581 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003582 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003583 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003584 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003585 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003586
3587 ret = btrfs_fill_inode(inode, &rdev);
3588 if (!ret)
3589 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003590
3591 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003592 if (!path)
3593 goto make_bad;
3594
Chris Mason39279cc2007-06-12 06:35:45 -04003595 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003596
Chris Mason39279cc2007-06-12 06:35:45 -04003597 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003598 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003599 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003600
Chris Mason5f39d392007-10-15 16:14:19 -04003601 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003602
3603 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003604 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003605
Chris Mason5f39d392007-10-15 16:14:19 -04003606 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3607 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003608 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003609 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003610 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3611 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003612 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003613
David Sterbaa937b972014-12-12 17:39:12 +01003614 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3615 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003616
David Sterbaa937b972014-12-12 17:39:12 +01003617 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3618 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003619
David Sterbaa937b972014-12-12 17:39:12 +01003620 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3621 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003622
chandan r9cc97d62012-07-04 12:48:07 +05303623 BTRFS_I(inode)->i_otime.tv_sec =
3624 btrfs_timespec_sec(leaf, &inode_item->otime);
3625 BTRFS_I(inode)->i_otime.tv_nsec =
3626 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003627
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003628 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003629 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003630 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3631
3632 /*
3633 * If we were modified in the current generation and evicted from memory
3634 * and then re-read we need to do a full sync since we don't have any
3635 * idea about which extents were modified before we were evicted from
3636 * cache.
3637 */
3638 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3639 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3640 &BTRFS_I(inode)->runtime_flags);
3641
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003642 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003643 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003644 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003645 rdev = btrfs_inode_rdev(leaf, inode_item);
3646
Josef Bacikaec74772008-07-24 12:12:38 -04003647 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003648 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003649
3650cache_index:
3651 path->slots[0]++;
3652 if (inode->i_nlink != 1 ||
3653 path->slots[0] >= btrfs_header_nritems(leaf))
3654 goto cache_acl;
3655
3656 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3657 if (location.objectid != btrfs_ino(inode))
3658 goto cache_acl;
3659
3660 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3661 if (location.type == BTRFS_INODE_REF_KEY) {
3662 struct btrfs_inode_ref *ref;
3663
3664 ref = (struct btrfs_inode_ref *)ptr;
3665 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3666 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3667 struct btrfs_inode_extref *extref;
3668
3669 extref = (struct btrfs_inode_extref *)ptr;
3670 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3671 extref);
3672 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003673cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003674 /*
3675 * try to precache a NULL acl entry for files that don't have
3676 * any xattrs or acls
3677 */
Li Zefan33345d012011-04-20 10:31:50 +08003678 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003679 btrfs_ino(inode), &first_xattr_slot);
3680 if (first_xattr_slot != -1) {
3681 path->slots[0] = first_xattr_slot;
3682 ret = btrfs_load_inode_props(inode, path);
3683 if (ret)
3684 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003685 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003686 btrfs_ino(inode),
3687 root->root_key.objectid, ret);
3688 }
3689 btrfs_free_path(path);
3690
Al Viro72c04902009-06-24 16:58:48 -04003691 if (!maybe_acls)
3692 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003693
Chris Mason39279cc2007-06-12 06:35:45 -04003694 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003695 case S_IFREG:
3696 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003697 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003698 inode->i_fop = &btrfs_file_operations;
3699 inode->i_op = &btrfs_file_inode_operations;
3700 break;
3701 case S_IFDIR:
3702 inode->i_fop = &btrfs_dir_file_operations;
3703 if (root == root->fs_info->tree_root)
3704 inode->i_op = &btrfs_dir_ro_inode_operations;
3705 else
3706 inode->i_op = &btrfs_dir_inode_operations;
3707 break;
3708 case S_IFLNK:
3709 inode->i_op = &btrfs_symlink_inode_operations;
3710 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3711 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003712 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003713 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003714 init_special_inode(inode, inode->i_mode, rdev);
3715 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003716 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003717
3718 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003719 return;
3720
3721make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003722 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003723 make_bad_inode(inode);
3724}
3725
Chris Masond352ac62008-09-29 15:18:18 -04003726/*
3727 * given a leaf and an inode, copy the inode fields into the leaf
3728 */
Chris Masone02119d2008-09-05 16:13:11 -04003729static void fill_inode_item(struct btrfs_trans_handle *trans,
3730 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003731 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003732 struct inode *inode)
3733{
Liu Bo51fab692012-12-27 09:01:21 +00003734 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003735
Liu Bo51fab692012-12-27 09:01:21 +00003736 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003737
Liu Bo51fab692012-12-27 09:01:21 +00003738 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3739 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3740 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3741 &token);
3742 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3743 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003744
David Sterbaa937b972014-12-12 17:39:12 +01003745 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003746 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003747 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003748 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003749
David Sterbaa937b972014-12-12 17:39:12 +01003750 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003751 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003752 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003753 inode->i_mtime.tv_nsec, &token);
3754
David Sterbaa937b972014-12-12 17:39:12 +01003755 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003756 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003757 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003758 inode->i_ctime.tv_nsec, &token);
3759
chandan r9cc97d62012-07-04 12:48:07 +05303760 btrfs_set_token_timespec_sec(leaf, &item->otime,
3761 BTRFS_I(inode)->i_otime.tv_sec, &token);
3762 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3763 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3764
Liu Bo51fab692012-12-27 09:01:21 +00003765 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3766 &token);
3767 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3768 &token);
3769 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3770 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3771 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3772 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3773 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003774}
3775
Chris Masond352ac62008-09-29 15:18:18 -04003776/*
3777 * copy everything in the in-memory inode into the btree.
3778 */
Chris Mason21151332011-11-10 20:39:08 -05003779static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003780 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003781{
3782 struct btrfs_inode_item *inode_item;
3783 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003784 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003785 int ret;
3786
3787 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003788 if (!path)
3789 return -ENOMEM;
3790
Chris Masonb9473432009-03-13 11:00:37 -04003791 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003792 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3793 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003794 if (ret) {
3795 if (ret > 0)
3796 ret = -ENOENT;
3797 goto failed;
3798 }
3799
Chris Mason5f39d392007-10-15 16:14:19 -04003800 leaf = path->nodes[0];
3801 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003802 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003803
Chris Masone02119d2008-09-05 16:13:11 -04003804 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003805 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003806 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003807 ret = 0;
3808failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003809 btrfs_free_path(path);
3810 return ret;
3811}
3812
Chris Masond352ac62008-09-29 15:18:18 -04003813/*
Chris Mason21151332011-11-10 20:39:08 -05003814 * copy everything in the in-memory inode into the btree.
3815 */
3816noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3817 struct btrfs_root *root, struct inode *inode)
3818{
3819 int ret;
3820
3821 /*
3822 * If the inode is a free space inode, we can deadlock during commit
3823 * if we put it into the delayed code.
3824 *
3825 * The data relocation inode should also be directly updated
3826 * without delay
3827 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003828 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003829 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3830 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003831 btrfs_update_root_times(trans, root);
3832
Chris Mason21151332011-11-10 20:39:08 -05003833 ret = btrfs_delayed_update_inode(trans, root, inode);
3834 if (!ret)
3835 btrfs_set_inode_last_trans(trans, inode);
3836 return ret;
3837 }
3838
3839 return btrfs_update_inode_item(trans, root, inode);
3840}
3841
Josef Bacikbe6aef62012-10-22 15:43:12 -04003842noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3843 struct btrfs_root *root,
3844 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003845{
3846 int ret;
3847
3848 ret = btrfs_update_inode(trans, root, inode);
3849 if (ret == -ENOSPC)
3850 return btrfs_update_inode_item(trans, root, inode);
3851 return ret;
3852}
3853
3854/*
Chris Masond352ac62008-09-29 15:18:18 -04003855 * unlink helper that gets used here in inode.c and in the tree logging
3856 * recovery code. It remove a link in a directory with a given name, and
3857 * also drops the back refs in the inode to the directory
3858 */
Al Viro92986792011-03-04 17:14:37 +00003859static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3860 struct btrfs_root *root,
3861 struct inode *dir, struct inode *inode,
3862 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003863{
3864 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003865 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003866 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003867 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003868 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003869 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003870 u64 ino = btrfs_ino(inode);
3871 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003872
3873 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003874 if (!path) {
3875 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003876 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003877 }
3878
Chris Masonb9473432009-03-13 11:00:37 -04003879 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003880 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003881 name, name_len, -1);
3882 if (IS_ERR(di)) {
3883 ret = PTR_ERR(di);
3884 goto err;
3885 }
3886 if (!di) {
3887 ret = -ENOENT;
3888 goto err;
3889 }
Chris Mason5f39d392007-10-15 16:14:19 -04003890 leaf = path->nodes[0];
3891 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003892 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003893 if (ret)
3894 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003895 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003896
Miao Xie67de1172013-12-26 13:07:06 +08003897 /*
3898 * If we don't have dir index, we have to get it by looking up
3899 * the inode ref, since we get the inode ref, remove it directly,
3900 * it is unnecessary to do delayed deletion.
3901 *
3902 * But if we have dir index, needn't search inode ref to get it.
3903 * Since the inode ref is close to the inode item, it is better
3904 * that we delay to delete it, and just do this deletion when
3905 * we update the inode item.
3906 */
3907 if (BTRFS_I(inode)->dir_index) {
3908 ret = btrfs_delayed_delete_inode_ref(inode);
3909 if (!ret) {
3910 index = BTRFS_I(inode)->dir_index;
3911 goto skip_backref;
3912 }
3913 }
3914
Li Zefan33345d012011-04-20 10:31:50 +08003915 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3916 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003917 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003918 btrfs_info(root->fs_info,
3919 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003920 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003921 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003922 goto err;
3923 }
Miao Xie67de1172013-12-26 13:07:06 +08003924skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003925 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003926 if (ret) {
3927 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003928 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003929 }
Chris Mason39279cc2007-06-12 06:35:45 -04003930
Chris Masone02119d2008-09-05 16:13:11 -04003931 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003932 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003933 if (ret != 0 && ret != -ENOENT) {
3934 btrfs_abort_transaction(trans, root, ret);
3935 goto err;
3936 }
Chris Masone02119d2008-09-05 16:13:11 -04003937
3938 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3939 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003940 if (ret == -ENOENT)
3941 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003942 else if (ret)
3943 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003944err:
3945 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003946 if (ret)
3947 goto out;
3948
3949 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003950 inode_inc_iversion(inode);
3951 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003952 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003953 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003954out:
Chris Mason39279cc2007-06-12 06:35:45 -04003955 return ret;
3956}
3957
Al Viro92986792011-03-04 17:14:37 +00003958int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3959 struct btrfs_root *root,
3960 struct inode *dir, struct inode *inode,
3961 const char *name, int name_len)
3962{
3963 int ret;
3964 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3965 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003966 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003967 ret = btrfs_update_inode(trans, root, inode);
3968 }
3969 return ret;
3970}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003971
3972/*
3973 * helper to start transaction for unlink and rmdir.
3974 *
Josef Bacikd52be812013-05-29 14:54:47 -04003975 * unlink and rmdir are special in btrfs, they do not always free space, so
3976 * if we cannot make our reservations the normal way try and see if there is
3977 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3978 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003979 */
Josef Bacikd52be812013-05-29 14:54:47 -04003980static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003981{
3982 struct btrfs_trans_handle *trans;
3983 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003984 int ret;
3985
Josef Bacike70bea52011-10-11 14:18:24 -04003986 /*
3987 * 1 for the possible orphan item
3988 * 1 for the dir item
3989 * 1 for the dir index
3990 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003991 * 1 for the inode
3992 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003993 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003994 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3995 return trans;
3996
Josef Bacikd52be812013-05-29 14:54:47 -04003997 if (PTR_ERR(trans) == -ENOSPC) {
3998 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003999
Josef Bacikd52be812013-05-29 14:54:47 -04004000 trans = btrfs_start_transaction(root, 0);
4001 if (IS_ERR(trans))
4002 return trans;
4003 ret = btrfs_cond_migrate_bytes(root->fs_info,
4004 &root->fs_info->trans_block_rsv,
4005 num_bytes, 5);
4006 if (ret) {
4007 btrfs_end_transaction(trans, root);
4008 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004009 }
Josef Bacik5a77d762011-11-01 14:32:23 -04004010 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04004011 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004012 }
Josef Bacikd52be812013-05-29 14:54:47 -04004013 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004014}
4015
Chris Mason39279cc2007-06-12 06:35:45 -04004016static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4017{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004018 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004019 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04004020 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004021 int ret;
4022
Josef Bacikd52be812013-05-29 14:54:47 -04004023 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004024 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004025 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004026
Chris Mason12fcfd22009-03-24 10:24:20 -04004027 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
4028
Chris Masone02119d2008-09-05 16:13:11 -04004029 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4030 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004031 if (ret)
4032 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004033
Yan, Zhenga22285a2010-05-16 10:48:46 -04004034 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004035 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004036 if (ret)
4037 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004038 }
Josef Bacik7b128762008-07-24 12:17:14 -04004039
Tsutomu Itohb5324022011-07-19 07:27:20 +00004040out:
Josef Bacikd52be812013-05-29 14:54:47 -04004041 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004042 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004043 return ret;
4044}
4045
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004046int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4047 struct btrfs_root *root,
4048 struct inode *dir, u64 objectid,
4049 const char *name, int name_len)
4050{
4051 struct btrfs_path *path;
4052 struct extent_buffer *leaf;
4053 struct btrfs_dir_item *di;
4054 struct btrfs_key key;
4055 u64 index;
4056 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004057 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004058
4059 path = btrfs_alloc_path();
4060 if (!path)
4061 return -ENOMEM;
4062
Li Zefan33345d012011-04-20 10:31:50 +08004063 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004064 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004065 if (IS_ERR_OR_NULL(di)) {
4066 if (!di)
4067 ret = -ENOENT;
4068 else
4069 ret = PTR_ERR(di);
4070 goto out;
4071 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004072
4073 leaf = path->nodes[0];
4074 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4075 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4076 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004077 if (ret) {
4078 btrfs_abort_transaction(trans, root, ret);
4079 goto out;
4080 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004081 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004082
4083 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4084 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004085 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004086 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004087 if (ret != -ENOENT) {
4088 btrfs_abort_transaction(trans, root, ret);
4089 goto out;
4090 }
Li Zefan33345d012011-04-20 10:31:50 +08004091 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004092 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004093 if (IS_ERR_OR_NULL(di)) {
4094 if (!di)
4095 ret = -ENOENT;
4096 else
4097 ret = PTR_ERR(di);
4098 btrfs_abort_transaction(trans, root, ret);
4099 goto out;
4100 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004101
4102 leaf = path->nodes[0];
4103 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004104 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004105 index = key.offset;
4106 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004107 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004108
Miao Xie16cdcec2011-04-22 18:12:22 +08004109 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004110 if (ret) {
4111 btrfs_abort_transaction(trans, root, ret);
4112 goto out;
4113 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004114
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004115 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004116 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004117 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004118 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004119 if (ret)
4120 btrfs_abort_transaction(trans, root, ret);
4121out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004122 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004123 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004124}
4125
Chris Mason39279cc2007-06-12 06:35:45 -04004126static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4127{
4128 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004129 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004130 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004131 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004132
David Sterbab3ae2442012-09-13 16:04:34 -06004133 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004134 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004135 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4136 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004137
Josef Bacikd52be812013-05-29 14:54:47 -04004138 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004139 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004140 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004141
Li Zefan33345d012011-04-20 10:31:50 +08004142 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004143 err = btrfs_unlink_subvol(trans, root, dir,
4144 BTRFS_I(inode)->location.objectid,
4145 dentry->d_name.name,
4146 dentry->d_name.len);
4147 goto out;
4148 }
4149
Josef Bacik7b128762008-07-24 12:17:14 -04004150 err = btrfs_orphan_add(trans, inode);
4151 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004152 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004153
Chris Mason39279cc2007-06-12 06:35:45 -04004154 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004155 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4156 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004157 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004158 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004159out:
Josef Bacikd52be812013-05-29 14:54:47 -04004160 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004161 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004162
Chris Mason39279cc2007-06-12 06:35:45 -04004163 return err;
4164}
4165
Chris Mason323ac952008-10-01 19:05:46 -04004166/*
Chris Mason39279cc2007-06-12 06:35:45 -04004167 * this can truncate away extent items, csum items and directory items.
4168 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004169 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004170 *
4171 * csum items that cross the new i_size are truncated to the new size
4172 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004173 *
4174 * min_type is the minimum key type to truncate down to. If set to 0, this
4175 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004176 */
Yan, Zheng80825102009-11-12 09:35:36 +00004177int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4178 struct btrfs_root *root,
4179 struct inode *inode,
4180 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004181{
Chris Mason39279cc2007-06-12 06:35:45 -04004182 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004183 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004184 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004185 struct btrfs_key key;
4186 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004187 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004188 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004189 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004190 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004191 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004192 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004193 int found_extent;
4194 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004195 int pending_del_nr = 0;
4196 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004197 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004198 int ret;
4199 int err = 0;
Chris Mason28ed1342014-12-17 09:41:04 -08004200 int be_nice = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004201 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004202 u64 bytes_deleted = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004203
4204 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004205
Chris Mason28ed1342014-12-17 09:41:04 -08004206 /*
4207 * for non-free space inodes and ref cows, we want to back off from
4208 * time to time
4209 */
4210 if (!btrfs_is_free_space_inode(inode) &&
4211 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4212 be_nice = 1;
4213
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004214 path = btrfs_alloc_path();
4215 if (!path)
4216 return -ENOMEM;
4217 path->reada = -1;
4218
Josef Bacik5dc562c2012-08-17 13:14:17 -04004219 /*
4220 * We want to drop from the next block forward in case this new size is
4221 * not block aligned since we will be keeping the last block of the
4222 * extent just the way it is.
4223 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004224 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4225 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004226 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4227 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004228
Miao Xie16cdcec2011-04-22 18:12:22 +08004229 /*
4230 * This function is also used to drop the items in the log tree before
4231 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4232 * it is used to drop the loged items. So we shouldn't kill the delayed
4233 * items.
4234 */
4235 if (min_type == 0 && root == BTRFS_I(inode)->root)
4236 btrfs_kill_delayed_inode_items(inode);
4237
Li Zefan33345d012011-04-20 10:31:50 +08004238 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004239 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004240 key.type = (u8)-1;
4241
Chris Mason85e21ba2008-01-29 15:11:36 -05004242search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004243 /*
4244 * with a 16K leaf size and 128MB extents, you can actually queue
4245 * up a huge file in a single leaf. Most of the time that
4246 * bytes_deleted is > 0, it will be huge by the time we get here
4247 */
4248 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
4249 if (btrfs_should_end_transaction(trans, root)) {
4250 err = -EAGAIN;
4251 goto error;
4252 }
4253 }
4254
4255
Chris Masonb9473432009-03-13 11:00:37 -04004256 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004257 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004258 if (ret < 0) {
4259 err = ret;
4260 goto out;
4261 }
Chris Masond3977122009-01-05 21:25:51 -05004262
Chris Mason85e21ba2008-01-29 15:11:36 -05004263 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004264 /* there are no items in the tree for us to truncate, we're
4265 * done
4266 */
Yan, Zheng80825102009-11-12 09:35:36 +00004267 if (path->slots[0] == 0)
4268 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004269 path->slots[0]--;
4270 }
4271
Chris Masond3977122009-01-05 21:25:51 -05004272 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004273 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004274 leaf = path->nodes[0];
4275 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004276 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004277
Li Zefan33345d012011-04-20 10:31:50 +08004278 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004279 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004280
Chris Mason85e21ba2008-01-29 15:11:36 -05004281 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004282 break;
4283
Chris Mason5f39d392007-10-15 16:14:19 -04004284 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004285 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004286 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004287 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004288 extent_type = btrfs_file_extent_type(leaf, fi);
4289 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004290 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004291 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004292 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004293 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004294 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004295 }
Yan008630c2007-11-07 13:31:09 -05004296 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004297 }
Yan, Zheng80825102009-11-12 09:35:36 +00004298 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004299 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004300 } else {
4301 if (item_end < new_size)
4302 break;
4303 if (found_key.offset >= new_size)
4304 del_item = 1;
4305 else
4306 del_item = 0;
4307 }
Chris Mason39279cc2007-06-12 06:35:45 -04004308 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004309 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004310 if (found_type != BTRFS_EXTENT_DATA_KEY)
4311 goto delete;
4312
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004313 if (del_item)
4314 last_size = found_key.offset;
4315 else
4316 last_size = new_size;
4317
Chris Mason179e29e2007-11-01 11:28:41 -04004318 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004319 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004320 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004321 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004322 u64 orig_num_bytes =
4323 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004324 extent_num_bytes = ALIGN(new_size -
4325 found_key.offset,
4326 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004327 btrfs_set_file_extent_num_bytes(leaf, fi,
4328 extent_num_bytes);
4329 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004330 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004331 if (test_bit(BTRFS_ROOT_REF_COWS,
4332 &root->state) &&
4333 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004334 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004335 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004336 } else {
Chris Masondb945352007-10-15 16:15:53 -04004337 extent_num_bytes =
4338 btrfs_file_extent_disk_num_bytes(leaf,
4339 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004340 extent_offset = found_key.offset -
4341 btrfs_file_extent_offset(leaf, fi);
4342
Chris Mason39279cc2007-06-12 06:35:45 -04004343 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004344 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004345 if (extent_start != 0) {
4346 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004347 if (test_bit(BTRFS_ROOT_REF_COWS,
4348 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004349 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004350 }
4351 }
Chris Mason90692182008-02-08 13:49:28 -05004352 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004353 /*
4354 * we can't truncate inline items that have had
4355 * special encodings
4356 */
4357 if (!del_item &&
4358 btrfs_file_extent_compression(leaf, fi) == 0 &&
4359 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4360 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004361 u32 size = new_size - found_key.offset;
4362
Miao Xie27cdeb72014-04-02 19:51:05 +08004363 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004364 inode_sub_bytes(inode, item_end + 1 -
4365 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004366
4367 /*
4368 * update the ram bytes to properly reflect
4369 * the new size of our item
4370 */
4371 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004372 size =
4373 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004374 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004375 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4376 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004377 inode_sub_bytes(inode, item_end + 1 -
4378 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004379 }
Chris Mason39279cc2007-06-12 06:35:45 -04004380 }
Chris Mason179e29e2007-11-01 11:28:41 -04004381delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004382 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004383 if (!pending_del_nr) {
4384 /* no pending yet, add ourselves */
4385 pending_del_slot = path->slots[0];
4386 pending_del_nr = 1;
4387 } else if (pending_del_nr &&
4388 path->slots[0] + 1 == pending_del_slot) {
4389 /* hop on the pending chunk */
4390 pending_del_nr++;
4391 pending_del_slot = path->slots[0];
4392 } else {
Chris Masond3977122009-01-05 21:25:51 -05004393 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004394 }
Chris Mason39279cc2007-06-12 06:35:45 -04004395 } else {
4396 break;
4397 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004398 if (found_extent &&
4399 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4400 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004401 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004402 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004403 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004404 extent_num_bytes, 0,
4405 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004406 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004407 BUG_ON(ret);
Chris Mason28ed1342014-12-17 09:41:04 -08004408 if (be_nice && pending_del_nr &&
4409 (pending_del_nr % 16 == 0) &&
4410 bytes_deleted > 1024 * 1024) {
4411 btrfs_async_run_delayed_refs(root,
4412 trans->delayed_ref_updates * 2, 0);
4413 }
Chris Mason39279cc2007-06-12 06:35:45 -04004414 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004415
Yan, Zheng80825102009-11-12 09:35:36 +00004416 if (found_type == BTRFS_INODE_ITEM_KEY)
4417 break;
4418
4419 if (path->slots[0] == 0 ||
4420 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004421 if (pending_del_nr) {
4422 ret = btrfs_del_items(trans, root, path,
4423 pending_del_slot,
4424 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004425 if (ret) {
4426 btrfs_abort_transaction(trans,
4427 root, ret);
4428 goto error;
4429 }
Yan, Zheng80825102009-11-12 09:35:36 +00004430 pending_del_nr = 0;
4431 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004432 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004433 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004434 } else {
4435 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004436 }
Chris Mason39279cc2007-06-12 06:35:45 -04004437 }
Yan, Zheng80825102009-11-12 09:35:36 +00004438out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004439 if (pending_del_nr) {
4440 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4441 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004442 if (ret)
4443 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004444 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004445error:
Filipe Mananadac57052014-08-29 20:54:26 +01004446 if (last_size != (u64)-1 &&
4447 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004448 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004449
Chris Mason39279cc2007-06-12 06:35:45 -04004450 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004451
4452 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
4453 unsigned long updates = trans->delayed_ref_updates;
4454 if (updates) {
4455 trans->delayed_ref_updates = 0;
4456 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4457 if (ret && !err)
4458 err = ret;
4459 }
4460 }
Yan, Zheng80825102009-11-12 09:35:36 +00004461 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004462}
4463
Chris Masona52d9a82007-08-27 16:49:44 -04004464/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004465 * btrfs_truncate_page - read, zero a chunk and write a page
4466 * @inode - inode that we're zeroing
4467 * @from - the offset to start zeroing
4468 * @len - the length to zero, 0 to zero the entire range respective to the
4469 * offset
4470 * @front - zero up to the offset instead of from the offset on
4471 *
4472 * This will find the page for the "from" offset and cow the page and zero the
4473 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004474 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004475int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4476 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004477{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004478 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004479 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004480 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4481 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004482 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004483 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004484 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004485 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4486 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4487 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004488 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004489 int ret = 0;
4490 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004491 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004492
Josef Bacik2aaa6652012-08-29 14:27:18 -04004493 if ((offset & (blocksize - 1)) == 0 &&
4494 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004495 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004496 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004497 if (ret)
4498 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004499
Chris Mason211c17f2008-05-15 09:13:45 -04004500again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004501 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004502 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004503 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004504 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004505 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004506 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004507
4508 page_start = page_offset(page);
4509 page_end = page_start + PAGE_CACHE_SIZE - 1;
4510
Chris Masona52d9a82007-08-27 16:49:44 -04004511 if (!PageUptodate(page)) {
4512 ret = btrfs_readpage(NULL, page);
4513 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004514 if (page->mapping != mapping) {
4515 unlock_page(page);
4516 page_cache_release(page);
4517 goto again;
4518 }
Chris Masona52d9a82007-08-27 16:49:44 -04004519 if (!PageUptodate(page)) {
4520 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004521 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004522 }
4523 }
Chris Mason211c17f2008-05-15 09:13:45 -04004524 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004525
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004526 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004527 set_page_extent_mapped(page);
4528
4529 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4530 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004531 unlock_extent_cached(io_tree, page_start, page_end,
4532 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004533 unlock_page(page);
4534 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004535 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004536 btrfs_put_ordered_extent(ordered);
4537 goto again;
4538 }
4539
Josef Bacik2ac55d42010-02-03 19:33:23 +00004540 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004541 EXTENT_DIRTY | EXTENT_DELALLOC |
4542 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004543 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004544
Josef Bacik2ac55d42010-02-03 19:33:23 +00004545 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4546 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004547 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004548 unlock_extent_cached(io_tree, page_start, page_end,
4549 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004550 goto out_unlock;
4551 }
4552
Chris Masone6dcd2d2008-07-17 12:53:50 -04004553 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004554 if (!len)
4555 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004556 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004557 if (front)
4558 memset(kaddr, 0, offset);
4559 else
4560 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004561 flush_dcache_page(page);
4562 kunmap(page);
4563 }
Chris Mason247e7432008-07-17 12:53:51 -04004564 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004565 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004566 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4567 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004568
Chris Mason89642222008-07-24 09:41:53 -04004569out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004570 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004571 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004572 unlock_page(page);
4573 page_cache_release(page);
4574out:
4575 return ret;
4576}
4577
Josef Bacik16e75492013-10-22 12:18:51 -04004578static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4579 u64 offset, u64 len)
4580{
4581 struct btrfs_trans_handle *trans;
4582 int ret;
4583
4584 /*
4585 * Still need to make sure the inode looks like it's been updated so
4586 * that any holes get logged if we fsync.
4587 */
4588 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4589 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4590 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4591 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4592 return 0;
4593 }
4594
4595 /*
4596 * 1 - for the one we're dropping
4597 * 1 - for the one we're adding
4598 * 1 - for updating the inode.
4599 */
4600 trans = btrfs_start_transaction(root, 3);
4601 if (IS_ERR(trans))
4602 return PTR_ERR(trans);
4603
4604 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4605 if (ret) {
4606 btrfs_abort_transaction(trans, root, ret);
4607 btrfs_end_transaction(trans, root);
4608 return ret;
4609 }
4610
4611 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4612 0, 0, len, 0, len, 0, 0, 0);
4613 if (ret)
4614 btrfs_abort_transaction(trans, root, ret);
4615 else
4616 btrfs_update_inode(trans, root, inode);
4617 btrfs_end_transaction(trans, root);
4618 return ret;
4619}
4620
Josef Bacik695a0d02011-03-04 15:46:53 -05004621/*
4622 * This function puts in dummy file extents for the area we're creating a hole
4623 * for. So if we are truncating this file to a larger size we need to insert
4624 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4625 * the range between oldsize and size
4626 */
Josef Bacika41ad392011-01-31 15:30:16 -05004627int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004628{
Yan Zheng9036c102008-10-30 14:19:41 -04004629 struct btrfs_root *root = BTRFS_I(inode)->root;
4630 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004631 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004632 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004633 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004634 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4635 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004636 u64 last_byte;
4637 u64 cur_offset;
4638 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004639 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004640
Josef Bacika71754f2013-06-17 17:14:39 -04004641 /*
4642 * If our size started in the middle of a page we need to zero out the
4643 * rest of the page before we expand the i_size, otherwise we could
4644 * expose stale data.
4645 */
4646 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4647 if (err)
4648 return err;
4649
Yan Zheng9036c102008-10-30 14:19:41 -04004650 if (size <= hole_start)
4651 return 0;
4652
Yan Zheng9036c102008-10-30 14:19:41 -04004653 while (1) {
4654 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004655
Josef Bacik2ac55d42010-02-03 19:33:23 +00004656 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004657 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004658 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4659 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004660 if (!ordered)
4661 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004662 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4663 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004664 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004665 btrfs_put_ordered_extent(ordered);
4666 }
4667
Yan Zheng9036c102008-10-30 14:19:41 -04004668 cur_offset = hole_start;
4669 while (1) {
4670 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4671 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004672 if (IS_ERR(em)) {
4673 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004674 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004675 break;
4676 }
Yan Zheng9036c102008-10-30 14:19:41 -04004677 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004678 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004679 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004680 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004681 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004682
Josef Bacik16e75492013-10-22 12:18:51 -04004683 err = maybe_insert_hole(root, inode, cur_offset,
4684 hole_size);
4685 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004686 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004687 btrfs_drop_extent_cache(inode, cur_offset,
4688 cur_offset + hole_size - 1, 0);
4689 hole_em = alloc_extent_map();
4690 if (!hole_em) {
4691 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4692 &BTRFS_I(inode)->runtime_flags);
4693 goto next;
4694 }
4695 hole_em->start = cur_offset;
4696 hole_em->len = hole_size;
4697 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004698
Josef Bacik5dc562c2012-08-17 13:14:17 -04004699 hole_em->block_start = EXTENT_MAP_HOLE;
4700 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004701 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004702 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004703 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4704 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004705 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004706
4707 while (1) {
4708 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004709 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004710 write_unlock(&em_tree->lock);
4711 if (err != -EEXIST)
4712 break;
4713 btrfs_drop_extent_cache(inode, cur_offset,
4714 cur_offset +
4715 hole_size - 1, 0);
4716 }
4717 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004718 }
Josef Bacik16e75492013-10-22 12:18:51 -04004719next:
Yan Zheng9036c102008-10-30 14:19:41 -04004720 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004721 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004722 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004723 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004724 break;
4725 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004726 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004727 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4728 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004729 return err;
4730}
4731
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004732static int wait_snapshoting_atomic_t(atomic_t *a)
4733{
4734 schedule();
4735 return 0;
4736}
4737
4738static void wait_for_snapshot_creation(struct btrfs_root *root)
4739{
4740 while (true) {
4741 int ret;
4742
4743 ret = btrfs_start_write_no_snapshoting(root);
4744 if (ret)
4745 break;
4746 wait_on_atomic_t(&root->will_be_snapshoted,
4747 wait_snapshoting_atomic_t,
4748 TASK_UNINTERRUPTIBLE);
4749 }
4750}
4751
Eric Sandeen3972f262013-01-12 02:57:22 +00004752static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004753{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004754 struct btrfs_root *root = BTRFS_I(inode)->root;
4755 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004756 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004757 loff_t newsize = attr->ia_size;
4758 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004759 int ret;
4760
Eric Sandeen3972f262013-01-12 02:57:22 +00004761 /*
4762 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4763 * special case where we need to update the times despite not having
4764 * these flags set. For all other operations the VFS set these flags
4765 * explicitly if it wants a timestamp update.
4766 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004767 if (newsize != oldsize) {
4768 inode_inc_iversion(inode);
4769 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4770 inode->i_ctime = inode->i_mtime =
4771 current_fs_time(inode->i_sb);
4772 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004773
Josef Bacika41ad392011-01-31 15:30:16 -05004774 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004775 truncate_pagecache(inode, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004776 /*
4777 * Don't do an expanding truncate while snapshoting is ongoing.
4778 * This is to ensure the snapshot captures a fully consistent
4779 * state of this file - if the snapshot captures this expanding
4780 * truncation, it must capture all writes that happened before
4781 * this truncation.
4782 */
4783 wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004784 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004785 if (ret) {
4786 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004787 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004788 }
Yan, Zheng80825102009-11-12 09:35:36 +00004789
Miao Xief4a2f4c2011-12-14 20:12:01 -05004790 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004791 if (IS_ERR(trans)) {
4792 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004793 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004794 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004795
4796 i_size_write(inode, newsize);
4797 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4798 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004799 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004800 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004801 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004802
Josef Bacika41ad392011-01-31 15:30:16 -05004803 /*
4804 * We're truncating a file that used to have good data down to
4805 * zero. Make sure it gets into the ordered flush list so that
4806 * any new writes get down to disk quickly.
4807 */
4808 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004809 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4810 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004811
Josef Bacikf3fe8202013-01-07 17:03:21 -05004812 /*
4813 * 1 for the orphan item we're going to add
4814 * 1 for the orphan item deletion.
4815 */
4816 trans = btrfs_start_transaction(root, 2);
4817 if (IS_ERR(trans))
4818 return PTR_ERR(trans);
4819
4820 /*
4821 * We need to do this in case we fail at _any_ point during the
4822 * actual truncate. Once we do the truncate_setsize we could
4823 * invalidate pages which forces any outstanding ordered io to
4824 * be instantly completed which will give us extents that need
4825 * to be truncated. If we fail to get an orphan inode down we
4826 * could have left over extents that were never meant to live,
4827 * so we need to garuntee from this point on that everything
4828 * will be consistent.
4829 */
4830 ret = btrfs_orphan_add(trans, inode);
4831 btrfs_end_transaction(trans, root);
4832 if (ret)
4833 return ret;
4834
Josef Bacika41ad392011-01-31 15:30:16 -05004835 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4836 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004837
4838 /* Disable nonlocked read DIO to avoid the end less truncate */
4839 btrfs_inode_block_unlocked_dio(inode);
4840 inode_dio_wait(inode);
4841 btrfs_inode_resume_unlocked_dio(inode);
4842
Josef Bacika41ad392011-01-31 15:30:16 -05004843 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004844 if (ret && inode->i_nlink) {
4845 int err;
4846
4847 /*
4848 * failed to truncate, disk_i_size is only adjusted down
4849 * as we remove extents, so it should represent the true
4850 * size of the inode, so reset the in memory size and
4851 * delete our orphan entry.
4852 */
4853 trans = btrfs_join_transaction(root);
4854 if (IS_ERR(trans)) {
4855 btrfs_orphan_del(NULL, inode);
4856 return ret;
4857 }
4858 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4859 err = btrfs_orphan_del(trans, inode);
4860 if (err)
4861 btrfs_abort_transaction(trans, root, err);
4862 btrfs_end_transaction(trans, root);
4863 }
Yan, Zheng80825102009-11-12 09:35:36 +00004864 }
4865
Josef Bacika41ad392011-01-31 15:30:16 -05004866 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004867}
4868
Chris Mason39279cc2007-06-12 06:35:45 -04004869static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4870{
4871 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004872 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004873 int err;
4874
Li Zefanb83cc962010-12-20 16:04:08 +08004875 if (btrfs_root_readonly(root))
4876 return -EROFS;
4877
Chris Mason39279cc2007-06-12 06:35:45 -04004878 err = inode_change_ok(inode, attr);
4879 if (err)
4880 return err;
4881
Chris Mason5a3f23d2009-03-31 13:27:11 -04004882 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004883 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004884 if (err)
4885 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004886 }
Yan Zheng9036c102008-10-30 14:19:41 -04004887
Christoph Hellwig10257742010-06-04 11:30:02 +02004888 if (attr->ia_valid) {
4889 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004890 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004891 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004892
Josef Bacik22c44fe2011-11-30 10:45:38 -05004893 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004894 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004895 }
4896
Chris Mason39279cc2007-06-12 06:35:45 -04004897 return err;
4898}
Chris Mason61295eb2008-01-14 16:24:38 -05004899
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004900/*
4901 * While truncating the inode pages during eviction, we get the VFS calling
4902 * btrfs_invalidatepage() against each page of the inode. This is slow because
4903 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4904 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4905 * extent_state structures over and over, wasting lots of time.
4906 *
4907 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4908 * those expensive operations on a per page basis and do only the ordered io
4909 * finishing, while we release here the extent_map and extent_state structures,
4910 * without the excessive merging and splitting.
4911 */
4912static void evict_inode_truncate_pages(struct inode *inode)
4913{
4914 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4915 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4916 struct rb_node *node;
4917
4918 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004919 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004920
4921 write_lock(&map_tree->lock);
4922 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4923 struct extent_map *em;
4924
4925 node = rb_first(&map_tree->map);
4926 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004927 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4928 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004929 remove_extent_mapping(map_tree, em);
4930 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004931 if (need_resched()) {
4932 write_unlock(&map_tree->lock);
4933 cond_resched();
4934 write_lock(&map_tree->lock);
4935 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004936 }
4937 write_unlock(&map_tree->lock);
4938
4939 spin_lock(&io_tree->lock);
4940 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4941 struct extent_state *state;
4942 struct extent_state *cached_state = NULL;
4943
4944 node = rb_first(&io_tree->state);
4945 state = rb_entry(node, struct extent_state, rb_node);
4946 atomic_inc(&state->refs);
4947 spin_unlock(&io_tree->lock);
4948
4949 lock_extent_bits(io_tree, state->start, state->end,
4950 0, &cached_state);
4951 clear_extent_bit(io_tree, state->start, state->end,
4952 EXTENT_LOCKED | EXTENT_DIRTY |
4953 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4954 EXTENT_DEFRAG, 1, 1,
4955 &cached_state, GFP_NOFS);
4956 free_extent_state(state);
4957
Filipe Manana7064dd52014-08-08 02:47:05 +01004958 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004959 spin_lock(&io_tree->lock);
4960 }
4961 spin_unlock(&io_tree->lock);
4962}
4963
Al Virobd555972010-06-07 11:35:40 -04004964void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004965{
4966 struct btrfs_trans_handle *trans;
4967 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004968 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004969 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004970 int ret;
4971
liubo1abe9b82011-03-24 11:18:59 +00004972 trace_btrfs_inode_evict(inode);
4973
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004974 evict_inode_truncate_pages(inode);
4975
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004976 if (inode->i_nlink &&
4977 ((btrfs_root_refs(&root->root_item) != 0 &&
4978 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4979 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004980 goto no_delete;
4981
Chris Mason39279cc2007-06-12 06:35:45 -04004982 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004983 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004984 goto no_delete;
4985 }
Al Virobd555972010-06-07 11:35:40 -04004986 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004987 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004988
Miao Xief6124962014-09-12 18:44:04 +08004989 btrfs_free_io_failure_record(inode, 0, (u64)-1);
4990
Yan, Zhengc71bf092009-11-12 09:34:40 +00004991 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004992 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004993 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004994 goto no_delete;
4995 }
4996
Yan, Zheng76dda932009-09-21 16:00:26 -04004997 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004998 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4999 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005000 goto no_delete;
5001 }
5002
Miao Xie0e8c36a2012-12-19 06:59:51 +00005003 ret = btrfs_commit_inode_delayed_inode(inode);
5004 if (ret) {
5005 btrfs_orphan_del(NULL, inode);
5006 goto no_delete;
5007 }
5008
Miao Xie66d8f3d2012-09-06 04:02:28 -06005009 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005010 if (!rsv) {
5011 btrfs_orphan_del(NULL, inode);
5012 goto no_delete;
5013 }
Josef Bacik4a338542011-08-29 11:01:31 -04005014 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005015 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005016 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005017
Chris Masondbe674a2008-07-17 12:54:05 -04005018 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005019
Josef Bacik4289a662011-08-05 13:22:24 -04005020 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005021 * This is a bit simpler than btrfs_truncate since we've already
5022 * reserved our space for our orphan item in the unlink, so we just
5023 * need to reserve some slack space in case we add bytes and update
5024 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005025 */
Yan, Zheng80825102009-11-12 09:35:36 +00005026 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005027 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5028 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005029
Josef Bacik726c35f2011-09-26 15:46:06 -04005030 /*
5031 * Try and steal from the global reserve since we will
5032 * likely not use this space anyway, we want to try as
5033 * hard as possible to get this to work.
5034 */
5035 if (ret)
5036 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
5037
Yan, Zhengd68fc572010-05-16 10:49:58 -04005038 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005039 btrfs_warn(root->fs_info,
5040 "Could not get space for a delete, will truncate on mount %d",
5041 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005042 btrfs_orphan_del(NULL, inode);
5043 btrfs_free_block_rsv(root, rsv);
5044 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005045 }
5046
Miao Xie0e8c36a2012-12-19 06:59:51 +00005047 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005048 if (IS_ERR(trans)) {
5049 btrfs_orphan_del(NULL, inode);
5050 btrfs_free_block_rsv(root, rsv);
5051 goto no_delete;
5052 }
5053
5054 trans->block_rsv = rsv;
5055
Yan, Zhengd68fc572010-05-16 10:49:58 -04005056 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005057 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005058 break;
5059
Miao Xie8407aa42012-09-07 01:43:32 -06005060 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005061 btrfs_end_transaction(trans, root);
5062 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005063 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005064 }
5065
Josef Bacik4289a662011-08-05 13:22:24 -04005066 btrfs_free_block_rsv(root, rsv);
5067
Josef Bacik4ef31a42013-08-13 14:10:08 -04005068 /*
5069 * Errors here aren't a big deal, it just means we leave orphan items
5070 * in the tree. They will be cleaned up on the next mount.
5071 */
Yan, Zheng80825102009-11-12 09:35:36 +00005072 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005073 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005074 btrfs_orphan_del(trans, inode);
5075 } else {
5076 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005077 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005078
Josef Bacik4289a662011-08-05 13:22:24 -04005079 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005080 if (!(root == root->fs_info->tree_root ||
5081 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005082 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005083
Chris Mason54aa1f42007-06-22 14:16:25 -04005084 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005085 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005086no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005087 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005088 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005089 return;
Chris Mason39279cc2007-06-12 06:35:45 -04005090}
5091
5092/*
5093 * this returns the key found in the dir entry in the location pointer.
5094 * If no dir entries were found, location->objectid is 0.
5095 */
5096static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5097 struct btrfs_key *location)
5098{
5099 const char *name = dentry->d_name.name;
5100 int namelen = dentry->d_name.len;
5101 struct btrfs_dir_item *di;
5102 struct btrfs_path *path;
5103 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005104 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005105
5106 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005107 if (!path)
5108 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005109
Li Zefan33345d012011-04-20 10:31:50 +08005110 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005111 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005112 if (IS_ERR(di))
5113 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005114
David Sterbac7040052011-04-19 18:00:01 +02005115 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005116 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005117
Chris Mason5f39d392007-10-15 16:14:19 -04005118 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005119out:
Chris Mason39279cc2007-06-12 06:35:45 -04005120 btrfs_free_path(path);
5121 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005122out_err:
5123 location->objectid = 0;
5124 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005125}
5126
5127/*
5128 * when we hit a tree root in a directory, the btrfs part of the inode
5129 * needs to be changed to reflect the root directory of the tree root. This
5130 * is kind of like crossing a mount point.
5131 */
5132static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005133 struct inode *dir,
5134 struct dentry *dentry,
5135 struct btrfs_key *location,
5136 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005137{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005138 struct btrfs_path *path;
5139 struct btrfs_root *new_root;
5140 struct btrfs_root_ref *ref;
5141 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005142 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005143 int ret;
5144 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005145
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005146 path = btrfs_alloc_path();
5147 if (!path) {
5148 err = -ENOMEM;
5149 goto out;
5150 }
Chris Mason39279cc2007-06-12 06:35:45 -04005151
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005152 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005153 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5154 key.type = BTRFS_ROOT_REF_KEY;
5155 key.offset = location->objectid;
5156
5157 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5158 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005159 if (ret) {
5160 if (ret < 0)
5161 err = ret;
5162 goto out;
5163 }
Chris Mason39279cc2007-06-12 06:35:45 -04005164
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005165 leaf = path->nodes[0];
5166 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005167 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005168 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5169 goto out;
5170
5171 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5172 (unsigned long)(ref + 1),
5173 dentry->d_name.len);
5174 if (ret)
5175 goto out;
5176
David Sterbab3b4aa72011-04-21 01:20:15 +02005177 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005178
5179 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5180 if (IS_ERR(new_root)) {
5181 err = PTR_ERR(new_root);
5182 goto out;
5183 }
5184
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005185 *sub_root = new_root;
5186 location->objectid = btrfs_root_dirid(&new_root->root_item);
5187 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005188 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005189 err = 0;
5190out:
5191 btrfs_free_path(path);
5192 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005193}
5194
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005195static void inode_tree_add(struct inode *inode)
5196{
5197 struct btrfs_root *root = BTRFS_I(inode)->root;
5198 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005199 struct rb_node **p;
5200 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005201 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005202 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005203
Al Viro1d3382cb2010-10-23 15:19:20 -04005204 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005205 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005206 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005207 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005208 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005209 while (*p) {
5210 parent = *p;
5211 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5212
Li Zefan33345d012011-04-20 10:31:50 +08005213 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005214 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005215 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005216 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005217 else {
5218 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005219 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005220 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005221 RB_CLEAR_NODE(parent);
5222 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005223 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005224 }
5225 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005226 rb_link_node(new, parent, p);
5227 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005228 spin_unlock(&root->inode_lock);
5229}
5230
5231static void inode_tree_del(struct inode *inode)
5232{
5233 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005234 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005235
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005236 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005237 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005238 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005239 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005240 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005241 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005242 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005243
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005244 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005245 synchronize_srcu(&root->fs_info->subvol_srcu);
5246 spin_lock(&root->inode_lock);
5247 empty = RB_EMPTY_ROOT(&root->inode_tree);
5248 spin_unlock(&root->inode_lock);
5249 if (empty)
5250 btrfs_add_dead_root(root);
5251 }
5252}
5253
Jeff Mahoney143bede2012-03-01 14:56:26 +01005254void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005255{
5256 struct rb_node *node;
5257 struct rb_node *prev;
5258 struct btrfs_inode *entry;
5259 struct inode *inode;
5260 u64 objectid = 0;
5261
Liu Bo7813b3d2014-02-10 17:37:25 +08005262 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5263 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005264
5265 spin_lock(&root->inode_lock);
5266again:
5267 node = root->inode_tree.rb_node;
5268 prev = NULL;
5269 while (node) {
5270 prev = node;
5271 entry = rb_entry(node, struct btrfs_inode, rb_node);
5272
Li Zefan33345d012011-04-20 10:31:50 +08005273 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005274 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005275 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005276 node = node->rb_right;
5277 else
5278 break;
5279 }
5280 if (!node) {
5281 while (prev) {
5282 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005283 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005284 node = prev;
5285 break;
5286 }
5287 prev = rb_next(prev);
5288 }
5289 }
5290 while (node) {
5291 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005292 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005293 inode = igrab(&entry->vfs_inode);
5294 if (inode) {
5295 spin_unlock(&root->inode_lock);
5296 if (atomic_read(&inode->i_count) > 1)
5297 d_prune_aliases(inode);
5298 /*
Al Viro45321ac2010-06-07 13:43:19 -04005299 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005300 * the inode cache when its usage count
5301 * hits zero.
5302 */
5303 iput(inode);
5304 cond_resched();
5305 spin_lock(&root->inode_lock);
5306 goto again;
5307 }
5308
5309 if (cond_resched_lock(&root->inode_lock))
5310 goto again;
5311
5312 node = rb_next(node);
5313 }
5314 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005315}
5316
Chris Masone02119d2008-09-05 16:13:11 -04005317static int btrfs_init_locked_inode(struct inode *inode, void *p)
5318{
5319 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005320 inode->i_ino = args->location->objectid;
5321 memcpy(&BTRFS_I(inode)->location, args->location,
5322 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005323 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005324 return 0;
5325}
5326
5327static int btrfs_find_actor(struct inode *inode, void *opaque)
5328{
5329 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005330 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005331 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005332}
5333
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005334static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005335 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005336 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005337{
5338 struct inode *inode;
5339 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005340 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005341
Chris Mason90d3e592014-01-09 17:28:00 -08005342 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005343 args.root = root;
5344
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005345 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005346 btrfs_init_locked_inode,
5347 (void *)&args);
5348 return inode;
5349}
5350
Balaji Rao1a54ef82008-07-21 02:01:04 +05305351/* Get an inode object given its location and corresponding root.
5352 * Returns in *is_new if the inode was read from disk
5353 */
5354struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005355 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305356{
5357 struct inode *inode;
5358
Chris Mason90d3e592014-01-09 17:28:00 -08005359 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305360 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005361 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305362
5363 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305364 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005365 if (!is_bad_inode(inode)) {
5366 inode_tree_add(inode);
5367 unlock_new_inode(inode);
5368 if (new)
5369 *new = 1;
5370 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005371 unlock_new_inode(inode);
5372 iput(inode);
5373 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005374 }
5375 }
5376
Balaji Rao1a54ef82008-07-21 02:01:04 +05305377 return inode;
5378}
5379
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005380static struct inode *new_simple_dir(struct super_block *s,
5381 struct btrfs_key *key,
5382 struct btrfs_root *root)
5383{
5384 struct inode *inode = new_inode(s);
5385
5386 if (!inode)
5387 return ERR_PTR(-ENOMEM);
5388
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005389 BTRFS_I(inode)->root = root;
5390 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005391 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005392
5393 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005394 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005395 inode->i_fop = &simple_dir_operations;
5396 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
chandan r9cc97d62012-07-04 12:48:07 +05305397 inode->i_mtime = CURRENT_TIME;
5398 inode->i_atime = inode->i_mtime;
5399 inode->i_ctime = inode->i_mtime;
5400 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005401
5402 return inode;
5403}
5404
Chris Mason3de45862008-11-17 21:02:50 -05005405struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005406{
Chris Masond3977122009-01-05 21:25:51 -05005407 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005408 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005409 struct btrfs_root *sub_root = root;
5410 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005411 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005412 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005413
5414 if (dentry->d_name.len > BTRFS_NAME_LEN)
5415 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005416
Jeff Layton39e3c952012-11-28 11:30:53 -05005417 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005418 if (ret < 0)
5419 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005420
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005421 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005422 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005423
5424 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005425 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005426 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005427 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005428
5429 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5430
Yan, Zheng76dda932009-09-21 16:00:26 -04005431 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005432 ret = fixup_tree_root_location(root, dir, dentry,
5433 &location, &sub_root);
5434 if (ret < 0) {
5435 if (ret != -ENOENT)
5436 inode = ERR_PTR(ret);
5437 else
5438 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5439 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005440 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005441 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005442 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5443
Julia Lawall34d19ba2011-01-24 19:55:19 +00005444 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005445 down_read(&root->fs_info->cleanup_work_sem);
5446 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005447 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005448 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005449 if (ret) {
5450 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005451 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005452 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005453 }
5454
Chris Mason3de45862008-11-17 21:02:50 -05005455 return inode;
5456}
5457
Nick Pigginfe15ce42011-01-07 17:49:23 +11005458static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005459{
5460 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005461 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005462
Li Zefan848cce02012-02-21 17:04:28 +08005463 if (!inode && !IS_ROOT(dentry))
5464 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005465
Li Zefan848cce02012-02-21 17:04:28 +08005466 if (inode) {
5467 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005468 if (btrfs_root_refs(&root->root_item) == 0)
5469 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005470
5471 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5472 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005473 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005474 return 0;
5475}
5476
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005477static void btrfs_dentry_release(struct dentry *dentry)
5478{
Daeseok Youn944a4512014-04-14 15:37:02 +09005479 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005480}
5481
Chris Mason3de45862008-11-17 21:02:50 -05005482static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005483 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005484{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005485 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005486
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005487 inode = btrfs_lookup_dentry(dir, dentry);
5488 if (IS_ERR(inode)) {
5489 if (PTR_ERR(inode) == -ENOENT)
5490 inode = NULL;
5491 else
5492 return ERR_CAST(inode);
5493 }
5494
Al Viro41d28bc2014-10-12 22:24:21 -04005495 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005496}
5497
Miao Xie16cdcec2011-04-22 18:12:22 +08005498unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005499 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5500};
5501
Al Viro9cdda8d2013-05-22 16:48:09 -04005502static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005503{
Al Viro9cdda8d2013-05-22 16:48:09 -04005504 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005505 struct btrfs_root *root = BTRFS_I(inode)->root;
5506 struct btrfs_item *item;
5507 struct btrfs_dir_item *di;
5508 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005509 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005510 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005511 struct list_head ins_list;
5512 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005513 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005514 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005515 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005516 unsigned char d_type;
5517 int over = 0;
5518 u32 di_cur;
5519 u32 di_total;
5520 u32 di_len;
5521 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005522 char tmp_name[32];
5523 char *name_ptr;
5524 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005525 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005526
5527 /* FIXME, use a real flag for deciding about the key type */
5528 if (root->fs_info->tree_root == root)
5529 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005530
Al Viro9cdda8d2013-05-22 16:48:09 -04005531 if (!dir_emit_dots(file, ctx))
5532 return 0;
5533
David Woodhouse49593bf2008-08-17 17:08:36 +01005534 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005535 if (!path)
5536 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005537
Josef Bacik026fd312011-05-13 10:32:11 -04005538 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005539
Miao Xie16cdcec2011-04-22 18:12:22 +08005540 if (key_type == BTRFS_DIR_INDEX_KEY) {
5541 INIT_LIST_HEAD(&ins_list);
5542 INIT_LIST_HEAD(&del_list);
5543 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5544 }
5545
David Sterba962a2982014-06-04 18:41:45 +02005546 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005547 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005548 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005549
Chris Mason39279cc2007-06-12 06:35:45 -04005550 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5551 if (ret < 0)
5552 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005553
5554 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005555 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005556 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005557 if (slot >= btrfs_header_nritems(leaf)) {
5558 ret = btrfs_next_leaf(root, path);
5559 if (ret < 0)
5560 goto err;
5561 else if (ret > 0)
5562 break;
5563 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005564 }
Chris Mason3de45862008-11-17 21:02:50 -05005565
Ross Kirkdd3cc162013-09-16 15:58:09 +01005566 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005567 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5568
5569 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005570 break;
David Sterba962a2982014-06-04 18:41:45 +02005571 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005572 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005573 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005574 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005575 if (key_type == BTRFS_DIR_INDEX_KEY &&
5576 btrfs_should_delete_dir_index(&del_list,
5577 found_key.offset))
5578 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005579
Al Viro9cdda8d2013-05-22 16:48:09 -04005580 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005581 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005582
Chris Mason39279cc2007-06-12 06:35:45 -04005583 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5584 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005585 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005586
5587 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005588 struct btrfs_key location;
5589
Josef Bacik22a94d42011-03-16 16:47:17 -04005590 if (verify_dir_item(root, leaf, di))
5591 break;
5592
Chris Mason5f39d392007-10-15 16:14:19 -04005593 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005594 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005595 name_ptr = tmp_name;
5596 } else {
5597 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005598 if (!name_ptr) {
5599 ret = -ENOMEM;
5600 goto err;
5601 }
Chris Mason5f39d392007-10-15 16:14:19 -04005602 }
5603 read_extent_buffer(leaf, name_ptr,
5604 (unsigned long)(di + 1), name_len);
5605
5606 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5607 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005608
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005609
Chris Mason3de45862008-11-17 21:02:50 -05005610 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005611 * skip it.
5612 *
5613 * In contrast to old kernels, we insert the snapshot's
5614 * dir item and dir index after it has been created, so
5615 * we won't find a reference to our own snapshot. We
5616 * still keep the following code for backward
5617 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005618 */
5619 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5620 location.objectid == root->root_key.objectid) {
5621 over = 0;
5622 goto skip;
5623 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005624 over = !dir_emit(ctx, name_ptr, name_len,
5625 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005626
Chris Mason3de45862008-11-17 21:02:50 -05005627skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005628 if (name_ptr != tmp_name)
5629 kfree(name_ptr);
5630
Chris Mason39279cc2007-06-12 06:35:45 -04005631 if (over)
5632 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005633 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005634 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005635 di_cur += di_len;
5636 di = (struct btrfs_dir_item *)((char *)di + di_len);
5637 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005638next:
5639 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005640 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005641
Miao Xie16cdcec2011-04-22 18:12:22 +08005642 if (key_type == BTRFS_DIR_INDEX_KEY) {
5643 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005644 ctx->pos++;
5645 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005646 if (ret)
5647 goto nopos;
5648 }
5649
David Woodhouse49593bf2008-08-17 17:08:36 +01005650 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005651 ctx->pos++;
5652
5653 /*
5654 * Stop new entries from being returned after we return the last
5655 * entry.
5656 *
5657 * New directory entries are assigned a strictly increasing
5658 * offset. This means that new entries created during readdir
5659 * are *guaranteed* to be seen in the future by that readdir.
5660 * This has broken buggy programs which operate on names as
5661 * they're returned by readdir. Until we re-use freed offsets
5662 * we have this hack to stop new entries from being returned
5663 * under the assumption that they'll never reach this huge
5664 * offset.
5665 *
5666 * This is being careful not to overflow 32bit loff_t unless the
5667 * last entry requires it because doing so has broken 32bit apps
5668 * in the past.
5669 */
5670 if (key_type == BTRFS_DIR_INDEX_KEY) {
5671 if (ctx->pos >= INT_MAX)
5672 ctx->pos = LLONG_MAX;
5673 else
5674 ctx->pos = INT_MAX;
5675 }
Chris Mason39279cc2007-06-12 06:35:45 -04005676nopos:
5677 ret = 0;
5678err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005679 if (key_type == BTRFS_DIR_INDEX_KEY)
5680 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005681 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005682 return ret;
5683}
5684
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005685int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005686{
5687 struct btrfs_root *root = BTRFS_I(inode)->root;
5688 struct btrfs_trans_handle *trans;
5689 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005690 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005691
Josef Bacik72ac3c02012-05-23 14:13:11 -04005692 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005693 return 0;
5694
Liu Bo83eea1f2012-07-10 05:28:39 -06005695 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005696 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005697
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005698 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005699 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005700 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005701 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005702 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005703 if (IS_ERR(trans))
5704 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005705 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005706 }
5707 return ret;
5708}
5709
5710/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005711 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005712 * inode changes. But, it is most likely to find the inode in cache.
5713 * FIXME, needs more benchmarking...there are no reasons other than performance
5714 * to keep or drop this code.
5715 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005716static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005717{
5718 struct btrfs_root *root = BTRFS_I(inode)->root;
5719 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005720 int ret;
5721
Josef Bacik72ac3c02012-05-23 14:13:11 -04005722 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005723 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005724
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005725 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005726 if (IS_ERR(trans))
5727 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005728
5729 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005730 if (ret && ret == -ENOSPC) {
5731 /* whoops, lets try again with the full transaction */
5732 btrfs_end_transaction(trans, root);
5733 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005734 if (IS_ERR(trans))
5735 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005736
Chris Mason94b60442010-05-26 11:02:00 -04005737 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005738 }
Chris Mason39279cc2007-06-12 06:35:45 -04005739 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005740 if (BTRFS_I(inode)->delayed_node)
5741 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005742
5743 return ret;
5744}
5745
5746/*
5747 * This is a copy of file_update_time. We need this so we can return error on
5748 * ENOSPC for updating the inode in the case of file write and mmap writes.
5749 */
Josef Bacike41f9412012-03-26 09:46:47 -04005750static int btrfs_update_time(struct inode *inode, struct timespec *now,
5751 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005752{
Alexander Block2bc5565282012-06-15 09:49:33 +02005753 struct btrfs_root *root = BTRFS_I(inode)->root;
5754
5755 if (btrfs_root_readonly(root))
5756 return -EROFS;
5757
Josef Bacike41f9412012-03-26 09:46:47 -04005758 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005759 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005760 if (flags & S_CTIME)
5761 inode->i_ctime = *now;
5762 if (flags & S_MTIME)
5763 inode->i_mtime = *now;
5764 if (flags & S_ATIME)
5765 inode->i_atime = *now;
5766 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005767}
5768
Chris Masond352ac62008-09-29 15:18:18 -04005769/*
5770 * find the highest existing sequence number in a directory
5771 * and then set the in-memory index_cnt variable to reflect
5772 * free sequence numbers
5773 */
Josef Bacikaec74772008-07-24 12:12:38 -04005774static int btrfs_set_inode_index_count(struct inode *inode)
5775{
5776 struct btrfs_root *root = BTRFS_I(inode)->root;
5777 struct btrfs_key key, found_key;
5778 struct btrfs_path *path;
5779 struct extent_buffer *leaf;
5780 int ret;
5781
Li Zefan33345d012011-04-20 10:31:50 +08005782 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005783 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005784 key.offset = (u64)-1;
5785
5786 path = btrfs_alloc_path();
5787 if (!path)
5788 return -ENOMEM;
5789
5790 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5791 if (ret < 0)
5792 goto out;
5793 /* FIXME: we should be able to handle this */
5794 if (ret == 0)
5795 goto out;
5796 ret = 0;
5797
5798 /*
5799 * MAGIC NUMBER EXPLANATION:
5800 * since we search a directory based on f_pos we have to start at 2
5801 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5802 * else has to start at 2
5803 */
5804 if (path->slots[0] == 0) {
5805 BTRFS_I(inode)->index_cnt = 2;
5806 goto out;
5807 }
5808
5809 path->slots[0]--;
5810
5811 leaf = path->nodes[0];
5812 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5813
Li Zefan33345d012011-04-20 10:31:50 +08005814 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005815 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005816 BTRFS_I(inode)->index_cnt = 2;
5817 goto out;
5818 }
5819
5820 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5821out:
5822 btrfs_free_path(path);
5823 return ret;
5824}
5825
Chris Masond352ac62008-09-29 15:18:18 -04005826/*
5827 * helper to find a free sequence number in a given directory. This current
5828 * code is very simple, later versions will do smarter things in the btree
5829 */
Chris Mason3de45862008-11-17 21:02:50 -05005830int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005831{
5832 int ret = 0;
5833
5834 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005835 ret = btrfs_inode_delayed_dir_index_count(dir);
5836 if (ret) {
5837 ret = btrfs_set_inode_index_count(dir);
5838 if (ret)
5839 return ret;
5840 }
Josef Bacikaec74772008-07-24 12:12:38 -04005841 }
5842
Chris Mason00e4e6b2008-08-05 11:18:09 -04005843 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005844 BTRFS_I(dir)->index_cnt++;
5845
5846 return ret;
5847}
5848
Chris Masonb0d5d102014-09-08 13:08:51 -07005849static int btrfs_insert_inode_locked(struct inode *inode)
5850{
5851 struct btrfs_iget_args args;
5852 args.location = &BTRFS_I(inode)->location;
5853 args.root = BTRFS_I(inode)->root;
5854
5855 return insert_inode_locked4(inode,
5856 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5857 btrfs_find_actor, &args);
5858}
5859
Chris Mason39279cc2007-06-12 06:35:45 -04005860static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5861 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005862 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005863 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005864 u64 ref_objectid, u64 objectid,
5865 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005866{
5867 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005868 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005869 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005870 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005871 struct btrfs_inode_ref *ref;
5872 struct btrfs_key key[2];
5873 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005874 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005875 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005876 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005877
Chris Mason5f39d392007-10-15 16:14:19 -04005878 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005879 if (!path)
5880 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005881
Chris Mason39279cc2007-06-12 06:35:45 -04005882 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005883 if (!inode) {
5884 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005885 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005886 }
Chris Mason39279cc2007-06-12 06:35:45 -04005887
Li Zefan581bb052011-04-20 10:06:11 +08005888 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005889 * O_TMPFILE, set link count to 0, so that after this point,
5890 * we fill in an inode item with the correct link count.
5891 */
5892 if (!name)
5893 set_nlink(inode, 0);
5894
5895 /*
Li Zefan581bb052011-04-20 10:06:11 +08005896 * we have to initialize this early, so we can reclaim the inode
5897 * number if we fail afterwards in this function.
5898 */
5899 inode->i_ino = objectid;
5900
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005901 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005902 trace_btrfs_inode_request(dir);
5903
Chris Mason3de45862008-11-17 21:02:50 -05005904 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005905 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005906 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005907 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005908 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005909 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005910 } else if (dir) {
5911 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005912 }
5913 /*
5914 * index_cnt is ignored for everything but a dir,
5915 * btrfs_get_inode_index_count has an explanation for the magic
5916 * number
5917 */
5918 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005919 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005920 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005921 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005922 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005923
Josef Bacik5dc562c2012-08-17 13:14:17 -04005924 /*
5925 * We could have gotten an inode number from somebody who was fsynced
5926 * and then removed in this same transaction, so let's just set full
5927 * sync since it will be a full sync anyway and this will blow away the
5928 * old info in the log.
5929 */
5930 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5931
Chris Mason9c583092008-01-29 15:15:18 -05005932 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005933 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005934 key[0].offset = 0;
5935
Chris Mason9c583092008-01-29 15:15:18 -05005936 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005937
5938 if (name) {
5939 /*
5940 * Start new inodes with an inode_ref. This is slightly more
5941 * efficient for small numbers of hard links since they will
5942 * be packed into one item. Extended refs will kick in if we
5943 * add more hard links than can fit in the ref item.
5944 */
5945 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005946 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005947 key[1].offset = ref_objectid;
5948
5949 sizes[1] = name_len + sizeof(*ref);
5950 }
Chris Mason9c583092008-01-29 15:15:18 -05005951
Chris Masonb0d5d102014-09-08 13:08:51 -07005952 location = &BTRFS_I(inode)->location;
5953 location->objectid = objectid;
5954 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005955 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005956
5957 ret = btrfs_insert_inode_locked(inode);
5958 if (ret < 0)
5959 goto fail;
5960
Chris Masonb9473432009-03-13 11:00:37 -04005961 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005962 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005963 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005964 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005965
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005966 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005967 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05305968
5969 inode->i_mtime = CURRENT_TIME;
5970 inode->i_atime = inode->i_mtime;
5971 inode->i_ctime = inode->i_mtime;
5972 BTRFS_I(inode)->i_otime = inode->i_mtime;
5973
Chris Mason5f39d392007-10-15 16:14:19 -04005974 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5975 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005976 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5977 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005978 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005979
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005980 if (name) {
5981 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5982 struct btrfs_inode_ref);
5983 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5984 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5985 ptr = (unsigned long)(ref + 1);
5986 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5987 }
Chris Mason9c583092008-01-29 15:15:18 -05005988
Chris Mason5f39d392007-10-15 16:14:19 -04005989 btrfs_mark_buffer_dirty(path->nodes[0]);
5990 btrfs_free_path(path);
5991
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005992 btrfs_inherit_iflags(inode, dir);
5993
Al Viro569254b2011-07-24 17:08:40 -04005994 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005995 if (btrfs_test_opt(root, NODATASUM))
5996 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005997 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005998 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5999 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006000 }
6001
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006002 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006003
6004 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006005 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006006
Alexander Block8ea05e32012-07-25 17:35:53 +02006007 btrfs_update_root_times(trans, root);
6008
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006009 ret = btrfs_inode_inherit_props(trans, inode, dir);
6010 if (ret)
6011 btrfs_err(root->fs_info,
6012 "error inheriting props for ino %llu (root %llu): %d",
6013 btrfs_ino(inode), root->root_key.objectid, ret);
6014
Chris Mason39279cc2007-06-12 06:35:45 -04006015 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006016
6017fail_unlock:
6018 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006019fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006020 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006021 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006022 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006023 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006024 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006025}
6026
6027static inline u8 btrfs_inode_type(struct inode *inode)
6028{
6029 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6030}
6031
Chris Masond352ac62008-09-29 15:18:18 -04006032/*
6033 * utility function to add 'inode' into 'parent_inode' with
6034 * a give name and a given sequence number.
6035 * if 'add_backref' is true, also insert a backref from the
6036 * inode to the parent directory.
6037 */
Chris Masone02119d2008-09-05 16:13:11 -04006038int btrfs_add_link(struct btrfs_trans_handle *trans,
6039 struct inode *parent_inode, struct inode *inode,
6040 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006041{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006042 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006043 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006044 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006045 u64 ino = btrfs_ino(inode);
6046 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006047
Li Zefan33345d012011-04-20 10:31:50 +08006048 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006049 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6050 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006051 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006052 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006053 key.offset = 0;
6054 }
Chris Mason39279cc2007-06-12 06:35:45 -04006055
Li Zefan33345d012011-04-20 10:31:50 +08006056 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006057 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6058 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006059 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006060 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006061 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6062 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006063 }
6064
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006065 /* Nothing to clean up yet */
6066 if (ret)
6067 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006068
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006069 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6070 parent_inode, &key,
6071 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006072 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006073 goto fail_dir_item;
6074 else if (ret) {
6075 btrfs_abort_transaction(trans, root, ret);
6076 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006077 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006078
6079 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6080 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006081 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006082 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
6083 ret = btrfs_update_inode(trans, root, parent_inode);
6084 if (ret)
6085 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006086 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006087
6088fail_dir_item:
6089 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6090 u64 local_index;
6091 int err;
6092 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6093 key.objectid, root->root_key.objectid,
6094 parent_ino, &local_index, name, name_len);
6095
6096 } else if (add_backref) {
6097 u64 local_index;
6098 int err;
6099
6100 err = btrfs_del_inode_ref(trans, root, name, name_len,
6101 ino, parent_ino, &local_index);
6102 }
6103 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006104}
6105
6106static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006107 struct inode *dir, struct dentry *dentry,
6108 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006109{
Josef Bacika1b075d2010-11-19 20:36:11 +00006110 int err = btrfs_add_link(trans, dir, inode,
6111 dentry->d_name.name, dentry->d_name.len,
6112 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006113 if (err > 0)
6114 err = -EEXIST;
6115 return err;
6116}
6117
Josef Bacik618e21d2007-07-11 10:18:17 -04006118static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006119 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006120{
6121 struct btrfs_trans_handle *trans;
6122 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006123 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006124 int err;
6125 int drop_inode = 0;
6126 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006127 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006128
6129 if (!new_valid_dev(rdev))
6130 return -EINVAL;
6131
Josef Bacik9ed74f22009-09-11 16:12:44 -04006132 /*
6133 * 2 for inode item and ref
6134 * 2 for dir items
6135 * 1 for xattr if selinux is on
6136 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006137 trans = btrfs_start_transaction(root, 5);
6138 if (IS_ERR(trans))
6139 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006140
Li Zefan581bb052011-04-20 10:06:11 +08006141 err = btrfs_find_free_ino(root, &objectid);
6142 if (err)
6143 goto out_unlock;
6144
Josef Bacikaec74772008-07-24 12:12:38 -04006145 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006146 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006147 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006148 if (IS_ERR(inode)) {
6149 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006150 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006151 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006152
Casey Schauflerad19db72011-12-15 10:09:07 -05006153 /*
6154 * If the active LSM wants to access the inode during
6155 * d_instantiate it needs these. Smack checks to see
6156 * if the filesystem supports xattrs by looking at the
6157 * ops vector.
6158 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006159 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006160 init_special_inode(inode, inode->i_mode, rdev);
6161
6162 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006163 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006164 goto out_unlock_inode;
6165
6166 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6167 if (err) {
6168 goto out_unlock_inode;
6169 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006170 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006171 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006172 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006173 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006174
Josef Bacik618e21d2007-07-11 10:18:17 -04006175out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006176 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006177 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006178 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006179 if (drop_inode) {
6180 inode_dec_link_count(inode);
6181 iput(inode);
6182 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006183 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006184
6185out_unlock_inode:
6186 drop_inode = 1;
6187 unlock_new_inode(inode);
6188 goto out_unlock;
6189
Josef Bacik618e21d2007-07-11 10:18:17 -04006190}
6191
Chris Mason39279cc2007-06-12 06:35:45 -04006192static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006193 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006194{
6195 struct btrfs_trans_handle *trans;
6196 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006197 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006198 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006199 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006200 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006201 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006202
Josef Bacik9ed74f22009-09-11 16:12:44 -04006203 /*
6204 * 2 for inode item and ref
6205 * 2 for dir items
6206 * 1 for xattr if selinux is on
6207 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006208 trans = btrfs_start_transaction(root, 5);
6209 if (IS_ERR(trans))
6210 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006211
Li Zefan581bb052011-04-20 10:06:11 +08006212 err = btrfs_find_free_ino(root, &objectid);
6213 if (err)
6214 goto out_unlock;
6215
Josef Bacikaec74772008-07-24 12:12:38 -04006216 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006217 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006218 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006219 if (IS_ERR(inode)) {
6220 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006221 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006222 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006223 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006224 /*
6225 * If the active LSM wants to access the inode during
6226 * d_instantiate it needs these. Smack checks to see
6227 * if the filesystem supports xattrs by looking at the
6228 * ops vector.
6229 */
6230 inode->i_fop = &btrfs_file_operations;
6231 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006232 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006233
6234 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6235 if (err)
6236 goto out_unlock_inode;
6237
6238 err = btrfs_update_inode(trans, root, inode);
6239 if (err)
6240 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006241
Josef Bacika1b075d2010-11-19 20:36:11 +00006242 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006243 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006244 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006245
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006246 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006247 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006248 d_instantiate(dentry, inode);
6249
Chris Mason39279cc2007-06-12 06:35:45 -04006250out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006251 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006252 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006253 inode_dec_link_count(inode);
6254 iput(inode);
6255 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006256 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006257 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006258 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006259
6260out_unlock_inode:
6261 unlock_new_inode(inode);
6262 goto out_unlock;
6263
Chris Mason39279cc2007-06-12 06:35:45 -04006264}
6265
6266static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6267 struct dentry *dentry)
6268{
6269 struct btrfs_trans_handle *trans;
6270 struct btrfs_root *root = BTRFS_I(dir)->root;
6271 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006272 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006273 int err;
6274 int drop_inode = 0;
6275
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006276 /* do not allow sys_link's with other subvols of the same device */
6277 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006278 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006279
Mark Fashehf1863732012-08-08 11:32:27 -07006280 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006281 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006282
Chris Mason3de45862008-11-17 21:02:50 -05006283 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006284 if (err)
6285 goto fail;
6286
Yan, Zhenga22285a2010-05-16 10:48:46 -04006287 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006288 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006289 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006290 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006291 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006292 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006293 if (IS_ERR(trans)) {
6294 err = PTR_ERR(trans);
6295 goto fail;
6296 }
Chris Mason5f39d392007-10-15 16:14:19 -04006297
Miao Xie67de1172013-12-26 13:07:06 +08006298 /* There are several dir indexes for this inode, clear the cache. */
6299 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006300 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006301 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006302 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006303 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006304 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006305
Josef Bacika1b075d2010-11-19 20:36:11 +00006306 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006307
Yan, Zhenga5719522009-09-24 09:17:31 -04006308 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006309 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006310 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006311 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006312 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006313 if (err)
6314 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006315 if (inode->i_nlink == 1) {
6316 /*
6317 * If new hard link count is 1, it's a file created
6318 * with open(2) O_TMPFILE flag.
6319 */
6320 err = btrfs_orphan_del(trans, inode);
6321 if (err)
6322 goto fail;
6323 }
Al Viro08c422c2011-12-23 07:58:13 -05006324 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006325 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006326 }
Chris Mason39279cc2007-06-12 06:35:45 -04006327
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006328 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006329 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006330fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006331 if (drop_inode) {
6332 inode_dec_link_count(inode);
6333 iput(inode);
6334 }
Liu Bob53d3f52012-11-14 14:34:34 +00006335 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006336 return err;
6337}
6338
Al Viro18bb1db2011-07-26 01:41:39 -04006339static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006340{
Chris Masonb9d86662008-05-02 16:13:49 -04006341 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006342 struct btrfs_trans_handle *trans;
6343 struct btrfs_root *root = BTRFS_I(dir)->root;
6344 int err = 0;
6345 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006346 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006347 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006348
Josef Bacik9ed74f22009-09-11 16:12:44 -04006349 /*
6350 * 2 items for inode and ref
6351 * 2 items for dir items
6352 * 1 for xattr if selinux is on
6353 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006354 trans = btrfs_start_transaction(root, 5);
6355 if (IS_ERR(trans))
6356 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006357
Li Zefan581bb052011-04-20 10:06:11 +08006358 err = btrfs_find_free_ino(root, &objectid);
6359 if (err)
6360 goto out_fail;
6361
Josef Bacikaec74772008-07-24 12:12:38 -04006362 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006363 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006364 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006365 if (IS_ERR(inode)) {
6366 err = PTR_ERR(inode);
6367 goto out_fail;
6368 }
Chris Mason5f39d392007-10-15 16:14:19 -04006369
Chris Mason39279cc2007-06-12 06:35:45 -04006370 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006371 /* these must be set before we unlock the inode */
6372 inode->i_op = &btrfs_dir_inode_operations;
6373 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006374
Eric Paris2a7dba32011-02-01 11:05:39 -05006375 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006376 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006377 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006378
Chris Masondbe674a2008-07-17 12:54:05 -04006379 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006380 err = btrfs_update_inode(trans, root, inode);
6381 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006382 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006383
Josef Bacika1b075d2010-11-19 20:36:11 +00006384 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6385 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006386 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006387 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006388
Chris Mason39279cc2007-06-12 06:35:45 -04006389 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006390 /*
6391 * mkdir is special. We're unlocking after we call d_instantiate
6392 * to avoid a race with nfsd calling d_instantiate.
6393 */
6394 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006395 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006396
6397out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006398 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006399 if (drop_on_err) {
6400 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006401 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006402 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006403 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006404 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006405 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006406
6407out_fail_inode:
6408 unlock_new_inode(inode);
6409 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006410}
6411
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006412/* Find next extent map of a given extent map, caller needs to ensure locks */
6413static struct extent_map *next_extent_map(struct extent_map *em)
6414{
6415 struct rb_node *next;
6416
6417 next = rb_next(&em->rb_node);
6418 if (!next)
6419 return NULL;
6420 return container_of(next, struct extent_map, rb_node);
6421}
6422
6423static struct extent_map *prev_extent_map(struct extent_map *em)
6424{
6425 struct rb_node *prev;
6426
6427 prev = rb_prev(&em->rb_node);
6428 if (!prev)
6429 return NULL;
6430 return container_of(prev, struct extent_map, rb_node);
6431}
6432
Chris Masond352ac62008-09-29 15:18:18 -04006433/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006434 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006435 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006436 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006437 */
Chris Mason3b951512008-04-17 11:29:12 -04006438static int merge_extent_mapping(struct extent_map_tree *em_tree,
6439 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006440 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006441 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006442{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006443 struct extent_map *prev;
6444 struct extent_map *next;
6445 u64 start;
6446 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006447 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006448
Chris Masone6dcd2d2008-07-17 12:53:50 -04006449 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006450
6451 if (existing->start > map_start) {
6452 next = existing;
6453 prev = prev_extent_map(next);
6454 } else {
6455 prev = existing;
6456 next = next_extent_map(prev);
6457 }
6458
6459 start = prev ? extent_map_end(prev) : em->start;
6460 start = max_t(u64, start, em->start);
6461 end = next ? next->start : extent_map_end(em);
6462 end = min_t(u64, end, extent_map_end(em));
6463 start_diff = start - em->start;
6464 em->start = start;
6465 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006466 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6467 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006468 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006469 em->block_len -= start_diff;
6470 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006471 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006472}
6473
Chris Masonc8b97812008-10-29 14:49:59 -04006474static noinline int uncompress_inline(struct btrfs_path *path,
6475 struct inode *inode, struct page *page,
6476 size_t pg_offset, u64 extent_offset,
6477 struct btrfs_file_extent_item *item)
6478{
6479 int ret;
6480 struct extent_buffer *leaf = path->nodes[0];
6481 char *tmp;
6482 size_t max_size;
6483 unsigned long inline_size;
6484 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006485 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006486
6487 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006488 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006489 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6490 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006491 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006492 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006493 if (!tmp)
6494 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006495 ptr = btrfs_file_extent_inline_start(item);
6496
6497 read_extent_buffer(leaf, tmp, ptr, inline_size);
6498
Chris Mason5b050f02008-11-11 09:34:41 -05006499 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006500 ret = btrfs_decompress(compress_type, tmp, page,
6501 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006502 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006503 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006504}
6505
Chris Masond352ac62008-09-29 15:18:18 -04006506/*
6507 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006508 * the ugly parts come from merging extents from the disk with the in-ram
6509 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006510 * where the in-ram extents might be locked pending data=ordered completion.
6511 *
6512 * This also copies inline extents directly into the page.
6513 */
Chris Masond3977122009-01-05 21:25:51 -05006514
Chris Masona52d9a82007-08-27 16:49:44 -04006515struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006516 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006517 int create)
6518{
6519 int ret;
6520 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006521 u64 extent_start = 0;
6522 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006523 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006524 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006525 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006526 struct btrfs_root *root = BTRFS_I(inode)->root;
6527 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006528 struct extent_buffer *leaf;
6529 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006530 struct extent_map *em = NULL;
6531 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006532 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006533 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006534 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006535
Chris Masona52d9a82007-08-27 16:49:44 -04006536again:
Chris Mason890871b2009-09-02 16:24:52 -04006537 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006538 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006539 if (em)
6540 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006541 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006542
Chris Masona52d9a82007-08-27 16:49:44 -04006543 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006544 if (em->start > start || em->start + em->len <= start)
6545 free_extent_map(em);
6546 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006547 free_extent_map(em);
6548 else
6549 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006550 }
David Sterba172ddd62011-04-21 00:48:27 +02006551 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006552 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006553 err = -ENOMEM;
6554 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006555 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006556 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006557 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006558 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006559 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006560 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006561
6562 if (!path) {
6563 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006564 if (!path) {
6565 err = -ENOMEM;
6566 goto out;
6567 }
6568 /*
6569 * Chances are we'll be called again, so go ahead and do
6570 * readahead
6571 */
6572 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006573 }
6574
Chris Mason179e29e2007-11-01 11:28:41 -04006575 ret = btrfs_lookup_file_extent(trans, root, path,
6576 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006577 if (ret < 0) {
6578 err = ret;
6579 goto out;
6580 }
6581
6582 if (ret != 0) {
6583 if (path->slots[0] == 0)
6584 goto not_found;
6585 path->slots[0]--;
6586 }
6587
Chris Mason5f39d392007-10-15 16:14:19 -04006588 leaf = path->nodes[0];
6589 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006590 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006591 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006592 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006593 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006594 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006595 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006596 /*
6597 * If we backup past the first extent we want to move forward
6598 * and see if there is an extent in front of us, otherwise we'll
6599 * say there is a hole for our whole search range which can
6600 * cause problems.
6601 */
6602 extent_end = start;
6603 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006604 }
6605
Chris Mason5f39d392007-10-15 16:14:19 -04006606 found_type = btrfs_file_extent_type(leaf, item);
6607 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006608 if (found_type == BTRFS_FILE_EXTENT_REG ||
6609 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006610 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006611 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006612 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6613 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006614 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006615 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006616 }
Josef Bacik25a50342013-10-14 12:08:38 -04006617next:
Yan Zheng9036c102008-10-30 14:19:41 -04006618 if (start >= extent_end) {
6619 path->slots[0]++;
6620 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6621 ret = btrfs_next_leaf(root, path);
6622 if (ret < 0) {
6623 err = ret;
6624 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006625 }
Yan Zheng9036c102008-10-30 14:19:41 -04006626 if (ret > 0)
6627 goto not_found;
6628 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006629 }
Yan Zheng9036c102008-10-30 14:19:41 -04006630 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6631 if (found_key.objectid != objectid ||
6632 found_key.type != BTRFS_EXTENT_DATA_KEY)
6633 goto not_found;
6634 if (start + len <= found_key.offset)
6635 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006636 if (start > found_key.offset)
6637 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006638 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006639 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006640 em->len = found_key.offset - start;
6641 goto not_found_em;
6642 }
6643
Filipe Manana7ffbb592014-06-09 03:48:05 +01006644 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6645
Yan Zhengd899e052008-10-30 14:25:28 -04006646 if (found_type == BTRFS_FILE_EXTENT_REG ||
6647 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006648 goto insert;
6649 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006650 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006651 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006652 size_t size;
6653 size_t extent_offset;
6654 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006655
Filipe Manana7ffbb592014-06-09 03:48:05 +01006656 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006657 goto out;
Yan689f9342007-10-29 11:41:07 -04006658
Chris Mason514ac8a2014-01-03 21:07:00 -08006659 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006660 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006661 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006662 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006663 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006664 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006665 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006666 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006667 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006668 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006669 if (btrfs_file_extent_compression(leaf, item) !=
6670 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006671 ret = uncompress_inline(path, inode, page,
6672 pg_offset,
6673 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006674 if (ret) {
6675 err = ret;
6676 goto out;
6677 }
Chris Masonc8b97812008-10-29 14:49:59 -04006678 } else {
6679 map = kmap(page);
6680 read_extent_buffer(leaf, map + pg_offset, ptr,
6681 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006682 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6683 memset(map + pg_offset + copy_size, 0,
6684 PAGE_CACHE_SIZE - pg_offset -
6685 copy_size);
6686 }
Chris Masonc8b97812008-10-29 14:49:59 -04006687 kunmap(page);
6688 }
Chris Mason179e29e2007-11-01 11:28:41 -04006689 flush_dcache_page(page);
6690 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006691 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006692 if (!trans) {
6693 kunmap(page);
6694 free_extent_map(em);
6695 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006696
David Sterbab3b4aa72011-04-21 01:20:15 +02006697 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006698 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006699
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006700 if (IS_ERR(trans))
6701 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006702 goto again;
6703 }
Chris Masonc8b97812008-10-29 14:49:59 -04006704 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006705 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006706 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006707 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006708 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006709 }
Chris Masond1310b22008-01-24 16:13:08 -05006710 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006711 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006712 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006713 }
6714not_found:
6715 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006716 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006717 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006718not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006719 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006720 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006721insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006722 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006723 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006724 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006725 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006726 err = -EIO;
6727 goto out;
6728 }
Chris Masond1310b22008-01-24 16:13:08 -05006729
6730 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006731 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006732 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006733 /* it is possible that someone inserted the extent into the tree
6734 * while we had the lock dropped. It is also possible that
6735 * an overlapping map exists in the tree
6736 */
Chris Masona52d9a82007-08-27 16:49:44 -04006737 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006738 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006739
6740 ret = 0;
6741
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006742 existing = search_extent_mapping(em_tree, start, len);
6743 /*
6744 * existing will always be non-NULL, since there must be
6745 * extent causing the -EEXIST.
6746 */
6747 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006748 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006749 /*
6750 * The existing extent map is the one nearest to
6751 * the [start, start + len) range which overlaps
6752 */
6753 err = merge_extent_mapping(em_tree, existing,
6754 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006755 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006756 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006757 free_extent_map(em);
6758 em = NULL;
6759 }
6760 } else {
6761 free_extent_map(em);
6762 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006763 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006764 }
Chris Masona52d9a82007-08-27 16:49:44 -04006765 }
Chris Mason890871b2009-09-02 16:24:52 -04006766 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006767out:
liubo1abe9b82011-03-24 11:18:59 +00006768
Steven Rostedt4cd8587c2013-11-14 22:57:29 -05006769 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006770
Chris Masonf4219502008-07-22 11:18:09 -04006771 if (path)
6772 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006773 if (trans) {
6774 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006775 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006776 err = ret;
6777 }
Chris Masona52d9a82007-08-27 16:49:44 -04006778 if (err) {
6779 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006780 return ERR_PTR(err);
6781 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006782 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006783 return em;
6784}
6785
Chris Masonec29ed52011-02-23 16:23:20 -05006786struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6787 size_t pg_offset, u64 start, u64 len,
6788 int create)
6789{
6790 struct extent_map *em;
6791 struct extent_map *hole_em = NULL;
6792 u64 range_start = start;
6793 u64 end;
6794 u64 found;
6795 u64 found_end;
6796 int err = 0;
6797
6798 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6799 if (IS_ERR(em))
6800 return em;
6801 if (em) {
6802 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006803 * if our em maps to
6804 * - a hole or
6805 * - a pre-alloc extent,
6806 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006807 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006808 if (em->block_start != EXTENT_MAP_HOLE &&
6809 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006810 return em;
6811 else
6812 hole_em = em;
6813 }
6814
6815 /* check to see if we've wrapped (len == -1 or similar) */
6816 end = start + len;
6817 if (end < start)
6818 end = (u64)-1;
6819 else
6820 end -= 1;
6821
6822 em = NULL;
6823
6824 /* ok, we didn't find anything, lets look for delalloc */
6825 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6826 end, len, EXTENT_DELALLOC, 1);
6827 found_end = range_start + found;
6828 if (found_end < range_start)
6829 found_end = (u64)-1;
6830
6831 /*
6832 * we didn't find anything useful, return
6833 * the original results from get_extent()
6834 */
6835 if (range_start > end || found_end <= start) {
6836 em = hole_em;
6837 hole_em = NULL;
6838 goto out;
6839 }
6840
6841 /* adjust the range_start to make sure it doesn't
6842 * go backwards from the start they passed in
6843 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306844 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006845 found = found_end - range_start;
6846
6847 if (found > 0) {
6848 u64 hole_start = start;
6849 u64 hole_len = len;
6850
David Sterba172ddd62011-04-21 00:48:27 +02006851 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006852 if (!em) {
6853 err = -ENOMEM;
6854 goto out;
6855 }
6856 /*
6857 * when btrfs_get_extent can't find anything it
6858 * returns one huge hole
6859 *
6860 * make sure what it found really fits our range, and
6861 * adjust to make sure it is based on the start from
6862 * the caller
6863 */
6864 if (hole_em) {
6865 u64 calc_end = extent_map_end(hole_em);
6866
6867 if (calc_end <= start || (hole_em->start > end)) {
6868 free_extent_map(hole_em);
6869 hole_em = NULL;
6870 } else {
6871 hole_start = max(hole_em->start, start);
6872 hole_len = calc_end - hole_start;
6873 }
6874 }
6875 em->bdev = NULL;
6876 if (hole_em && range_start > hole_start) {
6877 /* our hole starts before our delalloc, so we
6878 * have to return just the parts of the hole
6879 * that go until the delalloc starts
6880 */
6881 em->len = min(hole_len,
6882 range_start - hole_start);
6883 em->start = hole_start;
6884 em->orig_start = hole_start;
6885 /*
6886 * don't adjust block start at all,
6887 * it is fixed at EXTENT_MAP_HOLE
6888 */
6889 em->block_start = hole_em->block_start;
6890 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006891 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6892 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006893 } else {
6894 em->start = range_start;
6895 em->len = found;
6896 em->orig_start = range_start;
6897 em->block_start = EXTENT_MAP_DELALLOC;
6898 em->block_len = found;
6899 }
6900 } else if (hole_em) {
6901 return hole_em;
6902 }
6903out:
6904
6905 free_extent_map(hole_em);
6906 if (err) {
6907 free_extent_map(em);
6908 return ERR_PTR(err);
6909 }
6910 return em;
6911}
6912
Josef Bacik4b46fce2010-05-23 11:00:55 -04006913static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6914 u64 start, u64 len)
6915{
6916 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006917 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006918 struct btrfs_key ins;
6919 u64 alloc_hint;
6920 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006921
Josef Bacik4b46fce2010-05-23 11:00:55 -04006922 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006923 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08006924 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006925 if (ret)
6926 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006927
Josef Bacik70c8a912012-10-11 16:54:30 -04006928 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006929 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006930 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08006931 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006932 return em;
6933 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006934
6935 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6936 ins.offset, ins.offset, 0);
6937 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08006938 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006939 free_extent_map(em);
6940 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006941 }
Josef Bacik00361582013-08-14 14:02:47 -04006942
Josef Bacik4b46fce2010-05-23 11:00:55 -04006943 return em;
6944}
6945
Chris Mason46bfbb52010-05-26 11:04:10 -04006946/*
6947 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6948 * block must be cow'd
6949 */
Josef Bacik00361582013-08-14 14:02:47 -04006950noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006951 u64 *orig_start, u64 *orig_block_len,
6952 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006953{
Josef Bacik00361582013-08-14 14:02:47 -04006954 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006955 struct btrfs_path *path;
6956 int ret;
6957 struct extent_buffer *leaf;
6958 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006959 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006960 struct btrfs_file_extent_item *fi;
6961 struct btrfs_key key;
6962 u64 disk_bytenr;
6963 u64 backref_offset;
6964 u64 extent_end;
6965 u64 num_bytes;
6966 int slot;
6967 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006968 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006969
Chris Mason46bfbb52010-05-26 11:04:10 -04006970 path = btrfs_alloc_path();
6971 if (!path)
6972 return -ENOMEM;
6973
Josef Bacik00361582013-08-14 14:02:47 -04006974 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006975 offset, 0);
6976 if (ret < 0)
6977 goto out;
6978
6979 slot = path->slots[0];
6980 if (ret == 1) {
6981 if (slot == 0) {
6982 /* can't find the item, must cow */
6983 ret = 0;
6984 goto out;
6985 }
6986 slot--;
6987 }
6988 ret = 0;
6989 leaf = path->nodes[0];
6990 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006991 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006992 key.type != BTRFS_EXTENT_DATA_KEY) {
6993 /* not our file or wrong item type, must cow */
6994 goto out;
6995 }
6996
6997 if (key.offset > offset) {
6998 /* Wrong offset, must cow */
6999 goto out;
7000 }
7001
7002 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7003 found_type = btrfs_file_extent_type(leaf, fi);
7004 if (found_type != BTRFS_FILE_EXTENT_REG &&
7005 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7006 /* not a regular extent, must cow */
7007 goto out;
7008 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007009
7010 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7011 goto out;
7012
Miao Xiee77751a2013-12-27 21:11:50 +08007013 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7014 if (extent_end <= offset)
7015 goto out;
7016
Chris Mason46bfbb52010-05-26 11:04:10 -04007017 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007018 if (disk_bytenr == 0)
7019 goto out;
7020
7021 if (btrfs_file_extent_compression(leaf, fi) ||
7022 btrfs_file_extent_encryption(leaf, fi) ||
7023 btrfs_file_extent_other_encoding(leaf, fi))
7024 goto out;
7025
Chris Mason46bfbb52010-05-26 11:04:10 -04007026 backref_offset = btrfs_file_extent_offset(leaf, fi);
7027
Josef Bacik7ee9e442013-06-21 16:37:03 -04007028 if (orig_start) {
7029 *orig_start = key.offset - backref_offset;
7030 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7031 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7032 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007033
Chris Mason46bfbb52010-05-26 11:04:10 -04007034 if (btrfs_extent_readonly(root, disk_bytenr))
7035 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007036
7037 num_bytes = min(offset + *len, extent_end) - offset;
7038 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7039 u64 range_end;
7040
7041 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7042 ret = test_range_bit(io_tree, offset, range_end,
7043 EXTENT_DELALLOC, 0, NULL);
7044 if (ret) {
7045 ret = -EAGAIN;
7046 goto out;
7047 }
7048 }
7049
Josef Bacik1bda19e2013-10-18 12:10:36 -04007050 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007051
7052 /*
7053 * look for other files referencing this extent, if we
7054 * find any we must cow
7055 */
Josef Bacik00361582013-08-14 14:02:47 -04007056 trans = btrfs_join_transaction(root);
7057 if (IS_ERR(trans)) {
7058 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007059 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007060 }
7061
7062 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7063 key.offset - backref_offset, disk_bytenr);
7064 btrfs_end_transaction(trans, root);
7065 if (ret) {
7066 ret = 0;
7067 goto out;
7068 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007069
7070 /*
7071 * adjust disk_bytenr and num_bytes to cover just the bytes
7072 * in this extent we are about to write. If there
7073 * are any csums in that range we have to cow in order
7074 * to keep the csums correct
7075 */
7076 disk_bytenr += backref_offset;
7077 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007078 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7079 goto out;
7080 /*
7081 * all of the above have passed, it is safe to overwrite this extent
7082 * without cow
7083 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007084 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007085 ret = 1;
7086out:
7087 btrfs_free_path(path);
7088 return ret;
7089}
7090
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007091bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7092{
7093 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7094 int found = false;
7095 void **pagep = NULL;
7096 struct page *page = NULL;
7097 int start_idx;
7098 int end_idx;
7099
7100 start_idx = start >> PAGE_CACHE_SHIFT;
7101
7102 /*
7103 * end is the last byte in the last page. end == start is legal
7104 */
7105 end_idx = end >> PAGE_CACHE_SHIFT;
7106
7107 rcu_read_lock();
7108
7109 /* Most of the code in this while loop is lifted from
7110 * find_get_page. It's been modified to begin searching from a
7111 * page and return just the first page found in that range. If the
7112 * found idx is less than or equal to the end idx then we know that
7113 * a page exists. If no pages are found or if those pages are
7114 * outside of the range then we're fine (yay!) */
7115 while (page == NULL &&
7116 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7117 page = radix_tree_deref_slot(pagep);
7118 if (unlikely(!page))
7119 break;
7120
7121 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007122 if (radix_tree_deref_retry(page)) {
7123 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007124 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007125 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007126 /*
7127 * Otherwise, shmem/tmpfs must be storing a swap entry
7128 * here as an exceptional entry: so return it without
7129 * attempting to raise page count.
7130 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007131 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007132 break; /* TODO: Is this relevant for this use case? */
7133 }
7134
Filipe Manana91405152014-06-05 13:22:24 +01007135 if (!page_cache_get_speculative(page)) {
7136 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007137 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007138 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007139
7140 /*
7141 * Has the page moved?
7142 * This is part of the lockless pagecache protocol. See
7143 * include/linux/pagemap.h for details.
7144 */
7145 if (unlikely(page != *pagep)) {
7146 page_cache_release(page);
7147 page = NULL;
7148 }
7149 }
7150
7151 if (page) {
7152 if (page->index <= end_idx)
7153 found = true;
7154 page_cache_release(page);
7155 }
7156
7157 rcu_read_unlock();
7158 return found;
7159}
7160
Josef Bacikeb838e72012-07-31 16:28:48 -04007161static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7162 struct extent_state **cached_state, int writing)
7163{
7164 struct btrfs_ordered_extent *ordered;
7165 int ret = 0;
7166
7167 while (1) {
7168 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7169 0, cached_state);
7170 /*
7171 * We're concerned with the entire range that we're going to be
7172 * doing DIO to, so we need to make sure theres no ordered
7173 * extents in this range.
7174 */
7175 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7176 lockend - lockstart + 1);
7177
7178 /*
7179 * We need to make sure there are no buffered pages in this
7180 * range either, we could have raced between the invalidate in
7181 * generic_file_direct_write and locking the extent. The
7182 * invalidate needs to happen so that reads after a write do not
7183 * get stale data.
7184 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007185 if (!ordered &&
7186 (!writing ||
7187 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007188 break;
7189
7190 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7191 cached_state, GFP_NOFS);
7192
7193 if (ordered) {
7194 btrfs_start_ordered_extent(inode, ordered, 1);
7195 btrfs_put_ordered_extent(ordered);
7196 } else {
7197 /* Screw you mmap */
Filipe Manana728404d2014-10-10 09:43:11 +01007198 ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
Filipe Manana075bdbd2014-10-09 21:18:55 +01007199 if (ret)
7200 break;
7201 ret = filemap_fdatawait_range(inode->i_mapping,
7202 lockstart,
7203 lockend);
Josef Bacikeb838e72012-07-31 16:28:48 -04007204 if (ret)
7205 break;
7206
7207 /*
7208 * If we found a page that couldn't be invalidated just
7209 * fall back to buffered.
7210 */
7211 ret = invalidate_inode_pages2_range(inode->i_mapping,
7212 lockstart >> PAGE_CACHE_SHIFT,
7213 lockend >> PAGE_CACHE_SHIFT);
7214 if (ret)
7215 break;
7216 }
7217
7218 cond_resched();
7219 }
7220
7221 return ret;
7222}
7223
Josef Bacik69ffb542012-09-11 15:40:07 -04007224static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7225 u64 len, u64 orig_start,
7226 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007227 u64 orig_block_len, u64 ram_bytes,
7228 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007229{
7230 struct extent_map_tree *em_tree;
7231 struct extent_map *em;
7232 struct btrfs_root *root = BTRFS_I(inode)->root;
7233 int ret;
7234
7235 em_tree = &BTRFS_I(inode)->extent_tree;
7236 em = alloc_extent_map();
7237 if (!em)
7238 return ERR_PTR(-ENOMEM);
7239
7240 em->start = start;
7241 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007242 em->mod_start = start;
7243 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007244 em->len = len;
7245 em->block_len = block_len;
7246 em->block_start = block_start;
7247 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007248 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007249 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007250 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007251 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7252 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007253 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007254
7255 do {
7256 btrfs_drop_extent_cache(inode, em->start,
7257 em->start + em->len - 1, 0);
7258 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007259 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007260 write_unlock(&em_tree->lock);
7261 } while (ret == -EEXIST);
7262
7263 if (ret) {
7264 free_extent_map(em);
7265 return ERR_PTR(ret);
7266 }
7267
7268 return em;
7269}
7270
7271
Josef Bacik4b46fce2010-05-23 11:00:55 -04007272static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7273 struct buffer_head *bh_result, int create)
7274{
7275 struct extent_map *em;
7276 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007277 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007278 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007279 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007280 u64 len = bh_result->b_size;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007281 u64 *outstanding_extents = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007282 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007283 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007284
Miao Xie172a5042013-02-21 02:48:22 -07007285 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007286 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007287 else
Josef Bacikc3298612012-08-03 16:49:19 -04007288 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007289
Josef Bacikc3298612012-08-03 16:49:19 -04007290 lockstart = start;
7291 lockend = start + len - 1;
7292
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007293 if (current->journal_info) {
7294 /*
7295 * Need to pull our outstanding extents and set journal_info to NULL so
7296 * that anything that needs to check if there's a transction doesn't get
7297 * confused.
7298 */
7299 outstanding_extents = current->journal_info;
7300 current->journal_info = NULL;
7301 }
7302
Josef Bacikeb838e72012-07-31 16:28:48 -04007303 /*
7304 * If this errors out it's because we couldn't invalidate pagecache for
7305 * this range and we need to fallback to buffered.
7306 */
7307 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7308 return -ENOTBLK;
7309
Josef Bacik4b46fce2010-05-23 11:00:55 -04007310 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007311 if (IS_ERR(em)) {
7312 ret = PTR_ERR(em);
7313 goto unlock_err;
7314 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007315
7316 /*
7317 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7318 * io. INLINE is special, and we could probably kludge it in here, but
7319 * it's still buffered so for safety lets just fall back to the generic
7320 * buffered path.
7321 *
7322 * For COMPRESSED we _have_ to read the entire extent in so we can
7323 * decompress it, so there will be buffering required no matter what we
7324 * do, so go ahead and fallback to buffered.
7325 *
7326 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7327 * to buffered IO. Don't blame me, this is the price we pay for using
7328 * the generic code.
7329 */
7330 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7331 em->block_start == EXTENT_MAP_INLINE) {
7332 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007333 ret = -ENOTBLK;
7334 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007335 }
7336
7337 /* Just a good old fashioned hole, return */
7338 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7339 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7340 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007341 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007342 }
7343
7344 /*
7345 * We don't allocate a new extent in the following cases
7346 *
7347 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7348 * existing extent.
7349 * 2) The extent is marked as PREALLOC. We're good to go here and can
7350 * just use the extent.
7351 *
7352 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007353 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007354 len = min(len, em->len - (start - em->start));
7355 lockstart = start + len;
7356 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007357 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007358
7359 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7360 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7361 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007362 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007363 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007364
7365 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7366 type = BTRFS_ORDERED_PREALLOC;
7367 else
7368 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007369 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007370 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007371
Josef Bacik00361582013-08-14 14:02:47 -04007372 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007373 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007374 if (type == BTRFS_ORDERED_PREALLOC) {
7375 free_extent_map(em);
7376 em = create_pinned_em(inode, start, len,
7377 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007378 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007379 orig_block_len,
7380 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007381 if (IS_ERR(em)) {
7382 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007383 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007384 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007385 }
7386
Chris Mason46bfbb52010-05-26 11:04:10 -04007387 ret = btrfs_add_ordered_extent_dio(inode, start,
7388 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007389 if (ret) {
7390 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007391 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007392 }
7393 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007394 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007395 }
Josef Bacik00361582013-08-14 14:02:47 -04007396
Chris Mason46bfbb52010-05-26 11:04:10 -04007397 /*
7398 * this will cow the extent, reset the len in case we changed
7399 * it above
7400 */
7401 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007402 free_extent_map(em);
7403 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007404 if (IS_ERR(em)) {
7405 ret = PTR_ERR(em);
7406 goto unlock_err;
7407 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007408 len = min(len, em->len - (start - em->start));
7409unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007410 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7411 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007412 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007413 bh_result->b_bdev = em->bdev;
7414 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007415 if (create) {
7416 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7417 set_buffer_new(bh_result);
7418
7419 /*
7420 * Need to update the i_size under the extent lock so buffered
7421 * readers will get the updated i_size when we unlock.
7422 */
7423 if (start + len > i_size_read(inode))
7424 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007425
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007426 /*
7427 * If we have an outstanding_extents count still set then we're
7428 * within our reservation, otherwise we need to adjust our inode
7429 * counter appropriately.
7430 */
7431 if (*outstanding_extents) {
7432 (*outstanding_extents)--;
7433 } else {
Josef Bacik3e05bde2015-02-11 15:08:57 -05007434 spin_lock(&BTRFS_I(inode)->lock);
7435 BTRFS_I(inode)->outstanding_extents++;
7436 spin_unlock(&BTRFS_I(inode)->lock);
7437 }
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007438
7439 current->journal_info = outstanding_extents;
Josef Bacik32667892015-02-11 15:08:58 -05007440 btrfs_free_reserved_data_space(inode, len);
Josef Bacikc3473e82012-06-19 10:59:00 -04007441 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007442
Josef Bacikeb838e72012-07-31 16:28:48 -04007443 /*
7444 * In the case of write we need to clear and unlock the entire range,
7445 * in the case of read we need to unlock only the end area that we
7446 * aren't using if there is any left over space.
7447 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007448 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007449 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7450 lockend, unlock_bits, 1, 0,
7451 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007452 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007453 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007454 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007455
Josef Bacik4b46fce2010-05-23 11:00:55 -04007456 free_extent_map(em);
7457
7458 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007459
7460unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007461 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7462 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007463 if (outstanding_extents)
7464 current->journal_info = outstanding_extents;
Josef Bacikeb838e72012-07-31 16:28:48 -04007465 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007466}
7467
Miao Xie8b110e32014-09-12 18:44:03 +08007468static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7469 int rw, int mirror_num)
7470{
7471 struct btrfs_root *root = BTRFS_I(inode)->root;
7472 int ret;
7473
7474 BUG_ON(rw & REQ_WRITE);
7475
7476 bio_get(bio);
7477
7478 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7479 BTRFS_WQ_ENDIO_DIO_REPAIR);
7480 if (ret)
7481 goto err;
7482
7483 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7484err:
7485 bio_put(bio);
7486 return ret;
7487}
7488
7489static int btrfs_check_dio_repairable(struct inode *inode,
7490 struct bio *failed_bio,
7491 struct io_failure_record *failrec,
7492 int failed_mirror)
7493{
7494 int num_copies;
7495
7496 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7497 failrec->logical, failrec->len);
7498 if (num_copies == 1) {
7499 /*
7500 * we only have a single copy of the data, so don't bother with
7501 * all the retry and error correction code that follows. no
7502 * matter what the error is, it is very likely to persist.
7503 */
7504 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7505 num_copies, failrec->this_mirror, failed_mirror);
7506 return 0;
7507 }
7508
7509 failrec->failed_mirror = failed_mirror;
7510 failrec->this_mirror++;
7511 if (failrec->this_mirror == failed_mirror)
7512 failrec->this_mirror++;
7513
7514 if (failrec->this_mirror > num_copies) {
7515 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7516 num_copies, failrec->this_mirror, failed_mirror);
7517 return 0;
7518 }
7519
7520 return 1;
7521}
7522
7523static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7524 struct page *page, u64 start, u64 end,
7525 int failed_mirror, bio_end_io_t *repair_endio,
7526 void *repair_arg)
7527{
7528 struct io_failure_record *failrec;
7529 struct bio *bio;
7530 int isector;
7531 int read_mode;
7532 int ret;
7533
7534 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7535
7536 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7537 if (ret)
7538 return ret;
7539
7540 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7541 failed_mirror);
7542 if (!ret) {
7543 free_io_failure(inode, failrec);
7544 return -EIO;
7545 }
7546
7547 if (failed_bio->bi_vcnt > 1)
7548 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7549 else
7550 read_mode = READ_SYNC;
7551
7552 isector = start - btrfs_io_bio(failed_bio)->logical;
7553 isector >>= inode->i_sb->s_blocksize_bits;
7554 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7555 0, isector, repair_endio, repair_arg);
7556 if (!bio) {
7557 free_io_failure(inode, failrec);
7558 return -EIO;
7559 }
7560
7561 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7562 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7563 read_mode, failrec->this_mirror, failrec->in_validation);
7564
7565 ret = submit_dio_repair_bio(inode, bio, read_mode,
7566 failrec->this_mirror);
7567 if (ret) {
7568 free_io_failure(inode, failrec);
7569 bio_put(bio);
7570 }
7571
7572 return ret;
7573}
7574
7575struct btrfs_retry_complete {
7576 struct completion done;
7577 struct inode *inode;
7578 u64 start;
7579 int uptodate;
7580};
7581
7582static void btrfs_retry_endio_nocsum(struct bio *bio, int err)
7583{
7584 struct btrfs_retry_complete *done = bio->bi_private;
7585 struct bio_vec *bvec;
7586 int i;
7587
7588 if (err)
7589 goto end;
7590
7591 done->uptodate = 1;
7592 bio_for_each_segment_all(bvec, bio, i)
7593 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7594end:
7595 complete(&done->done);
7596 bio_put(bio);
7597}
7598
7599static int __btrfs_correct_data_nocsum(struct inode *inode,
7600 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007601{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007602 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007603 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007604 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007605 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007606 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007607
Miao Xiec1dc0892014-09-12 18:43:56 +08007608 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007609 done.inode = inode;
7610
7611 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7612try_again:
7613 done.uptodate = 0;
7614 done.start = start;
7615 init_completion(&done.done);
7616
7617 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7618 start + bvec->bv_len - 1,
7619 io_bio->mirror_num,
7620 btrfs_retry_endio_nocsum, &done);
7621 if (ret)
7622 return ret;
7623
7624 wait_for_completion(&done.done);
7625
7626 if (!done.uptodate) {
7627 /* We might have another mirror, so try again */
7628 goto try_again;
7629 }
7630
7631 start += bvec->bv_len;
7632 }
7633
7634 return 0;
7635}
7636
7637static void btrfs_retry_endio(struct bio *bio, int err)
7638{
7639 struct btrfs_retry_complete *done = bio->bi_private;
7640 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7641 struct bio_vec *bvec;
7642 int uptodate;
7643 int ret;
7644 int i;
7645
7646 if (err)
7647 goto end;
7648
7649 uptodate = 1;
7650 bio_for_each_segment_all(bvec, bio, i) {
7651 ret = __readpage_endio_check(done->inode, io_bio, i,
7652 bvec->bv_page, 0,
7653 done->start, bvec->bv_len);
7654 if (!ret)
7655 clean_io_failure(done->inode, done->start,
7656 bvec->bv_page, 0);
7657 else
7658 uptodate = 0;
7659 }
7660
7661 done->uptodate = uptodate;
7662end:
7663 complete(&done->done);
7664 bio_put(bio);
7665}
7666
7667static int __btrfs_subio_endio_read(struct inode *inode,
7668 struct btrfs_io_bio *io_bio, int err)
7669{
7670 struct bio_vec *bvec;
7671 struct btrfs_retry_complete done;
7672 u64 start;
7673 u64 offset = 0;
7674 int i;
7675 int ret;
7676
7677 err = 0;
7678 start = io_bio->logical;
7679 done.inode = inode;
7680
Miao Xiec1dc0892014-09-12 18:43:56 +08007681 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007682 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7683 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007684 if (likely(!ret))
7685 goto next;
7686try_again:
7687 done.uptodate = 0;
7688 done.start = start;
7689 init_completion(&done.done);
7690
7691 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7692 start + bvec->bv_len - 1,
7693 io_bio->mirror_num,
7694 btrfs_retry_endio, &done);
7695 if (ret) {
7696 err = ret;
7697 goto next;
7698 }
7699
7700 wait_for_completion(&done.done);
7701
7702 if (!done.uptodate) {
7703 /* We might have another mirror, so try again */
7704 goto try_again;
7705 }
7706next:
7707 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007708 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007709 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007710
7711 return err;
7712}
7713
Miao Xie8b110e32014-09-12 18:44:03 +08007714static int btrfs_subio_endio_read(struct inode *inode,
7715 struct btrfs_io_bio *io_bio, int err)
7716{
7717 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7718
7719 if (skip_csum) {
7720 if (unlikely(err))
7721 return __btrfs_correct_data_nocsum(inode, io_bio);
7722 else
7723 return 0;
7724 } else {
7725 return __btrfs_subio_endio_read(inode, io_bio, err);
7726 }
7727}
7728
Miao Xiec1dc0892014-09-12 18:43:56 +08007729static void btrfs_endio_direct_read(struct bio *bio, int err)
7730{
7731 struct btrfs_dio_private *dip = bio->bi_private;
7732 struct inode *inode = dip->inode;
7733 struct bio *dio_bio;
7734 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7735
Miao Xie8b110e32014-09-12 18:44:03 +08007736 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7737 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007738
Josef Bacik4b46fce2010-05-23 11:00:55 -04007739 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007740 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007741 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007742
Josef Bacik4b46fce2010-05-23 11:00:55 -04007743 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007744
7745 /* If we had a csum failure make sure to clear the uptodate flag */
7746 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007747 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7748 dio_end_io(dio_bio, err);
Miao Xie23ea8e52014-09-12 18:43:54 +08007749
7750 if (io_bio->end_io)
7751 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007752 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007753}
7754
7755static void btrfs_endio_direct_write(struct bio *bio, int err)
7756{
7757 struct btrfs_dio_private *dip = bio->bi_private;
7758 struct inode *inode = dip->inode;
7759 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007760 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007761 u64 ordered_offset = dip->logical_offset;
7762 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007763 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007764 int ret;
7765
7766 if (err)
7767 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007768again:
7769 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7770 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007771 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007772 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007773 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007774
Liu Bo9e0af232014-08-15 23:36:53 +08007775 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7776 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007777 btrfs_queue_work(root->fs_info->endio_write_workers,
7778 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007779out_test:
7780 /*
7781 * our bio might span multiple ordered extents. If we haven't
7782 * completed the accounting for the whole dio, go back and try again
7783 */
7784 if (ordered_offset < dip->logical_offset + dip->bytes) {
7785 ordered_bytes = dip->logical_offset + dip->bytes -
7786 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007787 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007788 goto again;
7789 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007790out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007791 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007792
Josef Bacik4b46fce2010-05-23 11:00:55 -04007793 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007794
7795 /* If we had an error make sure to clear the uptodate flag */
7796 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007797 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7798 dio_end_io(dio_bio, err);
7799 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007800}
7801
Chris Masoneaf25d92010-05-25 09:48:28 -04007802static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7803 struct bio *bio, int mirror_num,
7804 unsigned long bio_flags, u64 offset)
7805{
7806 int ret;
7807 struct btrfs_root *root = BTRFS_I(inode)->root;
7808 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007809 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007810 return 0;
7811}
7812
Miao Xiee65e1532010-11-22 03:04:43 +00007813static void btrfs_end_dio_bio(struct bio *bio, int err)
7814{
7815 struct btrfs_dio_private *dip = bio->bi_private;
7816
Miao Xie8b110e32014-09-12 18:44:03 +08007817 if (err)
7818 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
7819 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7820 btrfs_ino(dip->inode), bio->bi_rw,
7821 (unsigned long long)bio->bi_iter.bi_sector,
7822 bio->bi_iter.bi_size, err);
7823
7824 if (dip->subio_endio)
7825 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007826
7827 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007828 dip->errors = 1;
7829
7830 /*
7831 * before atomic variable goto zero, we must make sure
7832 * dip->errors is perceived to be set.
7833 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007834 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007835 }
7836
7837 /* if there are more bios still pending for this dio, just exit */
7838 if (!atomic_dec_and_test(&dip->pending_bios))
7839 goto out;
7840
Chris Mason9be33952013-05-17 18:30:14 -04007841 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007842 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007843 } else {
7844 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007845 bio_endio(dip->orig_bio, 0);
7846 }
7847out:
7848 bio_put(bio);
7849}
7850
7851static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7852 u64 first_sector, gfp_t gfp_flags)
7853{
7854 int nr_vecs = bio_get_nr_vecs(bdev);
7855 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7856}
7857
Miao Xiec1dc0892014-09-12 18:43:56 +08007858static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
7859 struct inode *inode,
7860 struct btrfs_dio_private *dip,
7861 struct bio *bio,
7862 u64 file_offset)
7863{
7864 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7865 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
7866 int ret;
7867
7868 /*
7869 * We load all the csum data we need when we submit
7870 * the first bio to reduce the csum tree search and
7871 * contention.
7872 */
7873 if (dip->logical_offset == file_offset) {
7874 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
7875 file_offset);
7876 if (ret)
7877 return ret;
7878 }
7879
7880 if (bio == dip->orig_bio)
7881 return 0;
7882
7883 file_offset -= dip->logical_offset;
7884 file_offset >>= inode->i_sb->s_blocksize_bits;
7885 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
7886
7887 return 0;
7888}
7889
Miao Xiee65e1532010-11-22 03:04:43 +00007890static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7891 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007892 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007893{
Miao Xiefacc8a222013-07-25 19:22:34 +08007894 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007895 int write = rw & REQ_WRITE;
7896 struct btrfs_root *root = BTRFS_I(inode)->root;
7897 int ret;
7898
Josef Bacikb812ce22012-11-16 13:56:32 -05007899 if (async_submit)
7900 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7901
Miao Xiee65e1532010-11-22 03:04:43 +00007902 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007903
7904 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02007905 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7906 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007907 if (ret)
7908 goto err;
7909 }
Miao Xiee65e1532010-11-22 03:04:43 +00007910
Josef Bacik1ae39932011-04-06 14:41:34 -04007911 if (skip_sum)
7912 goto map;
7913
7914 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007915 ret = btrfs_wq_submit_bio(root->fs_info,
7916 inode, rw, bio, 0, 0,
7917 file_offset,
7918 __btrfs_submit_bio_start_direct_io,
7919 __btrfs_submit_bio_done);
7920 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007921 } else if (write) {
7922 /*
7923 * If we aren't doing async submit, calculate the csum of the
7924 * bio now.
7925 */
7926 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7927 if (ret)
7928 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007929 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08007930 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
7931 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007932 if (ret)
7933 goto err;
7934 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007935map:
7936 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007937err:
7938 bio_put(bio);
7939 return ret;
7940}
7941
7942static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7943 int skip_sum)
7944{
7945 struct inode *inode = dip->inode;
7946 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007947 struct bio *bio;
7948 struct bio *orig_bio = dip->orig_bio;
7949 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007950 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007951 u64 file_offset = dip->logical_offset;
7952 u64 submit_len = 0;
7953 u64 map_length;
7954 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08007955 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04007956 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007957
Kent Overstreet4f024f32013-10-11 15:44:27 -07007958 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007959 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007960 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007961 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007962 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08007963
Kent Overstreet4f024f32013-10-11 15:44:27 -07007964 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007965 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007966 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04007967 goto submit;
7968 }
7969
David Woodhouse53b381b2013-01-29 18:40:14 -05007970 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08007971 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05007972 async_submit = 0;
7973 else
7974 async_submit = 1;
7975
Josef Bacik02f57c72011-04-06 14:25:44 -04007976 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7977 if (!bio)
7978 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08007979
Josef Bacik02f57c72011-04-06 14:25:44 -04007980 bio->bi_private = dip;
7981 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007982 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04007983 atomic_inc(&dip->pending_bios);
7984
Miao Xiee65e1532010-11-22 03:04:43 +00007985 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02007986 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00007987 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02007988 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00007989 /*
7990 * inc the count before we submit the bio so
7991 * we know the end IO handler won't happen before
7992 * we inc the count. Otherwise, the dip might get freed
7993 * before we're done setting it up
7994 */
7995 atomic_inc(&dip->pending_bios);
7996 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7997 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007998 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007999 if (ret) {
8000 bio_put(bio);
8001 atomic_dec(&dip->pending_bios);
8002 goto out_err;
8003 }
8004
Miao Xiee65e1532010-11-22 03:04:43 +00008005 start_sector += submit_len >> 9;
8006 file_offset += submit_len;
8007
8008 submit_len = 0;
8009 nr_pages = 0;
8010
8011 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8012 start_sector, GFP_NOFS);
8013 if (!bio)
8014 goto out_err;
8015 bio->bi_private = dip;
8016 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008017 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008018
Kent Overstreet4f024f32013-10-11 15:44:27 -07008019 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008020 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008021 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008022 &map_length, NULL, 0);
8023 if (ret) {
8024 bio_put(bio);
8025 goto out_err;
8026 }
8027 } else {
8028 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308029 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00008030 bvec++;
8031 }
8032 }
8033
Josef Bacik02f57c72011-04-06 14:25:44 -04008034submit:
Miao Xiee65e1532010-11-22 03:04:43 +00008035 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008036 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008037 if (!ret)
8038 return 0;
8039
8040 bio_put(bio);
8041out_err:
8042 dip->errors = 1;
8043 /*
8044 * before atomic variable goto zero, we must
8045 * make sure dip->errors is perceived to be set.
8046 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008047 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008048 if (atomic_dec_and_test(&dip->pending_bios))
8049 bio_io_error(dip->orig_bio);
8050
8051 /* bio_end_io() will handle error, so we needn't return it */
8052 return 0;
8053}
8054
Chris Mason9be33952013-05-17 18:30:14 -04008055static void btrfs_submit_direct(int rw, struct bio *dio_bio,
8056 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008057{
8058 struct btrfs_root *root = BTRFS_I(inode)->root;
8059 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04008060 struct bio *io_bio;
Miao Xie23ea8e52014-09-12 18:43:54 +08008061 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008062 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02008063 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008064 int ret = 0;
8065
8066 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8067
Chris Mason9be33952013-05-17 18:30:14 -04008068 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008069 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008070 ret = -ENOMEM;
8071 goto free_ordered;
8072 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008073
Miao Xiec1dc0892014-09-12 18:43:56 +08008074 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008075 if (!dip) {
8076 ret = -ENOMEM;
8077 goto free_io_bio;
8078 }
8079
8080 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008081 dip->inode = inode;
8082 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008083 dip->bytes = dio_bio->bi_iter.bi_size;
8084 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008085 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008086 dip->orig_bio = io_bio;
8087 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008088 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008089 btrfs_bio = btrfs_io_bio(io_bio);
8090 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008091
Miao Xiec1dc0892014-09-12 18:43:56 +08008092 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008093 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008094 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008095 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008096 dip->subio_endio = btrfs_subio_endio_read;
8097 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008098
Miao Xiee65e1532010-11-22 03:04:43 +00008099 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8100 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008101 return;
Chris Mason9be33952013-05-17 18:30:14 -04008102
Miao Xie23ea8e52014-09-12 18:43:54 +08008103 if (btrfs_bio->end_io)
8104 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008105free_io_bio:
8106 bio_put(io_bio);
8107
Josef Bacik4b46fce2010-05-23 11:00:55 -04008108free_ordered:
8109 /*
8110 * If this is a write, we need to clean up the reserved space and kill
8111 * the ordered extent.
8112 */
8113 if (write) {
8114 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05008115 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008116 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
8117 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
8118 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08008119 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008120 btrfs_put_ordered_extent(ordered);
8121 btrfs_put_ordered_extent(ordered);
8122 }
Chris Mason9be33952013-05-17 18:30:14 -04008123 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008124}
8125
Chris Mason5a5f79b2010-05-26 21:33:37 -04008126static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008127 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008128{
8129 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008130 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008131 unsigned blocksize_mask = root->sectorsize - 1;
8132 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008133
8134 if (offset & blocksize_mask)
8135 goto out;
8136
Al Viro28060d52014-03-22 05:15:17 -04008137 if (iov_iter_alignment(iter) & blocksize_mask)
8138 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008139
Al Viro28060d52014-03-22 05:15:17 -04008140 /* If this is a write we don't need to check anymore */
8141 if (rw & WRITE)
8142 return 0;
8143 /*
8144 * Check to make sure we don't have duplicate iov_base's in this
8145 * iovec, if so return EINVAL, otherwise we'll get csum errors
8146 * when reading back.
8147 */
8148 for (seg = 0; seg < iter->nr_segs; seg++) {
8149 for (i = seg + 1; i < iter->nr_segs; i++) {
8150 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008151 goto out;
8152 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008153 }
8154 retval = 0;
8155out:
8156 return retval;
8157}
Josef Bacikeb838e72012-07-31 16:28:48 -04008158
Chris Mason16432982008-04-10 10:23:21 -04008159static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05008160 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04008161{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008162 struct file *file = iocb->ki_filp;
8163 struct inode *inode = file->f_mapping->host;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008164 u64 outstanding_extents = 0;
Miao Xie09348562013-02-07 10:12:07 +00008165 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008166 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008167 bool wakeup = true;
8168 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008169 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008170
Al Viro28060d52014-03-22 05:15:17 -04008171 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008172 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008173
Miao Xie38851cc2013-02-08 07:04:11 +00008174 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008175 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008176
Josef Bacik0e267c42013-07-02 10:38:02 -04008177 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008178 * The generic stuff only does filemap_write_and_wait_range, which
8179 * isn't enough if we've written compressed pages to this area, so
8180 * we need to flush the dirty pages again to make absolutely sure
8181 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008182 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008183 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008184 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8185 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008186 filemap_fdatawrite_range(inode->i_mapping, offset,
8187 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008188
Miao Xie09348562013-02-07 10:12:07 +00008189 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008190 /*
8191 * If the write DIO is beyond the EOF, we need update
8192 * the isize, but it is protected by i_mutex. So we can
8193 * not unlock the i_mutex at this case.
8194 */
8195 if (offset + count <= inode->i_size) {
8196 mutex_unlock(&inode->i_mutex);
8197 relock = true;
8198 }
Miao Xie09348562013-02-07 10:12:07 +00008199 ret = btrfs_delalloc_reserve_space(inode, count);
8200 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008201 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008202 outstanding_extents = div64_u64(count +
8203 BTRFS_MAX_EXTENT_SIZE - 1,
8204 BTRFS_MAX_EXTENT_SIZE);
8205
8206 /*
8207 * We need to know how many extents we reserved so that we can
8208 * do the accounting properly if we go over the number we
8209 * originally calculated. Abuse current->journal_info for this.
8210 */
8211 current->journal_info = &outstanding_extents;
David Sterbaee39b432014-09-30 01:33:33 +02008212 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8213 &BTRFS_I(inode)->runtime_flags)) {
Miao Xie38851cc2013-02-08 07:04:11 +00008214 inode_dio_done(inode);
8215 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8216 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008217 }
8218
8219 ret = __blockdev_direct_IO(rw, iocb, inode,
8220 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05008221 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00008222 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00008223 if (rw & WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008224 current->journal_info = NULL;
Miao Xie09348562013-02-07 10:12:07 +00008225 if (ret < 0 && ret != -EIOCBQUEUED)
8226 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07008227 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00008228 btrfs_delalloc_release_space(inode,
8229 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008230 }
Miao Xie38851cc2013-02-08 07:04:11 +00008231out:
Miao Xie2e60a512013-02-08 07:01:08 +00008232 if (wakeup)
8233 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008234 if (relock)
8235 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008236
8237 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008238}
8239
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008240#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8241
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008242static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8243 __u64 start, __u64 len)
8244{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008245 int ret;
8246
8247 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8248 if (ret)
8249 return ret;
8250
Chris Masonec29ed52011-02-23 16:23:20 -05008251 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008252}
8253
Chris Mason9ebefb182007-06-15 13:50:00 -04008254int btrfs_readpage(struct file *file, struct page *page)
8255{
Chris Masond1310b22008-01-24 16:13:08 -05008256 struct extent_io_tree *tree;
8257 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008258 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008259}
Chris Mason1832a6d2007-12-21 16:27:21 -05008260
Chris Mason39279cc2007-06-12 06:35:45 -04008261static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8262{
Chris Masond1310b22008-01-24 16:13:08 -05008263 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04008264
8265
8266 if (current->flags & PF_MEMALLOC) {
8267 redirty_page_for_writepage(wbc, page);
8268 unlock_page(page);
8269 return 0;
8270 }
Chris Masond1310b22008-01-24 16:13:08 -05008271 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008272 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8273}
Chris Mason39279cc2007-06-12 06:35:45 -04008274
Eric Sandeen48a3b632013-04-25 20:41:01 +00008275static int btrfs_writepages(struct address_space *mapping,
8276 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008277{
Chris Masond1310b22008-01-24 16:13:08 -05008278 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008279
Chris Masond1310b22008-01-24 16:13:08 -05008280 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008281 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8282}
8283
Chris Mason3ab2fb52007-11-08 10:59:22 -05008284static int
8285btrfs_readpages(struct file *file, struct address_space *mapping,
8286 struct list_head *pages, unsigned nr_pages)
8287{
Chris Masond1310b22008-01-24 16:13:08 -05008288 struct extent_io_tree *tree;
8289 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008290 return extent_readpages(tree, mapping, pages, nr_pages,
8291 btrfs_get_extent);
8292}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008293static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008294{
Chris Masond1310b22008-01-24 16:13:08 -05008295 struct extent_io_tree *tree;
8296 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008297 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008298
Chris Masond1310b22008-01-24 16:13:08 -05008299 tree = &BTRFS_I(page->mapping->host)->io_tree;
8300 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008301 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008302 if (ret == 1) {
8303 ClearPagePrivate(page);
8304 set_page_private(page, 0);
8305 page_cache_release(page);
8306 }
8307 return ret;
8308}
Chris Mason39279cc2007-06-12 06:35:45 -04008309
Chris Masone6dcd2d2008-07-17 12:53:50 -04008310static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8311{
Chris Mason98509cfc2008-09-11 15:51:43 -04008312 if (PageWriteback(page) || PageDirty(page))
8313 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008314 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008315}
8316
Lukas Czernerd47992f2013-05-21 23:17:23 -04008317static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8318 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008319{
Josef Bacik5fd02042012-05-02 14:00:54 -04008320 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008321 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008322 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008323 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008324 u64 page_start = page_offset(page);
8325 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008326 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008327
Chris Mason8b62b722009-09-02 16:53:46 -04008328 /*
8329 * we have the page locked, so new writeback can't start,
8330 * and the dirty bit won't be cleared while we are here.
8331 *
8332 * Wait for IO on this page so that we can safely clear
8333 * the PagePrivate2 bit and do ordered accounting
8334 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008335 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008336
Josef Bacik5fd02042012-05-02 14:00:54 -04008337 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008338 if (offset) {
8339 btrfs_releasepage(page, GFP_NOFS);
8340 return;
8341 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008342
8343 if (!inode_evicting)
8344 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8345 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008346 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008347 /*
8348 * IO on this page will never be started, so we need
8349 * to account for any ordered extents now
8350 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008351 if (!inode_evicting)
8352 clear_extent_bit(tree, page_start, page_end,
8353 EXTENT_DIRTY | EXTENT_DELALLOC |
8354 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8355 EXTENT_DEFRAG, 1, 0, &cached_state,
8356 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008357 /*
8358 * whoever cleared the private bit is responsible
8359 * for the finish_ordered_io
8360 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008361 if (TestClearPagePrivate2(page)) {
8362 struct btrfs_ordered_inode_tree *tree;
8363 u64 new_len;
8364
8365 tree = &BTRFS_I(inode)->ordered_tree;
8366
8367 spin_lock_irq(&tree->lock);
8368 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8369 new_len = page_start - ordered->file_offset;
8370 if (new_len < ordered->truncated_len)
8371 ordered->truncated_len = new_len;
8372 spin_unlock_irq(&tree->lock);
8373
8374 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8375 page_start,
8376 PAGE_CACHE_SIZE, 1))
8377 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008378 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008379 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008380 if (!inode_evicting) {
8381 cached_state = NULL;
8382 lock_extent_bits(tree, page_start, page_end, 0,
8383 &cached_state);
8384 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008385 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008386
8387 if (!inode_evicting) {
8388 clear_extent_bit(tree, page_start, page_end,
8389 EXTENT_LOCKED | EXTENT_DIRTY |
8390 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8391 EXTENT_DEFRAG, 1, 1,
8392 &cached_state, GFP_NOFS);
8393
8394 __btrfs_releasepage(page, GFP_NOFS);
8395 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008396
Chris Mason4a096752008-07-21 10:29:44 -04008397 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008398 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008399 ClearPagePrivate(page);
8400 set_page_private(page, 0);
8401 page_cache_release(page);
8402 }
Chris Mason39279cc2007-06-12 06:35:45 -04008403}
8404
Chris Mason9ebefb182007-06-15 13:50:00 -04008405/*
8406 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8407 * called from a page fault handler when a page is first dirtied. Hence we must
8408 * be careful to check for EOF conditions here. We set the page up correctly
8409 * for a written page which means we get ENOSPC checking when writing into
8410 * holes and correct delalloc and unwritten extent mapping on filesystems that
8411 * support these features.
8412 *
8413 * We are not allowed to take the i_mutex here so we have to play games to
8414 * protect against truncate races as the page could now be beyond EOF. Because
8415 * vmtruncate() writes the inode size before removing pages, once we have the
8416 * page lock we can determine safely if the page is beyond EOF. If it is not
8417 * beyond EOF, then the page is guaranteed safe against truncation until we
8418 * unlock the page.
8419 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008420int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008421{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008422 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008423 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008424 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008425 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8426 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008427 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008428 char *kaddr;
8429 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008430 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008431 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008432 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008433 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008434 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008435
Jan Karab2b5ef52012-06-12 16:20:45 +02008436 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04008437 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008438 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008439 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008440 reserved = 1;
8441 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008442 if (ret) {
8443 if (ret == -ENOMEM)
8444 ret = VM_FAULT_OOM;
8445 else /* -ENOSPC, -EIO, etc */
8446 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008447 if (reserved)
8448 goto out;
8449 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008450 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008451
Nick Piggin56a76f82009-03-31 15:23:23 -07008452 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008453again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008454 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008455 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008456 page_start = page_offset(page);
8457 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04008458
Chris Mason9ebefb182007-06-15 13:50:00 -04008459 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008460 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008461 /* page got truncated out from underneath us */
8462 goto out_unlock;
8463 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008464 wait_on_page_writeback(page);
8465
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008466 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008467 set_page_extent_mapped(page);
8468
Chris Masoneb84ae02008-07-17 13:53:27 -04008469 /*
8470 * we can't set the delalloc bits if there are pending ordered
8471 * extents. Drop our locks and wait for them to finish
8472 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008473 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8474 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008475 unlock_extent_cached(io_tree, page_start, page_end,
8476 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008477 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008478 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008479 btrfs_put_ordered_extent(ordered);
8480 goto again;
8481 }
8482
Josef Bacikfbf19082009-10-01 17:10:23 -04008483 /*
8484 * XXX - page_mkwrite gets called every time the page is dirtied, even
8485 * if it was already dirty, so for space accounting reasons we need to
8486 * clear any delalloc bits for the range we are fixing to save. There
8487 * is probably a better way to do this, but for now keep consistent with
8488 * prepare_pages in the normal write path.
8489 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008490 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008491 EXTENT_DIRTY | EXTENT_DELALLOC |
8492 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008493 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008494
Josef Bacik2ac55d42010-02-03 19:33:23 +00008495 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8496 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008497 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008498 unlock_extent_cached(io_tree, page_start, page_end,
8499 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008500 ret = VM_FAULT_SIGBUS;
8501 goto out_unlock;
8502 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008503 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008504
8505 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008506 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008507 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008508 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008509 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008510
Chris Masone6dcd2d2008-07-17 12:53:50 -04008511 if (zero_start != PAGE_CACHE_SIZE) {
8512 kaddr = kmap(page);
8513 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8514 flush_dcache_page(page);
8515 kunmap(page);
8516 }
Chris Mason247e7432008-07-17 12:53:51 -04008517 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008518 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008519 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008520
Chris Mason257c62e2009-10-13 13:21:08 -04008521 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8522 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008523 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008524
Josef Bacik2ac55d42010-02-03 19:33:23 +00008525 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008526
8527out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008528 if (!ret) {
8529 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008530 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008531 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008532 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008533out:
Josef Bacikec39e182012-01-12 19:10:12 -05008534 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008535out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008536 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008537 return ret;
8538}
8539
Josef Bacika41ad392011-01-31 15:30:16 -05008540static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008541{
8542 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008543 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008544 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008545 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008546 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008547 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008548 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008549
Josef Bacik0ef8b722013-10-25 16:13:35 -04008550 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8551 (u64)-1);
8552 if (ret)
8553 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008554
Josef Bacikfcb80c22011-05-03 10:40:22 -04008555 /*
8556 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8557 * 3 things going on here
8558 *
8559 * 1) We need to reserve space for our orphan item and the space to
8560 * delete our orphan item. Lord knows we don't want to have a dangling
8561 * orphan item because we didn't reserve space to remove it.
8562 *
8563 * 2) We need to reserve space to update our inode.
8564 *
8565 * 3) We need to have something to cache all the space that is going to
8566 * be free'd up by the truncate operation, but also have some slack
8567 * space reserved in case it uses space during the truncate (thank you
8568 * very much snapshotting).
8569 *
8570 * And we need these to all be seperate. The fact is we can use alot of
8571 * space doing the truncate, and we have no earthly idea how much space
8572 * we will use, so we need the truncate reservation to be seperate so it
8573 * doesn't end up using space reserved for updating the inode or
8574 * removing the orphan item. We also need to be able to stop the
8575 * transaction and start a new one, which means we need to be able to
8576 * update the inode several times, and we have no idea of knowing how
8577 * many times that will be, so we can't just reserve 1 item for the
8578 * entirety of the opration, so that has to be done seperately as well.
8579 * Then there is the orphan item, which does indeed need to be held on
8580 * to for the whole operation, and we need nobody to touch this reserved
8581 * space except the orphan code.
8582 *
8583 * So that leaves us with
8584 *
8585 * 1) root->orphan_block_rsv - for the orphan deletion.
8586 * 2) rsv - for the truncate reservation, which we will steal from the
8587 * transaction reservation.
8588 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8589 * updating the inode.
8590 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008591 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008592 if (!rsv)
8593 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008594 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008595 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008596
Josef Bacik907cbce2011-08-08 13:46:15 -04008597 /*
Josef Bacik07127182011-08-19 10:29:59 -04008598 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008599 * 1 for updating the inode.
8600 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008601 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008602 if (IS_ERR(trans)) {
8603 err = PTR_ERR(trans);
8604 goto out;
8605 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008606
Josef Bacik907cbce2011-08-08 13:46:15 -04008607 /* Migrate the slack space for the truncate to our reserve */
8608 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8609 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008610 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008611
Chris Mason5a3f23d2009-03-31 13:27:11 -04008612 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008613 * So if we truncate and then write and fsync we normally would just
8614 * write the extents that changed, which is a problem if we need to
8615 * first truncate that entire inode. So set this flag so we write out
8616 * all of the extents in the inode to the sync log so we're completely
8617 * safe.
8618 */
8619 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008620 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008621
Yan, Zheng80825102009-11-12 09:35:36 +00008622 while (1) {
8623 ret = btrfs_truncate_inode_items(trans, root, inode,
8624 inode->i_size,
8625 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08008626 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05008627 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008628 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008629 }
Chris Mason39279cc2007-06-12 06:35:45 -04008630
Josef Bacikfcb80c22011-05-03 10:40:22 -04008631 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008632 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008633 if (ret) {
8634 err = ret;
8635 break;
8636 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008637
Yan, Zheng80825102009-11-12 09:35:36 +00008638 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008639 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008640
8641 trans = btrfs_start_transaction(root, 2);
8642 if (IS_ERR(trans)) {
8643 ret = err = PTR_ERR(trans);
8644 trans = NULL;
8645 break;
8646 }
8647
8648 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8649 rsv, min_size);
8650 BUG_ON(ret); /* shouldn't happen */
8651 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008652 }
8653
8654 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008655 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008656 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008657 if (ret)
8658 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008659 }
8660
Chris Mason917c16b2011-11-08 14:49:59 -05008661 if (trans) {
8662 trans->block_rsv = &root->fs_info->trans_block_rsv;
8663 ret = btrfs_update_inode(trans, root, inode);
8664 if (ret && !err)
8665 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008666
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008667 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008668 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008669 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008670
8671out:
8672 btrfs_free_block_rsv(root, rsv);
8673
Josef Bacik3893e332011-01-31 16:03:11 -05008674 if (ret && !err)
8675 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008676
Josef Bacik3893e332011-01-31 16:03:11 -05008677 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008678}
8679
Sven Wegener3b963622008-06-09 21:57:42 -04008680/*
Chris Masond352ac62008-09-29 15:18:18 -04008681 * create a new subvolume directory/inode (helper for the ioctl).
8682 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008683int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008684 struct btrfs_root *new_root,
8685 struct btrfs_root *parent_root,
8686 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008687{
Chris Mason39279cc2007-06-12 06:35:45 -04008688 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008689 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008690 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008691
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008692 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8693 new_dirid, new_dirid,
8694 S_IFDIR | (~current_umask() & S_IRWXUGO),
8695 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008696 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008697 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008698 inode->i_op = &btrfs_dir_inode_operations;
8699 inode->i_fop = &btrfs_dir_file_operations;
8700
Miklos Szeredibfe86842011-10-28 14:13:29 +02008701 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008702 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008703 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008704
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008705 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8706 if (err)
8707 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008708 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008709 new_root->root_key.objectid, err);
8710
Yan, Zheng76dda932009-09-21 16:00:26 -04008711 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008712
Yan, Zheng76dda932009-09-21 16:00:26 -04008713 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008714 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008715}
8716
Chris Mason39279cc2007-06-12 06:35:45 -04008717struct inode *btrfs_alloc_inode(struct super_block *sb)
8718{
8719 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008720 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008721
8722 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8723 if (!ei)
8724 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008725
8726 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008727 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008728 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008729 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008730 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008731 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008732 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008733 ei->disk_i_size = 0;
8734 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008735 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008736 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008737 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008738 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008739 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008740
Josef Bacik9e0baf62011-07-15 15:16:44 +00008741 spin_lock_init(&ei->lock);
8742 ei->outstanding_extents = 0;
8743 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008744
Josef Bacik72ac3c02012-05-23 14:13:11 -04008745 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008746 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008747
Miao Xie16cdcec2011-04-22 18:12:22 +08008748 ei->delayed_node = NULL;
8749
chandan r9cc97d62012-07-04 12:48:07 +05308750 ei->i_otime.tv_sec = 0;
8751 ei->i_otime.tv_nsec = 0;
8752
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008753 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008754 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008755 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8756 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008757 ei->io_tree.track_uptodate = 1;
8758 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008759 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008760 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008761 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008762 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008763 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008764 RB_CLEAR_NODE(&ei->rb_node);
8765
8766 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008767}
8768
Josef Bacikaaedb552013-10-11 14:44:09 -04008769#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8770void btrfs_test_destroy_inode(struct inode *inode)
8771{
8772 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8773 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8774}
8775#endif
8776
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008777static void btrfs_i_callback(struct rcu_head *head)
8778{
8779 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008780 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8781}
8782
Chris Mason39279cc2007-06-12 06:35:45 -04008783void btrfs_destroy_inode(struct inode *inode)
8784{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008785 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008786 struct btrfs_root *root = BTRFS_I(inode)->root;
8787
Al Virob3d9b7a2012-06-09 13:51:19 -04008788 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008789 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008790 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8791 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008792 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8793 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008794 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008795
Chris Mason5a3f23d2009-03-31 13:27:11 -04008796 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008797 * This can happen where we create an inode, but somebody else also
8798 * created the same inode and we need to destroy the one we already
8799 * created.
8800 */
8801 if (!root)
8802 goto free;
8803
Josef Bacik8a35d952012-05-23 14:26:42 -04008804 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8805 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008806 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008807 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008808 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008809 }
Josef Bacik7b128762008-07-24 12:17:14 -04008810
Chris Masond3977122009-01-05 21:25:51 -05008811 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008812 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8813 if (!ordered)
8814 break;
8815 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008816 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008817 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008818 btrfs_remove_ordered_extent(inode, ordered);
8819 btrfs_put_ordered_extent(ordered);
8820 btrfs_put_ordered_extent(ordered);
8821 }
8822 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008823 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008824 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008825free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008826 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008827}
8828
Al Viro45321ac2010-06-07 13:43:19 -04008829int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008830{
8831 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008832
Naohiro Aota6379ef92013-06-06 09:56:34 +00008833 if (root == NULL)
8834 return 1;
8835
Liu Bofa6ac872013-02-20 14:10:23 +00008836 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008837 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008838 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008839 else
Al Viro45321ac2010-06-07 13:43:19 -04008840 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008841}
8842
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008843static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008844{
8845 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8846
8847 inode_init_once(&ei->vfs_inode);
8848}
8849
8850void btrfs_destroy_cachep(void)
8851{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008852 /*
8853 * Make sure all delayed rcu free inodes are flushed before we
8854 * destroy cache.
8855 */
8856 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008857 if (btrfs_inode_cachep)
8858 kmem_cache_destroy(btrfs_inode_cachep);
8859 if (btrfs_trans_handle_cachep)
8860 kmem_cache_destroy(btrfs_trans_handle_cachep);
8861 if (btrfs_transaction_cachep)
8862 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008863 if (btrfs_path_cachep)
8864 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008865 if (btrfs_free_space_cachep)
8866 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008867 if (btrfs_delalloc_work_cachep)
8868 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008869}
8870
8871int btrfs_init_cachep(void)
8872{
David Sterba837e1972012-09-07 03:00:48 -06008873 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008874 sizeof(struct btrfs_inode), 0,
8875 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008876 if (!btrfs_inode_cachep)
8877 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008878
David Sterba837e1972012-09-07 03:00:48 -06008879 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008880 sizeof(struct btrfs_trans_handle), 0,
8881 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008882 if (!btrfs_trans_handle_cachep)
8883 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008884
David Sterba837e1972012-09-07 03:00:48 -06008885 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008886 sizeof(struct btrfs_transaction), 0,
8887 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008888 if (!btrfs_transaction_cachep)
8889 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008890
David Sterba837e1972012-09-07 03:00:48 -06008891 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008892 sizeof(struct btrfs_path), 0,
8893 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008894 if (!btrfs_path_cachep)
8895 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008896
David Sterba837e1972012-09-07 03:00:48 -06008897 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008898 sizeof(struct btrfs_free_space), 0,
8899 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8900 if (!btrfs_free_space_cachep)
8901 goto fail;
8902
Miao Xie8ccf6f192012-10-25 09:28:04 +00008903 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8904 sizeof(struct btrfs_delalloc_work), 0,
8905 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8906 NULL);
8907 if (!btrfs_delalloc_work_cachep)
8908 goto fail;
8909
Chris Mason39279cc2007-06-12 06:35:45 -04008910 return 0;
8911fail:
8912 btrfs_destroy_cachep();
8913 return -ENOMEM;
8914}
8915
8916static int btrfs_getattr(struct vfsmount *mnt,
8917 struct dentry *dentry, struct kstat *stat)
8918{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008919 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008920 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008921 u32 blocksize = inode->i_sb->s_blocksize;
8922
Chris Mason39279cc2007-06-12 06:35:45 -04008923 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008924 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008925 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008926
8927 spin_lock(&BTRFS_I(inode)->lock);
8928 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8929 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008930 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008931 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008932 return 0;
8933}
8934
Chris Masond3977122009-01-05 21:25:51 -05008935static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8936 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008937{
8938 struct btrfs_trans_handle *trans;
8939 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008940 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008941 struct inode *new_inode = new_dentry->d_inode;
8942 struct inode *old_inode = old_dentry->d_inode;
8943 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008944 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008945 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008946 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008947 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008948
Li Zefan33345d012011-04-20 10:31:50 +08008949 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008950 return -EPERM;
8951
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008952 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008953 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008954 return -EXDEV;
8955
Li Zefan33345d012011-04-20 10:31:50 +08008956 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8957 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008958 return -ENOTEMPTY;
8959
Chris Mason39279cc2007-06-12 06:35:45 -04008960 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008961 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008962 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008963
8964
8965 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008966 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008967 new_dentry->d_name.name,
8968 new_dentry->d_name.len);
8969
8970 if (ret) {
8971 if (ret == -EEXIST) {
8972 /* we shouldn't get
8973 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308974 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008975 return ret;
8976 }
8977 } else {
8978 /* maybe -EOVERFLOW */
8979 return ret;
8980 }
8981 }
8982 ret = 0;
8983
Chris Mason5a3f23d2009-03-31 13:27:11 -04008984 /*
Chris Mason8d875f92014-08-12 10:47:42 -07008985 * we're using rename to replace one file with another. Start IO on it
8986 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04008987 */
Chris Mason8d875f92014-08-12 10:47:42 -07008988 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04008989 filemap_flush(old_inode->i_mapping);
8990
Yan, Zheng76dda932009-09-21 16:00:26 -04008991 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008992 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008993 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008994 /*
8995 * We want to reserve the absolute worst case amount of items. So if
8996 * both inodes are subvols and we need to unlink them then that would
8997 * require 4 item modifications, but if they are both normal inodes it
8998 * would require 5 item modifications, so we'll assume their normal
8999 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9000 * should cover the worst case number of items we'll modify.
9001 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04009002 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009003 if (IS_ERR(trans)) {
9004 ret = PTR_ERR(trans);
9005 goto out_notrans;
9006 }
Chris Mason5f39d392007-10-15 16:14:19 -04009007
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009008 if (dest != root)
9009 btrfs_record_root_in_trans(trans, dest);
9010
Yan, Zhenga5719522009-09-24 09:17:31 -04009011 ret = btrfs_set_inode_index(new_dir, &index);
9012 if (ret)
9013 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009014
Miao Xie67de1172013-12-26 13:07:06 +08009015 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009016 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009017 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009018 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009019 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009020 ret = btrfs_insert_inode_ref(trans, dest,
9021 new_dentry->d_name.name,
9022 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009023 old_ino,
9024 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009025 if (ret)
9026 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009027 /*
9028 * this is an ugly little race, but the rename is required
9029 * to make sure that if we crash, the inode is either at the
9030 * old name or the new one. pinning the log transaction lets
9031 * us make sure we don't allow a log commit to come in after
9032 * we unlink the name but before we add the new name back in.
9033 */
9034 btrfs_pin_log_trans(root);
9035 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009036
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009037 inode_inc_iversion(old_dir);
9038 inode_inc_iversion(new_dir);
9039 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009040 old_dir->i_ctime = old_dir->i_mtime = ctime;
9041 new_dir->i_ctime = new_dir->i_mtime = ctime;
9042 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04009043
Chris Mason12fcfd22009-03-24 10:24:20 -04009044 if (old_dentry->d_parent != new_dentry->d_parent)
9045 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9046
Li Zefan33345d012011-04-20 10:31:50 +08009047 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009048 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9049 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9050 old_dentry->d_name.name,
9051 old_dentry->d_name.len);
9052 } else {
Al Viro92986792011-03-04 17:14:37 +00009053 ret = __btrfs_unlink_inode(trans, root, old_dir,
9054 old_dentry->d_inode,
9055 old_dentry->d_name.name,
9056 old_dentry->d_name.len);
9057 if (!ret)
9058 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009059 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009060 if (ret) {
9061 btrfs_abort_transaction(trans, root, ret);
9062 goto out_fail;
9063 }
Chris Mason39279cc2007-06-12 06:35:45 -04009064
9065 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009066 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009067 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08009068 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009069 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9070 root_objectid = BTRFS_I(new_inode)->location.objectid;
9071 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9072 root_objectid,
9073 new_dentry->d_name.name,
9074 new_dentry->d_name.len);
9075 BUG_ON(new_inode->i_nlink == 0);
9076 } else {
9077 ret = btrfs_unlink_inode(trans, dest, new_dir,
9078 new_dentry->d_inode,
9079 new_dentry->d_name.name,
9080 new_dentry->d_name.len);
9081 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009082 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04009083 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009084 if (ret) {
9085 btrfs_abort_transaction(trans, root, ret);
9086 goto out_fail;
9087 }
Chris Mason39279cc2007-06-12 06:35:45 -04009088 }
Josef Bacikaec74772008-07-24 12:12:38 -04009089
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009090 ret = btrfs_add_link(trans, new_dir, old_inode,
9091 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009092 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009093 if (ret) {
9094 btrfs_abort_transaction(trans, root, ret);
9095 goto out_fail;
9096 }
Chris Mason39279cc2007-06-12 06:35:45 -04009097
Miao Xie67de1172013-12-26 13:07:06 +08009098 if (old_inode->i_nlink == 1)
9099 BTRFS_I(old_inode)->dir_index = index;
9100
Li Zefan33345d012011-04-20 10:31:50 +08009101 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04009102 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00009103 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009104 btrfs_end_log_trans(root);
9105 }
Chris Mason39279cc2007-06-12 06:35:45 -04009106out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009107 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009108out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009109 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009110 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009111
Chris Mason39279cc2007-06-12 06:35:45 -04009112 return ret;
9113}
9114
Miklos Szeredi80ace852014-07-23 15:15:32 +02009115static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9116 struct inode *new_dir, struct dentry *new_dentry,
9117 unsigned int flags)
9118{
9119 if (flags & ~RENAME_NOREPLACE)
9120 return -EINVAL;
9121
9122 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
9123}
9124
Miao Xie8ccf6f192012-10-25 09:28:04 +00009125static void btrfs_run_delalloc_work(struct btrfs_work *work)
9126{
9127 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009128 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009129
9130 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9131 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009132 inode = delalloc_work->inode;
9133 if (delalloc_work->wait) {
9134 btrfs_wait_ordered_range(inode, 0, (u64)-1);
9135 } else {
9136 filemap_flush(inode->i_mapping);
9137 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9138 &BTRFS_I(inode)->runtime_flags))
9139 filemap_flush(inode->i_mapping);
9140 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00009141
9142 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009143 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009144 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009145 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009146 complete(&delalloc_work->completion);
9147}
9148
9149struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
9150 int wait, int delay_iput)
9151{
9152 struct btrfs_delalloc_work *work;
9153
9154 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
9155 if (!work)
9156 return NULL;
9157
9158 init_completion(&work->completion);
9159 INIT_LIST_HEAD(&work->list);
9160 work->inode = inode;
9161 work->wait = wait;
9162 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009163 WARN_ON_ONCE(!inode);
9164 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9165 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009166
9167 return work;
9168}
9169
9170void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9171{
9172 wait_for_completion(&work->completion);
9173 kmem_cache_free(btrfs_delalloc_work_cachep, work);
9174}
9175
Chris Masond352ac62008-09-29 15:18:18 -04009176/*
9177 * some fairly slow code that needs optimization. This walks the list
9178 * of all the inodes with pending delalloc and forces them to disk.
9179 */
Miao Xie6c255e62014-03-06 13:55:01 +08009180static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9181 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009182{
Chris Masonea8c2812008-08-04 23:17:27 -04009183 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009184 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009185 struct btrfs_delalloc_work *work, *next;
9186 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009187 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009188 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009189
Miao Xie8ccf6f192012-10-25 09:28:04 +00009190 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009191 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009192
Miao Xie573bfb72014-03-06 13:55:03 +08009193 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009194 spin_lock(&root->delalloc_lock);
9195 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009196 while (!list_empty(&splice)) {
9197 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009198 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009199
Miao Xieeb73c1b2013-05-15 07:48:22 +00009200 list_move_tail(&binode->delalloc_inodes,
9201 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009202 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009203 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009204 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009205 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009206 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009207 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009208
9209 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009210 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009211 if (delay_iput)
9212 btrfs_add_delayed_iput(inode);
9213 else
9214 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009215 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009216 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009217 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009218 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009219 btrfs_queue_work(root->fs_info->flush_workers,
9220 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009221 ret++;
9222 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009223 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009224 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009225 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009226 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009227 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009228
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009229out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009230 list_for_each_entry_safe(work, next, &works, list) {
9231 list_del_init(&work->list);
9232 btrfs_wait_and_free_delalloc_work(work);
9233 }
9234
Miao Xieeb73c1b2013-05-15 07:48:22 +00009235 if (!list_empty_careful(&splice)) {
9236 spin_lock(&root->delalloc_lock);
9237 list_splice_tail(&splice, &root->delalloc_inodes);
9238 spin_unlock(&root->delalloc_lock);
9239 }
Miao Xie573bfb72014-03-06 13:55:03 +08009240 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009241 return ret;
9242}
9243
9244int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9245{
9246 int ret;
9247
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009248 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009249 return -EROFS;
9250
Miao Xie6c255e62014-03-06 13:55:01 +08009251 ret = __start_delalloc_inodes(root, delay_iput, -1);
9252 if (ret > 0)
9253 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009254 /*
9255 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009256 * we have to make sure the IO is actually started and that
9257 * ordered extents get created before we return
9258 */
9259 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009260 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009261 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009262 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009263 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9264 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009265 }
9266 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009267 return ret;
9268}
9269
Miao Xie6c255e62014-03-06 13:55:01 +08009270int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9271 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009272{
9273 struct btrfs_root *root;
9274 struct list_head splice;
9275 int ret;
9276
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009277 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009278 return -EROFS;
9279
9280 INIT_LIST_HEAD(&splice);
9281
Miao Xie573bfb72014-03-06 13:55:03 +08009282 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009283 spin_lock(&fs_info->delalloc_root_lock);
9284 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009285 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009286 root = list_first_entry(&splice, struct btrfs_root,
9287 delalloc_root);
9288 root = btrfs_grab_fs_root(root);
9289 BUG_ON(!root);
9290 list_move_tail(&root->delalloc_root,
9291 &fs_info->delalloc_roots);
9292 spin_unlock(&fs_info->delalloc_root_lock);
9293
Miao Xie6c255e62014-03-06 13:55:01 +08009294 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009295 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009296 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009297 goto out;
9298
Miao Xie6c255e62014-03-06 13:55:01 +08009299 if (nr != -1) {
9300 nr -= ret;
9301 WARN_ON(nr < 0);
9302 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009303 spin_lock(&fs_info->delalloc_root_lock);
9304 }
9305 spin_unlock(&fs_info->delalloc_root_lock);
9306
Miao Xie6c255e62014-03-06 13:55:01 +08009307 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009308 atomic_inc(&fs_info->async_submit_draining);
9309 while (atomic_read(&fs_info->nr_async_submits) ||
9310 atomic_read(&fs_info->async_delalloc_pages)) {
9311 wait_event(fs_info->async_submit_wait,
9312 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9313 atomic_read(&fs_info->async_delalloc_pages) == 0));
9314 }
9315 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009316out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009317 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009318 spin_lock(&fs_info->delalloc_root_lock);
9319 list_splice_tail(&splice, &fs_info->delalloc_roots);
9320 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009321 }
Miao Xie573bfb72014-03-06 13:55:03 +08009322 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009323 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009324}
9325
Chris Mason39279cc2007-06-12 06:35:45 -04009326static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9327 const char *symname)
9328{
9329 struct btrfs_trans_handle *trans;
9330 struct btrfs_root *root = BTRFS_I(dir)->root;
9331 struct btrfs_path *path;
9332 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009333 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009334 int err;
9335 int drop_inode = 0;
9336 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309337 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009338 int name_len;
9339 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009340 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009341 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009342 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009343
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009344 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009345 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9346 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009347
Josef Bacik9ed74f22009-09-11 16:12:44 -04009348 /*
9349 * 2 items for inode item and ref
9350 * 2 items for dir items
9351 * 1 item for xattr if selinux is on
9352 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009353 trans = btrfs_start_transaction(root, 5);
9354 if (IS_ERR(trans))
9355 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009356
Li Zefan581bb052011-04-20 10:06:11 +08009357 err = btrfs_find_free_ino(root, &objectid);
9358 if (err)
9359 goto out_unlock;
9360
Josef Bacikaec74772008-07-24 12:12:38 -04009361 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009362 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04009363 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009364 if (IS_ERR(inode)) {
9365 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009366 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009367 }
Chris Mason39279cc2007-06-12 06:35:45 -04009368
Casey Schauflerad19db72011-12-15 10:09:07 -05009369 /*
9370 * If the active LSM wants to access the inode during
9371 * d_instantiate it needs these. Smack checks to see
9372 * if the filesystem supports xattrs by looking at the
9373 * ops vector.
9374 */
9375 inode->i_fop = &btrfs_file_operations;
9376 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009377 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009378 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9379
9380 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9381 if (err)
9382 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009383
Josef Bacika1b075d2010-11-19 20:36:11 +00009384 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009385 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009386 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009387
9388 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009389 if (!path) {
9390 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009391 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009392 }
Li Zefan33345d012011-04-20 10:31:50 +08009393 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009394 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009395 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009396 datasize = btrfs_file_extent_calc_inline_size(name_len);
9397 err = btrfs_insert_empty_item(trans, root, path, &key,
9398 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009399 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009400 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009401 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009402 }
Chris Mason5f39d392007-10-15 16:14:19 -04009403 leaf = path->nodes[0];
9404 ei = btrfs_item_ptr(leaf, path->slots[0],
9405 struct btrfs_file_extent_item);
9406 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9407 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009408 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009409 btrfs_set_file_extent_encryption(leaf, ei, 0);
9410 btrfs_set_file_extent_compression(leaf, ei, 0);
9411 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9412 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9413
Chris Mason39279cc2007-06-12 06:35:45 -04009414 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009415 write_extent_buffer(leaf, symname, ptr, name_len);
9416 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009417 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009418
Chris Mason39279cc2007-06-12 06:35:45 -04009419 inode->i_op = &btrfs_symlink_inode_operations;
9420 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -04009421 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009422 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009423 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009424 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009425 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009426 goto out_unlock_inode;
9427 }
9428
9429 unlock_new_inode(inode);
9430 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009431
9432out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009433 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009434 if (drop_inode) {
9435 inode_dec_link_count(inode);
9436 iput(inode);
9437 }
Liu Bob53d3f52012-11-14 14:34:34 +00009438 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009439 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009440
9441out_unlock_inode:
9442 drop_inode = 1;
9443 unlock_new_inode(inode);
9444 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009445}
Chris Mason16432982008-04-10 10:23:21 -04009446
Josef Bacik0af3d002010-06-21 14:48:16 -04009447static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9448 u64 start, u64 num_bytes, u64 min_size,
9449 loff_t actual_len, u64 *alloc_hint,
9450 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009451{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009452 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9453 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009454 struct btrfs_root *root = BTRFS_I(inode)->root;
9455 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009456 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009457 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009458 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009459 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009460 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009461
Josef Bacik0af3d002010-06-21 14:48:16 -04009462 if (trans)
9463 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009464 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009465 if (own_trans) {
9466 trans = btrfs_start_transaction(root, 3);
9467 if (IS_ERR(trans)) {
9468 ret = PTR_ERR(trans);
9469 break;
9470 }
Yan Zhengd899e052008-10-30 14:25:28 -04009471 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009472
Chris Mason154ea282013-03-05 11:11:26 -05009473 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9474 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009475 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009476 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009477 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009478 if (own_trans)
9479 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009480 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009481 }
9482
Yan Zhengd899e052008-10-30 14:25:28 -04009483 ret = insert_reserved_file_extent(trans, inode,
9484 cur_offset, ins.objectid,
9485 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009486 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009487 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009488 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009489 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009490 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009491 btrfs_abort_transaction(trans, root, ret);
9492 if (own_trans)
9493 btrfs_end_transaction(trans, root);
9494 break;
9495 }
Chris Masona1ed8352009-09-11 12:27:37 -04009496 btrfs_drop_extent_cache(inode, cur_offset,
9497 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009498
Josef Bacik5dc562c2012-08-17 13:14:17 -04009499 em = alloc_extent_map();
9500 if (!em) {
9501 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9502 &BTRFS_I(inode)->runtime_flags);
9503 goto next;
9504 }
9505
9506 em->start = cur_offset;
9507 em->orig_start = cur_offset;
9508 em->len = ins.offset;
9509 em->block_start = ins.objectid;
9510 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009511 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009512 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009513 em->bdev = root->fs_info->fs_devices->latest_bdev;
9514 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9515 em->generation = trans->transid;
9516
9517 while (1) {
9518 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009519 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009520 write_unlock(&em_tree->lock);
9521 if (ret != -EEXIST)
9522 break;
9523 btrfs_drop_extent_cache(inode, cur_offset,
9524 cur_offset + ins.offset - 1,
9525 0);
9526 }
9527 free_extent_map(em);
9528next:
Yan Zhengd899e052008-10-30 14:25:28 -04009529 num_bytes -= ins.offset;
9530 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009531 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009532
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009533 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009534 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009535 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009536 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009537 (actual_len > inode->i_size) &&
9538 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009539 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009540 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009541 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009542 i_size = cur_offset;
9543 i_size_write(inode, i_size);
9544 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009545 }
9546
Yan Zhengd899e052008-10-30 14:25:28 -04009547 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009548
9549 if (ret) {
9550 btrfs_abort_transaction(trans, root, ret);
9551 if (own_trans)
9552 btrfs_end_transaction(trans, root);
9553 break;
9554 }
Yan Zhengd899e052008-10-30 14:25:28 -04009555
Josef Bacik0af3d002010-06-21 14:48:16 -04009556 if (own_trans)
9557 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009558 }
Yan Zhengd899e052008-10-30 14:25:28 -04009559 return ret;
9560}
9561
Josef Bacik0af3d002010-06-21 14:48:16 -04009562int btrfs_prealloc_file_range(struct inode *inode, int mode,
9563 u64 start, u64 num_bytes, u64 min_size,
9564 loff_t actual_len, u64 *alloc_hint)
9565{
9566 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9567 min_size, actual_len, alloc_hint,
9568 NULL);
9569}
9570
9571int btrfs_prealloc_file_range_trans(struct inode *inode,
9572 struct btrfs_trans_handle *trans, int mode,
9573 u64 start, u64 num_bytes, u64 min_size,
9574 loff_t actual_len, u64 *alloc_hint)
9575{
9576 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9577 min_size, actual_len, alloc_hint, trans);
9578}
9579
Chris Masone6dcd2d2008-07-17 12:53:50 -04009580static int btrfs_set_page_dirty(struct page *page)
9581{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009582 return __set_page_dirty_nobuffers(page);
9583}
9584
Al Viro10556cb2011-06-20 19:28:19 -04009585static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009586{
Li Zefanb83cc962010-12-20 16:04:08 +08009587 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009588 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009589
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009590 if (mask & MAY_WRITE &&
9591 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9592 if (btrfs_root_readonly(root))
9593 return -EROFS;
9594 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9595 return -EACCES;
9596 }
Al Viro2830ba72011-06-20 19:16:29 -04009597 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009598}
Chris Mason39279cc2007-06-12 06:35:45 -04009599
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009600static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9601{
9602 struct btrfs_trans_handle *trans;
9603 struct btrfs_root *root = BTRFS_I(dir)->root;
9604 struct inode *inode = NULL;
9605 u64 objectid;
9606 u64 index;
9607 int ret = 0;
9608
9609 /*
9610 * 5 units required for adding orphan entry
9611 */
9612 trans = btrfs_start_transaction(root, 5);
9613 if (IS_ERR(trans))
9614 return PTR_ERR(trans);
9615
9616 ret = btrfs_find_free_ino(root, &objectid);
9617 if (ret)
9618 goto out;
9619
9620 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9621 btrfs_ino(dir), objectid, mode, &index);
9622 if (IS_ERR(inode)) {
9623 ret = PTR_ERR(inode);
9624 inode = NULL;
9625 goto out;
9626 }
9627
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009628 inode->i_fop = &btrfs_file_operations;
9629 inode->i_op = &btrfs_file_inode_operations;
9630
9631 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009632 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9633
Chris Masonb0d5d102014-09-08 13:08:51 -07009634 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9635 if (ret)
9636 goto out_inode;
9637
9638 ret = btrfs_update_inode(trans, root, inode);
9639 if (ret)
9640 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009641 ret = btrfs_orphan_add(trans, inode);
9642 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009643 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009644
Filipe Manana5762b5c2014-08-01 00:10:32 +01009645 /*
9646 * We set number of links to 0 in btrfs_new_inode(), and here we set
9647 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9648 * through:
9649 *
9650 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9651 */
9652 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009653 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009654 d_tmpfile(dentry, inode);
9655 mark_inode_dirty(inode);
9656
9657out:
9658 btrfs_end_transaction(trans, root);
9659 if (ret)
9660 iput(inode);
9661 btrfs_balance_delayed_items(root);
9662 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009663 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009664
9665out_inode:
9666 unlock_new_inode(inode);
9667 goto out;
9668
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009669}
9670
Filipe Mananab38ef712014-11-13 17:01:45 +00009671/* Inspired by filemap_check_errors() */
9672int btrfs_inode_check_errors(struct inode *inode)
9673{
9674 int ret = 0;
9675
9676 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
9677 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
9678 ret = -ENOSPC;
9679 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
9680 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
9681 ret = -EIO;
9682
9683 return ret;
9684}
9685
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009686static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009687 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009688 .lookup = btrfs_lookup,
9689 .create = btrfs_create,
9690 .unlink = btrfs_unlink,
9691 .link = btrfs_link,
9692 .mkdir = btrfs_mkdir,
9693 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009694 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009695 .symlink = btrfs_symlink,
9696 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009697 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009698 .setxattr = btrfs_setxattr,
9699 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009700 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009701 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009702 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009703 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009704 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009705 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009706 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009707};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009708static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009709 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009710 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009711 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009712 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009713 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009714};
Yan, Zheng76dda932009-09-21 16:00:26 -04009715
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009716static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009717 .llseek = generic_file_llseek,
9718 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009719 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009720 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009721#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009722 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009723#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009724 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009725 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009726};
9727
Chris Masond1310b22008-01-24 16:13:08 -05009728static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009729 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009730 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009731 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009732 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009733 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009734 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009735 .set_bit_hook = btrfs_set_bit_hook,
9736 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009737 .merge_extent_hook = btrfs_merge_extent_hook,
9738 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009739};
9740
Chris Mason35054392009-01-21 13:11:13 -05009741/*
9742 * btrfs doesn't support the bmap operation because swapfiles
9743 * use bmap to make a mapping of extents in the file. They assume
9744 * these extents won't change over the life of the file and they
9745 * use the bmap result to do IO directly to the drive.
9746 *
9747 * the btrfs bmap call would return logical addresses that aren't
9748 * suitable for IO and they also will change frequently as COW
9749 * operations happen. So, swapfile + btrfs == corruption.
9750 *
9751 * For now we're avoiding this by dropping bmap.
9752 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009753static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009754 .readpage = btrfs_readpage,
9755 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04009756 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009757 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009758 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009759 .invalidatepage = btrfs_invalidatepage,
9760 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009761 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009762 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009763};
9764
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009765static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009766 .readpage = btrfs_readpage,
9767 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009768 .invalidatepage = btrfs_invalidatepage,
9769 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009770};
9771
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009772static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009773 .getattr = btrfs_getattr,
9774 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009775 .setxattr = btrfs_setxattr,
9776 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009777 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009778 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009779 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009780 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009781 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009782 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009783 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009784};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009785static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009786 .getattr = btrfs_getattr,
9787 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009788 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009789 .setxattr = btrfs_setxattr,
9790 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009791 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009792 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009793 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009794 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009795 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009796};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009797static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009798 .readlink = generic_readlink,
9799 .follow_link = page_follow_link_light,
9800 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009801 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009802 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009803 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009804 .setxattr = btrfs_setxattr,
9805 .getxattr = btrfs_getxattr,
9806 .listxattr = btrfs_listxattr,
9807 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009808 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009809};
Yan, Zheng76dda932009-09-21 16:00:26 -04009810
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009811const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009812 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009813 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009814};