blob: 6ef97c184c7b3742ae5e35cd57ae0849c6930830 [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 Masonf03d9302009-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 Masonf03d9302009-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, Zheng24bbcf02009-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, Zheng24bbcf02009-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
Zhao Leid7c15172015-02-26 10:49:20 +08003114 down_read(&fs_info->delayed_iput_sem);
3115
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003116 spin_lock(&fs_info->delayed_iput_lock);
3117 list_splice_init(&fs_info->delayed_iputs, &list);
3118 spin_unlock(&fs_info->delayed_iput_lock);
3119
3120 while (!list_empty(&list)) {
3121 delayed = list_entry(list.next, struct delayed_iput, list);
3122 list_del(&delayed->list);
3123 iput(delayed->inode);
3124 kfree(delayed);
3125 }
Zhao Leid7c15172015-02-26 10:49:20 +08003126
3127 up_read(&root->fs_info->delayed_iput_sem);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003128}
3129
Yan, Zhengd68fc572010-05-16 10:49:58 -04003130/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003131 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003132 * files in the subvolume, it removes orphan item and frees block_rsv
3133 * structure.
3134 */
3135void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3136 struct btrfs_root *root)
3137{
Josef Bacik90290e12011-12-02 15:44:12 -05003138 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003139 int ret;
3140
Josef Bacik8a35d952012-05-23 14:26:42 -04003141 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003142 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3143 return;
3144
Josef Bacik90290e12011-12-02 15:44:12 -05003145 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003146 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003147 spin_unlock(&root->orphan_lock);
3148 return;
3149 }
3150
3151 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3152 spin_unlock(&root->orphan_lock);
3153 return;
3154 }
3155
3156 block_rsv = root->orphan_block_rsv;
3157 root->orphan_block_rsv = NULL;
3158 spin_unlock(&root->orphan_lock);
3159
Miao Xie27cdeb72014-04-02 19:51:05 +08003160 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003161 btrfs_root_refs(&root->root_item) > 0) {
3162 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3163 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003164 if (ret)
3165 btrfs_abort_transaction(trans, root, ret);
3166 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003167 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3168 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003169 }
3170
Josef Bacik90290e12011-12-02 15:44:12 -05003171 if (block_rsv) {
3172 WARN_ON(block_rsv->size > 0);
3173 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003174 }
3175}
3176
3177/*
Josef Bacik7b128762008-07-24 12:17:14 -04003178 * This creates an orphan entry for the given inode in case something goes
3179 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003180 *
3181 * NOTE: caller of this function should reserve 5 units of metadata for
3182 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003183 */
3184int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3185{
3186 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003187 struct btrfs_block_rsv *block_rsv = NULL;
3188 int reserve = 0;
3189 int insert = 0;
3190 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003191
Yan, Zhengd68fc572010-05-16 10:49:58 -04003192 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003193 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003194 if (!block_rsv)
3195 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003196 }
3197
Yan, Zhengd68fc572010-05-16 10:49:58 -04003198 spin_lock(&root->orphan_lock);
3199 if (!root->orphan_block_rsv) {
3200 root->orphan_block_rsv = block_rsv;
3201 } else if (block_rsv) {
3202 btrfs_free_block_rsv(root, block_rsv);
3203 block_rsv = NULL;
3204 }
Josef Bacik7b128762008-07-24 12:17:14 -04003205
Josef Bacik8a35d952012-05-23 14:26:42 -04003206 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3207 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003208#if 0
3209 /*
3210 * For proper ENOSPC handling, we should do orphan
3211 * cleanup when mounting. But this introduces backward
3212 * compatibility issue.
3213 */
3214 if (!xchg(&root->orphan_item_inserted, 1))
3215 insert = 2;
3216 else
3217 insert = 1;
3218#endif
3219 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003220 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003221 }
Josef Bacik7b128762008-07-24 12:17:14 -04003222
Josef Bacik72ac3c02012-05-23 14:13:11 -04003223 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3224 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003225 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003226 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003227
Yan, Zhengd68fc572010-05-16 10:49:58 -04003228 /* grab metadata reservation from transaction handle */
3229 if (reserve) {
3230 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003231 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003232 }
3233
3234 /* insert an orphan item to track this unlinked/truncated file */
3235 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003236 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003237 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003238 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003239 if (reserve) {
3240 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3241 &BTRFS_I(inode)->runtime_flags);
3242 btrfs_orphan_release_metadata(inode);
3243 }
3244 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003245 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3246 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003247 btrfs_abort_transaction(trans, root, ret);
3248 return ret;
3249 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003250 }
3251 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003252 }
3253
3254 /* insert an orphan item to track subvolume contains orphan files */
3255 if (insert >= 2) {
3256 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3257 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003258 if (ret && ret != -EEXIST) {
3259 btrfs_abort_transaction(trans, root, ret);
3260 return ret;
3261 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003262 }
3263 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003264}
3265
3266/*
3267 * We have done the truncate/delete so we can go ahead and remove the orphan
3268 * item for this particular inode.
3269 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003270static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3271 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003272{
3273 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003274 int delete_item = 0;
3275 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003276 int ret = 0;
3277
Yan, Zhengd68fc572010-05-16 10:49:58 -04003278 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003279 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3280 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003281 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003282
Josef Bacik72ac3c02012-05-23 14:13:11 -04003283 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3284 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003285 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003286 spin_unlock(&root->orphan_lock);
3287
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003288 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003289 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003290 if (trans)
3291 ret = btrfs_del_orphan_item(trans, root,
3292 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003293 }
Josef Bacik7b128762008-07-24 12:17:14 -04003294
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003295 if (release_rsv)
3296 btrfs_orphan_release_metadata(inode);
3297
Josef Bacik4ef31a42013-08-13 14:10:08 -04003298 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003299}
3300
3301/*
3302 * this cleans up any orphans that may be left on the list from the last use
3303 * of this root.
3304 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003305int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003306{
3307 struct btrfs_path *path;
3308 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003309 struct btrfs_key key, found_key;
3310 struct btrfs_trans_handle *trans;
3311 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003312 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003313 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3314
Yan, Zhengd68fc572010-05-16 10:49:58 -04003315 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003316 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003317
3318 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003319 if (!path) {
3320 ret = -ENOMEM;
3321 goto out;
3322 }
Josef Bacik7b128762008-07-24 12:17:14 -04003323 path->reada = -1;
3324
3325 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003326 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003327 key.offset = (u64)-1;
3328
Josef Bacik7b128762008-07-24 12:17:14 -04003329 while (1) {
3330 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003331 if (ret < 0)
3332 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003333
3334 /*
3335 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003336 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003337 * find the key and see if we have stuff that matches
3338 */
3339 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003340 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003341 if (path->slots[0] == 0)
3342 break;
3343 path->slots[0]--;
3344 }
3345
3346 /* pull out the item */
3347 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003348 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3349
3350 /* make sure the item matches what we want */
3351 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3352 break;
David Sterba962a2982014-06-04 18:41:45 +02003353 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003354 break;
3355
3356 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003357 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003358
3359 /*
3360 * this is where we are basically btrfs_lookup, without the
3361 * crossing root thing. we store the inode number in the
3362 * offset of the orphan item.
3363 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003364
3365 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003366 btrfs_err(root->fs_info,
3367 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003368 ret = -EINVAL;
3369 goto out;
3370 }
3371
3372 last_objectid = found_key.offset;
3373
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003374 found_key.objectid = found_key.offset;
3375 found_key.type = BTRFS_INODE_ITEM_KEY;
3376 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003377 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303378 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003379 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003380 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003381
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003382 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3383 struct btrfs_root *dead_root;
3384 struct btrfs_fs_info *fs_info = root->fs_info;
3385 int is_dead_root = 0;
3386
3387 /*
3388 * this is an orphan in the tree root. Currently these
3389 * could come from 2 sources:
3390 * a) a snapshot deletion in progress
3391 * b) a free space cache inode
3392 * We need to distinguish those two, as the snapshot
3393 * orphan must not get deleted.
3394 * find_dead_roots already ran before us, so if this
3395 * is a snapshot deletion, we should find the root
3396 * in the dead_roots list
3397 */
3398 spin_lock(&fs_info->trans_lock);
3399 list_for_each_entry(dead_root, &fs_info->dead_roots,
3400 root_list) {
3401 if (dead_root->root_key.objectid ==
3402 found_key.objectid) {
3403 is_dead_root = 1;
3404 break;
3405 }
3406 }
3407 spin_unlock(&fs_info->trans_lock);
3408 if (is_dead_root) {
3409 /* prevent this orphan from being found again */
3410 key.offset = found_key.objectid - 1;
3411 continue;
3412 }
3413 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003414 /*
3415 * Inode is already gone but the orphan item is still there,
3416 * kill the orphan item.
3417 */
3418 if (ret == -ESTALE) {
3419 trans = btrfs_start_transaction(root, 1);
3420 if (IS_ERR(trans)) {
3421 ret = PTR_ERR(trans);
3422 goto out;
3423 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003424 btrfs_debug(root->fs_info, "auto deleting %Lu",
3425 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003426 ret = btrfs_del_orphan_item(trans, root,
3427 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003428 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003429 if (ret)
3430 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003431 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003432 }
Josef Bacik7b128762008-07-24 12:17:14 -04003433
Josef Bacik7b128762008-07-24 12:17:14 -04003434 /*
3435 * add this inode to the orphan list so btrfs_orphan_del does
3436 * the proper thing when we hit it
3437 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003438 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3439 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003440 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003441
Josef Bacik7b128762008-07-24 12:17:14 -04003442 /* if we have links, this was a truncate, lets do that */
3443 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303444 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003445 iput(inode);
3446 continue;
3447 }
Josef Bacik7b128762008-07-24 12:17:14 -04003448 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003449
3450 /* 1 for the orphan item deletion. */
3451 trans = btrfs_start_transaction(root, 1);
3452 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003453 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003454 ret = PTR_ERR(trans);
3455 goto out;
3456 }
3457 ret = btrfs_orphan_add(trans, inode);
3458 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003459 if (ret) {
3460 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003461 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003462 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003463
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003464 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003465 if (ret)
3466 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003467 } else {
3468 nr_unlink++;
3469 }
3470
3471 /* this will do delete_inode and everything for us */
3472 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003473 if (ret)
3474 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003475 }
Miao Xie3254c872011-11-10 20:45:05 -05003476 /* release the path since we're done with it */
3477 btrfs_release_path(path);
3478
Yan, Zhengd68fc572010-05-16 10:49:58 -04003479 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3480
3481 if (root->orphan_block_rsv)
3482 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3483 (u64)-1);
3484
Miao Xie27cdeb72014-04-02 19:51:05 +08003485 if (root->orphan_block_rsv ||
3486 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003487 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003488 if (!IS_ERR(trans))
3489 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003490 }
Josef Bacik7b128762008-07-24 12:17:14 -04003491
3492 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003493 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003494 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003495 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003496
3497out:
3498 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003499 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003500 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003501 btrfs_free_path(path);
3502 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003503}
3504
Chris Masond352ac62008-09-29 15:18:18 -04003505/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003506 * very simple check to peek ahead in the leaf looking for xattrs. If we
3507 * don't find any xattrs, we know there can't be any acls.
3508 *
3509 * slot is the slot the inode is in, objectid is the objectid of the inode
3510 */
3511static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003512 int slot, u64 objectid,
3513 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003514{
3515 u32 nritems = btrfs_header_nritems(leaf);
3516 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003517 static u64 xattr_access = 0;
3518 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003519 int scanned = 0;
3520
Josef Bacikf23b5a52013-06-19 10:16:26 -04003521 if (!xattr_access) {
3522 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3523 strlen(POSIX_ACL_XATTR_ACCESS));
3524 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3525 strlen(POSIX_ACL_XATTR_DEFAULT));
3526 }
3527
Chris Mason46a53cc2009-04-27 11:47:50 -04003528 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003529 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003530 while (slot < nritems) {
3531 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3532
3533 /* we found a different objectid, there must not be acls */
3534 if (found_key.objectid != objectid)
3535 return 0;
3536
3537 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003538 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003539 if (*first_xattr_slot == -1)
3540 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003541 if (found_key.offset == xattr_access ||
3542 found_key.offset == xattr_default)
3543 return 1;
3544 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003545
3546 /*
3547 * we found a key greater than an xattr key, there can't
3548 * be any acls later on
3549 */
3550 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3551 return 0;
3552
3553 slot++;
3554 scanned++;
3555
3556 /*
3557 * it goes inode, inode backrefs, xattrs, extents,
3558 * so if there are a ton of hard links to an inode there can
3559 * be a lot of backrefs. Don't waste time searching too hard,
3560 * this is just an optimization
3561 */
3562 if (scanned >= 8)
3563 break;
3564 }
3565 /* we hit the end of the leaf before we found an xattr or
3566 * something larger than an xattr. We have to assume the inode
3567 * has acls
3568 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003569 if (*first_xattr_slot == -1)
3570 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003571 return 1;
3572}
3573
3574/*
Chris Masond352ac62008-09-29 15:18:18 -04003575 * read an inode from the btree into the in-memory inode
3576 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003577static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003578{
3579 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003580 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003581 struct btrfs_inode_item *inode_item;
3582 struct btrfs_root *root = BTRFS_I(inode)->root;
3583 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003584 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003585 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003586 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003587 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003588 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003589 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003590
3591 ret = btrfs_fill_inode(inode, &rdev);
3592 if (!ret)
3593 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003594
3595 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003596 if (!path)
3597 goto make_bad;
3598
Chris Mason39279cc2007-06-12 06:35:45 -04003599 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003600
Chris Mason39279cc2007-06-12 06:35:45 -04003601 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003602 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003603 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003604
Chris Mason5f39d392007-10-15 16:14:19 -04003605 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003606
3607 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003608 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003609
Chris Mason5f39d392007-10-15 16:14:19 -04003610 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3611 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003612 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003613 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003614 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3615 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003616 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003617
David Sterbaa937b972014-12-12 17:39:12 +01003618 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3619 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003620
David Sterbaa937b972014-12-12 17:39:12 +01003621 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3622 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003623
David Sterbaa937b972014-12-12 17:39:12 +01003624 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3625 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003626
chandan r9cc97d62012-07-04 12:48:07 +05303627 BTRFS_I(inode)->i_otime.tv_sec =
3628 btrfs_timespec_sec(leaf, &inode_item->otime);
3629 BTRFS_I(inode)->i_otime.tv_nsec =
3630 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003631
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003632 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003633 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003634 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3635
3636 /*
3637 * If we were modified in the current generation and evicted from memory
3638 * and then re-read we need to do a full sync since we don't have any
3639 * idea about which extents were modified before we were evicted from
3640 * cache.
3641 */
3642 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3643 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3644 &BTRFS_I(inode)->runtime_flags);
3645
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003646 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003647 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003648 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003649 rdev = btrfs_inode_rdev(leaf, inode_item);
3650
Josef Bacikaec74772008-07-24 12:12:38 -04003651 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003652 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003653
3654cache_index:
3655 path->slots[0]++;
3656 if (inode->i_nlink != 1 ||
3657 path->slots[0] >= btrfs_header_nritems(leaf))
3658 goto cache_acl;
3659
3660 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3661 if (location.objectid != btrfs_ino(inode))
3662 goto cache_acl;
3663
3664 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3665 if (location.type == BTRFS_INODE_REF_KEY) {
3666 struct btrfs_inode_ref *ref;
3667
3668 ref = (struct btrfs_inode_ref *)ptr;
3669 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3670 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3671 struct btrfs_inode_extref *extref;
3672
3673 extref = (struct btrfs_inode_extref *)ptr;
3674 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3675 extref);
3676 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003677cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003678 /*
3679 * try to precache a NULL acl entry for files that don't have
3680 * any xattrs or acls
3681 */
Li Zefan33345d012011-04-20 10:31:50 +08003682 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003683 btrfs_ino(inode), &first_xattr_slot);
3684 if (first_xattr_slot != -1) {
3685 path->slots[0] = first_xattr_slot;
3686 ret = btrfs_load_inode_props(inode, path);
3687 if (ret)
3688 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003689 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003690 btrfs_ino(inode),
3691 root->root_key.objectid, ret);
3692 }
3693 btrfs_free_path(path);
3694
Al Viro72c04902009-06-24 16:58:48 -04003695 if (!maybe_acls)
3696 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003697
Chris Mason39279cc2007-06-12 06:35:45 -04003698 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003699 case S_IFREG:
3700 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003701 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003702 inode->i_fop = &btrfs_file_operations;
3703 inode->i_op = &btrfs_file_inode_operations;
3704 break;
3705 case S_IFDIR:
3706 inode->i_fop = &btrfs_dir_file_operations;
3707 if (root == root->fs_info->tree_root)
3708 inode->i_op = &btrfs_dir_ro_inode_operations;
3709 else
3710 inode->i_op = &btrfs_dir_inode_operations;
3711 break;
3712 case S_IFLNK:
3713 inode->i_op = &btrfs_symlink_inode_operations;
3714 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3715 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003716 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003717 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003718 init_special_inode(inode, inode->i_mode, rdev);
3719 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003720 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003721
3722 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003723 return;
3724
3725make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003726 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003727 make_bad_inode(inode);
3728}
3729
Chris Masond352ac62008-09-29 15:18:18 -04003730/*
3731 * given a leaf and an inode, copy the inode fields into the leaf
3732 */
Chris Masone02119d2008-09-05 16:13:11 -04003733static void fill_inode_item(struct btrfs_trans_handle *trans,
3734 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003735 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003736 struct inode *inode)
3737{
Liu Bo51fab692012-12-27 09:01:21 +00003738 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003739
Liu Bo51fab692012-12-27 09:01:21 +00003740 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003741
Liu Bo51fab692012-12-27 09:01:21 +00003742 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3743 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3744 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3745 &token);
3746 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3747 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003748
David Sterbaa937b972014-12-12 17:39:12 +01003749 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003750 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003751 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003752 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003753
David Sterbaa937b972014-12-12 17:39:12 +01003754 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003755 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003756 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003757 inode->i_mtime.tv_nsec, &token);
3758
David Sterbaa937b972014-12-12 17:39:12 +01003759 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003760 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003761 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003762 inode->i_ctime.tv_nsec, &token);
3763
chandan r9cc97d62012-07-04 12:48:07 +05303764 btrfs_set_token_timespec_sec(leaf, &item->otime,
3765 BTRFS_I(inode)->i_otime.tv_sec, &token);
3766 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3767 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3768
Liu Bo51fab692012-12-27 09:01:21 +00003769 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3770 &token);
3771 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3772 &token);
3773 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3774 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3775 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3776 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3777 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003778}
3779
Chris Masond352ac62008-09-29 15:18:18 -04003780/*
3781 * copy everything in the in-memory inode into the btree.
3782 */
Chris Mason21151332011-11-10 20:39:08 -05003783static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003784 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003785{
3786 struct btrfs_inode_item *inode_item;
3787 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003788 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003789 int ret;
3790
3791 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003792 if (!path)
3793 return -ENOMEM;
3794
Chris Masonb9473432009-03-13 11:00:37 -04003795 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003796 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3797 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003798 if (ret) {
3799 if (ret > 0)
3800 ret = -ENOENT;
3801 goto failed;
3802 }
3803
Chris Mason5f39d392007-10-15 16:14:19 -04003804 leaf = path->nodes[0];
3805 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003806 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003807
Chris Masone02119d2008-09-05 16:13:11 -04003808 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003809 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003810 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003811 ret = 0;
3812failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003813 btrfs_free_path(path);
3814 return ret;
3815}
3816
Chris Masond352ac62008-09-29 15:18:18 -04003817/*
Chris Mason21151332011-11-10 20:39:08 -05003818 * copy everything in the in-memory inode into the btree.
3819 */
3820noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3821 struct btrfs_root *root, struct inode *inode)
3822{
3823 int ret;
3824
3825 /*
3826 * If the inode is a free space inode, we can deadlock during commit
3827 * if we put it into the delayed code.
3828 *
3829 * The data relocation inode should also be directly updated
3830 * without delay
3831 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003832 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003833 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3834 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003835 btrfs_update_root_times(trans, root);
3836
Chris Mason21151332011-11-10 20:39:08 -05003837 ret = btrfs_delayed_update_inode(trans, root, inode);
3838 if (!ret)
3839 btrfs_set_inode_last_trans(trans, inode);
3840 return ret;
3841 }
3842
3843 return btrfs_update_inode_item(trans, root, inode);
3844}
3845
Josef Bacikbe6aef62012-10-22 15:43:12 -04003846noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3847 struct btrfs_root *root,
3848 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003849{
3850 int ret;
3851
3852 ret = btrfs_update_inode(trans, root, inode);
3853 if (ret == -ENOSPC)
3854 return btrfs_update_inode_item(trans, root, inode);
3855 return ret;
3856}
3857
3858/*
Chris Masond352ac62008-09-29 15:18:18 -04003859 * unlink helper that gets used here in inode.c and in the tree logging
3860 * recovery code. It remove a link in a directory with a given name, and
3861 * also drops the back refs in the inode to the directory
3862 */
Al Viro92986792011-03-04 17:14:37 +00003863static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3864 struct btrfs_root *root,
3865 struct inode *dir, struct inode *inode,
3866 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003867{
3868 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003869 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003870 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003871 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003872 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003873 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003874 u64 ino = btrfs_ino(inode);
3875 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003876
3877 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003878 if (!path) {
3879 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003880 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003881 }
3882
Chris Masonb9473432009-03-13 11:00:37 -04003883 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003884 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003885 name, name_len, -1);
3886 if (IS_ERR(di)) {
3887 ret = PTR_ERR(di);
3888 goto err;
3889 }
3890 if (!di) {
3891 ret = -ENOENT;
3892 goto err;
3893 }
Chris Mason5f39d392007-10-15 16:14:19 -04003894 leaf = path->nodes[0];
3895 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003896 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003897 if (ret)
3898 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003899 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003900
Miao Xie67de1172013-12-26 13:07:06 +08003901 /*
3902 * If we don't have dir index, we have to get it by looking up
3903 * the inode ref, since we get the inode ref, remove it directly,
3904 * it is unnecessary to do delayed deletion.
3905 *
3906 * But if we have dir index, needn't search inode ref to get it.
3907 * Since the inode ref is close to the inode item, it is better
3908 * that we delay to delete it, and just do this deletion when
3909 * we update the inode item.
3910 */
3911 if (BTRFS_I(inode)->dir_index) {
3912 ret = btrfs_delayed_delete_inode_ref(inode);
3913 if (!ret) {
3914 index = BTRFS_I(inode)->dir_index;
3915 goto skip_backref;
3916 }
3917 }
3918
Li Zefan33345d012011-04-20 10:31:50 +08003919 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3920 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003921 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003922 btrfs_info(root->fs_info,
3923 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003924 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003925 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003926 goto err;
3927 }
Miao Xie67de1172013-12-26 13:07:06 +08003928skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003929 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003930 if (ret) {
3931 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003932 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003933 }
Chris Mason39279cc2007-06-12 06:35:45 -04003934
Chris Masone02119d2008-09-05 16:13:11 -04003935 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003936 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003937 if (ret != 0 && ret != -ENOENT) {
3938 btrfs_abort_transaction(trans, root, ret);
3939 goto err;
3940 }
Chris Masone02119d2008-09-05 16:13:11 -04003941
3942 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3943 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003944 if (ret == -ENOENT)
3945 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003946 else if (ret)
3947 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003948err:
3949 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003950 if (ret)
3951 goto out;
3952
3953 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003954 inode_inc_iversion(inode);
3955 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003956 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003957 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003958out:
Chris Mason39279cc2007-06-12 06:35:45 -04003959 return ret;
3960}
3961
Al Viro92986792011-03-04 17:14:37 +00003962int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3963 struct btrfs_root *root,
3964 struct inode *dir, struct inode *inode,
3965 const char *name, int name_len)
3966{
3967 int ret;
3968 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3969 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003970 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003971 ret = btrfs_update_inode(trans, root, inode);
3972 }
3973 return ret;
3974}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003975
3976/*
3977 * helper to start transaction for unlink and rmdir.
3978 *
Josef Bacikd52be812013-05-29 14:54:47 -04003979 * unlink and rmdir are special in btrfs, they do not always free space, so
3980 * if we cannot make our reservations the normal way try and see if there is
3981 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3982 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003983 */
Josef Bacikd52be812013-05-29 14:54:47 -04003984static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003985{
3986 struct btrfs_trans_handle *trans;
3987 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003988 int ret;
3989
Josef Bacike70bea52011-10-11 14:18:24 -04003990 /*
3991 * 1 for the possible orphan item
3992 * 1 for the dir item
3993 * 1 for the dir index
3994 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003995 * 1 for the inode
3996 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003997 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003998 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3999 return trans;
4000
Josef Bacikd52be812013-05-29 14:54:47 -04004001 if (PTR_ERR(trans) == -ENOSPC) {
4002 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004003
Josef Bacikd52be812013-05-29 14:54:47 -04004004 trans = btrfs_start_transaction(root, 0);
4005 if (IS_ERR(trans))
4006 return trans;
4007 ret = btrfs_cond_migrate_bytes(root->fs_info,
4008 &root->fs_info->trans_block_rsv,
4009 num_bytes, 5);
4010 if (ret) {
4011 btrfs_end_transaction(trans, root);
4012 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004013 }
Josef Bacik5a77d762011-11-01 14:32:23 -04004014 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04004015 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004016 }
Josef Bacikd52be812013-05-29 14:54:47 -04004017 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004018}
4019
Chris Mason39279cc2007-06-12 06:35:45 -04004020static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4021{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004022 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004023 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04004024 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004025 int ret;
4026
Josef Bacikd52be812013-05-29 14:54:47 -04004027 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004028 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004029 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004030
Chris Mason12fcfd22009-03-24 10:24:20 -04004031 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
4032
Chris Masone02119d2008-09-05 16:13:11 -04004033 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4034 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004035 if (ret)
4036 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004037
Yan, Zhenga22285a2010-05-16 10:48:46 -04004038 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004039 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004040 if (ret)
4041 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004042 }
Josef Bacik7b128762008-07-24 12:17:14 -04004043
Tsutomu Itohb5324022011-07-19 07:27:20 +00004044out:
Josef Bacikd52be812013-05-29 14:54:47 -04004045 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004046 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004047 return ret;
4048}
4049
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004050int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4051 struct btrfs_root *root,
4052 struct inode *dir, u64 objectid,
4053 const char *name, int name_len)
4054{
4055 struct btrfs_path *path;
4056 struct extent_buffer *leaf;
4057 struct btrfs_dir_item *di;
4058 struct btrfs_key key;
4059 u64 index;
4060 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004061 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004062
4063 path = btrfs_alloc_path();
4064 if (!path)
4065 return -ENOMEM;
4066
Li Zefan33345d012011-04-20 10:31:50 +08004067 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004068 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004069 if (IS_ERR_OR_NULL(di)) {
4070 if (!di)
4071 ret = -ENOENT;
4072 else
4073 ret = PTR_ERR(di);
4074 goto out;
4075 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004076
4077 leaf = path->nodes[0];
4078 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4079 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4080 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004081 if (ret) {
4082 btrfs_abort_transaction(trans, root, ret);
4083 goto out;
4084 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004085 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004086
4087 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4088 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004089 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004090 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004091 if (ret != -ENOENT) {
4092 btrfs_abort_transaction(trans, root, ret);
4093 goto out;
4094 }
Li Zefan33345d012011-04-20 10:31:50 +08004095 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004096 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004097 if (IS_ERR_OR_NULL(di)) {
4098 if (!di)
4099 ret = -ENOENT;
4100 else
4101 ret = PTR_ERR(di);
4102 btrfs_abort_transaction(trans, root, ret);
4103 goto out;
4104 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004105
4106 leaf = path->nodes[0];
4107 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004108 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004109 index = key.offset;
4110 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004111 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004112
Miao Xie16cdcec2011-04-22 18:12:22 +08004113 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004114 if (ret) {
4115 btrfs_abort_transaction(trans, root, ret);
4116 goto out;
4117 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004118
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004119 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004120 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004121 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004122 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004123 if (ret)
4124 btrfs_abort_transaction(trans, root, ret);
4125out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004126 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004127 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004128}
4129
Chris Mason39279cc2007-06-12 06:35:45 -04004130static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4131{
4132 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004133 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004134 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004135 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004136
David Sterbab3ae2442012-09-13 16:04:34 -06004137 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004138 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004139 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4140 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004141
Josef Bacikd52be812013-05-29 14:54:47 -04004142 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004143 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004144 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004145
Li Zefan33345d012011-04-20 10:31:50 +08004146 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004147 err = btrfs_unlink_subvol(trans, root, dir,
4148 BTRFS_I(inode)->location.objectid,
4149 dentry->d_name.name,
4150 dentry->d_name.len);
4151 goto out;
4152 }
4153
Josef Bacik7b128762008-07-24 12:17:14 -04004154 err = btrfs_orphan_add(trans, inode);
4155 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004156 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004157
Chris Mason39279cc2007-06-12 06:35:45 -04004158 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004159 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4160 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004161 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004162 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004163out:
Josef Bacikd52be812013-05-29 14:54:47 -04004164 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004165 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004166
Chris Mason39279cc2007-06-12 06:35:45 -04004167 return err;
4168}
4169
Chris Mason28f75a02015-02-04 06:59:29 -08004170static int truncate_space_check(struct btrfs_trans_handle *trans,
4171 struct btrfs_root *root,
4172 u64 bytes_deleted)
4173{
4174 int ret;
4175
4176 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
4177 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4178 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4179 if (!ret)
4180 trans->bytes_reserved += bytes_deleted;
4181 return ret;
4182
4183}
4184
Chris Mason323ac952008-10-01 19:05:46 -04004185/*
Chris Mason39279cc2007-06-12 06:35:45 -04004186 * this can truncate away extent items, csum items and directory items.
4187 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004188 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004189 *
4190 * csum items that cross the new i_size are truncated to the new size
4191 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004192 *
4193 * min_type is the minimum key type to truncate down to. If set to 0, this
4194 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004195 */
Yan, Zheng80825102009-11-12 09:35:36 +00004196int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4197 struct btrfs_root *root,
4198 struct inode *inode,
4199 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004200{
Chris Mason39279cc2007-06-12 06:35:45 -04004201 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004202 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004203 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004204 struct btrfs_key key;
4205 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004206 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004207 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004208 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004209 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004210 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004211 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004212 int found_extent;
4213 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004214 int pending_del_nr = 0;
4215 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004216 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004217 int ret;
4218 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004219 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004220 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004221 bool be_nice = 0;
4222 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004223 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004224
4225 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004226
Chris Mason28ed1342014-12-17 09:41:04 -08004227 /*
4228 * for non-free space inodes and ref cows, we want to back off from
4229 * time to time
4230 */
4231 if (!btrfs_is_free_space_inode(inode) &&
4232 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4233 be_nice = 1;
4234
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004235 path = btrfs_alloc_path();
4236 if (!path)
4237 return -ENOMEM;
4238 path->reada = -1;
4239
Josef Bacik5dc562c2012-08-17 13:14:17 -04004240 /*
4241 * We want to drop from the next block forward in case this new size is
4242 * not block aligned since we will be keeping the last block of the
4243 * extent just the way it is.
4244 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004245 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4246 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004247 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4248 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004249
Miao Xie16cdcec2011-04-22 18:12:22 +08004250 /*
4251 * This function is also used to drop the items in the log tree before
4252 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4253 * it is used to drop the loged items. So we shouldn't kill the delayed
4254 * items.
4255 */
4256 if (min_type == 0 && root == BTRFS_I(inode)->root)
4257 btrfs_kill_delayed_inode_items(inode);
4258
Li Zefan33345d012011-04-20 10:31:50 +08004259 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004260 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004261 key.type = (u8)-1;
4262
Chris Mason85e21ba2008-01-29 15:11:36 -05004263search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004264 /*
4265 * with a 16K leaf size and 128MB extents, you can actually queue
4266 * up a huge file in a single leaf. Most of the time that
4267 * bytes_deleted is > 0, it will be huge by the time we get here
4268 */
4269 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
4270 if (btrfs_should_end_transaction(trans, root)) {
4271 err = -EAGAIN;
4272 goto error;
4273 }
4274 }
4275
4276
Chris Masonb9473432009-03-13 11:00:37 -04004277 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004278 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004279 if (ret < 0) {
4280 err = ret;
4281 goto out;
4282 }
Chris Masond3977122009-01-05 21:25:51 -05004283
Chris Mason85e21ba2008-01-29 15:11:36 -05004284 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004285 /* there are no items in the tree for us to truncate, we're
4286 * done
4287 */
Yan, Zheng80825102009-11-12 09:35:36 +00004288 if (path->slots[0] == 0)
4289 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004290 path->slots[0]--;
4291 }
4292
Chris Masond3977122009-01-05 21:25:51 -05004293 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004294 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004295 leaf = path->nodes[0];
4296 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004297 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004298
Li Zefan33345d012011-04-20 10:31:50 +08004299 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004300 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004301
Chris Mason85e21ba2008-01-29 15:11:36 -05004302 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004303 break;
4304
Chris Mason5f39d392007-10-15 16:14:19 -04004305 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004306 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004307 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004308 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004309 extent_type = btrfs_file_extent_type(leaf, fi);
4310 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004311 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004312 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004313 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004314 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004315 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004316 }
Yan008630c2007-11-07 13:31:09 -05004317 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004318 }
Yan, Zheng80825102009-11-12 09:35:36 +00004319 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004320 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004321 } else {
4322 if (item_end < new_size)
4323 break;
4324 if (found_key.offset >= new_size)
4325 del_item = 1;
4326 else
4327 del_item = 0;
4328 }
Chris Mason39279cc2007-06-12 06:35:45 -04004329 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004330 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004331 if (found_type != BTRFS_EXTENT_DATA_KEY)
4332 goto delete;
4333
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004334 if (del_item)
4335 last_size = found_key.offset;
4336 else
4337 last_size = new_size;
4338
Chris Mason179e29e2007-11-01 11:28:41 -04004339 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004340 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004341 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004342 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004343 u64 orig_num_bytes =
4344 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004345 extent_num_bytes = ALIGN(new_size -
4346 found_key.offset,
4347 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004348 btrfs_set_file_extent_num_bytes(leaf, fi,
4349 extent_num_bytes);
4350 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004351 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004352 if (test_bit(BTRFS_ROOT_REF_COWS,
4353 &root->state) &&
4354 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004355 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004356 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004357 } else {
Chris Masondb945352007-10-15 16:15:53 -04004358 extent_num_bytes =
4359 btrfs_file_extent_disk_num_bytes(leaf,
4360 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004361 extent_offset = found_key.offset -
4362 btrfs_file_extent_offset(leaf, fi);
4363
Chris Mason39279cc2007-06-12 06:35:45 -04004364 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004365 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004366 if (extent_start != 0) {
4367 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004368 if (test_bit(BTRFS_ROOT_REF_COWS,
4369 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004370 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004371 }
4372 }
Chris Mason90692182008-02-08 13:49:28 -05004373 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004374 /*
4375 * we can't truncate inline items that have had
4376 * special encodings
4377 */
4378 if (!del_item &&
4379 btrfs_file_extent_compression(leaf, fi) == 0 &&
4380 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4381 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004382 u32 size = new_size - found_key.offset;
4383
Miao Xie27cdeb72014-04-02 19:51:05 +08004384 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004385 inode_sub_bytes(inode, item_end + 1 -
4386 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004387
4388 /*
4389 * update the ram bytes to properly reflect
4390 * the new size of our item
4391 */
4392 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004393 size =
4394 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004395 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004396 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4397 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004398 inode_sub_bytes(inode, item_end + 1 -
4399 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004400 }
Chris Mason39279cc2007-06-12 06:35:45 -04004401 }
Chris Mason179e29e2007-11-01 11:28:41 -04004402delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004403 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004404 if (!pending_del_nr) {
4405 /* no pending yet, add ourselves */
4406 pending_del_slot = path->slots[0];
4407 pending_del_nr = 1;
4408 } else if (pending_del_nr &&
4409 path->slots[0] + 1 == pending_del_slot) {
4410 /* hop on the pending chunk */
4411 pending_del_nr++;
4412 pending_del_slot = path->slots[0];
4413 } else {
Chris Masond3977122009-01-05 21:25:51 -05004414 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004415 }
Chris Mason39279cc2007-06-12 06:35:45 -04004416 } else {
4417 break;
4418 }
Chris Mason28f75a02015-02-04 06:59:29 -08004419 should_throttle = 0;
4420
Miao Xie27cdeb72014-04-02 19:51:05 +08004421 if (found_extent &&
4422 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4423 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004424 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004425 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004426 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004427 extent_num_bytes, 0,
4428 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004429 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004430 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004431 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004432 btrfs_async_run_delayed_refs(root,
4433 trans->delayed_ref_updates * 2, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004434 if (be_nice) {
4435 if (truncate_space_check(trans, root,
4436 extent_num_bytes)) {
4437 should_end = 1;
4438 }
4439 if (btrfs_should_throttle_delayed_refs(trans,
4440 root)) {
4441 should_throttle = 1;
4442 }
4443 }
Chris Mason39279cc2007-06-12 06:35:45 -04004444 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004445
Yan, Zheng80825102009-11-12 09:35:36 +00004446 if (found_type == BTRFS_INODE_ITEM_KEY)
4447 break;
4448
4449 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004450 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004451 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004452 if (pending_del_nr) {
4453 ret = btrfs_del_items(trans, root, path,
4454 pending_del_slot,
4455 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004456 if (ret) {
4457 btrfs_abort_transaction(trans,
4458 root, ret);
4459 goto error;
4460 }
Yan, Zheng80825102009-11-12 09:35:36 +00004461 pending_del_nr = 0;
4462 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004463 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004464 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004465 unsigned long updates = trans->delayed_ref_updates;
4466 if (updates) {
4467 trans->delayed_ref_updates = 0;
4468 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4469 if (ret && !err)
4470 err = ret;
4471 }
4472 }
Chris Mason28f75a02015-02-04 06:59:29 -08004473 /*
4474 * if we failed to refill our space rsv, bail out
4475 * and let the transaction restart
4476 */
4477 if (should_end) {
4478 err = -EAGAIN;
4479 goto error;
4480 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004481 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004482 } else {
4483 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004484 }
Chris Mason39279cc2007-06-12 06:35:45 -04004485 }
Yan, Zheng80825102009-11-12 09:35:36 +00004486out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004487 if (pending_del_nr) {
4488 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4489 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004490 if (ret)
4491 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004492 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004493error:
Filipe Mananadac57052014-08-29 20:54:26 +01004494 if (last_size != (u64)-1 &&
4495 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004496 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004497
Chris Mason39279cc2007-06-12 06:35:45 -04004498 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004499
Chris Mason28f75a02015-02-04 06:59:29 -08004500 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
Chris Mason28ed1342014-12-17 09:41:04 -08004501 unsigned long updates = trans->delayed_ref_updates;
4502 if (updates) {
4503 trans->delayed_ref_updates = 0;
4504 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4505 if (ret && !err)
4506 err = ret;
4507 }
4508 }
Yan, Zheng80825102009-11-12 09:35:36 +00004509 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004510}
4511
Chris Masona52d9a82007-08-27 16:49:44 -04004512/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004513 * btrfs_truncate_page - read, zero a chunk and write a page
4514 * @inode - inode that we're zeroing
4515 * @from - the offset to start zeroing
4516 * @len - the length to zero, 0 to zero the entire range respective to the
4517 * offset
4518 * @front - zero up to the offset instead of from the offset on
4519 *
4520 * This will find the page for the "from" offset and cow the page and zero the
4521 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004522 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004523int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4524 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004525{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004526 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004527 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004528 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4529 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004530 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004531 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004532 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004533 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4534 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4535 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004536 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004537 int ret = 0;
4538 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004539 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004540
Josef Bacik2aaa6652012-08-29 14:27:18 -04004541 if ((offset & (blocksize - 1)) == 0 &&
4542 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004543 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004544 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004545 if (ret)
4546 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004547
Chris Mason211c17f2008-05-15 09:13:45 -04004548again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004549 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004550 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004551 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004552 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004553 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004554 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004555
4556 page_start = page_offset(page);
4557 page_end = page_start + PAGE_CACHE_SIZE - 1;
4558
Chris Masona52d9a82007-08-27 16:49:44 -04004559 if (!PageUptodate(page)) {
4560 ret = btrfs_readpage(NULL, page);
4561 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004562 if (page->mapping != mapping) {
4563 unlock_page(page);
4564 page_cache_release(page);
4565 goto again;
4566 }
Chris Masona52d9a82007-08-27 16:49:44 -04004567 if (!PageUptodate(page)) {
4568 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004569 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004570 }
4571 }
Chris Mason211c17f2008-05-15 09:13:45 -04004572 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004573
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004574 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004575 set_page_extent_mapped(page);
4576
4577 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4578 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004579 unlock_extent_cached(io_tree, page_start, page_end,
4580 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004581 unlock_page(page);
4582 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004583 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004584 btrfs_put_ordered_extent(ordered);
4585 goto again;
4586 }
4587
Josef Bacik2ac55d42010-02-03 19:33:23 +00004588 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004589 EXTENT_DIRTY | EXTENT_DELALLOC |
4590 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004591 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004592
Josef Bacik2ac55d42010-02-03 19:33:23 +00004593 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4594 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004595 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004596 unlock_extent_cached(io_tree, page_start, page_end,
4597 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004598 goto out_unlock;
4599 }
4600
Chris Masone6dcd2d2008-07-17 12:53:50 -04004601 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004602 if (!len)
4603 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004604 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004605 if (front)
4606 memset(kaddr, 0, offset);
4607 else
4608 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004609 flush_dcache_page(page);
4610 kunmap(page);
4611 }
Chris Mason247e7432008-07-17 12:53:51 -04004612 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004613 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004614 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4615 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004616
Chris Mason89642222008-07-24 09:41:53 -04004617out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004618 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004619 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004620 unlock_page(page);
4621 page_cache_release(page);
4622out:
4623 return ret;
4624}
4625
Josef Bacik16e75492013-10-22 12:18:51 -04004626static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4627 u64 offset, u64 len)
4628{
4629 struct btrfs_trans_handle *trans;
4630 int ret;
4631
4632 /*
4633 * Still need to make sure the inode looks like it's been updated so
4634 * that any holes get logged if we fsync.
4635 */
4636 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4637 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4638 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4639 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4640 return 0;
4641 }
4642
4643 /*
4644 * 1 - for the one we're dropping
4645 * 1 - for the one we're adding
4646 * 1 - for updating the inode.
4647 */
4648 trans = btrfs_start_transaction(root, 3);
4649 if (IS_ERR(trans))
4650 return PTR_ERR(trans);
4651
4652 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4653 if (ret) {
4654 btrfs_abort_transaction(trans, root, ret);
4655 btrfs_end_transaction(trans, root);
4656 return ret;
4657 }
4658
4659 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4660 0, 0, len, 0, len, 0, 0, 0);
4661 if (ret)
4662 btrfs_abort_transaction(trans, root, ret);
4663 else
4664 btrfs_update_inode(trans, root, inode);
4665 btrfs_end_transaction(trans, root);
4666 return ret;
4667}
4668
Josef Bacik695a0d02011-03-04 15:46:53 -05004669/*
4670 * This function puts in dummy file extents for the area we're creating a hole
4671 * for. So if we are truncating this file to a larger size we need to insert
4672 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4673 * the range between oldsize and size
4674 */
Josef Bacika41ad392011-01-31 15:30:16 -05004675int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004676{
Yan Zheng9036c102008-10-30 14:19:41 -04004677 struct btrfs_root *root = BTRFS_I(inode)->root;
4678 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004679 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004680 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004681 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004682 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4683 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004684 u64 last_byte;
4685 u64 cur_offset;
4686 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004687 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004688
Josef Bacika71754f2013-06-17 17:14:39 -04004689 /*
4690 * If our size started in the middle of a page we need to zero out the
4691 * rest of the page before we expand the i_size, otherwise we could
4692 * expose stale data.
4693 */
4694 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4695 if (err)
4696 return err;
4697
Yan Zheng9036c102008-10-30 14:19:41 -04004698 if (size <= hole_start)
4699 return 0;
4700
Yan Zheng9036c102008-10-30 14:19:41 -04004701 while (1) {
4702 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004703
Josef Bacik2ac55d42010-02-03 19:33:23 +00004704 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004705 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004706 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4707 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004708 if (!ordered)
4709 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004710 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4711 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004712 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004713 btrfs_put_ordered_extent(ordered);
4714 }
4715
Yan Zheng9036c102008-10-30 14:19:41 -04004716 cur_offset = hole_start;
4717 while (1) {
4718 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4719 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004720 if (IS_ERR(em)) {
4721 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004722 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004723 break;
4724 }
Yan Zheng9036c102008-10-30 14:19:41 -04004725 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004726 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004727 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004728 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004729 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004730
Josef Bacik16e75492013-10-22 12:18:51 -04004731 err = maybe_insert_hole(root, inode, cur_offset,
4732 hole_size);
4733 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004734 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004735 btrfs_drop_extent_cache(inode, cur_offset,
4736 cur_offset + hole_size - 1, 0);
4737 hole_em = alloc_extent_map();
4738 if (!hole_em) {
4739 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4740 &BTRFS_I(inode)->runtime_flags);
4741 goto next;
4742 }
4743 hole_em->start = cur_offset;
4744 hole_em->len = hole_size;
4745 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004746
Josef Bacik5dc562c2012-08-17 13:14:17 -04004747 hole_em->block_start = EXTENT_MAP_HOLE;
4748 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004749 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004750 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004751 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4752 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004753 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004754
4755 while (1) {
4756 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004757 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004758 write_unlock(&em_tree->lock);
4759 if (err != -EEXIST)
4760 break;
4761 btrfs_drop_extent_cache(inode, cur_offset,
4762 cur_offset +
4763 hole_size - 1, 0);
4764 }
4765 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004766 }
Josef Bacik16e75492013-10-22 12:18:51 -04004767next:
Yan Zheng9036c102008-10-30 14:19:41 -04004768 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004769 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004770 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004771 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004772 break;
4773 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004774 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004775 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4776 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004777 return err;
4778}
4779
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004780static int wait_snapshoting_atomic_t(atomic_t *a)
4781{
4782 schedule();
4783 return 0;
4784}
4785
4786static void wait_for_snapshot_creation(struct btrfs_root *root)
4787{
4788 while (true) {
4789 int ret;
4790
4791 ret = btrfs_start_write_no_snapshoting(root);
4792 if (ret)
4793 break;
4794 wait_on_atomic_t(&root->will_be_snapshoted,
4795 wait_snapshoting_atomic_t,
4796 TASK_UNINTERRUPTIBLE);
4797 }
4798}
4799
Eric Sandeen3972f262013-01-12 02:57:22 +00004800static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004801{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004802 struct btrfs_root *root = BTRFS_I(inode)->root;
4803 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004804 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004805 loff_t newsize = attr->ia_size;
4806 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004807 int ret;
4808
Eric Sandeen3972f262013-01-12 02:57:22 +00004809 /*
4810 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4811 * special case where we need to update the times despite not having
4812 * these flags set. For all other operations the VFS set these flags
4813 * explicitly if it wants a timestamp update.
4814 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004815 if (newsize != oldsize) {
4816 inode_inc_iversion(inode);
4817 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4818 inode->i_ctime = inode->i_mtime =
4819 current_fs_time(inode->i_sb);
4820 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004821
Josef Bacika41ad392011-01-31 15:30:16 -05004822 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004823 truncate_pagecache(inode, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004824 /*
4825 * Don't do an expanding truncate while snapshoting is ongoing.
4826 * This is to ensure the snapshot captures a fully consistent
4827 * state of this file - if the snapshot captures this expanding
4828 * truncation, it must capture all writes that happened before
4829 * this truncation.
4830 */
4831 wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004832 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004833 if (ret) {
4834 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004835 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004836 }
Yan, Zheng80825102009-11-12 09:35:36 +00004837
Miao Xief4a2f4c2011-12-14 20:12:01 -05004838 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004839 if (IS_ERR(trans)) {
4840 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004841 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004842 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004843
4844 i_size_write(inode, newsize);
4845 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4846 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004847 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004848 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004849 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004850
Josef Bacika41ad392011-01-31 15:30:16 -05004851 /*
4852 * We're truncating a file that used to have good data down to
4853 * zero. Make sure it gets into the ordered flush list so that
4854 * any new writes get down to disk quickly.
4855 */
4856 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004857 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4858 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004859
Josef Bacikf3fe8202013-01-07 17:03:21 -05004860 /*
4861 * 1 for the orphan item we're going to add
4862 * 1 for the orphan item deletion.
4863 */
4864 trans = btrfs_start_transaction(root, 2);
4865 if (IS_ERR(trans))
4866 return PTR_ERR(trans);
4867
4868 /*
4869 * We need to do this in case we fail at _any_ point during the
4870 * actual truncate. Once we do the truncate_setsize we could
4871 * invalidate pages which forces any outstanding ordered io to
4872 * be instantly completed which will give us extents that need
4873 * to be truncated. If we fail to get an orphan inode down we
4874 * could have left over extents that were never meant to live,
4875 * so we need to garuntee from this point on that everything
4876 * will be consistent.
4877 */
4878 ret = btrfs_orphan_add(trans, inode);
4879 btrfs_end_transaction(trans, root);
4880 if (ret)
4881 return ret;
4882
Josef Bacika41ad392011-01-31 15:30:16 -05004883 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4884 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004885
4886 /* Disable nonlocked read DIO to avoid the end less truncate */
4887 btrfs_inode_block_unlocked_dio(inode);
4888 inode_dio_wait(inode);
4889 btrfs_inode_resume_unlocked_dio(inode);
4890
Josef Bacika41ad392011-01-31 15:30:16 -05004891 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004892 if (ret && inode->i_nlink) {
4893 int err;
4894
4895 /*
4896 * failed to truncate, disk_i_size is only adjusted down
4897 * as we remove extents, so it should represent the true
4898 * size of the inode, so reset the in memory size and
4899 * delete our orphan entry.
4900 */
4901 trans = btrfs_join_transaction(root);
4902 if (IS_ERR(trans)) {
4903 btrfs_orphan_del(NULL, inode);
4904 return ret;
4905 }
4906 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4907 err = btrfs_orphan_del(trans, inode);
4908 if (err)
4909 btrfs_abort_transaction(trans, root, err);
4910 btrfs_end_transaction(trans, root);
4911 }
Yan, Zheng80825102009-11-12 09:35:36 +00004912 }
4913
Josef Bacika41ad392011-01-31 15:30:16 -05004914 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004915}
4916
Chris Mason39279cc2007-06-12 06:35:45 -04004917static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4918{
4919 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004920 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004921 int err;
4922
Li Zefanb83cc962010-12-20 16:04:08 +08004923 if (btrfs_root_readonly(root))
4924 return -EROFS;
4925
Chris Mason39279cc2007-06-12 06:35:45 -04004926 err = inode_change_ok(inode, attr);
4927 if (err)
4928 return err;
4929
Chris Mason5a3f23d2009-03-31 13:27:11 -04004930 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004931 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004932 if (err)
4933 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004934 }
Yan Zheng9036c102008-10-30 14:19:41 -04004935
Christoph Hellwig10257742010-06-04 11:30:02 +02004936 if (attr->ia_valid) {
4937 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004938 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004939 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004940
Josef Bacik22c44fe2011-11-30 10:45:38 -05004941 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004942 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004943 }
4944
Chris Mason39279cc2007-06-12 06:35:45 -04004945 return err;
4946}
Chris Mason61295eb2008-01-14 16:24:38 -05004947
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004948/*
4949 * While truncating the inode pages during eviction, we get the VFS calling
4950 * btrfs_invalidatepage() against each page of the inode. This is slow because
4951 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4952 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4953 * extent_state structures over and over, wasting lots of time.
4954 *
4955 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4956 * those expensive operations on a per page basis and do only the ordered io
4957 * finishing, while we release here the extent_map and extent_state structures,
4958 * without the excessive merging and splitting.
4959 */
4960static void evict_inode_truncate_pages(struct inode *inode)
4961{
4962 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4963 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4964 struct rb_node *node;
4965
4966 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004967 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004968
4969 write_lock(&map_tree->lock);
4970 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4971 struct extent_map *em;
4972
4973 node = rb_first(&map_tree->map);
4974 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004975 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4976 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004977 remove_extent_mapping(map_tree, em);
4978 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004979 if (need_resched()) {
4980 write_unlock(&map_tree->lock);
4981 cond_resched();
4982 write_lock(&map_tree->lock);
4983 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004984 }
4985 write_unlock(&map_tree->lock);
4986
4987 spin_lock(&io_tree->lock);
4988 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4989 struct extent_state *state;
4990 struct extent_state *cached_state = NULL;
4991
4992 node = rb_first(&io_tree->state);
4993 state = rb_entry(node, struct extent_state, rb_node);
4994 atomic_inc(&state->refs);
4995 spin_unlock(&io_tree->lock);
4996
4997 lock_extent_bits(io_tree, state->start, state->end,
4998 0, &cached_state);
4999 clear_extent_bit(io_tree, state->start, state->end,
5000 EXTENT_LOCKED | EXTENT_DIRTY |
5001 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5002 EXTENT_DEFRAG, 1, 1,
5003 &cached_state, GFP_NOFS);
5004 free_extent_state(state);
5005
Filipe Manana7064dd52014-08-08 02:47:05 +01005006 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005007 spin_lock(&io_tree->lock);
5008 }
5009 spin_unlock(&io_tree->lock);
5010}
5011
Al Virobd555972010-06-07 11:35:40 -04005012void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005013{
5014 struct btrfs_trans_handle *trans;
5015 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005016 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005017 int steal_from_global = 0;
Josef Bacik07127182011-08-19 10:29:59 -04005018 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04005019 int ret;
5020
liubo1abe9b82011-03-24 11:18:59 +00005021 trace_btrfs_inode_evict(inode);
5022
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005023 evict_inode_truncate_pages(inode);
5024
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005025 if (inode->i_nlink &&
5026 ((btrfs_root_refs(&root->root_item) != 0 &&
5027 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5028 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005029 goto no_delete;
5030
Chris Mason39279cc2007-06-12 06:35:45 -04005031 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005032 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005033 goto no_delete;
5034 }
Al Virobd555972010-06-07 11:35:40 -04005035 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04005036 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005037
Miao Xief6124962014-09-12 18:44:04 +08005038 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5039
Yan, Zhengc71bf092009-11-12 09:34:40 +00005040 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06005041 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005042 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005043 goto no_delete;
5044 }
5045
Yan, Zheng76dda932009-09-21 16:00:26 -04005046 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005047 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5048 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005049 goto no_delete;
5050 }
5051
Miao Xie0e8c36a2012-12-19 06:59:51 +00005052 ret = btrfs_commit_inode_delayed_inode(inode);
5053 if (ret) {
5054 btrfs_orphan_del(NULL, inode);
5055 goto no_delete;
5056 }
5057
Miao Xie66d8f3d2012-09-06 04:02:28 -06005058 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005059 if (!rsv) {
5060 btrfs_orphan_del(NULL, inode);
5061 goto no_delete;
5062 }
Josef Bacik4a338542011-08-29 11:01:31 -04005063 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005064 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005065 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005066
Chris Masondbe674a2008-07-17 12:54:05 -04005067 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005068
Josef Bacik4289a662011-08-05 13:22:24 -04005069 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005070 * This is a bit simpler than btrfs_truncate since we've already
5071 * reserved our space for our orphan item in the unlink, so we just
5072 * need to reserve some slack space in case we add bytes and update
5073 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005074 */
Yan, Zheng80825102009-11-12 09:35:36 +00005075 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005076 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5077 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005078
Josef Bacik726c35f2011-09-26 15:46:06 -04005079 /*
5080 * Try and steal from the global reserve since we will
5081 * likely not use this space anyway, we want to try as
5082 * hard as possible to get this to work.
5083 */
5084 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005085 steal_from_global++;
5086 else
5087 steal_from_global = 0;
5088 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005089
Josef Bacik3bce8762015-02-24 12:35:51 -08005090 /*
5091 * steal_from_global == 0: we reserved stuff, hooray!
5092 * steal_from_global == 1: we didn't reserve stuff, boo!
5093 * steal_from_global == 2: we've committed, still not a lot of
5094 * room but maybe we'll have room in the global reserve this
5095 * time.
5096 * steal_from_global == 3: abandon all hope!
5097 */
5098 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005099 btrfs_warn(root->fs_info,
5100 "Could not get space for a delete, will truncate on mount %d",
5101 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005102 btrfs_orphan_del(NULL, inode);
5103 btrfs_free_block_rsv(root, rsv);
5104 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005105 }
5106
Miao Xie0e8c36a2012-12-19 06:59:51 +00005107 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005108 if (IS_ERR(trans)) {
5109 btrfs_orphan_del(NULL, inode);
5110 btrfs_free_block_rsv(root, rsv);
5111 goto no_delete;
5112 }
5113
Josef Bacik3bce8762015-02-24 12:35:51 -08005114 /*
5115 * We can't just steal from the global reserve, we need tomake
5116 * sure there is room to do it, if not we need to commit and try
5117 * again.
5118 */
5119 if (steal_from_global) {
5120 if (!btrfs_check_space_for_delayed_refs(trans, root))
5121 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5122 min_size);
5123 else
5124 ret = -ENOSPC;
5125 }
5126
5127 /*
5128 * Couldn't steal from the global reserve, we have too much
5129 * pending stuff built up, commit the transaction and try it
5130 * again.
5131 */
5132 if (ret) {
5133 ret = btrfs_commit_transaction(trans, root);
5134 if (ret) {
5135 btrfs_orphan_del(NULL, inode);
5136 btrfs_free_block_rsv(root, rsv);
5137 goto no_delete;
5138 }
5139 continue;
5140 } else {
5141 steal_from_global = 0;
5142 }
5143
Josef Bacik4289a662011-08-05 13:22:24 -04005144 trans->block_rsv = rsv;
5145
Yan, Zhengd68fc572010-05-16 10:49:58 -04005146 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005147 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005148 break;
5149
Miao Xie8407aa42012-09-07 01:43:32 -06005150 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005151 btrfs_end_transaction(trans, root);
5152 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005153 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005154 }
5155
Josef Bacik4289a662011-08-05 13:22:24 -04005156 btrfs_free_block_rsv(root, rsv);
5157
Josef Bacik4ef31a42013-08-13 14:10:08 -04005158 /*
5159 * Errors here aren't a big deal, it just means we leave orphan items
5160 * in the tree. They will be cleaned up on the next mount.
5161 */
Yan, Zheng80825102009-11-12 09:35:36 +00005162 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005163 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005164 btrfs_orphan_del(trans, inode);
5165 } else {
5166 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005167 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005168
Josef Bacik4289a662011-08-05 13:22:24 -04005169 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005170 if (!(root == root->fs_info->tree_root ||
5171 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005172 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005173
Chris Mason54aa1f42007-06-22 14:16:25 -04005174 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005175 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005176no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005177 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005178 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005179 return;
Chris Mason39279cc2007-06-12 06:35:45 -04005180}
5181
5182/*
5183 * this returns the key found in the dir entry in the location pointer.
5184 * If no dir entries were found, location->objectid is 0.
5185 */
5186static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5187 struct btrfs_key *location)
5188{
5189 const char *name = dentry->d_name.name;
5190 int namelen = dentry->d_name.len;
5191 struct btrfs_dir_item *di;
5192 struct btrfs_path *path;
5193 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005194 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005195
5196 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005197 if (!path)
5198 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005199
Li Zefan33345d012011-04-20 10:31:50 +08005200 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005201 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005202 if (IS_ERR(di))
5203 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005204
David Sterbac7040052011-04-19 18:00:01 +02005205 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005206 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005207
Chris Mason5f39d392007-10-15 16:14:19 -04005208 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005209out:
Chris Mason39279cc2007-06-12 06:35:45 -04005210 btrfs_free_path(path);
5211 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005212out_err:
5213 location->objectid = 0;
5214 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005215}
5216
5217/*
5218 * when we hit a tree root in a directory, the btrfs part of the inode
5219 * needs to be changed to reflect the root directory of the tree root. This
5220 * is kind of like crossing a mount point.
5221 */
5222static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005223 struct inode *dir,
5224 struct dentry *dentry,
5225 struct btrfs_key *location,
5226 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005227{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005228 struct btrfs_path *path;
5229 struct btrfs_root *new_root;
5230 struct btrfs_root_ref *ref;
5231 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005232 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005233 int ret;
5234 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005235
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005236 path = btrfs_alloc_path();
5237 if (!path) {
5238 err = -ENOMEM;
5239 goto out;
5240 }
Chris Mason39279cc2007-06-12 06:35:45 -04005241
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005242 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005243 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5244 key.type = BTRFS_ROOT_REF_KEY;
5245 key.offset = location->objectid;
5246
5247 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5248 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005249 if (ret) {
5250 if (ret < 0)
5251 err = ret;
5252 goto out;
5253 }
Chris Mason39279cc2007-06-12 06:35:45 -04005254
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005255 leaf = path->nodes[0];
5256 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005257 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005258 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5259 goto out;
5260
5261 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5262 (unsigned long)(ref + 1),
5263 dentry->d_name.len);
5264 if (ret)
5265 goto out;
5266
David Sterbab3b4aa72011-04-21 01:20:15 +02005267 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005268
5269 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5270 if (IS_ERR(new_root)) {
5271 err = PTR_ERR(new_root);
5272 goto out;
5273 }
5274
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005275 *sub_root = new_root;
5276 location->objectid = btrfs_root_dirid(&new_root->root_item);
5277 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005278 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005279 err = 0;
5280out:
5281 btrfs_free_path(path);
5282 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005283}
5284
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005285static void inode_tree_add(struct inode *inode)
5286{
5287 struct btrfs_root *root = BTRFS_I(inode)->root;
5288 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005289 struct rb_node **p;
5290 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005291 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005292 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005293
Al Viro1d3382cb2010-10-23 15:19:20 -04005294 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005295 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005296 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005297 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005298 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005299 while (*p) {
5300 parent = *p;
5301 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5302
Li Zefan33345d012011-04-20 10:31:50 +08005303 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005304 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005305 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005306 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005307 else {
5308 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005309 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005310 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005311 RB_CLEAR_NODE(parent);
5312 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005313 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005314 }
5315 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005316 rb_link_node(new, parent, p);
5317 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005318 spin_unlock(&root->inode_lock);
5319}
5320
5321static void inode_tree_del(struct inode *inode)
5322{
5323 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005324 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005325
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005326 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005327 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005328 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005329 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005330 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005331 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005332 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005333
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005334 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005335 synchronize_srcu(&root->fs_info->subvol_srcu);
5336 spin_lock(&root->inode_lock);
5337 empty = RB_EMPTY_ROOT(&root->inode_tree);
5338 spin_unlock(&root->inode_lock);
5339 if (empty)
5340 btrfs_add_dead_root(root);
5341 }
5342}
5343
Jeff Mahoney143bede2012-03-01 14:56:26 +01005344void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005345{
5346 struct rb_node *node;
5347 struct rb_node *prev;
5348 struct btrfs_inode *entry;
5349 struct inode *inode;
5350 u64 objectid = 0;
5351
Liu Bo7813b3d2014-02-10 17:37:25 +08005352 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5353 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005354
5355 spin_lock(&root->inode_lock);
5356again:
5357 node = root->inode_tree.rb_node;
5358 prev = NULL;
5359 while (node) {
5360 prev = node;
5361 entry = rb_entry(node, struct btrfs_inode, rb_node);
5362
Li Zefan33345d012011-04-20 10:31:50 +08005363 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005364 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005365 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005366 node = node->rb_right;
5367 else
5368 break;
5369 }
5370 if (!node) {
5371 while (prev) {
5372 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005373 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005374 node = prev;
5375 break;
5376 }
5377 prev = rb_next(prev);
5378 }
5379 }
5380 while (node) {
5381 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005382 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005383 inode = igrab(&entry->vfs_inode);
5384 if (inode) {
5385 spin_unlock(&root->inode_lock);
5386 if (atomic_read(&inode->i_count) > 1)
5387 d_prune_aliases(inode);
5388 /*
Al Viro45321ac2010-06-07 13:43:19 -04005389 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005390 * the inode cache when its usage count
5391 * hits zero.
5392 */
5393 iput(inode);
5394 cond_resched();
5395 spin_lock(&root->inode_lock);
5396 goto again;
5397 }
5398
5399 if (cond_resched_lock(&root->inode_lock))
5400 goto again;
5401
5402 node = rb_next(node);
5403 }
5404 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005405}
5406
Chris Masone02119d2008-09-05 16:13:11 -04005407static int btrfs_init_locked_inode(struct inode *inode, void *p)
5408{
5409 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005410 inode->i_ino = args->location->objectid;
5411 memcpy(&BTRFS_I(inode)->location, args->location,
5412 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005413 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005414 return 0;
5415}
5416
5417static int btrfs_find_actor(struct inode *inode, void *opaque)
5418{
5419 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005420 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005421 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005422}
5423
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005424static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005425 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005426 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005427{
5428 struct inode *inode;
5429 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005430 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005431
Chris Mason90d3e592014-01-09 17:28:00 -08005432 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005433 args.root = root;
5434
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005435 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005436 btrfs_init_locked_inode,
5437 (void *)&args);
5438 return inode;
5439}
5440
Balaji Rao1a54ef82008-07-21 02:01:04 +05305441/* Get an inode object given its location and corresponding root.
5442 * Returns in *is_new if the inode was read from disk
5443 */
5444struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005445 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305446{
5447 struct inode *inode;
5448
Chris Mason90d3e592014-01-09 17:28:00 -08005449 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305450 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005451 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305452
5453 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305454 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005455 if (!is_bad_inode(inode)) {
5456 inode_tree_add(inode);
5457 unlock_new_inode(inode);
5458 if (new)
5459 *new = 1;
5460 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005461 unlock_new_inode(inode);
5462 iput(inode);
5463 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005464 }
5465 }
5466
Balaji Rao1a54ef82008-07-21 02:01:04 +05305467 return inode;
5468}
5469
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005470static struct inode *new_simple_dir(struct super_block *s,
5471 struct btrfs_key *key,
5472 struct btrfs_root *root)
5473{
5474 struct inode *inode = new_inode(s);
5475
5476 if (!inode)
5477 return ERR_PTR(-ENOMEM);
5478
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005479 BTRFS_I(inode)->root = root;
5480 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005481 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005482
5483 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005484 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005485 inode->i_fop = &simple_dir_operations;
5486 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
chandan r9cc97d62012-07-04 12:48:07 +05305487 inode->i_mtime = CURRENT_TIME;
5488 inode->i_atime = inode->i_mtime;
5489 inode->i_ctime = inode->i_mtime;
5490 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005491
5492 return inode;
5493}
5494
Chris Mason3de45862008-11-17 21:02:50 -05005495struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005496{
Chris Masond3977122009-01-05 21:25:51 -05005497 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005498 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005499 struct btrfs_root *sub_root = root;
5500 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005501 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005502 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005503
5504 if (dentry->d_name.len > BTRFS_NAME_LEN)
5505 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005506
Jeff Layton39e3c952012-11-28 11:30:53 -05005507 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005508 if (ret < 0)
5509 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005510
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005511 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005512 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005513
5514 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005515 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005516 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005517 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005518
5519 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5520
Yan, Zheng76dda932009-09-21 16:00:26 -04005521 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005522 ret = fixup_tree_root_location(root, dir, dentry,
5523 &location, &sub_root);
5524 if (ret < 0) {
5525 if (ret != -ENOENT)
5526 inode = ERR_PTR(ret);
5527 else
5528 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5529 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005530 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005531 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005532 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5533
Julia Lawall34d19ba2011-01-24 19:55:19 +00005534 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005535 down_read(&root->fs_info->cleanup_work_sem);
5536 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005537 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005538 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005539 if (ret) {
5540 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005541 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005542 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005543 }
5544
Chris Mason3de45862008-11-17 21:02:50 -05005545 return inode;
5546}
5547
Nick Pigginfe15ce42011-01-07 17:49:23 +11005548static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005549{
5550 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005551 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005552
Li Zefan848cce02012-02-21 17:04:28 +08005553 if (!inode && !IS_ROOT(dentry))
5554 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005555
Li Zefan848cce02012-02-21 17:04:28 +08005556 if (inode) {
5557 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005558 if (btrfs_root_refs(&root->root_item) == 0)
5559 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005560
5561 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5562 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005563 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005564 return 0;
5565}
5566
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005567static void btrfs_dentry_release(struct dentry *dentry)
5568{
Daeseok Youn944a4512014-04-14 15:37:02 +09005569 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005570}
5571
Chris Mason3de45862008-11-17 21:02:50 -05005572static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005573 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005574{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005575 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005576
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005577 inode = btrfs_lookup_dentry(dir, dentry);
5578 if (IS_ERR(inode)) {
5579 if (PTR_ERR(inode) == -ENOENT)
5580 inode = NULL;
5581 else
5582 return ERR_CAST(inode);
5583 }
5584
Al Viro41d28bc2014-10-12 22:24:21 -04005585 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005586}
5587
Miao Xie16cdcec2011-04-22 18:12:22 +08005588unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005589 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5590};
5591
Al Viro9cdda8d2013-05-22 16:48:09 -04005592static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005593{
Al Viro9cdda8d2013-05-22 16:48:09 -04005594 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005595 struct btrfs_root *root = BTRFS_I(inode)->root;
5596 struct btrfs_item *item;
5597 struct btrfs_dir_item *di;
5598 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005599 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005600 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005601 struct list_head ins_list;
5602 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005603 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005604 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005605 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005606 unsigned char d_type;
5607 int over = 0;
5608 u32 di_cur;
5609 u32 di_total;
5610 u32 di_len;
5611 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005612 char tmp_name[32];
5613 char *name_ptr;
5614 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005615 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005616
5617 /* FIXME, use a real flag for deciding about the key type */
5618 if (root->fs_info->tree_root == root)
5619 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005620
Al Viro9cdda8d2013-05-22 16:48:09 -04005621 if (!dir_emit_dots(file, ctx))
5622 return 0;
5623
David Woodhouse49593bf2008-08-17 17:08:36 +01005624 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005625 if (!path)
5626 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005627
Josef Bacik026fd312011-05-13 10:32:11 -04005628 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005629
Miao Xie16cdcec2011-04-22 18:12:22 +08005630 if (key_type == BTRFS_DIR_INDEX_KEY) {
5631 INIT_LIST_HEAD(&ins_list);
5632 INIT_LIST_HEAD(&del_list);
5633 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5634 }
5635
David Sterba962a2982014-06-04 18:41:45 +02005636 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005637 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005638 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005639
Chris Mason39279cc2007-06-12 06:35:45 -04005640 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5641 if (ret < 0)
5642 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005643
5644 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005645 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005646 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005647 if (slot >= btrfs_header_nritems(leaf)) {
5648 ret = btrfs_next_leaf(root, path);
5649 if (ret < 0)
5650 goto err;
5651 else if (ret > 0)
5652 break;
5653 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005654 }
Chris Mason3de45862008-11-17 21:02:50 -05005655
Ross Kirkdd3cc162013-09-16 15:58:09 +01005656 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005657 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5658
5659 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005660 break;
David Sterba962a2982014-06-04 18:41:45 +02005661 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005662 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005663 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005664 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005665 if (key_type == BTRFS_DIR_INDEX_KEY &&
5666 btrfs_should_delete_dir_index(&del_list,
5667 found_key.offset))
5668 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005669
Al Viro9cdda8d2013-05-22 16:48:09 -04005670 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005671 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005672
Chris Mason39279cc2007-06-12 06:35:45 -04005673 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5674 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005675 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005676
5677 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005678 struct btrfs_key location;
5679
Josef Bacik22a94d42011-03-16 16:47:17 -04005680 if (verify_dir_item(root, leaf, di))
5681 break;
5682
Chris Mason5f39d392007-10-15 16:14:19 -04005683 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005684 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005685 name_ptr = tmp_name;
5686 } else {
5687 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005688 if (!name_ptr) {
5689 ret = -ENOMEM;
5690 goto err;
5691 }
Chris Mason5f39d392007-10-15 16:14:19 -04005692 }
5693 read_extent_buffer(leaf, name_ptr,
5694 (unsigned long)(di + 1), name_len);
5695
5696 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5697 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005698
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005699
Chris Mason3de45862008-11-17 21:02:50 -05005700 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005701 * skip it.
5702 *
5703 * In contrast to old kernels, we insert the snapshot's
5704 * dir item and dir index after it has been created, so
5705 * we won't find a reference to our own snapshot. We
5706 * still keep the following code for backward
5707 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005708 */
5709 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5710 location.objectid == root->root_key.objectid) {
5711 over = 0;
5712 goto skip;
5713 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005714 over = !dir_emit(ctx, name_ptr, name_len,
5715 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005716
Chris Mason3de45862008-11-17 21:02:50 -05005717skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005718 if (name_ptr != tmp_name)
5719 kfree(name_ptr);
5720
Chris Mason39279cc2007-06-12 06:35:45 -04005721 if (over)
5722 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005723 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005724 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005725 di_cur += di_len;
5726 di = (struct btrfs_dir_item *)((char *)di + di_len);
5727 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005728next:
5729 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005730 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005731
Miao Xie16cdcec2011-04-22 18:12:22 +08005732 if (key_type == BTRFS_DIR_INDEX_KEY) {
5733 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005734 ctx->pos++;
5735 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005736 if (ret)
5737 goto nopos;
5738 }
5739
David Woodhouse49593bf2008-08-17 17:08:36 +01005740 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005741 ctx->pos++;
5742
5743 /*
5744 * Stop new entries from being returned after we return the last
5745 * entry.
5746 *
5747 * New directory entries are assigned a strictly increasing
5748 * offset. This means that new entries created during readdir
5749 * are *guaranteed* to be seen in the future by that readdir.
5750 * This has broken buggy programs which operate on names as
5751 * they're returned by readdir. Until we re-use freed offsets
5752 * we have this hack to stop new entries from being returned
5753 * under the assumption that they'll never reach this huge
5754 * offset.
5755 *
5756 * This is being careful not to overflow 32bit loff_t unless the
5757 * last entry requires it because doing so has broken 32bit apps
5758 * in the past.
5759 */
5760 if (key_type == BTRFS_DIR_INDEX_KEY) {
5761 if (ctx->pos >= INT_MAX)
5762 ctx->pos = LLONG_MAX;
5763 else
5764 ctx->pos = INT_MAX;
5765 }
Chris Mason39279cc2007-06-12 06:35:45 -04005766nopos:
5767 ret = 0;
5768err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005769 if (key_type == BTRFS_DIR_INDEX_KEY)
5770 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005771 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005772 return ret;
5773}
5774
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005775int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005776{
5777 struct btrfs_root *root = BTRFS_I(inode)->root;
5778 struct btrfs_trans_handle *trans;
5779 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005780 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005781
Josef Bacik72ac3c02012-05-23 14:13:11 -04005782 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005783 return 0;
5784
Liu Bo83eea1f2012-07-10 05:28:39 -06005785 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005786 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005787
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005788 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005789 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005790 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005791 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005792 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005793 if (IS_ERR(trans))
5794 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005795 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005796 }
5797 return ret;
5798}
5799
5800/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005801 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005802 * inode changes. But, it is most likely to find the inode in cache.
5803 * FIXME, needs more benchmarking...there are no reasons other than performance
5804 * to keep or drop this code.
5805 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005806static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005807{
5808 struct btrfs_root *root = BTRFS_I(inode)->root;
5809 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005810 int ret;
5811
Josef Bacik72ac3c02012-05-23 14:13:11 -04005812 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005813 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005814
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005815 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005816 if (IS_ERR(trans))
5817 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005818
5819 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005820 if (ret && ret == -ENOSPC) {
5821 /* whoops, lets try again with the full transaction */
5822 btrfs_end_transaction(trans, root);
5823 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005824 if (IS_ERR(trans))
5825 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005826
Chris Mason94b60442010-05-26 11:02:00 -04005827 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005828 }
Chris Mason39279cc2007-06-12 06:35:45 -04005829 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005830 if (BTRFS_I(inode)->delayed_node)
5831 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005832
5833 return ret;
5834}
5835
5836/*
5837 * This is a copy of file_update_time. We need this so we can return error on
5838 * ENOSPC for updating the inode in the case of file write and mmap writes.
5839 */
Josef Bacike41f9412012-03-26 09:46:47 -04005840static int btrfs_update_time(struct inode *inode, struct timespec *now,
5841 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005842{
Alexander Block2bc5565282012-06-15 09:49:33 +02005843 struct btrfs_root *root = BTRFS_I(inode)->root;
5844
5845 if (btrfs_root_readonly(root))
5846 return -EROFS;
5847
Josef Bacike41f9412012-03-26 09:46:47 -04005848 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005849 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005850 if (flags & S_CTIME)
5851 inode->i_ctime = *now;
5852 if (flags & S_MTIME)
5853 inode->i_mtime = *now;
5854 if (flags & S_ATIME)
5855 inode->i_atime = *now;
5856 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005857}
5858
Chris Masond352ac62008-09-29 15:18:18 -04005859/*
5860 * find the highest existing sequence number in a directory
5861 * and then set the in-memory index_cnt variable to reflect
5862 * free sequence numbers
5863 */
Josef Bacikaec74772008-07-24 12:12:38 -04005864static int btrfs_set_inode_index_count(struct inode *inode)
5865{
5866 struct btrfs_root *root = BTRFS_I(inode)->root;
5867 struct btrfs_key key, found_key;
5868 struct btrfs_path *path;
5869 struct extent_buffer *leaf;
5870 int ret;
5871
Li Zefan33345d012011-04-20 10:31:50 +08005872 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005873 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005874 key.offset = (u64)-1;
5875
5876 path = btrfs_alloc_path();
5877 if (!path)
5878 return -ENOMEM;
5879
5880 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5881 if (ret < 0)
5882 goto out;
5883 /* FIXME: we should be able to handle this */
5884 if (ret == 0)
5885 goto out;
5886 ret = 0;
5887
5888 /*
5889 * MAGIC NUMBER EXPLANATION:
5890 * since we search a directory based on f_pos we have to start at 2
5891 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5892 * else has to start at 2
5893 */
5894 if (path->slots[0] == 0) {
5895 BTRFS_I(inode)->index_cnt = 2;
5896 goto out;
5897 }
5898
5899 path->slots[0]--;
5900
5901 leaf = path->nodes[0];
5902 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5903
Li Zefan33345d012011-04-20 10:31:50 +08005904 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005905 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005906 BTRFS_I(inode)->index_cnt = 2;
5907 goto out;
5908 }
5909
5910 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5911out:
5912 btrfs_free_path(path);
5913 return ret;
5914}
5915
Chris Masond352ac62008-09-29 15:18:18 -04005916/*
5917 * helper to find a free sequence number in a given directory. This current
5918 * code is very simple, later versions will do smarter things in the btree
5919 */
Chris Mason3de45862008-11-17 21:02:50 -05005920int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005921{
5922 int ret = 0;
5923
5924 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005925 ret = btrfs_inode_delayed_dir_index_count(dir);
5926 if (ret) {
5927 ret = btrfs_set_inode_index_count(dir);
5928 if (ret)
5929 return ret;
5930 }
Josef Bacikaec74772008-07-24 12:12:38 -04005931 }
5932
Chris Mason00e4e6b2008-08-05 11:18:09 -04005933 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005934 BTRFS_I(dir)->index_cnt++;
5935
5936 return ret;
5937}
5938
Chris Masonb0d5d102014-09-08 13:08:51 -07005939static int btrfs_insert_inode_locked(struct inode *inode)
5940{
5941 struct btrfs_iget_args args;
5942 args.location = &BTRFS_I(inode)->location;
5943 args.root = BTRFS_I(inode)->root;
5944
5945 return insert_inode_locked4(inode,
5946 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5947 btrfs_find_actor, &args);
5948}
5949
Chris Mason39279cc2007-06-12 06:35:45 -04005950static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5951 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005952 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005953 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005954 u64 ref_objectid, u64 objectid,
5955 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005956{
5957 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005958 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005959 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005960 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005961 struct btrfs_inode_ref *ref;
5962 struct btrfs_key key[2];
5963 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005964 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005965 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005966 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005967
Chris Mason5f39d392007-10-15 16:14:19 -04005968 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005969 if (!path)
5970 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005971
Chris Mason39279cc2007-06-12 06:35:45 -04005972 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005973 if (!inode) {
5974 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005975 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005976 }
Chris Mason39279cc2007-06-12 06:35:45 -04005977
Li Zefan581bb052011-04-20 10:06:11 +08005978 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005979 * O_TMPFILE, set link count to 0, so that after this point,
5980 * we fill in an inode item with the correct link count.
5981 */
5982 if (!name)
5983 set_nlink(inode, 0);
5984
5985 /*
Li Zefan581bb052011-04-20 10:06:11 +08005986 * we have to initialize this early, so we can reclaim the inode
5987 * number if we fail afterwards in this function.
5988 */
5989 inode->i_ino = objectid;
5990
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005991 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005992 trace_btrfs_inode_request(dir);
5993
Chris Mason3de45862008-11-17 21:02:50 -05005994 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005995 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005996 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005997 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005998 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005999 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006000 } else if (dir) {
6001 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006002 }
6003 /*
6004 * index_cnt is ignored for everything but a dir,
6005 * btrfs_get_inode_index_count has an explanation for the magic
6006 * number
6007 */
6008 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006009 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006010 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006011 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006012 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006013
Josef Bacik5dc562c2012-08-17 13:14:17 -04006014 /*
6015 * We could have gotten an inode number from somebody who was fsynced
6016 * and then removed in this same transaction, so let's just set full
6017 * sync since it will be a full sync anyway and this will blow away the
6018 * old info in the log.
6019 */
6020 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6021
Chris Mason9c583092008-01-29 15:15:18 -05006022 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006023 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006024 key[0].offset = 0;
6025
Chris Mason9c583092008-01-29 15:15:18 -05006026 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006027
6028 if (name) {
6029 /*
6030 * Start new inodes with an inode_ref. This is slightly more
6031 * efficient for small numbers of hard links since they will
6032 * be packed into one item. Extended refs will kick in if we
6033 * add more hard links than can fit in the ref item.
6034 */
6035 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006036 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006037 key[1].offset = ref_objectid;
6038
6039 sizes[1] = name_len + sizeof(*ref);
6040 }
Chris Mason9c583092008-01-29 15:15:18 -05006041
Chris Masonb0d5d102014-09-08 13:08:51 -07006042 location = &BTRFS_I(inode)->location;
6043 location->objectid = objectid;
6044 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006045 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006046
6047 ret = btrfs_insert_inode_locked(inode);
6048 if (ret < 0)
6049 goto fail;
6050
Chris Masonb9473432009-03-13 11:00:37 -04006051 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006052 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006053 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006054 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006055
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006056 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006057 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306058
6059 inode->i_mtime = CURRENT_TIME;
6060 inode->i_atime = inode->i_mtime;
6061 inode->i_ctime = inode->i_mtime;
6062 BTRFS_I(inode)->i_otime = inode->i_mtime;
6063
Chris Mason5f39d392007-10-15 16:14:19 -04006064 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6065 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006066 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6067 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006068 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006069
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006070 if (name) {
6071 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6072 struct btrfs_inode_ref);
6073 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6074 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6075 ptr = (unsigned long)(ref + 1);
6076 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6077 }
Chris Mason9c583092008-01-29 15:15:18 -05006078
Chris Mason5f39d392007-10-15 16:14:19 -04006079 btrfs_mark_buffer_dirty(path->nodes[0]);
6080 btrfs_free_path(path);
6081
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006082 btrfs_inherit_iflags(inode, dir);
6083
Al Viro569254b2011-07-24 17:08:40 -04006084 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04006085 if (btrfs_test_opt(root, NODATASUM))
6086 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06006087 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006088 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6089 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006090 }
6091
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006092 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006093
6094 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006095 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006096
Alexander Block8ea05e32012-07-25 17:35:53 +02006097 btrfs_update_root_times(trans, root);
6098
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006099 ret = btrfs_inode_inherit_props(trans, inode, dir);
6100 if (ret)
6101 btrfs_err(root->fs_info,
6102 "error inheriting props for ino %llu (root %llu): %d",
6103 btrfs_ino(inode), root->root_key.objectid, ret);
6104
Chris Mason39279cc2007-06-12 06:35:45 -04006105 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006106
6107fail_unlock:
6108 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006109fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006110 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006111 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006112 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006113 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006114 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006115}
6116
6117static inline u8 btrfs_inode_type(struct inode *inode)
6118{
6119 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6120}
6121
Chris Masond352ac62008-09-29 15:18:18 -04006122/*
6123 * utility function to add 'inode' into 'parent_inode' with
6124 * a give name and a given sequence number.
6125 * if 'add_backref' is true, also insert a backref from the
6126 * inode to the parent directory.
6127 */
Chris Masone02119d2008-09-05 16:13:11 -04006128int btrfs_add_link(struct btrfs_trans_handle *trans,
6129 struct inode *parent_inode, struct inode *inode,
6130 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006131{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006132 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006133 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006134 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006135 u64 ino = btrfs_ino(inode);
6136 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006137
Li Zefan33345d012011-04-20 10:31:50 +08006138 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006139 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6140 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006141 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006142 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006143 key.offset = 0;
6144 }
Chris Mason39279cc2007-06-12 06:35:45 -04006145
Li Zefan33345d012011-04-20 10:31:50 +08006146 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006147 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6148 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006149 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006150 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006151 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6152 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006153 }
6154
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006155 /* Nothing to clean up yet */
6156 if (ret)
6157 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006158
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006159 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6160 parent_inode, &key,
6161 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006162 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006163 goto fail_dir_item;
6164 else if (ret) {
6165 btrfs_abort_transaction(trans, root, ret);
6166 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006167 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006168
6169 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6170 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006171 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006172 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
6173 ret = btrfs_update_inode(trans, root, parent_inode);
6174 if (ret)
6175 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006176 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006177
6178fail_dir_item:
6179 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6180 u64 local_index;
6181 int err;
6182 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6183 key.objectid, root->root_key.objectid,
6184 parent_ino, &local_index, name, name_len);
6185
6186 } else if (add_backref) {
6187 u64 local_index;
6188 int err;
6189
6190 err = btrfs_del_inode_ref(trans, root, name, name_len,
6191 ino, parent_ino, &local_index);
6192 }
6193 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006194}
6195
6196static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006197 struct inode *dir, struct dentry *dentry,
6198 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006199{
Josef Bacika1b075d2010-11-19 20:36:11 +00006200 int err = btrfs_add_link(trans, dir, inode,
6201 dentry->d_name.name, dentry->d_name.len,
6202 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006203 if (err > 0)
6204 err = -EEXIST;
6205 return err;
6206}
6207
Josef Bacik618e21d2007-07-11 10:18:17 -04006208static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006209 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006210{
6211 struct btrfs_trans_handle *trans;
6212 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006213 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006214 int err;
6215 int drop_inode = 0;
6216 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006217 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006218
6219 if (!new_valid_dev(rdev))
6220 return -EINVAL;
6221
Josef Bacik9ed74f22009-09-11 16:12:44 -04006222 /*
6223 * 2 for inode item and ref
6224 * 2 for dir items
6225 * 1 for xattr if selinux is on
6226 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006227 trans = btrfs_start_transaction(root, 5);
6228 if (IS_ERR(trans))
6229 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006230
Li Zefan581bb052011-04-20 10:06:11 +08006231 err = btrfs_find_free_ino(root, &objectid);
6232 if (err)
6233 goto out_unlock;
6234
Josef Bacikaec74772008-07-24 12:12:38 -04006235 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006236 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006237 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006238 if (IS_ERR(inode)) {
6239 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006240 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006241 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006242
Casey Schauflerad19db72011-12-15 10:09:07 -05006243 /*
6244 * If the active LSM wants to access the inode during
6245 * d_instantiate it needs these. Smack checks to see
6246 * if the filesystem supports xattrs by looking at the
6247 * ops vector.
6248 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006249 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006250 init_special_inode(inode, inode->i_mode, rdev);
6251
6252 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006253 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006254 goto out_unlock_inode;
6255
6256 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6257 if (err) {
6258 goto out_unlock_inode;
6259 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006260 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006261 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006262 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006263 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006264
Josef Bacik618e21d2007-07-11 10:18:17 -04006265out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006266 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006267 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006268 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006269 if (drop_inode) {
6270 inode_dec_link_count(inode);
6271 iput(inode);
6272 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006273 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006274
6275out_unlock_inode:
6276 drop_inode = 1;
6277 unlock_new_inode(inode);
6278 goto out_unlock;
6279
Josef Bacik618e21d2007-07-11 10:18:17 -04006280}
6281
Chris Mason39279cc2007-06-12 06:35:45 -04006282static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006283 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006284{
6285 struct btrfs_trans_handle *trans;
6286 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006287 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006288 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006289 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006290 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006291 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006292
Josef Bacik9ed74f22009-09-11 16:12:44 -04006293 /*
6294 * 2 for inode item and ref
6295 * 2 for dir items
6296 * 1 for xattr if selinux is on
6297 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006298 trans = btrfs_start_transaction(root, 5);
6299 if (IS_ERR(trans))
6300 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006301
Li Zefan581bb052011-04-20 10:06:11 +08006302 err = btrfs_find_free_ino(root, &objectid);
6303 if (err)
6304 goto out_unlock;
6305
Josef Bacikaec74772008-07-24 12:12:38 -04006306 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006307 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006308 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006309 if (IS_ERR(inode)) {
6310 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006311 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006312 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006313 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006314 /*
6315 * If the active LSM wants to access the inode during
6316 * d_instantiate it needs these. Smack checks to see
6317 * if the filesystem supports xattrs by looking at the
6318 * ops vector.
6319 */
6320 inode->i_fop = &btrfs_file_operations;
6321 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006322 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006323
6324 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6325 if (err)
6326 goto out_unlock_inode;
6327
6328 err = btrfs_update_inode(trans, root, inode);
6329 if (err)
6330 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006331
Josef Bacika1b075d2010-11-19 20:36:11 +00006332 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006333 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006334 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006335
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006336 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006337 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006338 d_instantiate(dentry, inode);
6339
Chris Mason39279cc2007-06-12 06:35:45 -04006340out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006341 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006342 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006343 inode_dec_link_count(inode);
6344 iput(inode);
6345 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006346 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006347 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006348 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006349
6350out_unlock_inode:
6351 unlock_new_inode(inode);
6352 goto out_unlock;
6353
Chris Mason39279cc2007-06-12 06:35:45 -04006354}
6355
6356static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6357 struct dentry *dentry)
6358{
6359 struct btrfs_trans_handle *trans;
6360 struct btrfs_root *root = BTRFS_I(dir)->root;
6361 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006362 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006363 int err;
6364 int drop_inode = 0;
6365
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006366 /* do not allow sys_link's with other subvols of the same device */
6367 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006368 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006369
Mark Fashehf1863732012-08-08 11:32:27 -07006370 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006371 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006372
Chris Mason3de45862008-11-17 21:02:50 -05006373 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006374 if (err)
6375 goto fail;
6376
Yan, Zhenga22285a2010-05-16 10:48:46 -04006377 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006378 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006379 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006380 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006381 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006382 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006383 if (IS_ERR(trans)) {
6384 err = PTR_ERR(trans);
6385 goto fail;
6386 }
Chris Mason5f39d392007-10-15 16:14:19 -04006387
Miao Xie67de1172013-12-26 13:07:06 +08006388 /* There are several dir indexes for this inode, clear the cache. */
6389 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006390 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006391 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006392 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006393 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006394 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006395
Josef Bacika1b075d2010-11-19 20:36:11 +00006396 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006397
Yan, Zhenga5719522009-09-24 09:17:31 -04006398 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006399 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006400 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006401 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006402 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006403 if (err)
6404 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006405 if (inode->i_nlink == 1) {
6406 /*
6407 * If new hard link count is 1, it's a file created
6408 * with open(2) O_TMPFILE flag.
6409 */
6410 err = btrfs_orphan_del(trans, inode);
6411 if (err)
6412 goto fail;
6413 }
Al Viro08c422c2011-12-23 07:58:13 -05006414 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006415 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006416 }
Chris Mason39279cc2007-06-12 06:35:45 -04006417
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006418 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006419 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006420fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006421 if (drop_inode) {
6422 inode_dec_link_count(inode);
6423 iput(inode);
6424 }
Liu Bob53d3f52012-11-14 14:34:34 +00006425 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006426 return err;
6427}
6428
Al Viro18bb1db2011-07-26 01:41:39 -04006429static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006430{
Chris Masonb9d86662008-05-02 16:13:49 -04006431 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006432 struct btrfs_trans_handle *trans;
6433 struct btrfs_root *root = BTRFS_I(dir)->root;
6434 int err = 0;
6435 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006436 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006437 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006438
Josef Bacik9ed74f22009-09-11 16:12:44 -04006439 /*
6440 * 2 items for inode and ref
6441 * 2 items for dir items
6442 * 1 for xattr if selinux is on
6443 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006444 trans = btrfs_start_transaction(root, 5);
6445 if (IS_ERR(trans))
6446 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006447
Li Zefan581bb052011-04-20 10:06:11 +08006448 err = btrfs_find_free_ino(root, &objectid);
6449 if (err)
6450 goto out_fail;
6451
Josef Bacikaec74772008-07-24 12:12:38 -04006452 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006453 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006454 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006455 if (IS_ERR(inode)) {
6456 err = PTR_ERR(inode);
6457 goto out_fail;
6458 }
Chris Mason5f39d392007-10-15 16:14:19 -04006459
Chris Mason39279cc2007-06-12 06:35:45 -04006460 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006461 /* these must be set before we unlock the inode */
6462 inode->i_op = &btrfs_dir_inode_operations;
6463 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006464
Eric Paris2a7dba32011-02-01 11:05:39 -05006465 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006466 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006467 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006468
Chris Masondbe674a2008-07-17 12:54:05 -04006469 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006470 err = btrfs_update_inode(trans, root, inode);
6471 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006472 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006473
Josef Bacika1b075d2010-11-19 20:36:11 +00006474 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6475 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006476 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006477 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006478
Chris Mason39279cc2007-06-12 06:35:45 -04006479 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006480 /*
6481 * mkdir is special. We're unlocking after we call d_instantiate
6482 * to avoid a race with nfsd calling d_instantiate.
6483 */
6484 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006485 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006486
6487out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006488 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006489 if (drop_on_err) {
6490 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006491 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006492 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006493 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006494 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006495 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006496
6497out_fail_inode:
6498 unlock_new_inode(inode);
6499 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006500}
6501
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006502/* Find next extent map of a given extent map, caller needs to ensure locks */
6503static struct extent_map *next_extent_map(struct extent_map *em)
6504{
6505 struct rb_node *next;
6506
6507 next = rb_next(&em->rb_node);
6508 if (!next)
6509 return NULL;
6510 return container_of(next, struct extent_map, rb_node);
6511}
6512
6513static struct extent_map *prev_extent_map(struct extent_map *em)
6514{
6515 struct rb_node *prev;
6516
6517 prev = rb_prev(&em->rb_node);
6518 if (!prev)
6519 return NULL;
6520 return container_of(prev, struct extent_map, rb_node);
6521}
6522
Chris Masond352ac62008-09-29 15:18:18 -04006523/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006524 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006525 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006526 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006527 */
Chris Mason3b951512008-04-17 11:29:12 -04006528static int merge_extent_mapping(struct extent_map_tree *em_tree,
6529 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006530 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006531 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006532{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006533 struct extent_map *prev;
6534 struct extent_map *next;
6535 u64 start;
6536 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006537 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006538
Chris Masone6dcd2d2008-07-17 12:53:50 -04006539 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006540
6541 if (existing->start > map_start) {
6542 next = existing;
6543 prev = prev_extent_map(next);
6544 } else {
6545 prev = existing;
6546 next = next_extent_map(prev);
6547 }
6548
6549 start = prev ? extent_map_end(prev) : em->start;
6550 start = max_t(u64, start, em->start);
6551 end = next ? next->start : extent_map_end(em);
6552 end = min_t(u64, end, extent_map_end(em));
6553 start_diff = start - em->start;
6554 em->start = start;
6555 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006556 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6557 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006558 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006559 em->block_len -= start_diff;
6560 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006561 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006562}
6563
Chris Masonc8b97812008-10-29 14:49:59 -04006564static noinline int uncompress_inline(struct btrfs_path *path,
6565 struct inode *inode, struct page *page,
6566 size_t pg_offset, u64 extent_offset,
6567 struct btrfs_file_extent_item *item)
6568{
6569 int ret;
6570 struct extent_buffer *leaf = path->nodes[0];
6571 char *tmp;
6572 size_t max_size;
6573 unsigned long inline_size;
6574 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006575 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006576
6577 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006578 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006579 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6580 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006581 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006582 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006583 if (!tmp)
6584 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006585 ptr = btrfs_file_extent_inline_start(item);
6586
6587 read_extent_buffer(leaf, tmp, ptr, inline_size);
6588
Chris Mason5b050f02008-11-11 09:34:41 -05006589 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006590 ret = btrfs_decompress(compress_type, tmp, page,
6591 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006592 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006593 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006594}
6595
Chris Masond352ac62008-09-29 15:18:18 -04006596/*
6597 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006598 * the ugly parts come from merging extents from the disk with the in-ram
6599 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006600 * where the in-ram extents might be locked pending data=ordered completion.
6601 *
6602 * This also copies inline extents directly into the page.
6603 */
Chris Masond3977122009-01-05 21:25:51 -05006604
Chris Masona52d9a82007-08-27 16:49:44 -04006605struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006606 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006607 int create)
6608{
6609 int ret;
6610 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006611 u64 extent_start = 0;
6612 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006613 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006614 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006615 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006616 struct btrfs_root *root = BTRFS_I(inode)->root;
6617 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006618 struct extent_buffer *leaf;
6619 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006620 struct extent_map *em = NULL;
6621 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006622 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006623 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006624 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006625
Chris Masona52d9a82007-08-27 16:49:44 -04006626again:
Chris Mason890871b2009-09-02 16:24:52 -04006627 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006628 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006629 if (em)
6630 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006631 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006632
Chris Masona52d9a82007-08-27 16:49:44 -04006633 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006634 if (em->start > start || em->start + em->len <= start)
6635 free_extent_map(em);
6636 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006637 free_extent_map(em);
6638 else
6639 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006640 }
David Sterba172ddd62011-04-21 00:48:27 +02006641 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006642 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006643 err = -ENOMEM;
6644 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006645 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006646 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006647 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006648 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006649 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006650 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006651
6652 if (!path) {
6653 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006654 if (!path) {
6655 err = -ENOMEM;
6656 goto out;
6657 }
6658 /*
6659 * Chances are we'll be called again, so go ahead and do
6660 * readahead
6661 */
6662 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006663 }
6664
Chris Mason179e29e2007-11-01 11:28:41 -04006665 ret = btrfs_lookup_file_extent(trans, root, path,
6666 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006667 if (ret < 0) {
6668 err = ret;
6669 goto out;
6670 }
6671
6672 if (ret != 0) {
6673 if (path->slots[0] == 0)
6674 goto not_found;
6675 path->slots[0]--;
6676 }
6677
Chris Mason5f39d392007-10-15 16:14:19 -04006678 leaf = path->nodes[0];
6679 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006680 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006681 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006682 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006683 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006684 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006685 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006686 /*
6687 * If we backup past the first extent we want to move forward
6688 * and see if there is an extent in front of us, otherwise we'll
6689 * say there is a hole for our whole search range which can
6690 * cause problems.
6691 */
6692 extent_end = start;
6693 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006694 }
6695
Chris Mason5f39d392007-10-15 16:14:19 -04006696 found_type = btrfs_file_extent_type(leaf, item);
6697 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006698 if (found_type == BTRFS_FILE_EXTENT_REG ||
6699 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006700 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006701 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006702 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6703 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006704 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006705 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006706 }
Josef Bacik25a50342013-10-14 12:08:38 -04006707next:
Yan Zheng9036c102008-10-30 14:19:41 -04006708 if (start >= extent_end) {
6709 path->slots[0]++;
6710 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6711 ret = btrfs_next_leaf(root, path);
6712 if (ret < 0) {
6713 err = ret;
6714 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006715 }
Yan Zheng9036c102008-10-30 14:19:41 -04006716 if (ret > 0)
6717 goto not_found;
6718 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006719 }
Yan Zheng9036c102008-10-30 14:19:41 -04006720 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6721 if (found_key.objectid != objectid ||
6722 found_key.type != BTRFS_EXTENT_DATA_KEY)
6723 goto not_found;
6724 if (start + len <= found_key.offset)
6725 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006726 if (start > found_key.offset)
6727 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006728 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006729 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006730 em->len = found_key.offset - start;
6731 goto not_found_em;
6732 }
6733
Filipe Manana7ffbb592014-06-09 03:48:05 +01006734 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6735
Yan Zhengd899e052008-10-30 14:25:28 -04006736 if (found_type == BTRFS_FILE_EXTENT_REG ||
6737 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006738 goto insert;
6739 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006740 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006741 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006742 size_t size;
6743 size_t extent_offset;
6744 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006745
Filipe Manana7ffbb592014-06-09 03:48:05 +01006746 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006747 goto out;
Yan689f9342007-10-29 11:41:07 -04006748
Chris Mason514ac8a2014-01-03 21:07:00 -08006749 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006750 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006751 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006752 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006753 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006754 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006755 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006756 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006757 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006758 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006759 if (btrfs_file_extent_compression(leaf, item) !=
6760 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006761 ret = uncompress_inline(path, inode, page,
6762 pg_offset,
6763 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006764 if (ret) {
6765 err = ret;
6766 goto out;
6767 }
Chris Masonc8b97812008-10-29 14:49:59 -04006768 } else {
6769 map = kmap(page);
6770 read_extent_buffer(leaf, map + pg_offset, ptr,
6771 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006772 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6773 memset(map + pg_offset + copy_size, 0,
6774 PAGE_CACHE_SIZE - pg_offset -
6775 copy_size);
6776 }
Chris Masonc8b97812008-10-29 14:49:59 -04006777 kunmap(page);
6778 }
Chris Mason179e29e2007-11-01 11:28:41 -04006779 flush_dcache_page(page);
6780 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006781 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006782 if (!trans) {
6783 kunmap(page);
6784 free_extent_map(em);
6785 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006786
David Sterbab3b4aa72011-04-21 01:20:15 +02006787 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006788 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006789
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006790 if (IS_ERR(trans))
6791 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006792 goto again;
6793 }
Chris Masonc8b97812008-10-29 14:49:59 -04006794 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006795 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006796 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006797 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006798 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006799 }
Chris Masond1310b22008-01-24 16:13:08 -05006800 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006801 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006802 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006803 }
6804not_found:
6805 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006806 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006807 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006808not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006809 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006810 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006811insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006812 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006813 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006814 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006815 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006816 err = -EIO;
6817 goto out;
6818 }
Chris Masond1310b22008-01-24 16:13:08 -05006819
6820 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006821 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006822 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006823 /* it is possible that someone inserted the extent into the tree
6824 * while we had the lock dropped. It is also possible that
6825 * an overlapping map exists in the tree
6826 */
Chris Masona52d9a82007-08-27 16:49:44 -04006827 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006828 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006829
6830 ret = 0;
6831
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006832 existing = search_extent_mapping(em_tree, start, len);
6833 /*
6834 * existing will always be non-NULL, since there must be
6835 * extent causing the -EEXIST.
6836 */
6837 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006838 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006839 /*
6840 * The existing extent map is the one nearest to
6841 * the [start, start + len) range which overlaps
6842 */
6843 err = merge_extent_mapping(em_tree, existing,
6844 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006845 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006846 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006847 free_extent_map(em);
6848 em = NULL;
6849 }
6850 } else {
6851 free_extent_map(em);
6852 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006853 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006854 }
Chris Masona52d9a82007-08-27 16:49:44 -04006855 }
Chris Mason890871b2009-09-02 16:24:52 -04006856 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006857out:
liubo1abe9b82011-03-24 11:18:59 +00006858
Steven Rostedt4cd8587c2013-11-14 22:57:29 -05006859 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006860
Chris Masonf4219502008-07-22 11:18:09 -04006861 if (path)
6862 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006863 if (trans) {
6864 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006865 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006866 err = ret;
6867 }
Chris Masona52d9a82007-08-27 16:49:44 -04006868 if (err) {
6869 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006870 return ERR_PTR(err);
6871 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006872 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006873 return em;
6874}
6875
Chris Masonec29ed52011-02-23 16:23:20 -05006876struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6877 size_t pg_offset, u64 start, u64 len,
6878 int create)
6879{
6880 struct extent_map *em;
6881 struct extent_map *hole_em = NULL;
6882 u64 range_start = start;
6883 u64 end;
6884 u64 found;
6885 u64 found_end;
6886 int err = 0;
6887
6888 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6889 if (IS_ERR(em))
6890 return em;
6891 if (em) {
6892 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006893 * if our em maps to
6894 * - a hole or
6895 * - a pre-alloc extent,
6896 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006897 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006898 if (em->block_start != EXTENT_MAP_HOLE &&
6899 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006900 return em;
6901 else
6902 hole_em = em;
6903 }
6904
6905 /* check to see if we've wrapped (len == -1 or similar) */
6906 end = start + len;
6907 if (end < start)
6908 end = (u64)-1;
6909 else
6910 end -= 1;
6911
6912 em = NULL;
6913
6914 /* ok, we didn't find anything, lets look for delalloc */
6915 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6916 end, len, EXTENT_DELALLOC, 1);
6917 found_end = range_start + found;
6918 if (found_end < range_start)
6919 found_end = (u64)-1;
6920
6921 /*
6922 * we didn't find anything useful, return
6923 * the original results from get_extent()
6924 */
6925 if (range_start > end || found_end <= start) {
6926 em = hole_em;
6927 hole_em = NULL;
6928 goto out;
6929 }
6930
6931 /* adjust the range_start to make sure it doesn't
6932 * go backwards from the start they passed in
6933 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306934 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006935 found = found_end - range_start;
6936
6937 if (found > 0) {
6938 u64 hole_start = start;
6939 u64 hole_len = len;
6940
David Sterba172ddd62011-04-21 00:48:27 +02006941 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006942 if (!em) {
6943 err = -ENOMEM;
6944 goto out;
6945 }
6946 /*
6947 * when btrfs_get_extent can't find anything it
6948 * returns one huge hole
6949 *
6950 * make sure what it found really fits our range, and
6951 * adjust to make sure it is based on the start from
6952 * the caller
6953 */
6954 if (hole_em) {
6955 u64 calc_end = extent_map_end(hole_em);
6956
6957 if (calc_end <= start || (hole_em->start > end)) {
6958 free_extent_map(hole_em);
6959 hole_em = NULL;
6960 } else {
6961 hole_start = max(hole_em->start, start);
6962 hole_len = calc_end - hole_start;
6963 }
6964 }
6965 em->bdev = NULL;
6966 if (hole_em && range_start > hole_start) {
6967 /* our hole starts before our delalloc, so we
6968 * have to return just the parts of the hole
6969 * that go until the delalloc starts
6970 */
6971 em->len = min(hole_len,
6972 range_start - hole_start);
6973 em->start = hole_start;
6974 em->orig_start = hole_start;
6975 /*
6976 * don't adjust block start at all,
6977 * it is fixed at EXTENT_MAP_HOLE
6978 */
6979 em->block_start = hole_em->block_start;
6980 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006981 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6982 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006983 } else {
6984 em->start = range_start;
6985 em->len = found;
6986 em->orig_start = range_start;
6987 em->block_start = EXTENT_MAP_DELALLOC;
6988 em->block_len = found;
6989 }
6990 } else if (hole_em) {
6991 return hole_em;
6992 }
6993out:
6994
6995 free_extent_map(hole_em);
6996 if (err) {
6997 free_extent_map(em);
6998 return ERR_PTR(err);
6999 }
7000 return em;
7001}
7002
Josef Bacik4b46fce2010-05-23 11:00:55 -04007003static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7004 u64 start, u64 len)
7005{
7006 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007007 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007008 struct btrfs_key ins;
7009 u64 alloc_hint;
7010 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007011
Josef Bacik4b46fce2010-05-23 11:00:55 -04007012 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04007013 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007014 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007015 if (ret)
7016 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007017
Josef Bacik70c8a912012-10-11 16:54:30 -04007018 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007019 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04007020 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08007021 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007022 return em;
7023 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007024
7025 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
7026 ins.offset, ins.offset, 0);
7027 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08007028 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007029 free_extent_map(em);
7030 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007031 }
Josef Bacik00361582013-08-14 14:02:47 -04007032
Josef Bacik4b46fce2010-05-23 11:00:55 -04007033 return em;
7034}
7035
Chris Mason46bfbb52010-05-26 11:04:10 -04007036/*
7037 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7038 * block must be cow'd
7039 */
Josef Bacik00361582013-08-14 14:02:47 -04007040noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007041 u64 *orig_start, u64 *orig_block_len,
7042 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007043{
Josef Bacik00361582013-08-14 14:02:47 -04007044 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007045 struct btrfs_path *path;
7046 int ret;
7047 struct extent_buffer *leaf;
7048 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007049 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007050 struct btrfs_file_extent_item *fi;
7051 struct btrfs_key key;
7052 u64 disk_bytenr;
7053 u64 backref_offset;
7054 u64 extent_end;
7055 u64 num_bytes;
7056 int slot;
7057 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007058 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007059
Chris Mason46bfbb52010-05-26 11:04:10 -04007060 path = btrfs_alloc_path();
7061 if (!path)
7062 return -ENOMEM;
7063
Josef Bacik00361582013-08-14 14:02:47 -04007064 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007065 offset, 0);
7066 if (ret < 0)
7067 goto out;
7068
7069 slot = path->slots[0];
7070 if (ret == 1) {
7071 if (slot == 0) {
7072 /* can't find the item, must cow */
7073 ret = 0;
7074 goto out;
7075 }
7076 slot--;
7077 }
7078 ret = 0;
7079 leaf = path->nodes[0];
7080 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007081 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007082 key.type != BTRFS_EXTENT_DATA_KEY) {
7083 /* not our file or wrong item type, must cow */
7084 goto out;
7085 }
7086
7087 if (key.offset > offset) {
7088 /* Wrong offset, must cow */
7089 goto out;
7090 }
7091
7092 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7093 found_type = btrfs_file_extent_type(leaf, fi);
7094 if (found_type != BTRFS_FILE_EXTENT_REG &&
7095 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7096 /* not a regular extent, must cow */
7097 goto out;
7098 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007099
7100 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7101 goto out;
7102
Miao Xiee77751a2013-12-27 21:11:50 +08007103 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7104 if (extent_end <= offset)
7105 goto out;
7106
Chris Mason46bfbb52010-05-26 11:04:10 -04007107 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007108 if (disk_bytenr == 0)
7109 goto out;
7110
7111 if (btrfs_file_extent_compression(leaf, fi) ||
7112 btrfs_file_extent_encryption(leaf, fi) ||
7113 btrfs_file_extent_other_encoding(leaf, fi))
7114 goto out;
7115
Chris Mason46bfbb52010-05-26 11:04:10 -04007116 backref_offset = btrfs_file_extent_offset(leaf, fi);
7117
Josef Bacik7ee9e442013-06-21 16:37:03 -04007118 if (orig_start) {
7119 *orig_start = key.offset - backref_offset;
7120 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7121 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7122 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007123
Chris Mason46bfbb52010-05-26 11:04:10 -04007124 if (btrfs_extent_readonly(root, disk_bytenr))
7125 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007126
7127 num_bytes = min(offset + *len, extent_end) - offset;
7128 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7129 u64 range_end;
7130
7131 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7132 ret = test_range_bit(io_tree, offset, range_end,
7133 EXTENT_DELALLOC, 0, NULL);
7134 if (ret) {
7135 ret = -EAGAIN;
7136 goto out;
7137 }
7138 }
7139
Josef Bacik1bda19e2013-10-18 12:10:36 -04007140 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007141
7142 /*
7143 * look for other files referencing this extent, if we
7144 * find any we must cow
7145 */
Josef Bacik00361582013-08-14 14:02:47 -04007146 trans = btrfs_join_transaction(root);
7147 if (IS_ERR(trans)) {
7148 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007149 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007150 }
7151
7152 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7153 key.offset - backref_offset, disk_bytenr);
7154 btrfs_end_transaction(trans, root);
7155 if (ret) {
7156 ret = 0;
7157 goto out;
7158 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007159
7160 /*
7161 * adjust disk_bytenr and num_bytes to cover just the bytes
7162 * in this extent we are about to write. If there
7163 * are any csums in that range we have to cow in order
7164 * to keep the csums correct
7165 */
7166 disk_bytenr += backref_offset;
7167 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007168 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7169 goto out;
7170 /*
7171 * all of the above have passed, it is safe to overwrite this extent
7172 * without cow
7173 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007174 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007175 ret = 1;
7176out:
7177 btrfs_free_path(path);
7178 return ret;
7179}
7180
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007181bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7182{
7183 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7184 int found = false;
7185 void **pagep = NULL;
7186 struct page *page = NULL;
7187 int start_idx;
7188 int end_idx;
7189
7190 start_idx = start >> PAGE_CACHE_SHIFT;
7191
7192 /*
7193 * end is the last byte in the last page. end == start is legal
7194 */
7195 end_idx = end >> PAGE_CACHE_SHIFT;
7196
7197 rcu_read_lock();
7198
7199 /* Most of the code in this while loop is lifted from
7200 * find_get_page. It's been modified to begin searching from a
7201 * page and return just the first page found in that range. If the
7202 * found idx is less than or equal to the end idx then we know that
7203 * a page exists. If no pages are found or if those pages are
7204 * outside of the range then we're fine (yay!) */
7205 while (page == NULL &&
7206 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7207 page = radix_tree_deref_slot(pagep);
7208 if (unlikely(!page))
7209 break;
7210
7211 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007212 if (radix_tree_deref_retry(page)) {
7213 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007214 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007215 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007216 /*
7217 * Otherwise, shmem/tmpfs must be storing a swap entry
7218 * here as an exceptional entry: so return it without
7219 * attempting to raise page count.
7220 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007221 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007222 break; /* TODO: Is this relevant for this use case? */
7223 }
7224
Filipe Manana91405152014-06-05 13:22:24 +01007225 if (!page_cache_get_speculative(page)) {
7226 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007227 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007228 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007229
7230 /*
7231 * Has the page moved?
7232 * This is part of the lockless pagecache protocol. See
7233 * include/linux/pagemap.h for details.
7234 */
7235 if (unlikely(page != *pagep)) {
7236 page_cache_release(page);
7237 page = NULL;
7238 }
7239 }
7240
7241 if (page) {
7242 if (page->index <= end_idx)
7243 found = true;
7244 page_cache_release(page);
7245 }
7246
7247 rcu_read_unlock();
7248 return found;
7249}
7250
Josef Bacikeb838e72012-07-31 16:28:48 -04007251static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7252 struct extent_state **cached_state, int writing)
7253{
7254 struct btrfs_ordered_extent *ordered;
7255 int ret = 0;
7256
7257 while (1) {
7258 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7259 0, cached_state);
7260 /*
7261 * We're concerned with the entire range that we're going to be
7262 * doing DIO to, so we need to make sure theres no ordered
7263 * extents in this range.
7264 */
7265 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7266 lockend - lockstart + 1);
7267
7268 /*
7269 * We need to make sure there are no buffered pages in this
7270 * range either, we could have raced between the invalidate in
7271 * generic_file_direct_write and locking the extent. The
7272 * invalidate needs to happen so that reads after a write do not
7273 * get stale data.
7274 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007275 if (!ordered &&
7276 (!writing ||
7277 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007278 break;
7279
7280 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7281 cached_state, GFP_NOFS);
7282
7283 if (ordered) {
7284 btrfs_start_ordered_extent(inode, ordered, 1);
7285 btrfs_put_ordered_extent(ordered);
7286 } else {
7287 /* Screw you mmap */
Filipe Manana728404d2014-10-10 09:43:11 +01007288 ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
Filipe Manana075bdbd2014-10-09 21:18:55 +01007289 if (ret)
7290 break;
7291 ret = filemap_fdatawait_range(inode->i_mapping,
7292 lockstart,
7293 lockend);
Josef Bacikeb838e72012-07-31 16:28:48 -04007294 if (ret)
7295 break;
7296
7297 /*
7298 * If we found a page that couldn't be invalidated just
7299 * fall back to buffered.
7300 */
7301 ret = invalidate_inode_pages2_range(inode->i_mapping,
7302 lockstart >> PAGE_CACHE_SHIFT,
7303 lockend >> PAGE_CACHE_SHIFT);
7304 if (ret)
7305 break;
7306 }
7307
7308 cond_resched();
7309 }
7310
7311 return ret;
7312}
7313
Josef Bacik69ffb542012-09-11 15:40:07 -04007314static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7315 u64 len, u64 orig_start,
7316 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007317 u64 orig_block_len, u64 ram_bytes,
7318 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007319{
7320 struct extent_map_tree *em_tree;
7321 struct extent_map *em;
7322 struct btrfs_root *root = BTRFS_I(inode)->root;
7323 int ret;
7324
7325 em_tree = &BTRFS_I(inode)->extent_tree;
7326 em = alloc_extent_map();
7327 if (!em)
7328 return ERR_PTR(-ENOMEM);
7329
7330 em->start = start;
7331 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007332 em->mod_start = start;
7333 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007334 em->len = len;
7335 em->block_len = block_len;
7336 em->block_start = block_start;
7337 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007338 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007339 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007340 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007341 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7342 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007343 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007344
7345 do {
7346 btrfs_drop_extent_cache(inode, em->start,
7347 em->start + em->len - 1, 0);
7348 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007349 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007350 write_unlock(&em_tree->lock);
7351 } while (ret == -EEXIST);
7352
7353 if (ret) {
7354 free_extent_map(em);
7355 return ERR_PTR(ret);
7356 }
7357
7358 return em;
7359}
7360
7361
Josef Bacik4b46fce2010-05-23 11:00:55 -04007362static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7363 struct buffer_head *bh_result, int create)
7364{
7365 struct extent_map *em;
7366 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007367 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007368 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007369 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007370 u64 len = bh_result->b_size;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007371 u64 *outstanding_extents = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007372 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007373 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007374
Miao Xie172a5042013-02-21 02:48:22 -07007375 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007376 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007377 else
Josef Bacikc3298612012-08-03 16:49:19 -04007378 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007379
Josef Bacikc3298612012-08-03 16:49:19 -04007380 lockstart = start;
7381 lockend = start + len - 1;
7382
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007383 if (current->journal_info) {
7384 /*
7385 * Need to pull our outstanding extents and set journal_info to NULL so
7386 * that anything that needs to check if there's a transction doesn't get
7387 * confused.
7388 */
7389 outstanding_extents = current->journal_info;
7390 current->journal_info = NULL;
7391 }
7392
Josef Bacikeb838e72012-07-31 16:28:48 -04007393 /*
7394 * If this errors out it's because we couldn't invalidate pagecache for
7395 * this range and we need to fallback to buffered.
7396 */
7397 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7398 return -ENOTBLK;
7399
Josef Bacik4b46fce2010-05-23 11:00:55 -04007400 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007401 if (IS_ERR(em)) {
7402 ret = PTR_ERR(em);
7403 goto unlock_err;
7404 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007405
7406 /*
7407 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7408 * io. INLINE is special, and we could probably kludge it in here, but
7409 * it's still buffered so for safety lets just fall back to the generic
7410 * buffered path.
7411 *
7412 * For COMPRESSED we _have_ to read the entire extent in so we can
7413 * decompress it, so there will be buffering required no matter what we
7414 * do, so go ahead and fallback to buffered.
7415 *
7416 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7417 * to buffered IO. Don't blame me, this is the price we pay for using
7418 * the generic code.
7419 */
7420 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7421 em->block_start == EXTENT_MAP_INLINE) {
7422 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007423 ret = -ENOTBLK;
7424 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007425 }
7426
7427 /* Just a good old fashioned hole, return */
7428 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7429 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7430 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007431 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007432 }
7433
7434 /*
7435 * We don't allocate a new extent in the following cases
7436 *
7437 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7438 * existing extent.
7439 * 2) The extent is marked as PREALLOC. We're good to go here and can
7440 * just use the extent.
7441 *
7442 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007443 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007444 len = min(len, em->len - (start - em->start));
7445 lockstart = start + len;
7446 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007447 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007448
7449 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7450 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7451 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007452 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007453 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007454
7455 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7456 type = BTRFS_ORDERED_PREALLOC;
7457 else
7458 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007459 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007460 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007461
Josef Bacik00361582013-08-14 14:02:47 -04007462 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007463 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007464 if (type == BTRFS_ORDERED_PREALLOC) {
7465 free_extent_map(em);
7466 em = create_pinned_em(inode, start, len,
7467 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007468 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007469 orig_block_len,
7470 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007471 if (IS_ERR(em)) {
7472 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007473 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007474 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007475 }
7476
Chris Mason46bfbb52010-05-26 11:04:10 -04007477 ret = btrfs_add_ordered_extent_dio(inode, start,
7478 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007479 if (ret) {
7480 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007481 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007482 }
7483 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007484 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007485 }
Josef Bacik00361582013-08-14 14:02:47 -04007486
Chris Mason46bfbb52010-05-26 11:04:10 -04007487 /*
7488 * this will cow the extent, reset the len in case we changed
7489 * it above
7490 */
7491 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007492 free_extent_map(em);
7493 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007494 if (IS_ERR(em)) {
7495 ret = PTR_ERR(em);
7496 goto unlock_err;
7497 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007498 len = min(len, em->len - (start - em->start));
7499unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007500 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7501 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007502 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007503 bh_result->b_bdev = em->bdev;
7504 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007505 if (create) {
7506 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7507 set_buffer_new(bh_result);
7508
7509 /*
7510 * Need to update the i_size under the extent lock so buffered
7511 * readers will get the updated i_size when we unlock.
7512 */
7513 if (start + len > i_size_read(inode))
7514 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007515
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007516 /*
7517 * If we have an outstanding_extents count still set then we're
7518 * within our reservation, otherwise we need to adjust our inode
7519 * counter appropriately.
7520 */
7521 if (*outstanding_extents) {
7522 (*outstanding_extents)--;
7523 } else {
Josef Bacik3e05bde2015-02-11 15:08:57 -05007524 spin_lock(&BTRFS_I(inode)->lock);
7525 BTRFS_I(inode)->outstanding_extents++;
7526 spin_unlock(&BTRFS_I(inode)->lock);
7527 }
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007528
7529 current->journal_info = outstanding_extents;
Josef Bacik32667892015-02-11 15:08:58 -05007530 btrfs_free_reserved_data_space(inode, len);
Josef Bacikc3473e82012-06-19 10:59:00 -04007531 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007532
Josef Bacikeb838e72012-07-31 16:28:48 -04007533 /*
7534 * In the case of write we need to clear and unlock the entire range,
7535 * in the case of read we need to unlock only the end area that we
7536 * aren't using if there is any left over space.
7537 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007538 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007539 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7540 lockend, unlock_bits, 1, 0,
7541 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007542 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007543 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007544 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007545
Josef Bacik4b46fce2010-05-23 11:00:55 -04007546 free_extent_map(em);
7547
7548 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007549
7550unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007551 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7552 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007553 if (outstanding_extents)
7554 current->journal_info = outstanding_extents;
Josef Bacikeb838e72012-07-31 16:28:48 -04007555 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007556}
7557
Miao Xie8b110e32014-09-12 18:44:03 +08007558static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7559 int rw, int mirror_num)
7560{
7561 struct btrfs_root *root = BTRFS_I(inode)->root;
7562 int ret;
7563
7564 BUG_ON(rw & REQ_WRITE);
7565
7566 bio_get(bio);
7567
7568 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7569 BTRFS_WQ_ENDIO_DIO_REPAIR);
7570 if (ret)
7571 goto err;
7572
7573 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7574err:
7575 bio_put(bio);
7576 return ret;
7577}
7578
7579static int btrfs_check_dio_repairable(struct inode *inode,
7580 struct bio *failed_bio,
7581 struct io_failure_record *failrec,
7582 int failed_mirror)
7583{
7584 int num_copies;
7585
7586 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7587 failrec->logical, failrec->len);
7588 if (num_copies == 1) {
7589 /*
7590 * we only have a single copy of the data, so don't bother with
7591 * all the retry and error correction code that follows. no
7592 * matter what the error is, it is very likely to persist.
7593 */
7594 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7595 num_copies, failrec->this_mirror, failed_mirror);
7596 return 0;
7597 }
7598
7599 failrec->failed_mirror = failed_mirror;
7600 failrec->this_mirror++;
7601 if (failrec->this_mirror == failed_mirror)
7602 failrec->this_mirror++;
7603
7604 if (failrec->this_mirror > num_copies) {
7605 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7606 num_copies, failrec->this_mirror, failed_mirror);
7607 return 0;
7608 }
7609
7610 return 1;
7611}
7612
7613static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7614 struct page *page, u64 start, u64 end,
7615 int failed_mirror, bio_end_io_t *repair_endio,
7616 void *repair_arg)
7617{
7618 struct io_failure_record *failrec;
7619 struct bio *bio;
7620 int isector;
7621 int read_mode;
7622 int ret;
7623
7624 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7625
7626 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7627 if (ret)
7628 return ret;
7629
7630 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7631 failed_mirror);
7632 if (!ret) {
7633 free_io_failure(inode, failrec);
7634 return -EIO;
7635 }
7636
7637 if (failed_bio->bi_vcnt > 1)
7638 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7639 else
7640 read_mode = READ_SYNC;
7641
7642 isector = start - btrfs_io_bio(failed_bio)->logical;
7643 isector >>= inode->i_sb->s_blocksize_bits;
7644 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7645 0, isector, repair_endio, repair_arg);
7646 if (!bio) {
7647 free_io_failure(inode, failrec);
7648 return -EIO;
7649 }
7650
7651 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7652 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7653 read_mode, failrec->this_mirror, failrec->in_validation);
7654
7655 ret = submit_dio_repair_bio(inode, bio, read_mode,
7656 failrec->this_mirror);
7657 if (ret) {
7658 free_io_failure(inode, failrec);
7659 bio_put(bio);
7660 }
7661
7662 return ret;
7663}
7664
7665struct btrfs_retry_complete {
7666 struct completion done;
7667 struct inode *inode;
7668 u64 start;
7669 int uptodate;
7670};
7671
7672static void btrfs_retry_endio_nocsum(struct bio *bio, int err)
7673{
7674 struct btrfs_retry_complete *done = bio->bi_private;
7675 struct bio_vec *bvec;
7676 int i;
7677
7678 if (err)
7679 goto end;
7680
7681 done->uptodate = 1;
7682 bio_for_each_segment_all(bvec, bio, i)
7683 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7684end:
7685 complete(&done->done);
7686 bio_put(bio);
7687}
7688
7689static int __btrfs_correct_data_nocsum(struct inode *inode,
7690 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007691{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007692 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007693 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007694 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007695 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007696 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007697
Miao Xiec1dc0892014-09-12 18:43:56 +08007698 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007699 done.inode = inode;
7700
7701 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7702try_again:
7703 done.uptodate = 0;
7704 done.start = start;
7705 init_completion(&done.done);
7706
7707 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7708 start + bvec->bv_len - 1,
7709 io_bio->mirror_num,
7710 btrfs_retry_endio_nocsum, &done);
7711 if (ret)
7712 return ret;
7713
7714 wait_for_completion(&done.done);
7715
7716 if (!done.uptodate) {
7717 /* We might have another mirror, so try again */
7718 goto try_again;
7719 }
7720
7721 start += bvec->bv_len;
7722 }
7723
7724 return 0;
7725}
7726
7727static void btrfs_retry_endio(struct bio *bio, int err)
7728{
7729 struct btrfs_retry_complete *done = bio->bi_private;
7730 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7731 struct bio_vec *bvec;
7732 int uptodate;
7733 int ret;
7734 int i;
7735
7736 if (err)
7737 goto end;
7738
7739 uptodate = 1;
7740 bio_for_each_segment_all(bvec, bio, i) {
7741 ret = __readpage_endio_check(done->inode, io_bio, i,
7742 bvec->bv_page, 0,
7743 done->start, bvec->bv_len);
7744 if (!ret)
7745 clean_io_failure(done->inode, done->start,
7746 bvec->bv_page, 0);
7747 else
7748 uptodate = 0;
7749 }
7750
7751 done->uptodate = uptodate;
7752end:
7753 complete(&done->done);
7754 bio_put(bio);
7755}
7756
7757static int __btrfs_subio_endio_read(struct inode *inode,
7758 struct btrfs_io_bio *io_bio, int err)
7759{
7760 struct bio_vec *bvec;
7761 struct btrfs_retry_complete done;
7762 u64 start;
7763 u64 offset = 0;
7764 int i;
7765 int ret;
7766
7767 err = 0;
7768 start = io_bio->logical;
7769 done.inode = inode;
7770
Miao Xiec1dc0892014-09-12 18:43:56 +08007771 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007772 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7773 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007774 if (likely(!ret))
7775 goto next;
7776try_again:
7777 done.uptodate = 0;
7778 done.start = start;
7779 init_completion(&done.done);
7780
7781 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7782 start + bvec->bv_len - 1,
7783 io_bio->mirror_num,
7784 btrfs_retry_endio, &done);
7785 if (ret) {
7786 err = ret;
7787 goto next;
7788 }
7789
7790 wait_for_completion(&done.done);
7791
7792 if (!done.uptodate) {
7793 /* We might have another mirror, so try again */
7794 goto try_again;
7795 }
7796next:
7797 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007798 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007799 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007800
7801 return err;
7802}
7803
Miao Xie8b110e32014-09-12 18:44:03 +08007804static int btrfs_subio_endio_read(struct inode *inode,
7805 struct btrfs_io_bio *io_bio, int err)
7806{
7807 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7808
7809 if (skip_csum) {
7810 if (unlikely(err))
7811 return __btrfs_correct_data_nocsum(inode, io_bio);
7812 else
7813 return 0;
7814 } else {
7815 return __btrfs_subio_endio_read(inode, io_bio, err);
7816 }
7817}
7818
Miao Xiec1dc0892014-09-12 18:43:56 +08007819static void btrfs_endio_direct_read(struct bio *bio, int err)
7820{
7821 struct btrfs_dio_private *dip = bio->bi_private;
7822 struct inode *inode = dip->inode;
7823 struct bio *dio_bio;
7824 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7825
Miao Xie8b110e32014-09-12 18:44:03 +08007826 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7827 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007828
Josef Bacik4b46fce2010-05-23 11:00:55 -04007829 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007830 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007831 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007832
Josef Bacik4b46fce2010-05-23 11:00:55 -04007833 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007834
7835 /* If we had a csum failure make sure to clear the uptodate flag */
7836 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007837 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7838 dio_end_io(dio_bio, err);
Miao Xie23ea8e52014-09-12 18:43:54 +08007839
7840 if (io_bio->end_io)
7841 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007842 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007843}
7844
7845static void btrfs_endio_direct_write(struct bio *bio, int err)
7846{
7847 struct btrfs_dio_private *dip = bio->bi_private;
7848 struct inode *inode = dip->inode;
7849 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007850 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007851 u64 ordered_offset = dip->logical_offset;
7852 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007853 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007854 int ret;
7855
7856 if (err)
7857 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007858again:
7859 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7860 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007861 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007862 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007863 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007864
Liu Bo9e0af232014-08-15 23:36:53 +08007865 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7866 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007867 btrfs_queue_work(root->fs_info->endio_write_workers,
7868 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007869out_test:
7870 /*
7871 * our bio might span multiple ordered extents. If we haven't
7872 * completed the accounting for the whole dio, go back and try again
7873 */
7874 if (ordered_offset < dip->logical_offset + dip->bytes) {
7875 ordered_bytes = dip->logical_offset + dip->bytes -
7876 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007877 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007878 goto again;
7879 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007880out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007881 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007882
Josef Bacik4b46fce2010-05-23 11:00:55 -04007883 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007884
7885 /* If we had an error make sure to clear the uptodate flag */
7886 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007887 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7888 dio_end_io(dio_bio, err);
7889 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007890}
7891
Chris Masoneaf25d92010-05-25 09:48:28 -04007892static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7893 struct bio *bio, int mirror_num,
7894 unsigned long bio_flags, u64 offset)
7895{
7896 int ret;
7897 struct btrfs_root *root = BTRFS_I(inode)->root;
7898 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007899 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007900 return 0;
7901}
7902
Miao Xiee65e1532010-11-22 03:04:43 +00007903static void btrfs_end_dio_bio(struct bio *bio, int err)
7904{
7905 struct btrfs_dio_private *dip = bio->bi_private;
7906
Miao Xie8b110e32014-09-12 18:44:03 +08007907 if (err)
7908 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
7909 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7910 btrfs_ino(dip->inode), bio->bi_rw,
7911 (unsigned long long)bio->bi_iter.bi_sector,
7912 bio->bi_iter.bi_size, err);
7913
7914 if (dip->subio_endio)
7915 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007916
7917 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007918 dip->errors = 1;
7919
7920 /*
7921 * before atomic variable goto zero, we must make sure
7922 * dip->errors is perceived to be set.
7923 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007924 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007925 }
7926
7927 /* if there are more bios still pending for this dio, just exit */
7928 if (!atomic_dec_and_test(&dip->pending_bios))
7929 goto out;
7930
Chris Mason9be33952013-05-17 18:30:14 -04007931 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007932 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007933 } else {
7934 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007935 bio_endio(dip->orig_bio, 0);
7936 }
7937out:
7938 bio_put(bio);
7939}
7940
7941static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7942 u64 first_sector, gfp_t gfp_flags)
7943{
7944 int nr_vecs = bio_get_nr_vecs(bdev);
7945 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7946}
7947
Miao Xiec1dc0892014-09-12 18:43:56 +08007948static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
7949 struct inode *inode,
7950 struct btrfs_dio_private *dip,
7951 struct bio *bio,
7952 u64 file_offset)
7953{
7954 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7955 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
7956 int ret;
7957
7958 /*
7959 * We load all the csum data we need when we submit
7960 * the first bio to reduce the csum tree search and
7961 * contention.
7962 */
7963 if (dip->logical_offset == file_offset) {
7964 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
7965 file_offset);
7966 if (ret)
7967 return ret;
7968 }
7969
7970 if (bio == dip->orig_bio)
7971 return 0;
7972
7973 file_offset -= dip->logical_offset;
7974 file_offset >>= inode->i_sb->s_blocksize_bits;
7975 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
7976
7977 return 0;
7978}
7979
Miao Xiee65e1532010-11-22 03:04:43 +00007980static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7981 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007982 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007983{
Miao Xiefacc8a222013-07-25 19:22:34 +08007984 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007985 int write = rw & REQ_WRITE;
7986 struct btrfs_root *root = BTRFS_I(inode)->root;
7987 int ret;
7988
Josef Bacikb812ce22012-11-16 13:56:32 -05007989 if (async_submit)
7990 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7991
Miao Xiee65e1532010-11-22 03:04:43 +00007992 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007993
7994 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02007995 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7996 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007997 if (ret)
7998 goto err;
7999 }
Miao Xiee65e1532010-11-22 03:04:43 +00008000
Josef Bacik1ae39932011-04-06 14:41:34 -04008001 if (skip_sum)
8002 goto map;
8003
8004 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008005 ret = btrfs_wq_submit_bio(root->fs_info,
8006 inode, rw, bio, 0, 0,
8007 file_offset,
8008 __btrfs_submit_bio_start_direct_io,
8009 __btrfs_submit_bio_done);
8010 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008011 } else if (write) {
8012 /*
8013 * If we aren't doing async submit, calculate the csum of the
8014 * bio now.
8015 */
8016 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8017 if (ret)
8018 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008019 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008020 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8021 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008022 if (ret)
8023 goto err;
8024 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008025map:
8026 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008027err:
8028 bio_put(bio);
8029 return ret;
8030}
8031
8032static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8033 int skip_sum)
8034{
8035 struct inode *inode = dip->inode;
8036 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008037 struct bio *bio;
8038 struct bio *orig_bio = dip->orig_bio;
8039 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008040 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008041 u64 file_offset = dip->logical_offset;
8042 u64 submit_len = 0;
8043 u64 map_length;
8044 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08008045 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04008046 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008047
Kent Overstreet4f024f32013-10-11 15:44:27 -07008048 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008049 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008050 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008051 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008052 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008053
Kent Overstreet4f024f32013-10-11 15:44:27 -07008054 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008055 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008056 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008057 goto submit;
8058 }
8059
David Woodhouse53b381b2013-01-29 18:40:14 -05008060 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008061 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008062 async_submit = 0;
8063 else
8064 async_submit = 1;
8065
Josef Bacik02f57c72011-04-06 14:25:44 -04008066 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8067 if (!bio)
8068 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008069
Josef Bacik02f57c72011-04-06 14:25:44 -04008070 bio->bi_private = dip;
8071 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008072 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008073 atomic_inc(&dip->pending_bios);
8074
Miao Xiee65e1532010-11-22 03:04:43 +00008075 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02008076 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00008077 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02008078 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00008079 /*
8080 * inc the count before we submit the bio so
8081 * we know the end IO handler won't happen before
8082 * we inc the count. Otherwise, the dip might get freed
8083 * before we're done setting it up
8084 */
8085 atomic_inc(&dip->pending_bios);
8086 ret = __btrfs_submit_dio_bio(bio, inode, rw,
8087 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008088 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008089 if (ret) {
8090 bio_put(bio);
8091 atomic_dec(&dip->pending_bios);
8092 goto out_err;
8093 }
8094
Miao Xiee65e1532010-11-22 03:04:43 +00008095 start_sector += submit_len >> 9;
8096 file_offset += submit_len;
8097
8098 submit_len = 0;
8099 nr_pages = 0;
8100
8101 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8102 start_sector, GFP_NOFS);
8103 if (!bio)
8104 goto out_err;
8105 bio->bi_private = dip;
8106 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008107 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008108
Kent Overstreet4f024f32013-10-11 15:44:27 -07008109 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008110 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008111 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008112 &map_length, NULL, 0);
8113 if (ret) {
8114 bio_put(bio);
8115 goto out_err;
8116 }
8117 } else {
8118 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308119 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00008120 bvec++;
8121 }
8122 }
8123
Josef Bacik02f57c72011-04-06 14:25:44 -04008124submit:
Miao Xiee65e1532010-11-22 03:04:43 +00008125 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008126 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008127 if (!ret)
8128 return 0;
8129
8130 bio_put(bio);
8131out_err:
8132 dip->errors = 1;
8133 /*
8134 * before atomic variable goto zero, we must
8135 * make sure dip->errors is perceived to be set.
8136 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008137 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008138 if (atomic_dec_and_test(&dip->pending_bios))
8139 bio_io_error(dip->orig_bio);
8140
8141 /* bio_end_io() will handle error, so we needn't return it */
8142 return 0;
8143}
8144
Chris Mason9be33952013-05-17 18:30:14 -04008145static void btrfs_submit_direct(int rw, struct bio *dio_bio,
8146 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008147{
8148 struct btrfs_root *root = BTRFS_I(inode)->root;
8149 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04008150 struct bio *io_bio;
Miao Xie23ea8e52014-09-12 18:43:54 +08008151 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008152 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02008153 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008154 int ret = 0;
8155
8156 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8157
Chris Mason9be33952013-05-17 18:30:14 -04008158 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008159 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008160 ret = -ENOMEM;
8161 goto free_ordered;
8162 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008163
Miao Xiec1dc0892014-09-12 18:43:56 +08008164 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008165 if (!dip) {
8166 ret = -ENOMEM;
8167 goto free_io_bio;
8168 }
8169
8170 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008171 dip->inode = inode;
8172 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008173 dip->bytes = dio_bio->bi_iter.bi_size;
8174 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008175 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008176 dip->orig_bio = io_bio;
8177 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008178 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008179 btrfs_bio = btrfs_io_bio(io_bio);
8180 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008181
Miao Xiec1dc0892014-09-12 18:43:56 +08008182 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008183 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008184 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008185 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008186 dip->subio_endio = btrfs_subio_endio_read;
8187 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008188
Miao Xiee65e1532010-11-22 03:04:43 +00008189 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8190 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008191 return;
Chris Mason9be33952013-05-17 18:30:14 -04008192
Miao Xie23ea8e52014-09-12 18:43:54 +08008193 if (btrfs_bio->end_io)
8194 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008195free_io_bio:
8196 bio_put(io_bio);
8197
Josef Bacik4b46fce2010-05-23 11:00:55 -04008198free_ordered:
8199 /*
8200 * If this is a write, we need to clean up the reserved space and kill
8201 * the ordered extent.
8202 */
8203 if (write) {
8204 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05008205 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008206 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
8207 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
8208 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08008209 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008210 btrfs_put_ordered_extent(ordered);
8211 btrfs_put_ordered_extent(ordered);
8212 }
Chris Mason9be33952013-05-17 18:30:14 -04008213 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008214}
8215
Chris Mason5a5f79b2010-05-26 21:33:37 -04008216static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008217 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008218{
8219 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008220 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008221 unsigned blocksize_mask = root->sectorsize - 1;
8222 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008223
8224 if (offset & blocksize_mask)
8225 goto out;
8226
Al Viro28060d52014-03-22 05:15:17 -04008227 if (iov_iter_alignment(iter) & blocksize_mask)
8228 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008229
Al Viro28060d52014-03-22 05:15:17 -04008230 /* If this is a write we don't need to check anymore */
8231 if (rw & WRITE)
8232 return 0;
8233 /*
8234 * Check to make sure we don't have duplicate iov_base's in this
8235 * iovec, if so return EINVAL, otherwise we'll get csum errors
8236 * when reading back.
8237 */
8238 for (seg = 0; seg < iter->nr_segs; seg++) {
8239 for (i = seg + 1; i < iter->nr_segs; i++) {
8240 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008241 goto out;
8242 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008243 }
8244 retval = 0;
8245out:
8246 return retval;
8247}
Josef Bacikeb838e72012-07-31 16:28:48 -04008248
Chris Mason16432982008-04-10 10:23:21 -04008249static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05008250 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04008251{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008252 struct file *file = iocb->ki_filp;
8253 struct inode *inode = file->f_mapping->host;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008254 u64 outstanding_extents = 0;
Miao Xie09348562013-02-07 10:12:07 +00008255 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008256 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008257 bool wakeup = true;
8258 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008259 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008260
Al Viro28060d52014-03-22 05:15:17 -04008261 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008262 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008263
Miao Xie38851cc2013-02-08 07:04:11 +00008264 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008265 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008266
Josef Bacik0e267c42013-07-02 10:38:02 -04008267 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008268 * The generic stuff only does filemap_write_and_wait_range, which
8269 * isn't enough if we've written compressed pages to this area, so
8270 * we need to flush the dirty pages again to make absolutely sure
8271 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008272 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008273 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008274 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8275 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008276 filemap_fdatawrite_range(inode->i_mapping, offset,
8277 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008278
Miao Xie09348562013-02-07 10:12:07 +00008279 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008280 /*
8281 * If the write DIO is beyond the EOF, we need update
8282 * the isize, but it is protected by i_mutex. So we can
8283 * not unlock the i_mutex at this case.
8284 */
8285 if (offset + count <= inode->i_size) {
8286 mutex_unlock(&inode->i_mutex);
8287 relock = true;
8288 }
Miao Xie09348562013-02-07 10:12:07 +00008289 ret = btrfs_delalloc_reserve_space(inode, count);
8290 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008291 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008292 outstanding_extents = div64_u64(count +
8293 BTRFS_MAX_EXTENT_SIZE - 1,
8294 BTRFS_MAX_EXTENT_SIZE);
8295
8296 /*
8297 * We need to know how many extents we reserved so that we can
8298 * do the accounting properly if we go over the number we
8299 * originally calculated. Abuse current->journal_info for this.
8300 */
8301 current->journal_info = &outstanding_extents;
David Sterbaee39b432014-09-30 01:33:33 +02008302 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8303 &BTRFS_I(inode)->runtime_flags)) {
Miao Xie38851cc2013-02-08 07:04:11 +00008304 inode_dio_done(inode);
8305 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8306 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008307 }
8308
8309 ret = __blockdev_direct_IO(rw, iocb, inode,
8310 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05008311 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00008312 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00008313 if (rw & WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008314 current->journal_info = NULL;
Miao Xie09348562013-02-07 10:12:07 +00008315 if (ret < 0 && ret != -EIOCBQUEUED)
8316 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07008317 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00008318 btrfs_delalloc_release_space(inode,
8319 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008320 }
Miao Xie38851cc2013-02-08 07:04:11 +00008321out:
Miao Xie2e60a512013-02-08 07:01:08 +00008322 if (wakeup)
8323 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008324 if (relock)
8325 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008326
8327 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008328}
8329
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008330#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8331
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008332static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8333 __u64 start, __u64 len)
8334{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008335 int ret;
8336
8337 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8338 if (ret)
8339 return ret;
8340
Chris Masonec29ed52011-02-23 16:23:20 -05008341 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008342}
8343
Chris Mason9ebefb182007-06-15 13:50:00 -04008344int btrfs_readpage(struct file *file, struct page *page)
8345{
Chris Masond1310b22008-01-24 16:13:08 -05008346 struct extent_io_tree *tree;
8347 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008348 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008349}
Chris Mason1832a6d2007-12-21 16:27:21 -05008350
Chris Mason39279cc2007-06-12 06:35:45 -04008351static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8352{
Chris Masond1310b22008-01-24 16:13:08 -05008353 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04008354
8355
8356 if (current->flags & PF_MEMALLOC) {
8357 redirty_page_for_writepage(wbc, page);
8358 unlock_page(page);
8359 return 0;
8360 }
Chris Masond1310b22008-01-24 16:13:08 -05008361 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008362 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8363}
Chris Mason39279cc2007-06-12 06:35:45 -04008364
Eric Sandeen48a3b632013-04-25 20:41:01 +00008365static int btrfs_writepages(struct address_space *mapping,
8366 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008367{
Chris Masond1310b22008-01-24 16:13:08 -05008368 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008369
Chris Masond1310b22008-01-24 16:13:08 -05008370 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008371 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8372}
8373
Chris Mason3ab2fb52007-11-08 10:59:22 -05008374static int
8375btrfs_readpages(struct file *file, struct address_space *mapping,
8376 struct list_head *pages, unsigned nr_pages)
8377{
Chris Masond1310b22008-01-24 16:13:08 -05008378 struct extent_io_tree *tree;
8379 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008380 return extent_readpages(tree, mapping, pages, nr_pages,
8381 btrfs_get_extent);
8382}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008383static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008384{
Chris Masond1310b22008-01-24 16:13:08 -05008385 struct extent_io_tree *tree;
8386 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008387 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008388
Chris Masond1310b22008-01-24 16:13:08 -05008389 tree = &BTRFS_I(page->mapping->host)->io_tree;
8390 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008391 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008392 if (ret == 1) {
8393 ClearPagePrivate(page);
8394 set_page_private(page, 0);
8395 page_cache_release(page);
8396 }
8397 return ret;
8398}
Chris Mason39279cc2007-06-12 06:35:45 -04008399
Chris Masone6dcd2d2008-07-17 12:53:50 -04008400static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8401{
Chris Mason98509cf2008-09-11 15:51:43 -04008402 if (PageWriteback(page) || PageDirty(page))
8403 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008404 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008405}
8406
Lukas Czernerd47992f2013-05-21 23:17:23 -04008407static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8408 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008409{
Josef Bacik5fd02042012-05-02 14:00:54 -04008410 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008411 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008412 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008413 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008414 u64 page_start = page_offset(page);
8415 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008416 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008417
Chris Mason8b62b722009-09-02 16:53:46 -04008418 /*
8419 * we have the page locked, so new writeback can't start,
8420 * and the dirty bit won't be cleared while we are here.
8421 *
8422 * Wait for IO on this page so that we can safely clear
8423 * the PagePrivate2 bit and do ordered accounting
8424 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008425 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008426
Josef Bacik5fd02042012-05-02 14:00:54 -04008427 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008428 if (offset) {
8429 btrfs_releasepage(page, GFP_NOFS);
8430 return;
8431 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008432
8433 if (!inode_evicting)
8434 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8435 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008436 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008437 /*
8438 * IO on this page will never be started, so we need
8439 * to account for any ordered extents now
8440 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008441 if (!inode_evicting)
8442 clear_extent_bit(tree, page_start, page_end,
8443 EXTENT_DIRTY | EXTENT_DELALLOC |
8444 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8445 EXTENT_DEFRAG, 1, 0, &cached_state,
8446 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008447 /*
8448 * whoever cleared the private bit is responsible
8449 * for the finish_ordered_io
8450 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008451 if (TestClearPagePrivate2(page)) {
8452 struct btrfs_ordered_inode_tree *tree;
8453 u64 new_len;
8454
8455 tree = &BTRFS_I(inode)->ordered_tree;
8456
8457 spin_lock_irq(&tree->lock);
8458 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8459 new_len = page_start - ordered->file_offset;
8460 if (new_len < ordered->truncated_len)
8461 ordered->truncated_len = new_len;
8462 spin_unlock_irq(&tree->lock);
8463
8464 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8465 page_start,
8466 PAGE_CACHE_SIZE, 1))
8467 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008468 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008469 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008470 if (!inode_evicting) {
8471 cached_state = NULL;
8472 lock_extent_bits(tree, page_start, page_end, 0,
8473 &cached_state);
8474 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008475 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008476
8477 if (!inode_evicting) {
8478 clear_extent_bit(tree, page_start, page_end,
8479 EXTENT_LOCKED | EXTENT_DIRTY |
8480 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8481 EXTENT_DEFRAG, 1, 1,
8482 &cached_state, GFP_NOFS);
8483
8484 __btrfs_releasepage(page, GFP_NOFS);
8485 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008486
Chris Mason4a096752008-07-21 10:29:44 -04008487 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008488 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008489 ClearPagePrivate(page);
8490 set_page_private(page, 0);
8491 page_cache_release(page);
8492 }
Chris Mason39279cc2007-06-12 06:35:45 -04008493}
8494
Chris Mason9ebefb182007-06-15 13:50:00 -04008495/*
8496 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8497 * called from a page fault handler when a page is first dirtied. Hence we must
8498 * be careful to check for EOF conditions here. We set the page up correctly
8499 * for a written page which means we get ENOSPC checking when writing into
8500 * holes and correct delalloc and unwritten extent mapping on filesystems that
8501 * support these features.
8502 *
8503 * We are not allowed to take the i_mutex here so we have to play games to
8504 * protect against truncate races as the page could now be beyond EOF. Because
8505 * vmtruncate() writes the inode size before removing pages, once we have the
8506 * page lock we can determine safely if the page is beyond EOF. If it is not
8507 * beyond EOF, then the page is guaranteed safe against truncation until we
8508 * unlock the page.
8509 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008510int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008511{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008512 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008513 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008514 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008515 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8516 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008517 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008518 char *kaddr;
8519 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008520 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008521 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008522 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008523 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008524 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008525
Jan Karab2b5ef52012-06-12 16:20:45 +02008526 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04008527 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008528 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008529 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008530 reserved = 1;
8531 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008532 if (ret) {
8533 if (ret == -ENOMEM)
8534 ret = VM_FAULT_OOM;
8535 else /* -ENOSPC, -EIO, etc */
8536 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008537 if (reserved)
8538 goto out;
8539 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008540 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008541
Nick Piggin56a76f82009-03-31 15:23:23 -07008542 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008543again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008544 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008545 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008546 page_start = page_offset(page);
8547 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04008548
Chris Mason9ebefb182007-06-15 13:50:00 -04008549 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008550 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008551 /* page got truncated out from underneath us */
8552 goto out_unlock;
8553 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008554 wait_on_page_writeback(page);
8555
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008556 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008557 set_page_extent_mapped(page);
8558
Chris Masoneb84ae02008-07-17 13:53:27 -04008559 /*
8560 * we can't set the delalloc bits if there are pending ordered
8561 * extents. Drop our locks and wait for them to finish
8562 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008563 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8564 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008565 unlock_extent_cached(io_tree, page_start, page_end,
8566 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008567 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008568 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008569 btrfs_put_ordered_extent(ordered);
8570 goto again;
8571 }
8572
Josef Bacikfbf19082009-10-01 17:10:23 -04008573 /*
8574 * XXX - page_mkwrite gets called every time the page is dirtied, even
8575 * if it was already dirty, so for space accounting reasons we need to
8576 * clear any delalloc bits for the range we are fixing to save. There
8577 * is probably a better way to do this, but for now keep consistent with
8578 * prepare_pages in the normal write path.
8579 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008580 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008581 EXTENT_DIRTY | EXTENT_DELALLOC |
8582 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008583 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008584
Josef Bacik2ac55d42010-02-03 19:33:23 +00008585 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8586 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008587 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008588 unlock_extent_cached(io_tree, page_start, page_end,
8589 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008590 ret = VM_FAULT_SIGBUS;
8591 goto out_unlock;
8592 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008593 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008594
8595 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008596 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008597 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008598 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008599 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008600
Chris Masone6dcd2d2008-07-17 12:53:50 -04008601 if (zero_start != PAGE_CACHE_SIZE) {
8602 kaddr = kmap(page);
8603 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8604 flush_dcache_page(page);
8605 kunmap(page);
8606 }
Chris Mason247e7432008-07-17 12:53:51 -04008607 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008608 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008609 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008610
Chris Mason257c62e2009-10-13 13:21:08 -04008611 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8612 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008613 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008614
Josef Bacik2ac55d42010-02-03 19:33:23 +00008615 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008616
8617out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008618 if (!ret) {
8619 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008620 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008621 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008622 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008623out:
Josef Bacikec39e182012-01-12 19:10:12 -05008624 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008625out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008626 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008627 return ret;
8628}
8629
Josef Bacika41ad392011-01-31 15:30:16 -05008630static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008631{
8632 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008633 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008634 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008635 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008636 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008637 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008638 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008639
Josef Bacik0ef8b722013-10-25 16:13:35 -04008640 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8641 (u64)-1);
8642 if (ret)
8643 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008644
Josef Bacikfcb80c22011-05-03 10:40:22 -04008645 /*
8646 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8647 * 3 things going on here
8648 *
8649 * 1) We need to reserve space for our orphan item and the space to
8650 * delete our orphan item. Lord knows we don't want to have a dangling
8651 * orphan item because we didn't reserve space to remove it.
8652 *
8653 * 2) We need to reserve space to update our inode.
8654 *
8655 * 3) We need to have something to cache all the space that is going to
8656 * be free'd up by the truncate operation, but also have some slack
8657 * space reserved in case it uses space during the truncate (thank you
8658 * very much snapshotting).
8659 *
8660 * And we need these to all be seperate. The fact is we can use alot of
8661 * space doing the truncate, and we have no earthly idea how much space
8662 * we will use, so we need the truncate reservation to be seperate so it
8663 * doesn't end up using space reserved for updating the inode or
8664 * removing the orphan item. We also need to be able to stop the
8665 * transaction and start a new one, which means we need to be able to
8666 * update the inode several times, and we have no idea of knowing how
8667 * many times that will be, so we can't just reserve 1 item for the
8668 * entirety of the opration, so that has to be done seperately as well.
8669 * Then there is the orphan item, which does indeed need to be held on
8670 * to for the whole operation, and we need nobody to touch this reserved
8671 * space except the orphan code.
8672 *
8673 * So that leaves us with
8674 *
8675 * 1) root->orphan_block_rsv - for the orphan deletion.
8676 * 2) rsv - for the truncate reservation, which we will steal from the
8677 * transaction reservation.
8678 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8679 * updating the inode.
8680 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008681 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008682 if (!rsv)
8683 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008684 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008685 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008686
Josef Bacik907cbce2011-08-08 13:46:15 -04008687 /*
Josef Bacik07127182011-08-19 10:29:59 -04008688 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008689 * 1 for updating the inode.
8690 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008691 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008692 if (IS_ERR(trans)) {
8693 err = PTR_ERR(trans);
8694 goto out;
8695 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008696
Josef Bacik907cbce2011-08-08 13:46:15 -04008697 /* Migrate the slack space for the truncate to our reserve */
8698 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8699 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008700 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008701
Chris Mason5a3f23d2009-03-31 13:27:11 -04008702 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008703 * So if we truncate and then write and fsync we normally would just
8704 * write the extents that changed, which is a problem if we need to
8705 * first truncate that entire inode. So set this flag so we write out
8706 * all of the extents in the inode to the sync log so we're completely
8707 * safe.
8708 */
8709 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008710 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008711
Yan, Zheng80825102009-11-12 09:35:36 +00008712 while (1) {
8713 ret = btrfs_truncate_inode_items(trans, root, inode,
8714 inode->i_size,
8715 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08008716 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05008717 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008718 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008719 }
Chris Mason39279cc2007-06-12 06:35:45 -04008720
Josef Bacikfcb80c22011-05-03 10:40:22 -04008721 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008722 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008723 if (ret) {
8724 err = ret;
8725 break;
8726 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008727
Yan, Zheng80825102009-11-12 09:35:36 +00008728 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008729 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008730
8731 trans = btrfs_start_transaction(root, 2);
8732 if (IS_ERR(trans)) {
8733 ret = err = PTR_ERR(trans);
8734 trans = NULL;
8735 break;
8736 }
8737
8738 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8739 rsv, min_size);
8740 BUG_ON(ret); /* shouldn't happen */
8741 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008742 }
8743
8744 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008745 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008746 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008747 if (ret)
8748 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008749 }
8750
Chris Mason917c16b2011-11-08 14:49:59 -05008751 if (trans) {
8752 trans->block_rsv = &root->fs_info->trans_block_rsv;
8753 ret = btrfs_update_inode(trans, root, inode);
8754 if (ret && !err)
8755 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008756
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008757 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008758 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008759 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008760
8761out:
8762 btrfs_free_block_rsv(root, rsv);
8763
Josef Bacik3893e332011-01-31 16:03:11 -05008764 if (ret && !err)
8765 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008766
Josef Bacik3893e332011-01-31 16:03:11 -05008767 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008768}
8769
Sven Wegener3b963622008-06-09 21:57:42 -04008770/*
Chris Masond352ac62008-09-29 15:18:18 -04008771 * create a new subvolume directory/inode (helper for the ioctl).
8772 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008773int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008774 struct btrfs_root *new_root,
8775 struct btrfs_root *parent_root,
8776 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008777{
Chris Mason39279cc2007-06-12 06:35:45 -04008778 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008779 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008780 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008781
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008782 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8783 new_dirid, new_dirid,
8784 S_IFDIR | (~current_umask() & S_IRWXUGO),
8785 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008786 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008787 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008788 inode->i_op = &btrfs_dir_inode_operations;
8789 inode->i_fop = &btrfs_dir_file_operations;
8790
Miklos Szeredibfe86842011-10-28 14:13:29 +02008791 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008792 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008793 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008794
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008795 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8796 if (err)
8797 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008798 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008799 new_root->root_key.objectid, err);
8800
Yan, Zheng76dda932009-09-21 16:00:26 -04008801 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008802
Yan, Zheng76dda932009-09-21 16:00:26 -04008803 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008804 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008805}
8806
Chris Mason39279cc2007-06-12 06:35:45 -04008807struct inode *btrfs_alloc_inode(struct super_block *sb)
8808{
8809 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008810 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008811
8812 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8813 if (!ei)
8814 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008815
8816 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008817 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008818 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008819 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008820 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008821 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008822 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008823 ei->disk_i_size = 0;
8824 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008825 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008826 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008827 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008828 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008829 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008830
Josef Bacik9e0baf62011-07-15 15:16:44 +00008831 spin_lock_init(&ei->lock);
8832 ei->outstanding_extents = 0;
8833 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008834
Josef Bacik72ac3c02012-05-23 14:13:11 -04008835 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008836 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008837
Miao Xie16cdcec2011-04-22 18:12:22 +08008838 ei->delayed_node = NULL;
8839
chandan r9cc97d62012-07-04 12:48:07 +05308840 ei->i_otime.tv_sec = 0;
8841 ei->i_otime.tv_nsec = 0;
8842
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008843 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008844 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008845 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8846 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008847 ei->io_tree.track_uptodate = 1;
8848 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008849 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008850 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008851 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008852 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008853 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008854 RB_CLEAR_NODE(&ei->rb_node);
8855
8856 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008857}
8858
Josef Bacikaaedb552013-10-11 14:44:09 -04008859#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8860void btrfs_test_destroy_inode(struct inode *inode)
8861{
8862 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8863 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8864}
8865#endif
8866
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008867static void btrfs_i_callback(struct rcu_head *head)
8868{
8869 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008870 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8871}
8872
Chris Mason39279cc2007-06-12 06:35:45 -04008873void btrfs_destroy_inode(struct inode *inode)
8874{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008875 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008876 struct btrfs_root *root = BTRFS_I(inode)->root;
8877
Al Virob3d9b7a2012-06-09 13:51:19 -04008878 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008879 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008880 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8881 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008882 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8883 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008884 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008885
Chris Mason5a3f23d2009-03-31 13:27:11 -04008886 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008887 * This can happen where we create an inode, but somebody else also
8888 * created the same inode and we need to destroy the one we already
8889 * created.
8890 */
8891 if (!root)
8892 goto free;
8893
Josef Bacik8a35d952012-05-23 14:26:42 -04008894 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8895 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008896 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008897 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008898 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008899 }
Josef Bacik7b128762008-07-24 12:17:14 -04008900
Chris Masond3977122009-01-05 21:25:51 -05008901 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008902 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8903 if (!ordered)
8904 break;
8905 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008906 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008907 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008908 btrfs_remove_ordered_extent(inode, ordered);
8909 btrfs_put_ordered_extent(ordered);
8910 btrfs_put_ordered_extent(ordered);
8911 }
8912 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008913 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008914 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008915free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008916 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008917}
8918
Al Viro45321ac2010-06-07 13:43:19 -04008919int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008920{
8921 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008922
Naohiro Aota6379ef92013-06-06 09:56:34 +00008923 if (root == NULL)
8924 return 1;
8925
Liu Bofa6ac872013-02-20 14:10:23 +00008926 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008927 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008928 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008929 else
Al Viro45321ac2010-06-07 13:43:19 -04008930 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008931}
8932
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008933static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008934{
8935 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8936
8937 inode_init_once(&ei->vfs_inode);
8938}
8939
8940void btrfs_destroy_cachep(void)
8941{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008942 /*
8943 * Make sure all delayed rcu free inodes are flushed before we
8944 * destroy cache.
8945 */
8946 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008947 if (btrfs_inode_cachep)
8948 kmem_cache_destroy(btrfs_inode_cachep);
8949 if (btrfs_trans_handle_cachep)
8950 kmem_cache_destroy(btrfs_trans_handle_cachep);
8951 if (btrfs_transaction_cachep)
8952 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008953 if (btrfs_path_cachep)
8954 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008955 if (btrfs_free_space_cachep)
8956 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008957 if (btrfs_delalloc_work_cachep)
8958 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008959}
8960
8961int btrfs_init_cachep(void)
8962{
David Sterba837e1972012-09-07 03:00:48 -06008963 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008964 sizeof(struct btrfs_inode), 0,
8965 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008966 if (!btrfs_inode_cachep)
8967 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008968
David Sterba837e1972012-09-07 03:00:48 -06008969 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008970 sizeof(struct btrfs_trans_handle), 0,
8971 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008972 if (!btrfs_trans_handle_cachep)
8973 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008974
David Sterba837e1972012-09-07 03:00:48 -06008975 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008976 sizeof(struct btrfs_transaction), 0,
8977 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008978 if (!btrfs_transaction_cachep)
8979 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008980
David Sterba837e1972012-09-07 03:00:48 -06008981 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008982 sizeof(struct btrfs_path), 0,
8983 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008984 if (!btrfs_path_cachep)
8985 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008986
David Sterba837e1972012-09-07 03:00:48 -06008987 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008988 sizeof(struct btrfs_free_space), 0,
8989 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8990 if (!btrfs_free_space_cachep)
8991 goto fail;
8992
Miao Xie8ccf6f192012-10-25 09:28:04 +00008993 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8994 sizeof(struct btrfs_delalloc_work), 0,
8995 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8996 NULL);
8997 if (!btrfs_delalloc_work_cachep)
8998 goto fail;
8999
Chris Mason39279cc2007-06-12 06:35:45 -04009000 return 0;
9001fail:
9002 btrfs_destroy_cachep();
9003 return -ENOMEM;
9004}
9005
9006static int btrfs_getattr(struct vfsmount *mnt,
9007 struct dentry *dentry, struct kstat *stat)
9008{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009009 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04009010 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05009011 u32 blocksize = inode->i_sb->s_blocksize;
9012
Chris Mason39279cc2007-06-12 06:35:45 -04009013 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009014 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05009015 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009016
9017 spin_lock(&BTRFS_I(inode)->lock);
9018 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9019 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009020 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009021 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009022 return 0;
9023}
9024
Chris Masond3977122009-01-05 21:25:51 -05009025static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9026 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009027{
9028 struct btrfs_trans_handle *trans;
9029 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009030 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04009031 struct inode *new_inode = new_dentry->d_inode;
9032 struct inode *old_inode = old_dentry->d_inode;
9033 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009034 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009035 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009036 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009037 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009038
Li Zefan33345d012011-04-20 10:31:50 +08009039 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009040 return -EPERM;
9041
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009042 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009043 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009044 return -EXDEV;
9045
Li Zefan33345d012011-04-20 10:31:50 +08009046 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9047 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009048 return -ENOTEMPTY;
9049
Chris Mason39279cc2007-06-12 06:35:45 -04009050 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009051 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009052 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009053
9054
9055 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009056 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009057 new_dentry->d_name.name,
9058 new_dentry->d_name.len);
9059
9060 if (ret) {
9061 if (ret == -EEXIST) {
9062 /* we shouldn't get
9063 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309064 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009065 return ret;
9066 }
9067 } else {
9068 /* maybe -EOVERFLOW */
9069 return ret;
9070 }
9071 }
9072 ret = 0;
9073
Chris Mason5a3f23d2009-03-31 13:27:11 -04009074 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009075 * we're using rename to replace one file with another. Start IO on it
9076 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009077 */
Chris Mason8d875f92014-08-12 10:47:42 -07009078 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009079 filemap_flush(old_inode->i_mapping);
9080
Yan, Zheng76dda932009-09-21 16:00:26 -04009081 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009082 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009083 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009084 /*
9085 * We want to reserve the absolute worst case amount of items. So if
9086 * both inodes are subvols and we need to unlink them then that would
9087 * require 4 item modifications, but if they are both normal inodes it
9088 * would require 5 item modifications, so we'll assume their normal
9089 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9090 * should cover the worst case number of items we'll modify.
9091 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04009092 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009093 if (IS_ERR(trans)) {
9094 ret = PTR_ERR(trans);
9095 goto out_notrans;
9096 }
Chris Mason5f39d392007-10-15 16:14:19 -04009097
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009098 if (dest != root)
9099 btrfs_record_root_in_trans(trans, dest);
9100
Yan, Zhenga5719522009-09-24 09:17:31 -04009101 ret = btrfs_set_inode_index(new_dir, &index);
9102 if (ret)
9103 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009104
Miao Xie67de1172013-12-26 13:07:06 +08009105 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009106 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009107 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009108 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009109 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009110 ret = btrfs_insert_inode_ref(trans, dest,
9111 new_dentry->d_name.name,
9112 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009113 old_ino,
9114 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009115 if (ret)
9116 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009117 /*
9118 * this is an ugly little race, but the rename is required
9119 * to make sure that if we crash, the inode is either at the
9120 * old name or the new one. pinning the log transaction lets
9121 * us make sure we don't allow a log commit to come in after
9122 * we unlink the name but before we add the new name back in.
9123 */
9124 btrfs_pin_log_trans(root);
9125 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009126
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009127 inode_inc_iversion(old_dir);
9128 inode_inc_iversion(new_dir);
9129 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009130 old_dir->i_ctime = old_dir->i_mtime = ctime;
9131 new_dir->i_ctime = new_dir->i_mtime = ctime;
9132 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04009133
Chris Mason12fcfd22009-03-24 10:24:20 -04009134 if (old_dentry->d_parent != new_dentry->d_parent)
9135 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9136
Li Zefan33345d012011-04-20 10:31:50 +08009137 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009138 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9139 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9140 old_dentry->d_name.name,
9141 old_dentry->d_name.len);
9142 } else {
Al Viro92986792011-03-04 17:14:37 +00009143 ret = __btrfs_unlink_inode(trans, root, old_dir,
9144 old_dentry->d_inode,
9145 old_dentry->d_name.name,
9146 old_dentry->d_name.len);
9147 if (!ret)
9148 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009149 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009150 if (ret) {
9151 btrfs_abort_transaction(trans, root, ret);
9152 goto out_fail;
9153 }
Chris Mason39279cc2007-06-12 06:35:45 -04009154
9155 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009156 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009157 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08009158 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009159 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9160 root_objectid = BTRFS_I(new_inode)->location.objectid;
9161 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9162 root_objectid,
9163 new_dentry->d_name.name,
9164 new_dentry->d_name.len);
9165 BUG_ON(new_inode->i_nlink == 0);
9166 } else {
9167 ret = btrfs_unlink_inode(trans, dest, new_dir,
9168 new_dentry->d_inode,
9169 new_dentry->d_name.name,
9170 new_dentry->d_name.len);
9171 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009172 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04009173 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009174 if (ret) {
9175 btrfs_abort_transaction(trans, root, ret);
9176 goto out_fail;
9177 }
Chris Mason39279cc2007-06-12 06:35:45 -04009178 }
Josef Bacikaec74772008-07-24 12:12:38 -04009179
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009180 ret = btrfs_add_link(trans, new_dir, old_inode,
9181 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009182 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009183 if (ret) {
9184 btrfs_abort_transaction(trans, root, ret);
9185 goto out_fail;
9186 }
Chris Mason39279cc2007-06-12 06:35:45 -04009187
Miao Xie67de1172013-12-26 13:07:06 +08009188 if (old_inode->i_nlink == 1)
9189 BTRFS_I(old_inode)->dir_index = index;
9190
Li Zefan33345d012011-04-20 10:31:50 +08009191 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04009192 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00009193 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009194 btrfs_end_log_trans(root);
9195 }
Chris Mason39279cc2007-06-12 06:35:45 -04009196out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009197 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009198out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009199 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009200 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009201
Chris Mason39279cc2007-06-12 06:35:45 -04009202 return ret;
9203}
9204
Miklos Szeredi80ace852014-07-23 15:15:32 +02009205static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9206 struct inode *new_dir, struct dentry *new_dentry,
9207 unsigned int flags)
9208{
9209 if (flags & ~RENAME_NOREPLACE)
9210 return -EINVAL;
9211
9212 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
9213}
9214
Miao Xie8ccf6f192012-10-25 09:28:04 +00009215static void btrfs_run_delalloc_work(struct btrfs_work *work)
9216{
9217 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009218 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009219
9220 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9221 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009222 inode = delalloc_work->inode;
9223 if (delalloc_work->wait) {
9224 btrfs_wait_ordered_range(inode, 0, (u64)-1);
9225 } else {
9226 filemap_flush(inode->i_mapping);
9227 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9228 &BTRFS_I(inode)->runtime_flags))
9229 filemap_flush(inode->i_mapping);
9230 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00009231
9232 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009233 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009234 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009235 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009236 complete(&delalloc_work->completion);
9237}
9238
9239struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
9240 int wait, int delay_iput)
9241{
9242 struct btrfs_delalloc_work *work;
9243
9244 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
9245 if (!work)
9246 return NULL;
9247
9248 init_completion(&work->completion);
9249 INIT_LIST_HEAD(&work->list);
9250 work->inode = inode;
9251 work->wait = wait;
9252 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009253 WARN_ON_ONCE(!inode);
9254 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9255 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009256
9257 return work;
9258}
9259
9260void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9261{
9262 wait_for_completion(&work->completion);
9263 kmem_cache_free(btrfs_delalloc_work_cachep, work);
9264}
9265
Chris Masond352ac62008-09-29 15:18:18 -04009266/*
9267 * some fairly slow code that needs optimization. This walks the list
9268 * of all the inodes with pending delalloc and forces them to disk.
9269 */
Miao Xie6c255e62014-03-06 13:55:01 +08009270static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9271 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009272{
Chris Masonea8c2812008-08-04 23:17:27 -04009273 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009274 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009275 struct btrfs_delalloc_work *work, *next;
9276 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009277 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009278 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009279
Miao Xie8ccf6f192012-10-25 09:28:04 +00009280 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009281 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009282
Miao Xie573bfb72014-03-06 13:55:03 +08009283 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009284 spin_lock(&root->delalloc_lock);
9285 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009286 while (!list_empty(&splice)) {
9287 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009288 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009289
Miao Xieeb73c1b2013-05-15 07:48:22 +00009290 list_move_tail(&binode->delalloc_inodes,
9291 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009292 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009293 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009294 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009295 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009296 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009297 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009298
9299 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009300 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009301 if (delay_iput)
9302 btrfs_add_delayed_iput(inode);
9303 else
9304 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009305 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009306 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009307 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009308 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009309 btrfs_queue_work(root->fs_info->flush_workers,
9310 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009311 ret++;
9312 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009313 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009314 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009315 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009316 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009317 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009318
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009319out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009320 list_for_each_entry_safe(work, next, &works, list) {
9321 list_del_init(&work->list);
9322 btrfs_wait_and_free_delalloc_work(work);
9323 }
9324
Miao Xieeb73c1b2013-05-15 07:48:22 +00009325 if (!list_empty_careful(&splice)) {
9326 spin_lock(&root->delalloc_lock);
9327 list_splice_tail(&splice, &root->delalloc_inodes);
9328 spin_unlock(&root->delalloc_lock);
9329 }
Miao Xie573bfb72014-03-06 13:55:03 +08009330 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009331 return ret;
9332}
9333
9334int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9335{
9336 int ret;
9337
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009338 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009339 return -EROFS;
9340
Miao Xie6c255e62014-03-06 13:55:01 +08009341 ret = __start_delalloc_inodes(root, delay_iput, -1);
9342 if (ret > 0)
9343 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009344 /*
9345 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009346 * we have to make sure the IO is actually started and that
9347 * ordered extents get created before we return
9348 */
9349 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009350 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009351 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009352 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009353 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9354 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009355 }
9356 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009357 return ret;
9358}
9359
Miao Xie6c255e62014-03-06 13:55:01 +08009360int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9361 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009362{
9363 struct btrfs_root *root;
9364 struct list_head splice;
9365 int ret;
9366
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009367 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009368 return -EROFS;
9369
9370 INIT_LIST_HEAD(&splice);
9371
Miao Xie573bfb72014-03-06 13:55:03 +08009372 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009373 spin_lock(&fs_info->delalloc_root_lock);
9374 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009375 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009376 root = list_first_entry(&splice, struct btrfs_root,
9377 delalloc_root);
9378 root = btrfs_grab_fs_root(root);
9379 BUG_ON(!root);
9380 list_move_tail(&root->delalloc_root,
9381 &fs_info->delalloc_roots);
9382 spin_unlock(&fs_info->delalloc_root_lock);
9383
Miao Xie6c255e62014-03-06 13:55:01 +08009384 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009385 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009386 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009387 goto out;
9388
Miao Xie6c255e62014-03-06 13:55:01 +08009389 if (nr != -1) {
9390 nr -= ret;
9391 WARN_ON(nr < 0);
9392 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009393 spin_lock(&fs_info->delalloc_root_lock);
9394 }
9395 spin_unlock(&fs_info->delalloc_root_lock);
9396
Miao Xie6c255e62014-03-06 13:55:01 +08009397 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009398 atomic_inc(&fs_info->async_submit_draining);
9399 while (atomic_read(&fs_info->nr_async_submits) ||
9400 atomic_read(&fs_info->async_delalloc_pages)) {
9401 wait_event(fs_info->async_submit_wait,
9402 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9403 atomic_read(&fs_info->async_delalloc_pages) == 0));
9404 }
9405 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009406out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009407 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009408 spin_lock(&fs_info->delalloc_root_lock);
9409 list_splice_tail(&splice, &fs_info->delalloc_roots);
9410 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009411 }
Miao Xie573bfb72014-03-06 13:55:03 +08009412 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009413 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009414}
9415
Chris Mason39279cc2007-06-12 06:35:45 -04009416static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9417 const char *symname)
9418{
9419 struct btrfs_trans_handle *trans;
9420 struct btrfs_root *root = BTRFS_I(dir)->root;
9421 struct btrfs_path *path;
9422 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009423 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009424 int err;
9425 int drop_inode = 0;
9426 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309427 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009428 int name_len;
9429 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009430 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009431 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009432 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009433
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009434 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009435 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9436 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009437
Josef Bacik9ed74f22009-09-11 16:12:44 -04009438 /*
9439 * 2 items for inode item and ref
9440 * 2 items for dir items
9441 * 1 item for xattr if selinux is on
9442 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009443 trans = btrfs_start_transaction(root, 5);
9444 if (IS_ERR(trans))
9445 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009446
Li Zefan581bb052011-04-20 10:06:11 +08009447 err = btrfs_find_free_ino(root, &objectid);
9448 if (err)
9449 goto out_unlock;
9450
Josef Bacikaec74772008-07-24 12:12:38 -04009451 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009452 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04009453 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009454 if (IS_ERR(inode)) {
9455 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009456 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009457 }
Chris Mason39279cc2007-06-12 06:35:45 -04009458
Casey Schauflerad19db72011-12-15 10:09:07 -05009459 /*
9460 * If the active LSM wants to access the inode during
9461 * d_instantiate it needs these. Smack checks to see
9462 * if the filesystem supports xattrs by looking at the
9463 * ops vector.
9464 */
9465 inode->i_fop = &btrfs_file_operations;
9466 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009467 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009468 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9469
9470 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9471 if (err)
9472 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009473
Josef Bacika1b075d2010-11-19 20:36:11 +00009474 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009475 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009476 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009477
9478 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009479 if (!path) {
9480 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009481 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009482 }
Li Zefan33345d012011-04-20 10:31:50 +08009483 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009484 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009485 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009486 datasize = btrfs_file_extent_calc_inline_size(name_len);
9487 err = btrfs_insert_empty_item(trans, root, path, &key,
9488 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009489 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009490 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009491 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009492 }
Chris Mason5f39d392007-10-15 16:14:19 -04009493 leaf = path->nodes[0];
9494 ei = btrfs_item_ptr(leaf, path->slots[0],
9495 struct btrfs_file_extent_item);
9496 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9497 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009498 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009499 btrfs_set_file_extent_encryption(leaf, ei, 0);
9500 btrfs_set_file_extent_compression(leaf, ei, 0);
9501 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9502 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9503
Chris Mason39279cc2007-06-12 06:35:45 -04009504 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009505 write_extent_buffer(leaf, symname, ptr, name_len);
9506 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009507 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009508
Chris Mason39279cc2007-06-12 06:35:45 -04009509 inode->i_op = &btrfs_symlink_inode_operations;
9510 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -04009511 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009512 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009513 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009514 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009515 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009516 goto out_unlock_inode;
9517 }
9518
9519 unlock_new_inode(inode);
9520 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009521
9522out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009523 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009524 if (drop_inode) {
9525 inode_dec_link_count(inode);
9526 iput(inode);
9527 }
Liu Bob53d3f52012-11-14 14:34:34 +00009528 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009529 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009530
9531out_unlock_inode:
9532 drop_inode = 1;
9533 unlock_new_inode(inode);
9534 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009535}
Chris Mason16432982008-04-10 10:23:21 -04009536
Josef Bacik0af3d002010-06-21 14:48:16 -04009537static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9538 u64 start, u64 num_bytes, u64 min_size,
9539 loff_t actual_len, u64 *alloc_hint,
9540 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009541{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009542 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9543 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009544 struct btrfs_root *root = BTRFS_I(inode)->root;
9545 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009546 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009547 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009548 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009549 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009550 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009551
Josef Bacik0af3d002010-06-21 14:48:16 -04009552 if (trans)
9553 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009554 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009555 if (own_trans) {
9556 trans = btrfs_start_transaction(root, 3);
9557 if (IS_ERR(trans)) {
9558 ret = PTR_ERR(trans);
9559 break;
9560 }
Yan Zhengd899e052008-10-30 14:25:28 -04009561 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009562
Chris Mason154ea282013-03-05 11:11:26 -05009563 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9564 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009565 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009566 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009567 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009568 if (own_trans)
9569 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009570 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009571 }
9572
Yan Zhengd899e052008-10-30 14:25:28 -04009573 ret = insert_reserved_file_extent(trans, inode,
9574 cur_offset, ins.objectid,
9575 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009576 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009577 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009578 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009579 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009580 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009581 btrfs_abort_transaction(trans, root, ret);
9582 if (own_trans)
9583 btrfs_end_transaction(trans, root);
9584 break;
9585 }
Chris Masona1ed8352009-09-11 12:27:37 -04009586 btrfs_drop_extent_cache(inode, cur_offset,
9587 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009588
Josef Bacik5dc562c2012-08-17 13:14:17 -04009589 em = alloc_extent_map();
9590 if (!em) {
9591 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9592 &BTRFS_I(inode)->runtime_flags);
9593 goto next;
9594 }
9595
9596 em->start = cur_offset;
9597 em->orig_start = cur_offset;
9598 em->len = ins.offset;
9599 em->block_start = ins.objectid;
9600 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009601 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009602 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009603 em->bdev = root->fs_info->fs_devices->latest_bdev;
9604 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9605 em->generation = trans->transid;
9606
9607 while (1) {
9608 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009609 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009610 write_unlock(&em_tree->lock);
9611 if (ret != -EEXIST)
9612 break;
9613 btrfs_drop_extent_cache(inode, cur_offset,
9614 cur_offset + ins.offset - 1,
9615 0);
9616 }
9617 free_extent_map(em);
9618next:
Yan Zhengd899e052008-10-30 14:25:28 -04009619 num_bytes -= ins.offset;
9620 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009621 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009622
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009623 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009624 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009625 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009626 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009627 (actual_len > inode->i_size) &&
9628 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009629 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009630 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009631 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009632 i_size = cur_offset;
9633 i_size_write(inode, i_size);
9634 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009635 }
9636
Yan Zhengd899e052008-10-30 14:25:28 -04009637 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009638
9639 if (ret) {
9640 btrfs_abort_transaction(trans, root, ret);
9641 if (own_trans)
9642 btrfs_end_transaction(trans, root);
9643 break;
9644 }
Yan Zhengd899e052008-10-30 14:25:28 -04009645
Josef Bacik0af3d002010-06-21 14:48:16 -04009646 if (own_trans)
9647 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009648 }
Yan Zhengd899e052008-10-30 14:25:28 -04009649 return ret;
9650}
9651
Josef Bacik0af3d002010-06-21 14:48:16 -04009652int btrfs_prealloc_file_range(struct inode *inode, int mode,
9653 u64 start, u64 num_bytes, u64 min_size,
9654 loff_t actual_len, u64 *alloc_hint)
9655{
9656 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9657 min_size, actual_len, alloc_hint,
9658 NULL);
9659}
9660
9661int btrfs_prealloc_file_range_trans(struct inode *inode,
9662 struct btrfs_trans_handle *trans, int mode,
9663 u64 start, u64 num_bytes, u64 min_size,
9664 loff_t actual_len, u64 *alloc_hint)
9665{
9666 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9667 min_size, actual_len, alloc_hint, trans);
9668}
9669
Chris Masone6dcd2d2008-07-17 12:53:50 -04009670static int btrfs_set_page_dirty(struct page *page)
9671{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009672 return __set_page_dirty_nobuffers(page);
9673}
9674
Al Viro10556cb2011-06-20 19:28:19 -04009675static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009676{
Li Zefanb83cc962010-12-20 16:04:08 +08009677 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009678 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009679
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009680 if (mask & MAY_WRITE &&
9681 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9682 if (btrfs_root_readonly(root))
9683 return -EROFS;
9684 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9685 return -EACCES;
9686 }
Al Viro2830ba72011-06-20 19:16:29 -04009687 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009688}
Chris Mason39279cc2007-06-12 06:35:45 -04009689
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009690static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9691{
9692 struct btrfs_trans_handle *trans;
9693 struct btrfs_root *root = BTRFS_I(dir)->root;
9694 struct inode *inode = NULL;
9695 u64 objectid;
9696 u64 index;
9697 int ret = 0;
9698
9699 /*
9700 * 5 units required for adding orphan entry
9701 */
9702 trans = btrfs_start_transaction(root, 5);
9703 if (IS_ERR(trans))
9704 return PTR_ERR(trans);
9705
9706 ret = btrfs_find_free_ino(root, &objectid);
9707 if (ret)
9708 goto out;
9709
9710 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9711 btrfs_ino(dir), objectid, mode, &index);
9712 if (IS_ERR(inode)) {
9713 ret = PTR_ERR(inode);
9714 inode = NULL;
9715 goto out;
9716 }
9717
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009718 inode->i_fop = &btrfs_file_operations;
9719 inode->i_op = &btrfs_file_inode_operations;
9720
9721 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009722 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9723
Chris Masonb0d5d102014-09-08 13:08:51 -07009724 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9725 if (ret)
9726 goto out_inode;
9727
9728 ret = btrfs_update_inode(trans, root, inode);
9729 if (ret)
9730 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009731 ret = btrfs_orphan_add(trans, inode);
9732 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009733 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009734
Filipe Manana5762b5c2014-08-01 00:10:32 +01009735 /*
9736 * We set number of links to 0 in btrfs_new_inode(), and here we set
9737 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9738 * through:
9739 *
9740 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9741 */
9742 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009743 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009744 d_tmpfile(dentry, inode);
9745 mark_inode_dirty(inode);
9746
9747out:
9748 btrfs_end_transaction(trans, root);
9749 if (ret)
9750 iput(inode);
9751 btrfs_balance_delayed_items(root);
9752 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009753 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009754
9755out_inode:
9756 unlock_new_inode(inode);
9757 goto out;
9758
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009759}
9760
Filipe Mananab38ef712014-11-13 17:01:45 +00009761/* Inspired by filemap_check_errors() */
9762int btrfs_inode_check_errors(struct inode *inode)
9763{
9764 int ret = 0;
9765
9766 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
9767 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
9768 ret = -ENOSPC;
9769 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
9770 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
9771 ret = -EIO;
9772
9773 return ret;
9774}
9775
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009776static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009777 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009778 .lookup = btrfs_lookup,
9779 .create = btrfs_create,
9780 .unlink = btrfs_unlink,
9781 .link = btrfs_link,
9782 .mkdir = btrfs_mkdir,
9783 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009784 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009785 .symlink = btrfs_symlink,
9786 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009787 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009788 .setxattr = btrfs_setxattr,
9789 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009790 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009791 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009792 .permission = btrfs_permission,
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,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009795 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009796 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009797};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009798static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009799 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009800 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009801 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009802 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009803 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009804};
Yan, Zheng76dda932009-09-21 16:00:26 -04009805
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009806static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009807 .llseek = generic_file_llseek,
9808 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009809 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009810 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009811#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009812 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009813#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009814 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009815 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009816};
9817
Chris Masond1310b22008-01-24 16:13:08 -05009818static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009819 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009820 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009821 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009822 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009823 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009824 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009825 .set_bit_hook = btrfs_set_bit_hook,
9826 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009827 .merge_extent_hook = btrfs_merge_extent_hook,
9828 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009829};
9830
Chris Mason35054392009-01-21 13:11:13 -05009831/*
9832 * btrfs doesn't support the bmap operation because swapfiles
9833 * use bmap to make a mapping of extents in the file. They assume
9834 * these extents won't change over the life of the file and they
9835 * use the bmap result to do IO directly to the drive.
9836 *
9837 * the btrfs bmap call would return logical addresses that aren't
9838 * suitable for IO and they also will change frequently as COW
9839 * operations happen. So, swapfile + btrfs == corruption.
9840 *
9841 * For now we're avoiding this by dropping bmap.
9842 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009843static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009844 .readpage = btrfs_readpage,
9845 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04009846 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009847 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009848 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009849 .invalidatepage = btrfs_invalidatepage,
9850 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009851 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009852 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009853};
9854
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009855static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009856 .readpage = btrfs_readpage,
9857 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009858 .invalidatepage = btrfs_invalidatepage,
9859 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009860};
9861
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009862static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009863 .getattr = btrfs_getattr,
9864 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009865 .setxattr = btrfs_setxattr,
9866 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009867 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009868 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009869 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009870 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009871 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009872 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009873 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009874};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009875static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009876 .getattr = btrfs_getattr,
9877 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009878 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009879 .setxattr = btrfs_setxattr,
9880 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009881 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009882 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009883 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009884 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009885 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009886};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009887static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009888 .readlink = generic_readlink,
9889 .follow_link = page_follow_link_light,
9890 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009891 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009892 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009893 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009894 .setxattr = btrfs_setxattr,
9895 .getxattr = btrfs_getxattr,
9896 .listxattr = btrfs_listxattr,
9897 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009898 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009899};
Yan, Zheng76dda932009-09-21 16:00:26 -04009900
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009901const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009902 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009903 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009904};