blob: 141df0ce6f9318be05e24864934e2421013d5de7 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070035#include <linux/aio.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050037#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040038#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040039#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020041#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050042#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000043#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050044#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040045#include <linux/posix_acl_xattr.h>
Guenter Roeck4a3d1ca2015-03-13 01:58:46 -070046#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040052#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040053#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040054#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020055#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040056#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050057#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050058#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080059#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000060#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040061#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000062#include "props.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070069static const struct inode_operations btrfs_dir_inode_operations;
70static const struct inode_operations btrfs_symlink_inode_operations;
71static const struct inode_operations btrfs_dir_ro_inode_operations;
72static const struct inode_operations btrfs_special_inode_operations;
73static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070074static const struct address_space_operations btrfs_aops;
75static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070076static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050077static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040078
79static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000080static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081struct kmem_cache *btrfs_trans_handle_cachep;
82struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040083struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050084struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040085
86#define S_SHIFT 12
87static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
88 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
89 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
90 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
91 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
92 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
93 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
94 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
95};
96
Eric Sandeen3972f262013-01-12 02:57:22 +000097static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050098static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040099static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500100static noinline int cow_file_range(struct inode *inode,
101 struct page *locked_page,
102 u64 start, u64 end, int *page_started,
103 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400104static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
105 u64 len, u64 orig_start,
106 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400107 u64 orig_block_len, u64 ram_bytes,
108 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400109
Eric Sandeen48a3b632013-04-25 20:41:01 +0000110static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400111
Josef Bacik6a3891c2015-03-16 17:38:52 -0400112#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
113void btrfs_test_inode_set_ops(struct inode *inode)
114{
115 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
116}
117#endif
118
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000119static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500120 struct inode *inode, struct inode *dir,
121 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500122{
123 int err;
124
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000125 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500126 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500127 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500128 return err;
129}
130
Chris Masond352ac62008-09-29 15:18:18 -0400131/*
Chris Masonc8b97812008-10-29 14:49:59 -0400132 * this does all the hard work for inserting an inline extent into
133 * the btree. The caller should have done a btrfs_drop_extents so that
134 * no overlapping inline items exist in the btree
135 */
Chris Mason40f76582014-05-21 13:35:51 -0700136static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000137 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400138 struct btrfs_root *root, struct inode *inode,
139 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000140 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400141 struct page **compressed_pages)
142{
Chris Masonc8b97812008-10-29 14:49:59 -0400143 struct extent_buffer *leaf;
144 struct page *page = NULL;
145 char *kaddr;
146 unsigned long ptr;
147 struct btrfs_file_extent_item *ei;
148 int err = 0;
149 int ret;
150 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400151 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400152
Li Zefanfe3f5662011-03-28 08:30:38 +0000153 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400154 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400155
Chris Masonc8b97812008-10-29 14:49:59 -0400156 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000157
158 if (!extent_inserted) {
159 struct btrfs_key key;
160 size_t datasize;
161
162 key.objectid = btrfs_ino(inode);
163 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200164 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165
166 datasize = btrfs_file_extent_calc_inline_size(cur_size);
167 path->leave_spinning = 1;
168 ret = btrfs_insert_empty_item(trans, root, path, &key,
169 datasize);
170 if (ret) {
171 err = ret;
172 goto fail;
173 }
Chris Masonc8b97812008-10-29 14:49:59 -0400174 }
175 leaf = path->nodes[0];
176 ei = btrfs_item_ptr(leaf, path->slots[0],
177 struct btrfs_file_extent_item);
178 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
179 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
180 btrfs_set_file_extent_encryption(leaf, ei, 0);
181 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
182 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
183 ptr = btrfs_file_extent_inline_start(ei);
184
Li Zefan261507a02010-12-17 14:21:50 +0800185 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400186 struct page *cpage;
187 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500188 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400189 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500190 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400191 PAGE_CACHE_SIZE);
192
Cong Wang7ac687d2011-11-25 23:14:28 +0800193 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400194 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800195 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400196
197 i++;
198 ptr += cur_size;
199 compressed_size -= cur_size;
200 }
201 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800202 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400203 } else {
204 page = find_get_page(inode->i_mapping,
205 start >> PAGE_CACHE_SHIFT);
206 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800207 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400208 offset = start & (PAGE_CACHE_SIZE - 1);
209 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800210 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400211 page_cache_release(page);
212 }
213 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000214 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400215
Yan, Zhengc2167752009-11-12 09:34:21 +0000216 /*
217 * we're an inline extent, so nobody can
218 * extend the file past i_size without locking
219 * a page we already have locked.
220 *
221 * We must do any isize and inode updates
222 * before we unlock the pages. Otherwise we
223 * could end up racing with unlink.
224 */
Chris Masonc8b97812008-10-29 14:49:59 -0400225 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100226 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000227
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100228 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400229fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400230 return err;
231}
232
233
234/*
235 * conditionally insert an inline extent into the file. This
236 * does the checks required to make sure the data is small enough
237 * to fit as an inline extent.
238 */
Josef Bacik00361582013-08-14 14:02:47 -0400239static noinline int cow_file_range_inline(struct btrfs_root *root,
240 struct inode *inode, u64 start,
241 u64 end, size_t compressed_size,
242 int compress_type,
243 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400244{
Josef Bacik00361582013-08-14 14:02:47 -0400245 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400246 u64 isize = i_size_read(inode);
247 u64 actual_end = min(end + 1, isize);
248 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000249 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400250 u64 data_len = inline_len;
251 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000252 struct btrfs_path *path;
253 int extent_inserted = 0;
254 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400255
256 if (compressed_size)
257 data_len = compressed_size;
258
259 if (start > 0 ||
Wang Shilong354877b2014-07-17 11:44:11 +0800260 actual_end > PAGE_CACHE_SIZE ||
261 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400262 (!compressed_size &&
263 (actual_end & (root->sectorsize - 1)) == 0) ||
264 end + 1 < isize ||
265 data_len > root->fs_info->max_inline) {
266 return 1;
267 }
268
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000269 path = btrfs_alloc_path();
270 if (!path)
271 return -ENOMEM;
272
Josef Bacik00361582013-08-14 14:02:47 -0400273 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000274 if (IS_ERR(trans)) {
275 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400276 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000277 }
Josef Bacik00361582013-08-14 14:02:47 -0400278 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
279
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000280 if (compressed_size && compressed_pages)
281 extent_item_size = btrfs_file_extent_calc_inline_size(
282 compressed_size);
283 else
284 extent_item_size = btrfs_file_extent_calc_inline_size(
285 inline_len);
286
287 ret = __btrfs_drop_extents(trans, root, inode, path,
288 start, aligned_end, NULL,
289 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400290 if (ret) {
291 btrfs_abort_transaction(trans, root, ret);
292 goto out;
293 }
Chris Masonc8b97812008-10-29 14:49:59 -0400294
295 if (isize > actual_end)
296 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000297 ret = insert_inline_extent(trans, path, extent_inserted,
298 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400299 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000300 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400301 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100302 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400303 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400304 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400305 ret = 1;
306 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100307 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400308
Josef Bacikbdc20e62013-02-28 13:23:38 -0500309 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400310 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400311 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400312out:
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000313 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400314 btrfs_end_transaction(trans, root);
315 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400316}
317
Chris Mason771ed682008-11-06 22:02:51 -0500318struct async_extent {
319 u64 start;
320 u64 ram_size;
321 u64 compressed_size;
322 struct page **pages;
323 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800324 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500325 struct list_head list;
326};
327
328struct async_cow {
329 struct inode *inode;
330 struct btrfs_root *root;
331 struct page *locked_page;
332 u64 start;
333 u64 end;
334 struct list_head extents;
335 struct btrfs_work work;
336};
337
338static noinline int add_async_extent(struct async_cow *cow,
339 u64 start, u64 ram_size,
340 u64 compressed_size,
341 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800342 unsigned long nr_pages,
343 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500344{
345 struct async_extent *async_extent;
346
347 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100348 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500349 async_extent->start = start;
350 async_extent->ram_size = ram_size;
351 async_extent->compressed_size = compressed_size;
352 async_extent->pages = pages;
353 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800354 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500355 list_add_tail(&async_extent->list, &cow->extents);
356 return 0;
357}
358
Wang Shilongf79707b2014-07-17 11:44:09 +0800359static inline int inode_need_compress(struct inode *inode)
360{
361 struct btrfs_root *root = BTRFS_I(inode)->root;
362
363 /* force compress */
364 if (btrfs_test_opt(root, FORCE_COMPRESS))
365 return 1;
366 /* bad compression ratios */
367 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
368 return 0;
369 if (btrfs_test_opt(root, COMPRESS) ||
370 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
371 BTRFS_I(inode)->force_compress)
372 return 1;
373 return 0;
374}
375
Chris Masonc8b97812008-10-29 14:49:59 -0400376/*
Chris Mason771ed682008-11-06 22:02:51 -0500377 * we create compressed extents in two phases. The first
378 * phase compresses a range of pages that have already been
379 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400380 *
Chris Mason771ed682008-11-06 22:02:51 -0500381 * This is done inside an ordered work queue, and the compression
382 * is spread across many cpus. The actual IO submission is step
383 * two, and the ordered work queue takes care of making sure that
384 * happens in the same order things were put onto the queue by
385 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400386 *
Chris Mason771ed682008-11-06 22:02:51 -0500387 * If this code finds it can't get good compression, it puts an
388 * entry onto the work queue to write the uncompressed bytes. This
389 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300390 * are written in the same order that the flusher thread sent them
391 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400392 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100393static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500394 struct page *locked_page,
395 u64 start, u64 end,
396 struct async_cow *async_cow,
397 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400398{
399 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400400 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400401 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400402 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500403 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400404 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400405 struct page **pages = NULL;
406 unsigned long nr_pages;
407 unsigned long nr_pages_ret = 0;
408 unsigned long total_compressed = 0;
409 unsigned long total_in = 0;
410 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500411 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400412 int i;
413 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800414 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400415 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400416
Liu Bo4cb13e52012-03-29 09:57:45 -0400417 /* if this is a small write inside eof, kick off a defrag */
418 if ((end - start + 1) < 16 * 1024 &&
419 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400420 btrfs_add_inode_defrag(NULL, inode);
421
Chris Mason42dc7ba2008-12-15 11:44:56 -0500422 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400423again:
424 will_compress = 0;
425 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
426 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
427
Chris Masonf03d9301f2009-02-04 09:31:06 -0500428 /*
429 * we don't want to send crud past the end of i_size through
430 * compression, that's just a waste of CPU time. So, if the
431 * end of the file is before the start of our current
432 * requested range of bytes, we bail out to the uncompressed
433 * cleanup code that can deal with all of this.
434 *
435 * It isn't really the fastest way to fix things, but this is a
436 * very uncommon corner.
437 */
438 if (actual_end <= start)
439 goto cleanup_and_bail_uncompressed;
440
Chris Masonc8b97812008-10-29 14:49:59 -0400441 total_compressed = actual_end - start;
442
Shilong Wang4bcbb332014-10-07 18:44:35 -0400443 /*
444 * skip compression for a small file range(<=blocksize) that
445 * isn't an inline extent, since it dosen't save disk space at all.
446 */
447 if (total_compressed <= blocksize &&
448 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
449 goto cleanup_and_bail_uncompressed;
450
Chris Masonc8b97812008-10-29 14:49:59 -0400451 /* we want to make sure that amount of ram required to uncompress
452 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500453 * of a compressed extent to 128k. This is a crucial number
454 * because it also controls how easily we can spread reads across
455 * cpus for decompression.
456 *
457 * We also want to make sure the amount of IO required to do
458 * a random read is reasonably small, so we limit the size of
459 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400460 */
461 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000462 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500463 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400464 total_in = 0;
465 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400466
Chris Mason771ed682008-11-06 22:02:51 -0500467 /*
468 * we do compression for mount -o compress and when the
469 * inode has not been flagged as nocompress. This flag can
470 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400471 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800472 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400473 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100474 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800475 if (!pages) {
476 /* just bail out to the uncompressed code */
477 goto cont;
478 }
Chris Mason179e29e2007-11-01 11:28:41 -0400479
Li Zefan261507a02010-12-17 14:21:50 +0800480 if (BTRFS_I(inode)->force_compress)
481 compress_type = BTRFS_I(inode)->force_compress;
482
Chris Mason4adaa612013-03-26 13:07:00 -0400483 /*
484 * we need to call clear_page_dirty_for_io on each
485 * page in the range. Otherwise applications with the file
486 * mmap'd can wander in and change the page contents while
487 * we are compressing them.
488 *
489 * If the compression fails for any reason, we set the pages
490 * dirty again later on.
491 */
492 extent_range_clear_dirty_for_io(inode, start, end);
493 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800494 ret = btrfs_compress_pages(compress_type,
495 inode->i_mapping, start,
496 total_compressed, pages,
497 nr_pages, &nr_pages_ret,
498 &total_in,
499 &total_compressed,
500 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400501
502 if (!ret) {
503 unsigned long offset = total_compressed &
504 (PAGE_CACHE_SIZE - 1);
505 struct page *page = pages[nr_pages_ret - 1];
506 char *kaddr;
507
508 /* zero the tail end of the last page, we might be
509 * sending it down to disk
510 */
511 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800512 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400513 memset(kaddr + offset, 0,
514 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800515 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400516 }
517 will_compress = 1;
518 }
519 }
Li Zefan560f7d72011-09-08 10:22:01 +0800520cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400521 if (start == 0) {
522 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500523 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400524 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500525 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400526 */
Josef Bacik00361582013-08-14 14:02:47 -0400527 ret = cow_file_range_inline(root, inode, start, end,
528 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400529 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500530 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400531 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000532 total_compressed,
533 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400534 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100535 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400536 unsigned long clear_flags = EXTENT_DELALLOC |
537 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100538 unsigned long page_error_op;
539
Josef Bacik151a41b2013-07-29 13:22:24 -0400540 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100541 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400542
Chris Mason771ed682008-11-06 22:02:51 -0500543 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100544 * inline extent creation worked or returned error,
545 * we don't need to create any more async work items.
546 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500547 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400548 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400549 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400550 PAGE_CLEAR_DIRTY |
551 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100552 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400553 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400554 goto free_pages_out;
555 }
556 }
557
558 if (will_compress) {
559 /*
560 * we aren't doing an inline extent round the compressed size
561 * up to a block size boundary so the allocator does sane
562 * things
563 */
Qu Wenruofda28322013-02-26 08:10:22 +0000564 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400565
566 /*
567 * one last check to make sure the compression is really a
568 * win, compare the page count read with the blocks on disk
569 */
Qu Wenruofda28322013-02-26 08:10:22 +0000570 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400571 if (total_compressed >= total_in) {
572 will_compress = 0;
573 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400574 num_bytes = total_in;
575 }
576 }
577 if (!will_compress && pages) {
578 /*
579 * the compression code ran but failed to make things smaller,
580 * free any pages it allocated and our page pointer array
581 */
582 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400583 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400584 page_cache_release(pages[i]);
585 }
586 kfree(pages);
587 pages = NULL;
588 total_compressed = 0;
589 nr_pages_ret = 0;
590
591 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500592 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
593 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500594 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500595 }
Chris Masonc8b97812008-10-29 14:49:59 -0400596 }
Chris Mason771ed682008-11-06 22:02:51 -0500597 if (will_compress) {
598 *num_added += 1;
599
600 /* the async work queues will take care of doing actual
601 * allocation on disk for these compressed pages,
602 * and will submit them to the elevator.
603 */
604 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800605 total_compressed, pages, nr_pages_ret,
606 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500607
Yan, Zheng24ae6362010-12-06 07:02:36 +0000608 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500609 start += num_bytes;
610 pages = NULL;
611 cond_resched();
612 goto again;
613 }
614 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500615cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500616 /*
617 * No compression, but we still need to write the pages in
618 * the file we've been given so far. redirty the locked
619 * page if it corresponds to our extent and set things up
620 * for the async work queue to run cow_file_range to do
621 * the normal delalloc dance
622 */
623 if (page_offset(locked_page) >= start &&
624 page_offset(locked_page) <= end) {
625 __set_page_dirty_nobuffers(locked_page);
626 /* unlocked later on in the async handlers */
627 }
Chris Mason4adaa612013-03-26 13:07:00 -0400628 if (redirty)
629 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800630 add_async_extent(async_cow, start, end - start + 1,
631 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500632 *num_added += 1;
633 }
634
Filipe Mananac44f6492014-10-09 21:15:44 +0100635 return;
Chris Mason771ed682008-11-06 22:02:51 -0500636
637free_pages_out:
638 for (i = 0; i < nr_pages_ret; i++) {
639 WARN_ON(pages[i]->mapping);
640 page_cache_release(pages[i]);
641 }
Chris Masond3977122009-01-05 21:25:51 -0500642 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500643}
Chris Mason771ed682008-11-06 22:02:51 -0500644
Filipe Manana40ae8372014-10-06 22:14:24 +0100645static void free_async_extent_pages(struct async_extent *async_extent)
646{
647 int i;
648
649 if (!async_extent->pages)
650 return;
651
652 for (i = 0; i < async_extent->nr_pages; i++) {
653 WARN_ON(async_extent->pages[i]->mapping);
654 page_cache_release(async_extent->pages[i]);
655 }
656 kfree(async_extent->pages);
657 async_extent->nr_pages = 0;
658 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500659}
660
661/*
662 * phase two of compressed writeback. This is the ordered portion
663 * of the code, which only gets called in the order the work was
664 * queued. We walk all the async extents created by compress_file_range
665 * and send them down to the disk.
666 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100667static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500668 struct async_cow *async_cow)
669{
670 struct async_extent *async_extent;
671 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500672 struct btrfs_key ins;
673 struct extent_map *em;
674 struct btrfs_root *root = BTRFS_I(inode)->root;
675 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
676 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500677 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500678
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500679again:
Chris Masond3977122009-01-05 21:25:51 -0500680 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500681 async_extent = list_entry(async_cow->extents.next,
682 struct async_extent, list);
683 list_del(&async_extent->list);
684
685 io_tree = &BTRFS_I(inode)->io_tree;
686
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500687retry:
Chris Mason771ed682008-11-06 22:02:51 -0500688 /* did the compression code fall back to uncompressed IO? */
689 if (!async_extent->pages) {
690 int page_started = 0;
691 unsigned long nr_written = 0;
692
693 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000694 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100695 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500696
697 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500698 ret = cow_file_range(inode, async_cow->locked_page,
699 async_extent->start,
700 async_extent->start +
701 async_extent->ram_size - 1,
702 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500703
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100704 /* JDM XXX */
705
Chris Mason771ed682008-11-06 22:02:51 -0500706 /*
707 * if page_started, cow_file_range inserted an
708 * inline extent and took care of all the unlocking
709 * and IO for us. Otherwise, we need to submit
710 * all those pages down to the drive.
711 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500712 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500713 extent_write_locked_range(io_tree,
714 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500715 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500716 async_extent->ram_size - 1,
717 btrfs_get_extent,
718 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500719 else if (ret)
720 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500721 kfree(async_extent);
722 cond_resched();
723 continue;
724 }
725
726 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100727 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500728
Josef Bacik00361582013-08-14 14:02:47 -0400729 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500730 async_extent->compressed_size,
731 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800732 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500733 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100734 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500735
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400736 if (ret == -ENOSPC) {
737 unlock_extent(io_tree, async_extent->start,
738 async_extent->start +
739 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800740
741 /*
742 * we need to redirty the pages if we decide to
743 * fallback to uncompressed IO, otherwise we
744 * will not submit these pages down to lower
745 * layers.
746 */
747 extent_range_redirty_for_io(inode,
748 async_extent->start,
749 async_extent->start +
750 async_extent->ram_size - 1);
751
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100752 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400753 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500754 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500755 }
756
Yan, Zhengc2167752009-11-12 09:34:21 +0000757 /*
758 * here we're doing allocation and writeback of the
759 * compressed pages
760 */
761 btrfs_drop_extent_cache(inode, async_extent->start,
762 async_extent->start +
763 async_extent->ram_size - 1, 0);
764
David Sterba172ddd62011-04-21 00:48:27 +0200765 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000766 if (!em) {
767 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500768 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000769 }
Chris Mason771ed682008-11-06 22:02:51 -0500770 em->start = async_extent->start;
771 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500772 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400773 em->mod_start = em->start;
774 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500775
776 em->block_start = ins.objectid;
777 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500778 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400779 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500780 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800781 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500782 set_bit(EXTENT_FLAG_PINNED, &em->flags);
783 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400784 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500785
Chris Masond3977122009-01-05 21:25:51 -0500786 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400787 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400788 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400789 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500790 if (ret != -EEXIST) {
791 free_extent_map(em);
792 break;
793 }
794 btrfs_drop_extent_cache(inode, async_extent->start,
795 async_extent->start +
796 async_extent->ram_size - 1, 0);
797 }
798
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500799 if (ret)
800 goto out_free_reserve;
801
Li Zefan261507a02010-12-17 14:21:50 +0800802 ret = btrfs_add_ordered_extent_compress(inode,
803 async_extent->start,
804 ins.objectid,
805 async_extent->ram_size,
806 ins.offset,
807 BTRFS_ORDERED_COMPRESSED,
808 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100809 if (ret) {
810 btrfs_drop_extent_cache(inode, async_extent->start,
811 async_extent->start +
812 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500813 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100814 }
Chris Mason771ed682008-11-06 22:02:51 -0500815
Chris Mason771ed682008-11-06 22:02:51 -0500816 /*
817 * clear dirty, set writeback and unlock the pages.
818 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400819 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400820 async_extent->start +
821 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400822 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
823 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400824 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500825 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500826 async_extent->start,
827 async_extent->ram_size,
828 ins.objectid,
829 ins.offset, async_extent->pages,
830 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100831 if (ret) {
832 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
833 struct page *p = async_extent->pages[0];
834 const u64 start = async_extent->start;
835 const u64 end = start + async_extent->ram_size - 1;
836
837 p->mapping = inode->i_mapping;
838 tree->ops->writepage_end_io_hook(p, start, end,
839 NULL, 0);
840 p->mapping = NULL;
841 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
842 PAGE_END_WRITEBACK |
843 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100844 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100845 }
Chris Mason771ed682008-11-06 22:02:51 -0500846 alloc_hint = ins.objectid + ins.offset;
847 kfree(async_extent);
848 cond_resched();
849 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100850 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500851out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800852 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100853out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400854 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500855 async_extent->start +
856 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400857 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400858 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
859 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100860 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
861 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100862 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100863 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500864 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500865}
866
Josef Bacik4b46fce2010-05-23 11:00:55 -0400867static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
868 u64 num_bytes)
869{
870 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
871 struct extent_map *em;
872 u64 alloc_hint = 0;
873
874 read_lock(&em_tree->lock);
875 em = search_extent_mapping(em_tree, start, num_bytes);
876 if (em) {
877 /*
878 * if block start isn't an actual block number then find the
879 * first block in this inode and use that as a hint. If that
880 * block is also bogus then just don't worry about it.
881 */
882 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
883 free_extent_map(em);
884 em = search_extent_mapping(em_tree, 0, 0);
885 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
886 alloc_hint = em->block_start;
887 if (em)
888 free_extent_map(em);
889 } else {
890 alloc_hint = em->block_start;
891 free_extent_map(em);
892 }
893 }
894 read_unlock(&em_tree->lock);
895
896 return alloc_hint;
897}
898
Chris Mason771ed682008-11-06 22:02:51 -0500899/*
900 * when extent_io.c finds a delayed allocation range in the file,
901 * the call backs end up in this code. The basic idea is to
902 * allocate extents on disk for the range, and create ordered data structs
903 * in ram to track those extents.
904 *
905 * locked_page is the page that writepage had locked already. We use
906 * it to make sure we don't do extra locks or unlocks.
907 *
908 * *page_started is set to one if we unlock locked_page and do everything
909 * required to start IO on it. It may be clean and already done with
910 * IO when we return.
911 */
Josef Bacik00361582013-08-14 14:02:47 -0400912static noinline int cow_file_range(struct inode *inode,
913 struct page *locked_page,
914 u64 start, u64 end, int *page_started,
915 unsigned long *nr_written,
916 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500917{
Josef Bacik00361582013-08-14 14:02:47 -0400918 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500919 u64 alloc_hint = 0;
920 u64 num_bytes;
921 unsigned long ram_size;
922 u64 disk_num_bytes;
923 u64 cur_alloc_size;
924 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500925 struct btrfs_key ins;
926 struct extent_map *em;
927 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
928 int ret = 0;
929
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400930 if (btrfs_is_free_space_inode(inode)) {
931 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500932 ret = -EINVAL;
933 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400934 }
Chris Mason771ed682008-11-06 22:02:51 -0500935
Qu Wenruofda28322013-02-26 08:10:22 +0000936 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500937 num_bytes = max(blocksize, num_bytes);
938 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500939
Chris Mason4cb53002011-05-24 15:35:30 -0400940 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400941 if (num_bytes < 64 * 1024 &&
942 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400943 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400944
Chris Mason771ed682008-11-06 22:02:51 -0500945 if (start == 0) {
946 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400947 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
948 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500949 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400950 extent_clear_unlock_delalloc(inode, start, end, NULL,
951 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400952 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400953 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
954 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000955
Chris Mason771ed682008-11-06 22:02:51 -0500956 *nr_written = *nr_written +
957 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
958 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500959 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100960 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100961 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500962 }
963 }
Chris Masonc8b97812008-10-29 14:49:59 -0400964
965 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200966 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400967
Josef Bacik4b46fce2010-05-23 11:00:55 -0400968 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400969 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400970
Chris Masond3977122009-01-05 21:25:51 -0500971 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400972 unsigned long op;
973
Josef Bacik287a0ab2010-03-19 18:07:23 +0000974 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400975 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500976 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800977 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400978 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100979 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500980
David Sterba172ddd62011-04-21 00:48:27 +0200981 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000982 if (!em) {
983 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000984 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000985 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400986 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500987 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500988 ram_size = ins.offset;
989 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400990 em->mod_start = em->start;
991 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400992
Chris Masone6dcd2d2008-07-17 12:53:50 -0400993 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400994 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500995 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400996 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400997 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400998 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400999 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001000
Chris Masond3977122009-01-05 21:25:51 -05001001 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001002 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001003 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001004 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001005 if (ret != -EEXIST) {
1006 free_extent_map(em);
1007 break;
1008 }
1009 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001010 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001011 }
Liu Boace68ba2013-04-22 10:53:47 +00001012 if (ret)
1013 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001014
Chris Mason98d20f62008-04-14 09:46:10 -04001015 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001016 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001017 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001018 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001019 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001020
Yan Zheng17d217f2008-12-12 10:03:38 -05001021 if (root->root_key.objectid ==
1022 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1023 ret = btrfs_reloc_clone_csums(inode, start,
1024 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001025 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001026 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001027 }
1028
Chris Masond3977122009-01-05 21:25:51 -05001029 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001030 break;
Chris Masond3977122009-01-05 21:25:51 -05001031
Chris Masonc8b97812008-10-29 14:49:59 -04001032 /* we're not doing compressed IO, don't unlock the first
1033 * page (which the caller expects to stay locked), don't
1034 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001035 *
1036 * Do set the Private2 bit so we know this page was properly
1037 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001038 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001039 op = unlock ? PAGE_UNLOCK : 0;
1040 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001041
Josef Bacikc2790a22013-07-29 11:20:47 -04001042 extent_clear_unlock_delalloc(inode, start,
1043 start + ram_size - 1, locked_page,
1044 EXTENT_LOCKED | EXTENT_DELALLOC,
1045 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001046 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001047 num_bytes -= cur_alloc_size;
1048 alloc_hint = ins.objectid + ins.offset;
1049 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001050 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001051out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001052 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001053
Filipe Mananad9f85962014-08-25 10:43:00 +01001054out_drop_extent_cache:
1055 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001056out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001057 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001058out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001059 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001060 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1061 EXTENT_DELALLOC | EXTENT_DEFRAG,
1062 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1063 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001064 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001065}
Chris Masonc8b97812008-10-29 14:49:59 -04001066
Chris Mason771ed682008-11-06 22:02:51 -05001067/*
1068 * work queue call back to started compression on a file and pages
1069 */
1070static noinline void async_cow_start(struct btrfs_work *work)
1071{
1072 struct async_cow *async_cow;
1073 int num_added = 0;
1074 async_cow = container_of(work, struct async_cow, work);
1075
1076 compress_file_range(async_cow->inode, async_cow->locked_page,
1077 async_cow->start, async_cow->end, async_cow,
1078 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001079 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001080 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001081 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001082 }
Chris Mason771ed682008-11-06 22:02:51 -05001083}
1084
1085/*
1086 * work queue call back to submit previously compressed pages
1087 */
1088static noinline void async_cow_submit(struct btrfs_work *work)
1089{
1090 struct async_cow *async_cow;
1091 struct btrfs_root *root;
1092 unsigned long nr_pages;
1093
1094 async_cow = container_of(work, struct async_cow, work);
1095
1096 root = async_cow->root;
1097 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1098 PAGE_CACHE_SHIFT;
1099
Josef Bacik66657b32012-08-01 15:36:24 -04001100 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001101 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001102 waitqueue_active(&root->fs_info->async_submit_wait))
1103 wake_up(&root->fs_info->async_submit_wait);
1104
Chris Masond3977122009-01-05 21:25:51 -05001105 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001106 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001107}
Chris Masonc8b97812008-10-29 14:49:59 -04001108
Chris Mason771ed682008-11-06 22:02:51 -05001109static noinline void async_cow_free(struct btrfs_work *work)
1110{
1111 struct async_cow *async_cow;
1112 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001113 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001114 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001115 kfree(async_cow);
1116}
1117
1118static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1119 u64 start, u64 end, int *page_started,
1120 unsigned long *nr_written)
1121{
1122 struct async_cow *async_cow;
1123 struct btrfs_root *root = BTRFS_I(inode)->root;
1124 unsigned long nr_pages;
1125 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001126 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001127
Chris Masona3429ab2009-10-08 12:30:20 -04001128 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1129 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001130 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001131 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001132 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001133 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001134 async_cow->root = root;
1135 async_cow->locked_page = locked_page;
1136 async_cow->start = start;
1137
Wang Shilongf79707b2014-07-17 11:44:09 +08001138 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1139 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001140 cur_end = end;
1141 else
1142 cur_end = min(end, start + 512 * 1024 - 1);
1143
1144 async_cow->end = cur_end;
1145 INIT_LIST_HEAD(&async_cow->extents);
1146
Liu Bo9e0af232014-08-15 23:36:53 +08001147 btrfs_init_work(&async_cow->work,
1148 btrfs_delalloc_helper,
1149 async_cow_start, async_cow_submit,
1150 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001151
Chris Mason771ed682008-11-06 22:02:51 -05001152 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1153 PAGE_CACHE_SHIFT;
1154 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1155
Qu Wenruoafe3d242014-02-28 10:46:07 +08001156 btrfs_queue_work(root->fs_info->delalloc_workers,
1157 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001158
1159 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1160 wait_event(root->fs_info->async_submit_wait,
1161 (atomic_read(&root->fs_info->async_delalloc_pages) <
1162 limit));
1163 }
1164
Chris Masond3977122009-01-05 21:25:51 -05001165 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001166 atomic_read(&root->fs_info->async_delalloc_pages)) {
1167 wait_event(root->fs_info->async_submit_wait,
1168 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1169 0));
1170 }
1171
1172 *nr_written += nr_pages;
1173 start = cur_end + 1;
1174 }
1175 *page_started = 1;
1176 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001177}
1178
Chris Masond3977122009-01-05 21:25:51 -05001179static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001180 u64 bytenr, u64 num_bytes)
1181{
1182 int ret;
1183 struct btrfs_ordered_sum *sums;
1184 LIST_HEAD(list);
1185
Yan Zheng07d400a2009-01-06 11:42:00 -05001186 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001187 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001188 if (ret == 0 && list_empty(&list))
1189 return 0;
1190
1191 while (!list_empty(&list)) {
1192 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1193 list_del(&sums->list);
1194 kfree(sums);
1195 }
1196 return 1;
1197}
1198
Chris Masond352ac62008-09-29 15:18:18 -04001199/*
1200 * when nowcow writeback call back. This checks for snapshots or COW copies
1201 * of the extents that exist in the file, and COWs the file as required.
1202 *
1203 * If no cow copies or snapshots exist, we write directly to the existing
1204 * blocks on disk
1205 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001206static noinline int run_delalloc_nocow(struct inode *inode,
1207 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001208 u64 start, u64 end, int *page_started, int force,
1209 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001210{
Chris Masonbe20aa92007-12-17 20:14:01 -05001211 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001212 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001213 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001214 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001215 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001216 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001217 u64 cow_start;
1218 u64 cur_offset;
1219 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001220 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001221 u64 disk_bytenr;
1222 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001223 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001224 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001225 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001226 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001227 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 int nocow;
1229 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001230 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001231 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001232
1233 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001234 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001235 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1236 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001237 EXTENT_DO_ACCOUNTING |
1238 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001239 PAGE_CLEAR_DIRTY |
1240 PAGE_SET_WRITEBACK |
1241 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001242 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001243 }
Li Zefan82d59022011-04-20 10:33:24 +08001244
Liu Bo83eea1f2012-07-10 05:28:39 -06001245 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001246
1247 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001248 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001249 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001250 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001251
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001252 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001253 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1254 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001255 EXTENT_DO_ACCOUNTING |
1256 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001257 PAGE_CLEAR_DIRTY |
1258 PAGE_SET_WRITEBACK |
1259 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001260 btrfs_free_path(path);
1261 return PTR_ERR(trans);
1262 }
1263
Josef Bacik74b21072011-04-13 12:02:53 -04001264 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001265
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 cow_start = (u64)-1;
1267 cur_offset = start;
1268 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001269 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001271 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001272 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001273 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1274 leaf = path->nodes[0];
1275 btrfs_item_key_to_cpu(leaf, &found_key,
1276 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001277 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 found_key.type == BTRFS_EXTENT_DATA_KEY)
1279 path->slots[0]--;
1280 }
1281 check_prev = 0;
1282next_slot:
1283 leaf = path->nodes[0];
1284 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1285 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001286 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001287 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001288 if (ret > 0)
1289 break;
1290 leaf = path->nodes[0];
1291 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001292
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 nocow = 0;
1294 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001295 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001297
Li Zefan33345d012011-04-20 10:31:50 +08001298 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001299 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1300 found_key.offset > end)
1301 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001302
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 if (found_key.offset > cur_offset) {
1304 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001305 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001306 goto out_check;
1307 }
Chris Masonc31f8832008-01-08 15:46:31 -05001308
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 fi = btrfs_item_ptr(leaf, path->slots[0],
1310 struct btrfs_file_extent_item);
1311 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001312
Josef Bacikcc95bef2013-04-04 14:31:27 -04001313 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001314 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1315 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001317 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 extent_end = found_key.offset +
1319 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001320 disk_num_bytes =
1321 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001322 if (extent_end <= start) {
1323 path->slots[0]++;
1324 goto next_slot;
1325 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001326 if (disk_bytenr == 0)
1327 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001328 if (btrfs_file_extent_compression(leaf, fi) ||
1329 btrfs_file_extent_encryption(leaf, fi) ||
1330 btrfs_file_extent_other_encoding(leaf, fi))
1331 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001332 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1333 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001334 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001335 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001336 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001337 found_key.offset -
1338 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001339 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001340 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001341 disk_bytenr += cur_offset - found_key.offset;
1342 num_bytes = min(end + 1, extent_end) - cur_offset;
1343 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001344 * if there are pending snapshots for this root,
1345 * we fall into common COW way.
1346 */
1347 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001348 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001349 if (!err)
1350 goto out_check;
1351 }
1352 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001353 * force cow if csum exists in the range.
1354 * this ensure that csum for a given extent are
1355 * either valid or do not exist.
1356 */
1357 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1358 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 nocow = 1;
1360 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1361 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001362 btrfs_file_extent_inline_len(leaf,
1363 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 extent_end = ALIGN(extent_end, root->sectorsize);
1365 } else {
1366 BUG_ON(1);
1367 }
1368out_check:
1369 if (extent_end <= start) {
1370 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001371 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001372 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 goto next_slot;
1374 }
1375 if (!nocow) {
1376 if (cow_start == (u64)-1)
1377 cow_start = cur_offset;
1378 cur_offset = extent_end;
1379 if (cur_offset > end)
1380 break;
1381 path->slots[0]++;
1382 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001383 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001384
David Sterbab3b4aa72011-04-21 01:20:15 +02001385 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001386 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001387 ret = cow_file_range(inode, locked_page,
1388 cow_start, found_key.offset - 1,
1389 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001390 if (ret) {
1391 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001392 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001393 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001394 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001395 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001396 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001397
Yan Zhengd899e052008-10-30 14:25:28 -04001398 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1399 struct extent_map *em;
1400 struct extent_map_tree *em_tree;
1401 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001402 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001403 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001404 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001405 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001406 em->len = num_bytes;
1407 em->block_len = num_bytes;
1408 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001409 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001410 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001411 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001412 em->mod_start = em->start;
1413 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001414 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001415 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001416 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001417 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001418 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001419 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001420 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001421 if (ret != -EEXIST) {
1422 free_extent_map(em);
1423 break;
1424 }
1425 btrfs_drop_extent_cache(inode, em->start,
1426 em->start + em->len - 1, 0);
1427 }
1428 type = BTRFS_ORDERED_PREALLOC;
1429 } else {
1430 type = BTRFS_ORDERED_NOCOW;
1431 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001432
1433 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001434 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001435 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001436
Yan, Zhengefa56462010-05-16 10:49:59 -04001437 if (root->root_key.objectid ==
1438 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1439 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1440 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001441 if (ret) {
1442 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001443 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001444 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001445 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001446 }
1447
Josef Bacikc2790a22013-07-29 11:20:47 -04001448 extent_clear_unlock_delalloc(inode, cur_offset,
1449 cur_offset + num_bytes - 1,
1450 locked_page, EXTENT_LOCKED |
1451 EXTENT_DELALLOC, PAGE_UNLOCK |
1452 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001453 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001454 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001455 cur_offset = extent_end;
1456 if (cur_offset > end)
1457 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001458 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001459 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001460
Josef Bacik17ca04a2012-05-31 15:58:55 -04001461 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001462 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001463 cur_offset = end;
1464 }
1465
Yan Zheng80ff3852008-10-30 14:20:02 -04001466 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001467 ret = cow_file_range(inode, locked_page, cow_start, end,
1468 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001469 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001470 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001471 }
1472
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001473error:
Miao Xiea698d0752012-09-20 01:51:59 -06001474 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001475 if (!ret)
1476 ret = err;
1477
Josef Bacik17ca04a2012-05-31 15:58:55 -04001478 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001479 extent_clear_unlock_delalloc(inode, cur_offset, end,
1480 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001481 EXTENT_DELALLOC | EXTENT_DEFRAG |
1482 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1483 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001484 PAGE_SET_WRITEBACK |
1485 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001486 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001487 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001488}
1489
Wang Shilong47059d92014-07-03 18:22:07 +08001490static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1491{
1492
1493 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1494 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1495 return 0;
1496
1497 /*
1498 * @defrag_bytes is a hint value, no spinlock held here,
1499 * if is not zero, it means the file is defragging.
1500 * Force cow if given extent needs to be defragged.
1501 */
1502 if (BTRFS_I(inode)->defrag_bytes &&
1503 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1504 EXTENT_DEFRAG, 0, NULL))
1505 return 1;
1506
1507 return 0;
1508}
1509
Chris Masond352ac62008-09-29 15:18:18 -04001510/*
1511 * extent_io.c call back to do delayed allocation processing
1512 */
Chris Masonc8b97812008-10-29 14:49:59 -04001513static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001514 u64 start, u64 end, int *page_started,
1515 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001516{
Chris Masonbe20aa92007-12-17 20:14:01 -05001517 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001518 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001519
Wang Shilong47059d92014-07-03 18:22:07 +08001520 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001521 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001522 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001523 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001524 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001525 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001526 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001527 ret = cow_file_range(inode, locked_page, start, end,
1528 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001529 } else {
1530 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1531 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001532 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001533 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001534 }
Chris Masonb888db22007-08-27 16:49:44 -04001535 return ret;
1536}
1537
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001538static void btrfs_split_extent_hook(struct inode *inode,
1539 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001540{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001541 u64 size;
1542
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001543 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001544 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001545 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001546
Josef Bacikdcab6a32015-02-11 15:08:59 -05001547 size = orig->end - orig->start + 1;
1548 if (size > BTRFS_MAX_EXTENT_SIZE) {
1549 u64 num_extents;
1550 u64 new_size;
1551
1552 /*
Josef Bacikba117212015-03-13 15:01:24 -04001553 * See the explanation in btrfs_merge_extent_hook, the same
1554 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001555 */
1556 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001557 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001558 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001559 new_size = split - orig->start;
1560 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1561 BTRFS_MAX_EXTENT_SIZE);
1562 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1563 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001564 return;
1565 }
1566
Josef Bacik9e0baf62011-07-15 15:16:44 +00001567 spin_lock(&BTRFS_I(inode)->lock);
1568 BTRFS_I(inode)->outstanding_extents++;
1569 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001570}
1571
1572/*
1573 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1574 * extents so we can keep track of new extents that are just merged onto old
1575 * extents, such as when we are doing sequential writes, so we can properly
1576 * account for the metadata space we'll need.
1577 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001578static void btrfs_merge_extent_hook(struct inode *inode,
1579 struct extent_state *new,
1580 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001581{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001582 u64 new_size, old_size;
1583 u64 num_extents;
1584
Josef Bacik9ed74f22009-09-11 16:12:44 -04001585 /* not delalloc, ignore it */
1586 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001587 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001588
Josef Bacik8461a3d2015-03-13 15:12:08 -04001589 if (new->start > other->start)
1590 new_size = new->end - other->start + 1;
1591 else
1592 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001593
1594 /* we're not bigger than the max, unreserve the space and go */
1595 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1596 spin_lock(&BTRFS_I(inode)->lock);
1597 BTRFS_I(inode)->outstanding_extents--;
1598 spin_unlock(&BTRFS_I(inode)->lock);
1599 return;
1600 }
1601
1602 /*
Josef Bacikba117212015-03-13 15:01:24 -04001603 * We have to add up either side to figure out how many extents were
1604 * accounted for before we merged into one big extent. If the number of
1605 * extents we accounted for is <= the amount we need for the new range
1606 * then we can return, otherwise drop. Think of it like this
1607 *
1608 * [ 4k][MAX_SIZE]
1609 *
1610 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1611 * need 2 outstanding extents, on one side we have 1 and the other side
1612 * we have 1 so they are == and we can return. But in this case
1613 *
1614 * [MAX_SIZE+4k][MAX_SIZE+4k]
1615 *
1616 * Each range on their own accounts for 2 extents, but merged together
1617 * they are only 3 extents worth of accounting, so we need to drop in
1618 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001619 */
Josef Bacikba117212015-03-13 15:01:24 -04001620 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1622 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001623 old_size = new->end - new->start + 1;
1624 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1625 BTRFS_MAX_EXTENT_SIZE);
1626
Josef Bacikdcab6a32015-02-11 15:08:59 -05001627 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001628 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001629 return;
1630
Josef Bacik9e0baf62011-07-15 15:16:44 +00001631 spin_lock(&BTRFS_I(inode)->lock);
1632 BTRFS_I(inode)->outstanding_extents--;
1633 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001634}
1635
Miao Xieeb73c1b2013-05-15 07:48:22 +00001636static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1637 struct inode *inode)
1638{
1639 spin_lock(&root->delalloc_lock);
1640 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1641 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1642 &root->delalloc_inodes);
1643 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1644 &BTRFS_I(inode)->runtime_flags);
1645 root->nr_delalloc_inodes++;
1646 if (root->nr_delalloc_inodes == 1) {
1647 spin_lock(&root->fs_info->delalloc_root_lock);
1648 BUG_ON(!list_empty(&root->delalloc_root));
1649 list_add_tail(&root->delalloc_root,
1650 &root->fs_info->delalloc_roots);
1651 spin_unlock(&root->fs_info->delalloc_root_lock);
1652 }
1653 }
1654 spin_unlock(&root->delalloc_lock);
1655}
1656
1657static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1658 struct inode *inode)
1659{
1660 spin_lock(&root->delalloc_lock);
1661 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1662 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1663 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1664 &BTRFS_I(inode)->runtime_flags);
1665 root->nr_delalloc_inodes--;
1666 if (!root->nr_delalloc_inodes) {
1667 spin_lock(&root->fs_info->delalloc_root_lock);
1668 BUG_ON(list_empty(&root->delalloc_root));
1669 list_del_init(&root->delalloc_root);
1670 spin_unlock(&root->fs_info->delalloc_root_lock);
1671 }
1672 }
1673 spin_unlock(&root->delalloc_lock);
1674}
1675
Chris Masond352ac62008-09-29 15:18:18 -04001676/*
1677 * extent_io.c set_bit_hook, used to track delayed allocation
1678 * bytes in this file, and to maintain the list of inodes that
1679 * have pending delalloc work to be done.
1680 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001681static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001682 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001683{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001684
Wang Shilong47059d92014-07-03 18:22:07 +08001685 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1686 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001687 /*
1688 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001689 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001690 * bit, which is only set or cleared with irqs on
1691 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001692 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001693 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001694 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001695 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001696
Josef Bacik9e0baf62011-07-15 15:16:44 +00001697 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001698 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001699 } else {
1700 spin_lock(&BTRFS_I(inode)->lock);
1701 BTRFS_I(inode)->outstanding_extents++;
1702 spin_unlock(&BTRFS_I(inode)->lock);
1703 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001704
Josef Bacik6a3891c2015-03-16 17:38:52 -04001705 /* For sanity tests */
1706 if (btrfs_test_is_dummy_root(root))
1707 return;
1708
Miao Xie963d6782013-01-29 10:10:51 +00001709 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1710 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001711 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001712 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001713 if (*bits & EXTENT_DEFRAG)
1714 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001715 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001716 &BTRFS_I(inode)->runtime_flags))
1717 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001718 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001719 }
Chris Mason291d6732008-01-29 15:55:23 -05001720}
1721
Chris Masond352ac62008-09-29 15:18:18 -04001722/*
1723 * extent_io.c clear_bit_hook, see set_bit_hook for why
1724 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001725static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001726 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001727 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001728{
Wang Shilong47059d92014-07-03 18:22:07 +08001729 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001730 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1731 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001732
1733 spin_lock(&BTRFS_I(inode)->lock);
1734 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1735 BTRFS_I(inode)->defrag_bytes -= len;
1736 spin_unlock(&BTRFS_I(inode)->lock);
1737
Chris Mason75eff682008-12-15 15:54:40 -05001738 /*
1739 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001740 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001741 * bit, which is only set or cleared with irqs on
1742 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001743 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001744 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001745 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001746
Josef Bacik9e0baf62011-07-15 15:16:44 +00001747 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001748 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001749 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1750 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001751 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001752 spin_unlock(&BTRFS_I(inode)->lock);
1753 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001754
Josef Bacikb6d08f02013-09-27 14:57:43 -04001755 /*
1756 * We don't reserve metadata space for space cache inodes so we
1757 * don't need to call dellalloc_release_metadata if there is an
1758 * error.
1759 */
1760 if (*bits & EXTENT_DO_ACCOUNTING &&
1761 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001762 btrfs_delalloc_release_metadata(inode, len);
1763
Josef Bacik6a3891c2015-03-16 17:38:52 -04001764 /* For sanity tests. */
1765 if (btrfs_test_is_dummy_root(root))
1766 return;
1767
Josef Bacik0cb59c92010-07-02 12:14:14 -04001768 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001769 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001770 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001771
Miao Xie963d6782013-01-29 10:10:51 +00001772 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1773 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001774 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001775 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001776 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001777 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001778 &BTRFS_I(inode)->runtime_flags))
1779 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001780 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001781 }
Chris Mason291d6732008-01-29 15:55:23 -05001782}
1783
Chris Masond352ac62008-09-29 15:18:18 -04001784/*
1785 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1786 * we don't create bios that span stripes or chunks
1787 */
David Woodhouse64a16702009-07-15 23:29:37 +01001788int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001789 size_t size, struct bio *bio,
1790 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001791{
1792 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001793 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001794 u64 length = 0;
1795 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001796 int ret;
1797
Chris Mason771ed682008-11-06 22:02:51 -05001798 if (bio_flags & EXTENT_BIO_COMPRESSED)
1799 return 0;
1800
Kent Overstreet4f024f32013-10-11 15:44:27 -07001801 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001802 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001803 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001804 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001805 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001806 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001807 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001808 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001809 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001810}
1811
Chris Masond352ac62008-09-29 15:18:18 -04001812/*
1813 * in order to insert checksums into the metadata in large chunks,
1814 * we wait until bio submission time. All the pages in the bio are
1815 * checksummed and sums are attached onto the ordered extent record.
1816 *
1817 * At IO completion time the cums attached on the ordered extent record
1818 * are inserted into the btree
1819 */
Chris Masond3977122009-01-05 21:25:51 -05001820static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1821 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001822 unsigned long bio_flags,
1823 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001824{
Chris Mason065631f2008-02-20 12:07:25 -05001825 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001826 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001827
Chris Masond20f7042008-12-08 16:58:54 -05001828 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001829 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001830 return 0;
1831}
Chris Masone0156402008-04-16 11:15:20 -04001832
Chris Mason4a69a412008-11-06 22:03:00 -05001833/*
1834 * in order to insert checksums into the metadata in large chunks,
1835 * we wait until bio submission time. All the pages in the bio are
1836 * checksummed and sums are attached onto the ordered extent record.
1837 *
1838 * At IO completion time the cums attached on the ordered extent record
1839 * are inserted into the btree
1840 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001841static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001842 int mirror_num, unsigned long bio_flags,
1843 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001844{
1845 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001846 int ret;
1847
1848 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1849 if (ret)
1850 bio_endio(bio, ret);
1851 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001852}
1853
Chris Masond352ac62008-09-29 15:18:18 -04001854/*
Chris Masoncad321a2008-12-17 14:51:42 -05001855 * extent_io.c submission hook. This does the right thing for csum calculation
1856 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001857 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001858static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001859 int mirror_num, unsigned long bio_flags,
1860 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001861{
1862 struct btrfs_root *root = BTRFS_I(inode)->root;
1863 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001864 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001865 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001866 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001867
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001868 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001869
Liu Bo83eea1f2012-07-10 05:28:39 -06001870 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001871 metadata = 2;
1872
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001873 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001874 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1875 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001876 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001877
Chris Masond20f7042008-12-08 16:58:54 -05001878 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001879 ret = btrfs_submit_compressed_read(inode, bio,
1880 mirror_num,
1881 bio_flags);
1882 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001883 } else if (!skip_sum) {
1884 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1885 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001886 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001887 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001888 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001889 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001890 /* csum items have already been cloned */
1891 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1892 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001893 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001894 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001895 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001896 bio_flags, bio_offset,
1897 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001898 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001899 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001900 } else if (!skip_sum) {
1901 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1902 if (ret)
1903 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001904 }
1905
Chris Mason0b86a832008-03-24 15:01:56 -04001906mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001907 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1908
1909out:
1910 if (ret < 0)
1911 bio_endio(bio, ret);
1912 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001913}
Chris Mason6885f302008-02-20 16:11:05 -05001914
Chris Masond352ac62008-09-29 15:18:18 -04001915/*
1916 * given a list of ordered sums record them in the inode. This happens
1917 * at IO completion time based on sums calculated at bio submission time.
1918 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001919static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001920 struct inode *inode, u64 file_offset,
1921 struct list_head *list)
1922{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001923 struct btrfs_ordered_sum *sum;
1924
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001925 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001926 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001927 btrfs_csum_file_blocks(trans,
1928 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001929 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001930 }
1931 return 0;
1932}
1933
Josef Bacik2ac55d42010-02-03 19:33:23 +00001934int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1935 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001936{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001937 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001938 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001939 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001940}
1941
Chris Masond352ac62008-09-29 15:18:18 -04001942/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001943struct btrfs_writepage_fixup {
1944 struct page *page;
1945 struct btrfs_work work;
1946};
1947
Christoph Hellwigb2950862008-12-02 09:54:17 -05001948static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001949{
1950 struct btrfs_writepage_fixup *fixup;
1951 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001952 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001953 struct page *page;
1954 struct inode *inode;
1955 u64 page_start;
1956 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001957 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001958
1959 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1960 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001961again:
Chris Mason247e7432008-07-17 12:53:51 -04001962 lock_page(page);
1963 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1964 ClearPageChecked(page);
1965 goto out_page;
1966 }
1967
1968 inode = page->mapping->host;
1969 page_start = page_offset(page);
1970 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1971
Josef Bacik2ac55d42010-02-03 19:33:23 +00001972 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001973 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001974
1975 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001976 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001977 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001978
1979 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1980 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001981 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1982 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001983 unlock_page(page);
1984 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001985 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001986 goto again;
1987 }
Chris Mason247e7432008-07-17 12:53:51 -04001988
Jeff Mahoney87826df2012-02-15 16:23:57 +01001989 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1990 if (ret) {
1991 mapping_set_error(page->mapping, ret);
1992 end_extent_writepage(page, ret, page_start, page_end);
1993 ClearPageChecked(page);
1994 goto out;
1995 }
1996
Josef Bacik2ac55d42010-02-03 19:33:23 +00001997 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001998 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001999 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002000out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002001 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2002 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002003out_page:
2004 unlock_page(page);
2005 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002006 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002007}
2008
2009/*
2010 * There are a few paths in the higher layers of the kernel that directly
2011 * set the page dirty bit without asking the filesystem if it is a
2012 * good idea. This causes problems because we want to make sure COW
2013 * properly happens and the data=ordered rules are followed.
2014 *
Chris Masonc8b97812008-10-29 14:49:59 -04002015 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002016 * hasn't been properly setup for IO. We kick off an async process
2017 * to fix it up. The async helper will wait for ordered extents, set
2018 * the delalloc bit and make it safe to write the page.
2019 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002020static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002021{
2022 struct inode *inode = page->mapping->host;
2023 struct btrfs_writepage_fixup *fixup;
2024 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002025
Chris Mason8b62b722009-09-02 16:53:46 -04002026 /* this page is properly in the ordered list */
2027 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002028 return 0;
2029
2030 if (PageChecked(page))
2031 return -EAGAIN;
2032
2033 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2034 if (!fixup)
2035 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002036
Chris Mason247e7432008-07-17 12:53:51 -04002037 SetPageChecked(page);
2038 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002039 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2040 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002041 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002042 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002043 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002044}
2045
Yan Zhengd899e052008-10-30 14:25:28 -04002046static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2047 struct inode *inode, u64 file_pos,
2048 u64 disk_bytenr, u64 disk_num_bytes,
2049 u64 num_bytes, u64 ram_bytes,
2050 u8 compression, u8 encryption,
2051 u16 other_encoding, int extent_type)
2052{
2053 struct btrfs_root *root = BTRFS_I(inode)->root;
2054 struct btrfs_file_extent_item *fi;
2055 struct btrfs_path *path;
2056 struct extent_buffer *leaf;
2057 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002058 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002059 int ret;
2060
2061 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002062 if (!path)
2063 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002064
Chris Masona1ed8352009-09-11 12:27:37 -04002065 /*
2066 * we may be replacing one extent in the tree with another.
2067 * The new extent is pinned in the extent map, and we don't want
2068 * to drop it from the cache until it is completely in the btree.
2069 *
2070 * So, tell btrfs_drop_extents to leave this extent in the cache.
2071 * the caller is expected to unpin it and allow it to be merged
2072 * with the others.
2073 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002074 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2075 file_pos + num_bytes, NULL, 0,
2076 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002077 if (ret)
2078 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002079
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002080 if (!extent_inserted) {
2081 ins.objectid = btrfs_ino(inode);
2082 ins.offset = file_pos;
2083 ins.type = BTRFS_EXTENT_DATA_KEY;
2084
2085 path->leave_spinning = 1;
2086 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2087 sizeof(*fi));
2088 if (ret)
2089 goto out;
2090 }
Yan Zhengd899e052008-10-30 14:25:28 -04002091 leaf = path->nodes[0];
2092 fi = btrfs_item_ptr(leaf, path->slots[0],
2093 struct btrfs_file_extent_item);
2094 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2095 btrfs_set_file_extent_type(leaf, fi, extent_type);
2096 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2097 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2098 btrfs_set_file_extent_offset(leaf, fi, 0);
2099 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2100 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2101 btrfs_set_file_extent_compression(leaf, fi, compression);
2102 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2103 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002104
Yan Zhengd899e052008-10-30 14:25:28 -04002105 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002106 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002107
2108 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002109
2110 ins.objectid = disk_bytenr;
2111 ins.offset = disk_num_bytes;
2112 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002113 ret = btrfs_alloc_reserved_file_extent(trans, root,
2114 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002115 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002116out:
Yan Zhengd899e052008-10-30 14:25:28 -04002117 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002118
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002119 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002120}
2121
Liu Bo38c227d2013-01-29 03:18:40 +00002122/* snapshot-aware defrag */
2123struct sa_defrag_extent_backref {
2124 struct rb_node node;
2125 struct old_sa_defrag_extent *old;
2126 u64 root_id;
2127 u64 inum;
2128 u64 file_pos;
2129 u64 extent_offset;
2130 u64 num_bytes;
2131 u64 generation;
2132};
2133
2134struct old_sa_defrag_extent {
2135 struct list_head list;
2136 struct new_sa_defrag_extent *new;
2137
2138 u64 extent_offset;
2139 u64 bytenr;
2140 u64 offset;
2141 u64 len;
2142 int count;
2143};
2144
2145struct new_sa_defrag_extent {
2146 struct rb_root root;
2147 struct list_head head;
2148 struct btrfs_path *path;
2149 struct inode *inode;
2150 u64 file_pos;
2151 u64 len;
2152 u64 bytenr;
2153 u64 disk_len;
2154 u8 compress_type;
2155};
2156
2157static int backref_comp(struct sa_defrag_extent_backref *b1,
2158 struct sa_defrag_extent_backref *b2)
2159{
2160 if (b1->root_id < b2->root_id)
2161 return -1;
2162 else if (b1->root_id > b2->root_id)
2163 return 1;
2164
2165 if (b1->inum < b2->inum)
2166 return -1;
2167 else if (b1->inum > b2->inum)
2168 return 1;
2169
2170 if (b1->file_pos < b2->file_pos)
2171 return -1;
2172 else if (b1->file_pos > b2->file_pos)
2173 return 1;
2174
2175 /*
2176 * [------------------------------] ===> (a range of space)
2177 * |<--->| |<---->| =============> (fs/file tree A)
2178 * |<---------------------------->| ===> (fs/file tree B)
2179 *
2180 * A range of space can refer to two file extents in one tree while
2181 * refer to only one file extent in another tree.
2182 *
2183 * So we may process a disk offset more than one time(two extents in A)
2184 * and locate at the same extent(one extent in B), then insert two same
2185 * backrefs(both refer to the extent in B).
2186 */
2187 return 0;
2188}
2189
2190static void backref_insert(struct rb_root *root,
2191 struct sa_defrag_extent_backref *backref)
2192{
2193 struct rb_node **p = &root->rb_node;
2194 struct rb_node *parent = NULL;
2195 struct sa_defrag_extent_backref *entry;
2196 int ret;
2197
2198 while (*p) {
2199 parent = *p;
2200 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2201
2202 ret = backref_comp(backref, entry);
2203 if (ret < 0)
2204 p = &(*p)->rb_left;
2205 else
2206 p = &(*p)->rb_right;
2207 }
2208
2209 rb_link_node(&backref->node, parent, p);
2210 rb_insert_color(&backref->node, root);
2211}
2212
2213/*
2214 * Note the backref might has changed, and in this case we just return 0.
2215 */
2216static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2217 void *ctx)
2218{
2219 struct btrfs_file_extent_item *extent;
2220 struct btrfs_fs_info *fs_info;
2221 struct old_sa_defrag_extent *old = ctx;
2222 struct new_sa_defrag_extent *new = old->new;
2223 struct btrfs_path *path = new->path;
2224 struct btrfs_key key;
2225 struct btrfs_root *root;
2226 struct sa_defrag_extent_backref *backref;
2227 struct extent_buffer *leaf;
2228 struct inode *inode = new->inode;
2229 int slot;
2230 int ret;
2231 u64 extent_offset;
2232 u64 num_bytes;
2233
2234 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2235 inum == btrfs_ino(inode))
2236 return 0;
2237
2238 key.objectid = root_id;
2239 key.type = BTRFS_ROOT_ITEM_KEY;
2240 key.offset = (u64)-1;
2241
2242 fs_info = BTRFS_I(inode)->root->fs_info;
2243 root = btrfs_read_fs_root_no_name(fs_info, &key);
2244 if (IS_ERR(root)) {
2245 if (PTR_ERR(root) == -ENOENT)
2246 return 0;
2247 WARN_ON(1);
2248 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2249 inum, offset, root_id);
2250 return PTR_ERR(root);
2251 }
2252
2253 key.objectid = inum;
2254 key.type = BTRFS_EXTENT_DATA_KEY;
2255 if (offset > (u64)-1 << 32)
2256 key.offset = 0;
2257 else
2258 key.offset = offset;
2259
2260 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302261 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002262 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002263 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002264
2265 while (1) {
2266 cond_resched();
2267
2268 leaf = path->nodes[0];
2269 slot = path->slots[0];
2270
2271 if (slot >= btrfs_header_nritems(leaf)) {
2272 ret = btrfs_next_leaf(root, path);
2273 if (ret < 0) {
2274 goto out;
2275 } else if (ret > 0) {
2276 ret = 0;
2277 goto out;
2278 }
2279 continue;
2280 }
2281
2282 path->slots[0]++;
2283
2284 btrfs_item_key_to_cpu(leaf, &key, slot);
2285
2286 if (key.objectid > inum)
2287 goto out;
2288
2289 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2290 continue;
2291
2292 extent = btrfs_item_ptr(leaf, slot,
2293 struct btrfs_file_extent_item);
2294
2295 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2296 continue;
2297
Liu Boe68afa42013-07-01 22:13:26 +08002298 /*
2299 * 'offset' refers to the exact key.offset,
2300 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2301 * (key.offset - extent_offset).
2302 */
2303 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002304 continue;
2305
Liu Boe68afa42013-07-01 22:13:26 +08002306 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002307 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002308
Liu Bo38c227d2013-01-29 03:18:40 +00002309 if (extent_offset >= old->extent_offset + old->offset +
2310 old->len || extent_offset + num_bytes <=
2311 old->extent_offset + old->offset)
2312 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002313 break;
2314 }
2315
2316 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2317 if (!backref) {
2318 ret = -ENOENT;
2319 goto out;
2320 }
2321
2322 backref->root_id = root_id;
2323 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002324 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002325 backref->num_bytes = num_bytes;
2326 backref->extent_offset = extent_offset;
2327 backref->generation = btrfs_file_extent_generation(leaf, extent);
2328 backref->old = old;
2329 backref_insert(&new->root, backref);
2330 old->count++;
2331out:
2332 btrfs_release_path(path);
2333 WARN_ON(ret);
2334 return ret;
2335}
2336
2337static noinline bool record_extent_backrefs(struct btrfs_path *path,
2338 struct new_sa_defrag_extent *new)
2339{
2340 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2341 struct old_sa_defrag_extent *old, *tmp;
2342 int ret;
2343
2344 new->path = path;
2345
2346 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002347 ret = iterate_inodes_from_logical(old->bytenr +
2348 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002349 path, record_one_backref,
2350 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002351 if (ret < 0 && ret != -ENOENT)
2352 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002353
2354 /* no backref to be processed for this extent */
2355 if (!old->count) {
2356 list_del(&old->list);
2357 kfree(old);
2358 }
2359 }
2360
2361 if (list_empty(&new->head))
2362 return false;
2363
2364 return true;
2365}
2366
2367static int relink_is_mergable(struct extent_buffer *leaf,
2368 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002369 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002370{
Liu Bo116e0022013-08-02 16:30:40 +08002371 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002372 return 0;
2373
2374 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2375 return 0;
2376
Liu Bo116e0022013-08-02 16:30:40 +08002377 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2378 return 0;
2379
2380 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002381 btrfs_file_extent_other_encoding(leaf, fi))
2382 return 0;
2383
2384 return 1;
2385}
2386
2387/*
2388 * Note the backref might has changed, and in this case we just return 0.
2389 */
2390static noinline int relink_extent_backref(struct btrfs_path *path,
2391 struct sa_defrag_extent_backref *prev,
2392 struct sa_defrag_extent_backref *backref)
2393{
2394 struct btrfs_file_extent_item *extent;
2395 struct btrfs_file_extent_item *item;
2396 struct btrfs_ordered_extent *ordered;
2397 struct btrfs_trans_handle *trans;
2398 struct btrfs_fs_info *fs_info;
2399 struct btrfs_root *root;
2400 struct btrfs_key key;
2401 struct extent_buffer *leaf;
2402 struct old_sa_defrag_extent *old = backref->old;
2403 struct new_sa_defrag_extent *new = old->new;
2404 struct inode *src_inode = new->inode;
2405 struct inode *inode;
2406 struct extent_state *cached = NULL;
2407 int ret = 0;
2408 u64 start;
2409 u64 len;
2410 u64 lock_start;
2411 u64 lock_end;
2412 bool merge = false;
2413 int index;
2414
2415 if (prev && prev->root_id == backref->root_id &&
2416 prev->inum == backref->inum &&
2417 prev->file_pos + prev->num_bytes == backref->file_pos)
2418 merge = true;
2419
2420 /* step 1: get root */
2421 key.objectid = backref->root_id;
2422 key.type = BTRFS_ROOT_ITEM_KEY;
2423 key.offset = (u64)-1;
2424
2425 fs_info = BTRFS_I(src_inode)->root->fs_info;
2426 index = srcu_read_lock(&fs_info->subvol_srcu);
2427
2428 root = btrfs_read_fs_root_no_name(fs_info, &key);
2429 if (IS_ERR(root)) {
2430 srcu_read_unlock(&fs_info->subvol_srcu, index);
2431 if (PTR_ERR(root) == -ENOENT)
2432 return 0;
2433 return PTR_ERR(root);
2434 }
Liu Bo38c227d2013-01-29 03:18:40 +00002435
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002436 if (btrfs_root_readonly(root)) {
2437 srcu_read_unlock(&fs_info->subvol_srcu, index);
2438 return 0;
2439 }
2440
Liu Bo38c227d2013-01-29 03:18:40 +00002441 /* step 2: get inode */
2442 key.objectid = backref->inum;
2443 key.type = BTRFS_INODE_ITEM_KEY;
2444 key.offset = 0;
2445
2446 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2447 if (IS_ERR(inode)) {
2448 srcu_read_unlock(&fs_info->subvol_srcu, index);
2449 return 0;
2450 }
2451
2452 srcu_read_unlock(&fs_info->subvol_srcu, index);
2453
2454 /* step 3: relink backref */
2455 lock_start = backref->file_pos;
2456 lock_end = backref->file_pos + backref->num_bytes - 1;
2457 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2458 0, &cached);
2459
2460 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2461 if (ordered) {
2462 btrfs_put_ordered_extent(ordered);
2463 goto out_unlock;
2464 }
2465
2466 trans = btrfs_join_transaction(root);
2467 if (IS_ERR(trans)) {
2468 ret = PTR_ERR(trans);
2469 goto out_unlock;
2470 }
2471
2472 key.objectid = backref->inum;
2473 key.type = BTRFS_EXTENT_DATA_KEY;
2474 key.offset = backref->file_pos;
2475
2476 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2477 if (ret < 0) {
2478 goto out_free_path;
2479 } else if (ret > 0) {
2480 ret = 0;
2481 goto out_free_path;
2482 }
2483
2484 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2485 struct btrfs_file_extent_item);
2486
2487 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2488 backref->generation)
2489 goto out_free_path;
2490
2491 btrfs_release_path(path);
2492
2493 start = backref->file_pos;
2494 if (backref->extent_offset < old->extent_offset + old->offset)
2495 start += old->extent_offset + old->offset -
2496 backref->extent_offset;
2497
2498 len = min(backref->extent_offset + backref->num_bytes,
2499 old->extent_offset + old->offset + old->len);
2500 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2501
2502 ret = btrfs_drop_extents(trans, root, inode, start,
2503 start + len, 1);
2504 if (ret)
2505 goto out_free_path;
2506again:
2507 key.objectid = btrfs_ino(inode);
2508 key.type = BTRFS_EXTENT_DATA_KEY;
2509 key.offset = start;
2510
Liu Boa09a0a72013-03-11 09:20:58 +00002511 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002512 if (merge) {
2513 struct btrfs_file_extent_item *fi;
2514 u64 extent_len;
2515 struct btrfs_key found_key;
2516
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002517 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002518 if (ret < 0)
2519 goto out_free_path;
2520
2521 path->slots[0]--;
2522 leaf = path->nodes[0];
2523 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2524
2525 fi = btrfs_item_ptr(leaf, path->slots[0],
2526 struct btrfs_file_extent_item);
2527 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2528
Liu Bo116e0022013-08-02 16:30:40 +08002529 if (extent_len + found_key.offset == start &&
2530 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002531 btrfs_set_file_extent_num_bytes(leaf, fi,
2532 extent_len + len);
2533 btrfs_mark_buffer_dirty(leaf);
2534 inode_add_bytes(inode, len);
2535
2536 ret = 1;
2537 goto out_free_path;
2538 } else {
2539 merge = false;
2540 btrfs_release_path(path);
2541 goto again;
2542 }
2543 }
2544
2545 ret = btrfs_insert_empty_item(trans, root, path, &key,
2546 sizeof(*extent));
2547 if (ret) {
2548 btrfs_abort_transaction(trans, root, ret);
2549 goto out_free_path;
2550 }
2551
2552 leaf = path->nodes[0];
2553 item = btrfs_item_ptr(leaf, path->slots[0],
2554 struct btrfs_file_extent_item);
2555 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2556 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2557 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2558 btrfs_set_file_extent_num_bytes(leaf, item, len);
2559 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2560 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2561 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2562 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2563 btrfs_set_file_extent_encryption(leaf, item, 0);
2564 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2565
2566 btrfs_mark_buffer_dirty(leaf);
2567 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002568 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002569
2570 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2571 new->disk_len, 0,
2572 backref->root_id, backref->inum,
2573 new->file_pos, 0); /* start - extent_offset */
2574 if (ret) {
2575 btrfs_abort_transaction(trans, root, ret);
2576 goto out_free_path;
2577 }
2578
2579 ret = 1;
2580out_free_path:
2581 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002582 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002583 btrfs_end_transaction(trans, root);
2584out_unlock:
2585 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2586 &cached, GFP_NOFS);
2587 iput(inode);
2588 return ret;
2589}
2590
Liu Bo6f519562013-10-29 10:45:05 +08002591static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2592{
2593 struct old_sa_defrag_extent *old, *tmp;
2594
2595 if (!new)
2596 return;
2597
2598 list_for_each_entry_safe(old, tmp, &new->head, list) {
2599 list_del(&old->list);
2600 kfree(old);
2601 }
2602 kfree(new);
2603}
2604
Liu Bo38c227d2013-01-29 03:18:40 +00002605static void relink_file_extents(struct new_sa_defrag_extent *new)
2606{
2607 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002608 struct sa_defrag_extent_backref *backref;
2609 struct sa_defrag_extent_backref *prev = NULL;
2610 struct inode *inode;
2611 struct btrfs_root *root;
2612 struct rb_node *node;
2613 int ret;
2614
2615 inode = new->inode;
2616 root = BTRFS_I(inode)->root;
2617
2618 path = btrfs_alloc_path();
2619 if (!path)
2620 return;
2621
2622 if (!record_extent_backrefs(path, new)) {
2623 btrfs_free_path(path);
2624 goto out;
2625 }
2626 btrfs_release_path(path);
2627
2628 while (1) {
2629 node = rb_first(&new->root);
2630 if (!node)
2631 break;
2632 rb_erase(node, &new->root);
2633
2634 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2635
2636 ret = relink_extent_backref(path, prev, backref);
2637 WARN_ON(ret < 0);
2638
2639 kfree(prev);
2640
2641 if (ret == 1)
2642 prev = backref;
2643 else
2644 prev = NULL;
2645 cond_resched();
2646 }
2647 kfree(prev);
2648
2649 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002650out:
Liu Bo6f519562013-10-29 10:45:05 +08002651 free_sa_defrag_extent(new);
2652
Liu Bo38c227d2013-01-29 03:18:40 +00002653 atomic_dec(&root->fs_info->defrag_running);
2654 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002655}
2656
2657static struct new_sa_defrag_extent *
2658record_old_file_extents(struct inode *inode,
2659 struct btrfs_ordered_extent *ordered)
2660{
2661 struct btrfs_root *root = BTRFS_I(inode)->root;
2662 struct btrfs_path *path;
2663 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002664 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002665 struct new_sa_defrag_extent *new;
2666 int ret;
2667
2668 new = kmalloc(sizeof(*new), GFP_NOFS);
2669 if (!new)
2670 return NULL;
2671
2672 new->inode = inode;
2673 new->file_pos = ordered->file_offset;
2674 new->len = ordered->len;
2675 new->bytenr = ordered->start;
2676 new->disk_len = ordered->disk_len;
2677 new->compress_type = ordered->compress_type;
2678 new->root = RB_ROOT;
2679 INIT_LIST_HEAD(&new->head);
2680
2681 path = btrfs_alloc_path();
2682 if (!path)
2683 goto out_kfree;
2684
2685 key.objectid = btrfs_ino(inode);
2686 key.type = BTRFS_EXTENT_DATA_KEY;
2687 key.offset = new->file_pos;
2688
2689 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2690 if (ret < 0)
2691 goto out_free_path;
2692 if (ret > 0 && path->slots[0] > 0)
2693 path->slots[0]--;
2694
2695 /* find out all the old extents for the file range */
2696 while (1) {
2697 struct btrfs_file_extent_item *extent;
2698 struct extent_buffer *l;
2699 int slot;
2700 u64 num_bytes;
2701 u64 offset;
2702 u64 end;
2703 u64 disk_bytenr;
2704 u64 extent_offset;
2705
2706 l = path->nodes[0];
2707 slot = path->slots[0];
2708
2709 if (slot >= btrfs_header_nritems(l)) {
2710 ret = btrfs_next_leaf(root, path);
2711 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002712 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002713 else if (ret > 0)
2714 break;
2715 continue;
2716 }
2717
2718 btrfs_item_key_to_cpu(l, &key, slot);
2719
2720 if (key.objectid != btrfs_ino(inode))
2721 break;
2722 if (key.type != BTRFS_EXTENT_DATA_KEY)
2723 break;
2724 if (key.offset >= new->file_pos + new->len)
2725 break;
2726
2727 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2728
2729 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2730 if (key.offset + num_bytes < new->file_pos)
2731 goto next;
2732
2733 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2734 if (!disk_bytenr)
2735 goto next;
2736
2737 extent_offset = btrfs_file_extent_offset(l, extent);
2738
2739 old = kmalloc(sizeof(*old), GFP_NOFS);
2740 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002741 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002742
2743 offset = max(new->file_pos, key.offset);
2744 end = min(new->file_pos + new->len, key.offset + num_bytes);
2745
2746 old->bytenr = disk_bytenr;
2747 old->extent_offset = extent_offset;
2748 old->offset = offset - key.offset;
2749 old->len = end - offset;
2750 old->new = new;
2751 old->count = 0;
2752 list_add_tail(&old->list, &new->head);
2753next:
2754 path->slots[0]++;
2755 cond_resched();
2756 }
2757
2758 btrfs_free_path(path);
2759 atomic_inc(&root->fs_info->defrag_running);
2760
2761 return new;
2762
Liu Bo38c227d2013-01-29 03:18:40 +00002763out_free_path:
2764 btrfs_free_path(path);
2765out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002766 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002767 return NULL;
2768}
2769
Miao Xiee570fd22014-06-19 10:42:50 +08002770static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2771 u64 start, u64 len)
2772{
2773 struct btrfs_block_group_cache *cache;
2774
2775 cache = btrfs_lookup_block_group(root->fs_info, start);
2776 ASSERT(cache);
2777
2778 spin_lock(&cache->lock);
2779 cache->delalloc_bytes -= len;
2780 spin_unlock(&cache->lock);
2781
2782 btrfs_put_block_group(cache);
2783}
2784
Chris Masond352ac62008-09-29 15:18:18 -04002785/* as ordered data IO finishes, this gets called so we can finish
2786 * an ordered extent if the range of bytes in the file it covers are
2787 * fully written.
2788 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002789static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002790{
Josef Bacik5fd02042012-05-02 14:00:54 -04002791 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002792 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002793 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002794 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002795 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002796 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002797 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002798 int ret = 0;
2799 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002800 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002801 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002802
Liu Bo83eea1f2012-07-10 05:28:39 -06002803 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002804
Josef Bacik5fd02042012-05-02 14:00:54 -04002805 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2806 ret = -EIO;
2807 goto out;
2808 }
2809
Miao Xief6124962014-09-12 18:44:04 +08002810 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2811 ordered_extent->file_offset +
2812 ordered_extent->len - 1);
2813
Josef Bacik77cef2e2013-08-29 13:57:21 -04002814 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2815 truncated = true;
2816 logical_len = ordered_extent->truncated_len;
2817 /* Truncated the entire extent, don't bother adding */
2818 if (!logical_len)
2819 goto out;
2820 }
2821
Yan, Zhengc2167752009-11-12 09:34:21 +00002822 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002823 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002824 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2825 if (nolock)
2826 trans = btrfs_join_transaction_nolock(root);
2827 else
2828 trans = btrfs_join_transaction(root);
2829 if (IS_ERR(trans)) {
2830 ret = PTR_ERR(trans);
2831 trans = NULL;
2832 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002833 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002834 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2835 ret = btrfs_update_inode_fallback(trans, root, inode);
2836 if (ret) /* -ENOMEM or corruption */
2837 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002838 goto out;
2839 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002840
Josef Bacik2ac55d42010-02-03 19:33:23 +00002841 lock_extent_bits(io_tree, ordered_extent->file_offset,
2842 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002843 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002844
Liu Bo38c227d2013-01-29 03:18:40 +00002845 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2846 ordered_extent->file_offset + ordered_extent->len - 1,
2847 EXTENT_DEFRAG, 1, cached_state);
2848 if (ret) {
2849 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002850 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002851 /* the inode is shared */
2852 new = record_old_file_extents(inode, ordered_extent);
2853
2854 clear_extent_bit(io_tree, ordered_extent->file_offset,
2855 ordered_extent->file_offset + ordered_extent->len - 1,
2856 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2857 }
2858
Josef Bacik0cb59c92010-07-02 12:14:14 -04002859 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002860 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002861 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002862 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002863 if (IS_ERR(trans)) {
2864 ret = PTR_ERR(trans);
2865 trans = NULL;
2866 goto out_unlock;
2867 }
Chris Masona79b7d42014-05-22 16:18:52 -07002868
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002869 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002870
Chris Masonc8b97812008-10-29 14:49:59 -04002871 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002872 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002873 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002874 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002875 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002876 ordered_extent->file_offset,
2877 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002878 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002879 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002880 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002881 ret = insert_reserved_file_extent(trans, inode,
2882 ordered_extent->file_offset,
2883 ordered_extent->start,
2884 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002885 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002886 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002887 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002888 if (!ret)
2889 btrfs_release_delalloc_bytes(root,
2890 ordered_extent->start,
2891 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002892 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002893 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2894 ordered_extent->file_offset, ordered_extent->len,
2895 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002896 if (ret < 0) {
2897 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002898 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002899 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002900
Chris Masone6dcd2d2008-07-17 12:53:50 -04002901 add_pending_csums(trans, inode, ordered_extent->file_offset,
2902 &ordered_extent->list);
2903
Josef Bacik6c760c02012-11-09 10:53:21 -05002904 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2905 ret = btrfs_update_inode_fallback(trans, root, inode);
2906 if (ret) { /* -ENOMEM or corruption */
2907 btrfs_abort_transaction(trans, root, ret);
2908 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002909 }
2910 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002911out_unlock:
2912 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2913 ordered_extent->file_offset +
2914 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002915out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002916 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002917 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002918 if (trans)
2919 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002920
Josef Bacik77cef2e2013-08-29 13:57:21 -04002921 if (ret || truncated) {
2922 u64 start, end;
2923
2924 if (truncated)
2925 start = ordered_extent->file_offset + logical_len;
2926 else
2927 start = ordered_extent->file_offset;
2928 end = ordered_extent->file_offset + ordered_extent->len - 1;
2929 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2930
2931 /* Drop the cache for the part of the extent we didn't write. */
2932 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002933
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002934 /*
2935 * If the ordered extent had an IOERR or something else went
2936 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002937 * back to the allocator. We only free the extent in the
2938 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002939 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002940 if ((ret || !logical_len) &&
2941 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002942 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2943 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002944 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002945 }
2946
2947
Josef Bacik5fd02042012-05-02 14:00:54 -04002948 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002949 * This needs to be done to make sure anybody waiting knows we are done
2950 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002951 */
2952 btrfs_remove_ordered_extent(inode, ordered_extent);
2953
Liu Bo38c227d2013-01-29 03:18:40 +00002954 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002955 if (new) {
2956 if (ret) {
2957 free_sa_defrag_extent(new);
2958 atomic_dec(&root->fs_info->defrag_running);
2959 } else {
2960 relink_file_extents(new);
2961 }
2962 }
Liu Bo38c227d2013-01-29 03:18:40 +00002963
Chris Masone6dcd2d2008-07-17 12:53:50 -04002964 /* once for us */
2965 btrfs_put_ordered_extent(ordered_extent);
2966 /* once for the tree */
2967 btrfs_put_ordered_extent(ordered_extent);
2968
Josef Bacik5fd02042012-05-02 14:00:54 -04002969 return ret;
2970}
2971
2972static void finish_ordered_fn(struct btrfs_work *work)
2973{
2974 struct btrfs_ordered_extent *ordered_extent;
2975 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2976 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002977}
2978
Christoph Hellwigb2950862008-12-02 09:54:17 -05002979static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002980 struct extent_state *state, int uptodate)
2981{
Josef Bacik5fd02042012-05-02 14:00:54 -04002982 struct inode *inode = page->mapping->host;
2983 struct btrfs_root *root = BTRFS_I(inode)->root;
2984 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002985 struct btrfs_workqueue *wq;
2986 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002987
liubo1abe9b82011-03-24 11:18:59 +00002988 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2989
Chris Mason8b62b722009-09-02 16:53:46 -04002990 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002991 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2992 end - start + 1, uptodate))
2993 return 0;
2994
Liu Bo9e0af232014-08-15 23:36:53 +08002995 if (btrfs_is_free_space_inode(inode)) {
2996 wq = root->fs_info->endio_freespace_worker;
2997 func = btrfs_freespace_write_helper;
2998 } else {
2999 wq = root->fs_info->endio_write_workers;
3000 func = btrfs_endio_write_helper;
3001 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003002
Liu Bo9e0af232014-08-15 23:36:53 +08003003 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3004 NULL);
3005 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003006
3007 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003008}
3009
Miao Xiedc380ae2014-09-12 18:43:55 +08003010static int __readpage_endio_check(struct inode *inode,
3011 struct btrfs_io_bio *io_bio,
3012 int icsum, struct page *page,
3013 int pgoff, u64 start, size_t len)
3014{
3015 char *kaddr;
3016 u32 csum_expected;
3017 u32 csum = ~(u32)0;
3018 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
3019 DEFAULT_RATELIMIT_BURST);
3020
3021 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3022
3023 kaddr = kmap_atomic(page);
3024 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3025 btrfs_csum_final(csum, (char *)&csum);
3026 if (csum != csum_expected)
3027 goto zeroit;
3028
3029 kunmap_atomic(kaddr);
3030 return 0;
3031zeroit:
3032 if (__ratelimit(&_rs))
David Sterbaf0954c62014-12-19 18:38:44 +01003033 btrfs_warn(BTRFS_I(inode)->root->fs_info,
Miao Xiedc380ae2014-09-12 18:43:55 +08003034 "csum failed ino %llu off %llu csum %u expected csum %u",
3035 btrfs_ino(inode), start, csum, csum_expected);
3036 memset(kaddr + pgoff, 1, len);
3037 flush_dcache_page(page);
3038 kunmap_atomic(kaddr);
3039 if (csum_expected == 0)
3040 return 0;
3041 return -EIO;
3042}
3043
Chris Masond352ac62008-09-29 15:18:18 -04003044/*
Chris Masond352ac62008-09-29 15:18:18 -04003045 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003046 * if there's a match, we allow the bio to finish. If not, the code in
3047 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003048 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003049static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3050 u64 phy_offset, struct page *page,
3051 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003052{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003053 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003054 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003055 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003056 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003057
Chris Masond20f7042008-12-08 16:58:54 -05003058 if (PageChecked(page)) {
3059 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003060 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003061 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003062
3063 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003064 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003065
Yan Zheng17d217f2008-12-12 10:03:38 -05003066 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003067 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05003068 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
3069 GFP_NOFS);
3070 return 0;
3071 }
3072
Miao Xiefacc8a222013-07-25 19:22:34 +08003073 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003074 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3075 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003076}
Chris Masonb888db22007-08-27 16:49:44 -04003077
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003078struct delayed_iput {
3079 struct list_head list;
3080 struct inode *inode;
3081};
3082
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003083/* JDM: If this is fs-wide, why can't we add a pointer to
3084 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003085void btrfs_add_delayed_iput(struct inode *inode)
3086{
3087 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3088 struct delayed_iput *delayed;
3089
3090 if (atomic_add_unless(&inode->i_count, -1, 1))
3091 return;
3092
3093 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3094 delayed->inode = inode;
3095
3096 spin_lock(&fs_info->delayed_iput_lock);
3097 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3098 spin_unlock(&fs_info->delayed_iput_lock);
3099}
3100
3101void btrfs_run_delayed_iputs(struct btrfs_root *root)
3102{
3103 LIST_HEAD(list);
3104 struct btrfs_fs_info *fs_info = root->fs_info;
3105 struct delayed_iput *delayed;
3106 int empty;
3107
3108 spin_lock(&fs_info->delayed_iput_lock);
3109 empty = list_empty(&fs_info->delayed_iputs);
3110 spin_unlock(&fs_info->delayed_iput_lock);
3111 if (empty)
3112 return;
3113
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003114 spin_lock(&fs_info->delayed_iput_lock);
3115 list_splice_init(&fs_info->delayed_iputs, &list);
3116 spin_unlock(&fs_info->delayed_iput_lock);
3117
3118 while (!list_empty(&list)) {
3119 delayed = list_entry(list.next, struct delayed_iput, list);
3120 list_del(&delayed->list);
3121 iput(delayed->inode);
3122 kfree(delayed);
3123 }
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003124}
3125
Yan, Zhengd68fc572010-05-16 10:49:58 -04003126/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003127 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003128 * files in the subvolume, it removes orphan item and frees block_rsv
3129 * structure.
3130 */
3131void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3132 struct btrfs_root *root)
3133{
Josef Bacik90290e12011-12-02 15:44:12 -05003134 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003135 int ret;
3136
Josef Bacik8a35d952012-05-23 14:26:42 -04003137 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003138 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3139 return;
3140
Josef Bacik90290e12011-12-02 15:44:12 -05003141 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003142 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003143 spin_unlock(&root->orphan_lock);
3144 return;
3145 }
3146
3147 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3148 spin_unlock(&root->orphan_lock);
3149 return;
3150 }
3151
3152 block_rsv = root->orphan_block_rsv;
3153 root->orphan_block_rsv = NULL;
3154 spin_unlock(&root->orphan_lock);
3155
Miao Xie27cdeb72014-04-02 19:51:05 +08003156 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003157 btrfs_root_refs(&root->root_item) > 0) {
3158 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3159 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003160 if (ret)
3161 btrfs_abort_transaction(trans, root, ret);
3162 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003163 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3164 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003165 }
3166
Josef Bacik90290e12011-12-02 15:44:12 -05003167 if (block_rsv) {
3168 WARN_ON(block_rsv->size > 0);
3169 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003170 }
3171}
3172
3173/*
Josef Bacik7b128762008-07-24 12:17:14 -04003174 * This creates an orphan entry for the given inode in case something goes
3175 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003176 *
3177 * NOTE: caller of this function should reserve 5 units of metadata for
3178 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003179 */
3180int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3181{
3182 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003183 struct btrfs_block_rsv *block_rsv = NULL;
3184 int reserve = 0;
3185 int insert = 0;
3186 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003187
Yan, Zhengd68fc572010-05-16 10:49:58 -04003188 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003189 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003190 if (!block_rsv)
3191 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003192 }
3193
Yan, Zhengd68fc572010-05-16 10:49:58 -04003194 spin_lock(&root->orphan_lock);
3195 if (!root->orphan_block_rsv) {
3196 root->orphan_block_rsv = block_rsv;
3197 } else if (block_rsv) {
3198 btrfs_free_block_rsv(root, block_rsv);
3199 block_rsv = NULL;
3200 }
Josef Bacik7b128762008-07-24 12:17:14 -04003201
Josef Bacik8a35d952012-05-23 14:26:42 -04003202 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3203 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003204#if 0
3205 /*
3206 * For proper ENOSPC handling, we should do orphan
3207 * cleanup when mounting. But this introduces backward
3208 * compatibility issue.
3209 */
3210 if (!xchg(&root->orphan_item_inserted, 1))
3211 insert = 2;
3212 else
3213 insert = 1;
3214#endif
3215 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003216 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003217 }
Josef Bacik7b128762008-07-24 12:17:14 -04003218
Josef Bacik72ac3c02012-05-23 14:13:11 -04003219 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3220 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003221 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003222 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003223
Yan, Zhengd68fc572010-05-16 10:49:58 -04003224 /* grab metadata reservation from transaction handle */
3225 if (reserve) {
3226 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003227 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003228 }
3229
3230 /* insert an orphan item to track this unlinked/truncated file */
3231 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003232 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003233 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003234 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003235 if (reserve) {
3236 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3237 &BTRFS_I(inode)->runtime_flags);
3238 btrfs_orphan_release_metadata(inode);
3239 }
3240 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003241 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3242 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003243 btrfs_abort_transaction(trans, root, ret);
3244 return ret;
3245 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003246 }
3247 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003248 }
3249
3250 /* insert an orphan item to track subvolume contains orphan files */
3251 if (insert >= 2) {
3252 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3253 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003254 if (ret && ret != -EEXIST) {
3255 btrfs_abort_transaction(trans, root, ret);
3256 return ret;
3257 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003258 }
3259 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003260}
3261
3262/*
3263 * We have done the truncate/delete so we can go ahead and remove the orphan
3264 * item for this particular inode.
3265 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003266static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3267 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003268{
3269 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003270 int delete_item = 0;
3271 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003272 int ret = 0;
3273
Yan, Zhengd68fc572010-05-16 10:49:58 -04003274 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003275 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3276 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003278
Josef Bacik72ac3c02012-05-23 14:13:11 -04003279 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3280 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003281 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003282 spin_unlock(&root->orphan_lock);
3283
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003284 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003285 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003286 if (trans)
3287 ret = btrfs_del_orphan_item(trans, root,
3288 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003289 }
Josef Bacik7b128762008-07-24 12:17:14 -04003290
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003291 if (release_rsv)
3292 btrfs_orphan_release_metadata(inode);
3293
Josef Bacik4ef31a42013-08-13 14:10:08 -04003294 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003295}
3296
3297/*
3298 * this cleans up any orphans that may be left on the list from the last use
3299 * of this root.
3300 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003301int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003302{
3303 struct btrfs_path *path;
3304 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003305 struct btrfs_key key, found_key;
3306 struct btrfs_trans_handle *trans;
3307 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003308 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003309 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3310
Yan, Zhengd68fc572010-05-16 10:49:58 -04003311 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003312 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003313
3314 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003315 if (!path) {
3316 ret = -ENOMEM;
3317 goto out;
3318 }
Josef Bacik7b128762008-07-24 12:17:14 -04003319 path->reada = -1;
3320
3321 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003322 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003323 key.offset = (u64)-1;
3324
Josef Bacik7b128762008-07-24 12:17:14 -04003325 while (1) {
3326 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003327 if (ret < 0)
3328 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003329
3330 /*
3331 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003332 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003333 * find the key and see if we have stuff that matches
3334 */
3335 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003336 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003337 if (path->slots[0] == 0)
3338 break;
3339 path->slots[0]--;
3340 }
3341
3342 /* pull out the item */
3343 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003344 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3345
3346 /* make sure the item matches what we want */
3347 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3348 break;
David Sterba962a2982014-06-04 18:41:45 +02003349 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003350 break;
3351
3352 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003353 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003354
3355 /*
3356 * this is where we are basically btrfs_lookup, without the
3357 * crossing root thing. we store the inode number in the
3358 * offset of the orphan item.
3359 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003360
3361 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003362 btrfs_err(root->fs_info,
3363 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003364 ret = -EINVAL;
3365 goto out;
3366 }
3367
3368 last_objectid = found_key.offset;
3369
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003370 found_key.objectid = found_key.offset;
3371 found_key.type = BTRFS_INODE_ITEM_KEY;
3372 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003373 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303374 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003375 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003376 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003377
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003378 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3379 struct btrfs_root *dead_root;
3380 struct btrfs_fs_info *fs_info = root->fs_info;
3381 int is_dead_root = 0;
3382
3383 /*
3384 * this is an orphan in the tree root. Currently these
3385 * could come from 2 sources:
3386 * a) a snapshot deletion in progress
3387 * b) a free space cache inode
3388 * We need to distinguish those two, as the snapshot
3389 * orphan must not get deleted.
3390 * find_dead_roots already ran before us, so if this
3391 * is a snapshot deletion, we should find the root
3392 * in the dead_roots list
3393 */
3394 spin_lock(&fs_info->trans_lock);
3395 list_for_each_entry(dead_root, &fs_info->dead_roots,
3396 root_list) {
3397 if (dead_root->root_key.objectid ==
3398 found_key.objectid) {
3399 is_dead_root = 1;
3400 break;
3401 }
3402 }
3403 spin_unlock(&fs_info->trans_lock);
3404 if (is_dead_root) {
3405 /* prevent this orphan from being found again */
3406 key.offset = found_key.objectid - 1;
3407 continue;
3408 }
3409 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003410 /*
3411 * Inode is already gone but the orphan item is still there,
3412 * kill the orphan item.
3413 */
3414 if (ret == -ESTALE) {
3415 trans = btrfs_start_transaction(root, 1);
3416 if (IS_ERR(trans)) {
3417 ret = PTR_ERR(trans);
3418 goto out;
3419 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003420 btrfs_debug(root->fs_info, "auto deleting %Lu",
3421 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003422 ret = btrfs_del_orphan_item(trans, root,
3423 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003424 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003425 if (ret)
3426 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003427 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003428 }
Josef Bacik7b128762008-07-24 12:17:14 -04003429
Josef Bacik7b128762008-07-24 12:17:14 -04003430 /*
3431 * add this inode to the orphan list so btrfs_orphan_del does
3432 * the proper thing when we hit it
3433 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003434 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3435 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003436 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003437
Josef Bacik7b128762008-07-24 12:17:14 -04003438 /* if we have links, this was a truncate, lets do that */
3439 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303440 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003441 iput(inode);
3442 continue;
3443 }
Josef Bacik7b128762008-07-24 12:17:14 -04003444 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003445
3446 /* 1 for the orphan item deletion. */
3447 trans = btrfs_start_transaction(root, 1);
3448 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003449 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003450 ret = PTR_ERR(trans);
3451 goto out;
3452 }
3453 ret = btrfs_orphan_add(trans, inode);
3454 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003455 if (ret) {
3456 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003457 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003458 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003459
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003460 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003461 if (ret)
3462 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003463 } else {
3464 nr_unlink++;
3465 }
3466
3467 /* this will do delete_inode and everything for us */
3468 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003469 if (ret)
3470 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003471 }
Miao Xie3254c872011-11-10 20:45:05 -05003472 /* release the path since we're done with it */
3473 btrfs_release_path(path);
3474
Yan, Zhengd68fc572010-05-16 10:49:58 -04003475 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3476
3477 if (root->orphan_block_rsv)
3478 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3479 (u64)-1);
3480
Miao Xie27cdeb72014-04-02 19:51:05 +08003481 if (root->orphan_block_rsv ||
3482 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003483 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003484 if (!IS_ERR(trans))
3485 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003486 }
Josef Bacik7b128762008-07-24 12:17:14 -04003487
3488 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003489 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003490 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003491 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003492
3493out:
3494 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003495 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003496 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003497 btrfs_free_path(path);
3498 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003499}
3500
Chris Masond352ac62008-09-29 15:18:18 -04003501/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003502 * very simple check to peek ahead in the leaf looking for xattrs. If we
3503 * don't find any xattrs, we know there can't be any acls.
3504 *
3505 * slot is the slot the inode is in, objectid is the objectid of the inode
3506 */
3507static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003508 int slot, u64 objectid,
3509 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003510{
3511 u32 nritems = btrfs_header_nritems(leaf);
3512 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003513 static u64 xattr_access = 0;
3514 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003515 int scanned = 0;
3516
Josef Bacikf23b5a52013-06-19 10:16:26 -04003517 if (!xattr_access) {
3518 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3519 strlen(POSIX_ACL_XATTR_ACCESS));
3520 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3521 strlen(POSIX_ACL_XATTR_DEFAULT));
3522 }
3523
Chris Mason46a53cc2009-04-27 11:47:50 -04003524 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003525 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003526 while (slot < nritems) {
3527 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3528
3529 /* we found a different objectid, there must not be acls */
3530 if (found_key.objectid != objectid)
3531 return 0;
3532
3533 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003534 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003535 if (*first_xattr_slot == -1)
3536 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003537 if (found_key.offset == xattr_access ||
3538 found_key.offset == xattr_default)
3539 return 1;
3540 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003541
3542 /*
3543 * we found a key greater than an xattr key, there can't
3544 * be any acls later on
3545 */
3546 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3547 return 0;
3548
3549 slot++;
3550 scanned++;
3551
3552 /*
3553 * it goes inode, inode backrefs, xattrs, extents,
3554 * so if there are a ton of hard links to an inode there can
3555 * be a lot of backrefs. Don't waste time searching too hard,
3556 * this is just an optimization
3557 */
3558 if (scanned >= 8)
3559 break;
3560 }
3561 /* we hit the end of the leaf before we found an xattr or
3562 * something larger than an xattr. We have to assume the inode
3563 * has acls
3564 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003565 if (*first_xattr_slot == -1)
3566 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003567 return 1;
3568}
3569
3570/*
Chris Masond352ac62008-09-29 15:18:18 -04003571 * read an inode from the btree into the in-memory inode
3572 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003573static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003574{
3575 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003576 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003577 struct btrfs_inode_item *inode_item;
3578 struct btrfs_root *root = BTRFS_I(inode)->root;
3579 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003580 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003581 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003582 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003583 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003584 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003585 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003586
3587 ret = btrfs_fill_inode(inode, &rdev);
3588 if (!ret)
3589 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003590
3591 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003592 if (!path)
3593 goto make_bad;
3594
Chris Mason39279cc2007-06-12 06:35:45 -04003595 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003596
Chris Mason39279cc2007-06-12 06:35:45 -04003597 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003598 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003599 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003600
Chris Mason5f39d392007-10-15 16:14:19 -04003601 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003602
3603 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003604 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003605
Chris Mason5f39d392007-10-15 16:14:19 -04003606 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3607 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003608 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003609 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003610 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3611 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003612 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003613
David Sterbaa937b972014-12-12 17:39:12 +01003614 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3615 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003616
David Sterbaa937b972014-12-12 17:39:12 +01003617 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3618 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003619
David Sterbaa937b972014-12-12 17:39:12 +01003620 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3621 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003622
chandan r9cc97d62012-07-04 12:48:07 +05303623 BTRFS_I(inode)->i_otime.tv_sec =
3624 btrfs_timespec_sec(leaf, &inode_item->otime);
3625 BTRFS_I(inode)->i_otime.tv_nsec =
3626 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003627
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003628 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003629 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003630 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3631
3632 /*
3633 * If we were modified in the current generation and evicted from memory
3634 * and then re-read we need to do a full sync since we don't have any
3635 * idea about which extents were modified before we were evicted from
3636 * cache.
3637 */
3638 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3639 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3640 &BTRFS_I(inode)->runtime_flags);
3641
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003642 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003643 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003644 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003645 rdev = btrfs_inode_rdev(leaf, inode_item);
3646
Josef Bacikaec74772008-07-24 12:12:38 -04003647 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003648 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003649
3650cache_index:
3651 path->slots[0]++;
3652 if (inode->i_nlink != 1 ||
3653 path->slots[0] >= btrfs_header_nritems(leaf))
3654 goto cache_acl;
3655
3656 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3657 if (location.objectid != btrfs_ino(inode))
3658 goto cache_acl;
3659
3660 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3661 if (location.type == BTRFS_INODE_REF_KEY) {
3662 struct btrfs_inode_ref *ref;
3663
3664 ref = (struct btrfs_inode_ref *)ptr;
3665 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3666 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3667 struct btrfs_inode_extref *extref;
3668
3669 extref = (struct btrfs_inode_extref *)ptr;
3670 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3671 extref);
3672 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003673cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003674 /*
3675 * try to precache a NULL acl entry for files that don't have
3676 * any xattrs or acls
3677 */
Li Zefan33345d012011-04-20 10:31:50 +08003678 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003679 btrfs_ino(inode), &first_xattr_slot);
3680 if (first_xattr_slot != -1) {
3681 path->slots[0] = first_xattr_slot;
3682 ret = btrfs_load_inode_props(inode, path);
3683 if (ret)
3684 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003685 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003686 btrfs_ino(inode),
3687 root->root_key.objectid, ret);
3688 }
3689 btrfs_free_path(path);
3690
Al Viro72c04902009-06-24 16:58:48 -04003691 if (!maybe_acls)
3692 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003693
Chris Mason39279cc2007-06-12 06:35:45 -04003694 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003695 case S_IFREG:
3696 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003697 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003698 inode->i_fop = &btrfs_file_operations;
3699 inode->i_op = &btrfs_file_inode_operations;
3700 break;
3701 case S_IFDIR:
3702 inode->i_fop = &btrfs_dir_file_operations;
3703 if (root == root->fs_info->tree_root)
3704 inode->i_op = &btrfs_dir_ro_inode_operations;
3705 else
3706 inode->i_op = &btrfs_dir_inode_operations;
3707 break;
3708 case S_IFLNK:
3709 inode->i_op = &btrfs_symlink_inode_operations;
3710 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3711 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003712 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003713 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003714 init_special_inode(inode, inode->i_mode, rdev);
3715 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003716 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003717
3718 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003719 return;
3720
3721make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003722 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003723 make_bad_inode(inode);
3724}
3725
Chris Masond352ac62008-09-29 15:18:18 -04003726/*
3727 * given a leaf and an inode, copy the inode fields into the leaf
3728 */
Chris Masone02119d2008-09-05 16:13:11 -04003729static void fill_inode_item(struct btrfs_trans_handle *trans,
3730 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003731 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003732 struct inode *inode)
3733{
Liu Bo51fab692012-12-27 09:01:21 +00003734 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003735
Liu Bo51fab692012-12-27 09:01:21 +00003736 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003737
Liu Bo51fab692012-12-27 09:01:21 +00003738 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3739 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3740 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3741 &token);
3742 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3743 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003744
David Sterbaa937b972014-12-12 17:39:12 +01003745 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003746 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003747 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003748 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003749
David Sterbaa937b972014-12-12 17:39:12 +01003750 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003751 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003752 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003753 inode->i_mtime.tv_nsec, &token);
3754
David Sterbaa937b972014-12-12 17:39:12 +01003755 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003756 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003757 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003758 inode->i_ctime.tv_nsec, &token);
3759
chandan r9cc97d62012-07-04 12:48:07 +05303760 btrfs_set_token_timespec_sec(leaf, &item->otime,
3761 BTRFS_I(inode)->i_otime.tv_sec, &token);
3762 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3763 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3764
Liu Bo51fab692012-12-27 09:01:21 +00003765 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3766 &token);
3767 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3768 &token);
3769 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3770 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3771 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3772 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3773 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003774}
3775
Chris Masond352ac62008-09-29 15:18:18 -04003776/*
3777 * copy everything in the in-memory inode into the btree.
3778 */
Chris Mason21151332011-11-10 20:39:08 -05003779static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003780 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003781{
3782 struct btrfs_inode_item *inode_item;
3783 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003784 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003785 int ret;
3786
3787 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003788 if (!path)
3789 return -ENOMEM;
3790
Chris Masonb9473432009-03-13 11:00:37 -04003791 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003792 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3793 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003794 if (ret) {
3795 if (ret > 0)
3796 ret = -ENOENT;
3797 goto failed;
3798 }
3799
Chris Mason5f39d392007-10-15 16:14:19 -04003800 leaf = path->nodes[0];
3801 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003802 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003803
Chris Masone02119d2008-09-05 16:13:11 -04003804 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003805 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003806 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003807 ret = 0;
3808failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003809 btrfs_free_path(path);
3810 return ret;
3811}
3812
Chris Masond352ac62008-09-29 15:18:18 -04003813/*
Chris Mason21151332011-11-10 20:39:08 -05003814 * copy everything in the in-memory inode into the btree.
3815 */
3816noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3817 struct btrfs_root *root, struct inode *inode)
3818{
3819 int ret;
3820
3821 /*
3822 * If the inode is a free space inode, we can deadlock during commit
3823 * if we put it into the delayed code.
3824 *
3825 * The data relocation inode should also be directly updated
3826 * without delay
3827 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003828 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003829 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3830 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003831 btrfs_update_root_times(trans, root);
3832
Chris Mason21151332011-11-10 20:39:08 -05003833 ret = btrfs_delayed_update_inode(trans, root, inode);
3834 if (!ret)
3835 btrfs_set_inode_last_trans(trans, inode);
3836 return ret;
3837 }
3838
3839 return btrfs_update_inode_item(trans, root, inode);
3840}
3841
Josef Bacikbe6aef62012-10-22 15:43:12 -04003842noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3843 struct btrfs_root *root,
3844 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003845{
3846 int ret;
3847
3848 ret = btrfs_update_inode(trans, root, inode);
3849 if (ret == -ENOSPC)
3850 return btrfs_update_inode_item(trans, root, inode);
3851 return ret;
3852}
3853
3854/*
Chris Masond352ac62008-09-29 15:18:18 -04003855 * unlink helper that gets used here in inode.c and in the tree logging
3856 * recovery code. It remove a link in a directory with a given name, and
3857 * also drops the back refs in the inode to the directory
3858 */
Al Viro92986792011-03-04 17:14:37 +00003859static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3860 struct btrfs_root *root,
3861 struct inode *dir, struct inode *inode,
3862 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003863{
3864 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003865 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003866 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003867 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003868 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003869 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003870 u64 ino = btrfs_ino(inode);
3871 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003872
3873 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003874 if (!path) {
3875 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003876 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003877 }
3878
Chris Masonb9473432009-03-13 11:00:37 -04003879 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003880 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003881 name, name_len, -1);
3882 if (IS_ERR(di)) {
3883 ret = PTR_ERR(di);
3884 goto err;
3885 }
3886 if (!di) {
3887 ret = -ENOENT;
3888 goto err;
3889 }
Chris Mason5f39d392007-10-15 16:14:19 -04003890 leaf = path->nodes[0];
3891 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003892 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003893 if (ret)
3894 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003895 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003896
Miao Xie67de1172013-12-26 13:07:06 +08003897 /*
3898 * If we don't have dir index, we have to get it by looking up
3899 * the inode ref, since we get the inode ref, remove it directly,
3900 * it is unnecessary to do delayed deletion.
3901 *
3902 * But if we have dir index, needn't search inode ref to get it.
3903 * Since the inode ref is close to the inode item, it is better
3904 * that we delay to delete it, and just do this deletion when
3905 * we update the inode item.
3906 */
3907 if (BTRFS_I(inode)->dir_index) {
3908 ret = btrfs_delayed_delete_inode_ref(inode);
3909 if (!ret) {
3910 index = BTRFS_I(inode)->dir_index;
3911 goto skip_backref;
3912 }
3913 }
3914
Li Zefan33345d012011-04-20 10:31:50 +08003915 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3916 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003917 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003918 btrfs_info(root->fs_info,
3919 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003920 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003921 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003922 goto err;
3923 }
Miao Xie67de1172013-12-26 13:07:06 +08003924skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003925 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003926 if (ret) {
3927 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003928 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003929 }
Chris Mason39279cc2007-06-12 06:35:45 -04003930
Chris Masone02119d2008-09-05 16:13:11 -04003931 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003932 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003933 if (ret != 0 && ret != -ENOENT) {
3934 btrfs_abort_transaction(trans, root, ret);
3935 goto err;
3936 }
Chris Masone02119d2008-09-05 16:13:11 -04003937
3938 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3939 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003940 if (ret == -ENOENT)
3941 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003942 else if (ret)
3943 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003944err:
3945 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003946 if (ret)
3947 goto out;
3948
3949 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003950 inode_inc_iversion(inode);
3951 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003952 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003953 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003954out:
Chris Mason39279cc2007-06-12 06:35:45 -04003955 return ret;
3956}
3957
Al Viro92986792011-03-04 17:14:37 +00003958int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3959 struct btrfs_root *root,
3960 struct inode *dir, struct inode *inode,
3961 const char *name, int name_len)
3962{
3963 int ret;
3964 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3965 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003966 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003967 ret = btrfs_update_inode(trans, root, inode);
3968 }
3969 return ret;
3970}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003971
3972/*
3973 * helper to start transaction for unlink and rmdir.
3974 *
Josef Bacikd52be812013-05-29 14:54:47 -04003975 * unlink and rmdir are special in btrfs, they do not always free space, so
3976 * if we cannot make our reservations the normal way try and see if there is
3977 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3978 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003979 */
Josef Bacikd52be812013-05-29 14:54:47 -04003980static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003981{
3982 struct btrfs_trans_handle *trans;
3983 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003984 int ret;
3985
Josef Bacike70bea52011-10-11 14:18:24 -04003986 /*
3987 * 1 for the possible orphan item
3988 * 1 for the dir item
3989 * 1 for the dir index
3990 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003991 * 1 for the inode
3992 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003993 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003994 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3995 return trans;
3996
Josef Bacikd52be812013-05-29 14:54:47 -04003997 if (PTR_ERR(trans) == -ENOSPC) {
3998 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003999
Josef Bacikd52be812013-05-29 14:54:47 -04004000 trans = btrfs_start_transaction(root, 0);
4001 if (IS_ERR(trans))
4002 return trans;
4003 ret = btrfs_cond_migrate_bytes(root->fs_info,
4004 &root->fs_info->trans_block_rsv,
4005 num_bytes, 5);
4006 if (ret) {
4007 btrfs_end_transaction(trans, root);
4008 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004009 }
Josef Bacik5a77d762011-11-01 14:32:23 -04004010 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04004011 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004012 }
Josef Bacikd52be812013-05-29 14:54:47 -04004013 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004014}
4015
Chris Mason39279cc2007-06-12 06:35:45 -04004016static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4017{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004018 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004019 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04004020 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004021 int ret;
4022
Josef Bacikd52be812013-05-29 14:54:47 -04004023 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004024 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004025 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004026
Chris Mason12fcfd22009-03-24 10:24:20 -04004027 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
4028
Chris Masone02119d2008-09-05 16:13:11 -04004029 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4030 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004031 if (ret)
4032 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004033
Yan, Zhenga22285a2010-05-16 10:48:46 -04004034 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004035 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004036 if (ret)
4037 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004038 }
Josef Bacik7b128762008-07-24 12:17:14 -04004039
Tsutomu Itohb5324022011-07-19 07:27:20 +00004040out:
Josef Bacikd52be812013-05-29 14:54:47 -04004041 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004042 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004043 return ret;
4044}
4045
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004046int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4047 struct btrfs_root *root,
4048 struct inode *dir, u64 objectid,
4049 const char *name, int name_len)
4050{
4051 struct btrfs_path *path;
4052 struct extent_buffer *leaf;
4053 struct btrfs_dir_item *di;
4054 struct btrfs_key key;
4055 u64 index;
4056 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004057 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004058
4059 path = btrfs_alloc_path();
4060 if (!path)
4061 return -ENOMEM;
4062
Li Zefan33345d012011-04-20 10:31:50 +08004063 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004064 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004065 if (IS_ERR_OR_NULL(di)) {
4066 if (!di)
4067 ret = -ENOENT;
4068 else
4069 ret = PTR_ERR(di);
4070 goto out;
4071 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004072
4073 leaf = path->nodes[0];
4074 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4075 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4076 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004077 if (ret) {
4078 btrfs_abort_transaction(trans, root, ret);
4079 goto out;
4080 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004081 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004082
4083 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4084 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004085 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004086 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004087 if (ret != -ENOENT) {
4088 btrfs_abort_transaction(trans, root, ret);
4089 goto out;
4090 }
Li Zefan33345d012011-04-20 10:31:50 +08004091 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004092 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004093 if (IS_ERR_OR_NULL(di)) {
4094 if (!di)
4095 ret = -ENOENT;
4096 else
4097 ret = PTR_ERR(di);
4098 btrfs_abort_transaction(trans, root, ret);
4099 goto out;
4100 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004101
4102 leaf = path->nodes[0];
4103 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004104 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004105 index = key.offset;
4106 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004107 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004108
Miao Xie16cdcec2011-04-22 18:12:22 +08004109 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004110 if (ret) {
4111 btrfs_abort_transaction(trans, root, ret);
4112 goto out;
4113 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004114
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004115 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004116 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004117 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004118 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004119 if (ret)
4120 btrfs_abort_transaction(trans, root, ret);
4121out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004122 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004123 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004124}
4125
Chris Mason39279cc2007-06-12 06:35:45 -04004126static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4127{
4128 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004129 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004130 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004131 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004132
David Sterbab3ae2442012-09-13 16:04:34 -06004133 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004134 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004135 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4136 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004137
Josef Bacikd52be812013-05-29 14:54:47 -04004138 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004139 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004140 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004141
Li Zefan33345d012011-04-20 10:31:50 +08004142 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004143 err = btrfs_unlink_subvol(trans, root, dir,
4144 BTRFS_I(inode)->location.objectid,
4145 dentry->d_name.name,
4146 dentry->d_name.len);
4147 goto out;
4148 }
4149
Josef Bacik7b128762008-07-24 12:17:14 -04004150 err = btrfs_orphan_add(trans, inode);
4151 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004152 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004153
Chris Mason39279cc2007-06-12 06:35:45 -04004154 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004155 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4156 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004157 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004158 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004159out:
Josef Bacikd52be812013-05-29 14:54:47 -04004160 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004161 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004162
Chris Mason39279cc2007-06-12 06:35:45 -04004163 return err;
4164}
4165
Chris Mason28f75a02015-02-04 06:59:29 -08004166static int truncate_space_check(struct btrfs_trans_handle *trans,
4167 struct btrfs_root *root,
4168 u64 bytes_deleted)
4169{
4170 int ret;
4171
4172 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
4173 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4174 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4175 if (!ret)
4176 trans->bytes_reserved += bytes_deleted;
4177 return ret;
4178
4179}
4180
Chris Mason323ac952008-10-01 19:05:46 -04004181/*
Chris Mason39279cc2007-06-12 06:35:45 -04004182 * this can truncate away extent items, csum items and directory items.
4183 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004184 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004185 *
4186 * csum items that cross the new i_size are truncated to the new size
4187 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004188 *
4189 * min_type is the minimum key type to truncate down to. If set to 0, this
4190 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004191 */
Yan, Zheng80825102009-11-12 09:35:36 +00004192int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4193 struct btrfs_root *root,
4194 struct inode *inode,
4195 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004196{
Chris Mason39279cc2007-06-12 06:35:45 -04004197 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004198 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004199 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004200 struct btrfs_key key;
4201 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004202 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004203 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004204 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004205 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004206 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004207 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004208 int found_extent;
4209 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004210 int pending_del_nr = 0;
4211 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004212 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004213 int ret;
4214 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004215 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004216 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004217 bool be_nice = 0;
4218 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004219 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004220
4221 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004222
Chris Mason28ed1342014-12-17 09:41:04 -08004223 /*
4224 * for non-free space inodes and ref cows, we want to back off from
4225 * time to time
4226 */
4227 if (!btrfs_is_free_space_inode(inode) &&
4228 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4229 be_nice = 1;
4230
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004231 path = btrfs_alloc_path();
4232 if (!path)
4233 return -ENOMEM;
4234 path->reada = -1;
4235
Josef Bacik5dc562c2012-08-17 13:14:17 -04004236 /*
4237 * We want to drop from the next block forward in case this new size is
4238 * not block aligned since we will be keeping the last block of the
4239 * extent just the way it is.
4240 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004241 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4242 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004243 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4244 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004245
Miao Xie16cdcec2011-04-22 18:12:22 +08004246 /*
4247 * This function is also used to drop the items in the log tree before
4248 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4249 * it is used to drop the loged items. So we shouldn't kill the delayed
4250 * items.
4251 */
4252 if (min_type == 0 && root == BTRFS_I(inode)->root)
4253 btrfs_kill_delayed_inode_items(inode);
4254
Li Zefan33345d012011-04-20 10:31:50 +08004255 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004256 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004257 key.type = (u8)-1;
4258
Chris Mason85e21ba2008-01-29 15:11:36 -05004259search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004260 /*
4261 * with a 16K leaf size and 128MB extents, you can actually queue
4262 * up a huge file in a single leaf. Most of the time that
4263 * bytes_deleted is > 0, it will be huge by the time we get here
4264 */
4265 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
4266 if (btrfs_should_end_transaction(trans, root)) {
4267 err = -EAGAIN;
4268 goto error;
4269 }
4270 }
4271
4272
Chris Masonb9473432009-03-13 11:00:37 -04004273 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004274 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004275 if (ret < 0) {
4276 err = ret;
4277 goto out;
4278 }
Chris Masond3977122009-01-05 21:25:51 -05004279
Chris Mason85e21ba2008-01-29 15:11:36 -05004280 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004281 /* there are no items in the tree for us to truncate, we're
4282 * done
4283 */
Yan, Zheng80825102009-11-12 09:35:36 +00004284 if (path->slots[0] == 0)
4285 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004286 path->slots[0]--;
4287 }
4288
Chris Masond3977122009-01-05 21:25:51 -05004289 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004290 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004291 leaf = path->nodes[0];
4292 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004293 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004294
Li Zefan33345d012011-04-20 10:31:50 +08004295 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004296 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004297
Chris Mason85e21ba2008-01-29 15:11:36 -05004298 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004299 break;
4300
Chris Mason5f39d392007-10-15 16:14:19 -04004301 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004302 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004303 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004304 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004305 extent_type = btrfs_file_extent_type(leaf, fi);
4306 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004307 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004308 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004309 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004310 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004311 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004312 }
Yan008630c2007-11-07 13:31:09 -05004313 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004314 }
Yan, Zheng80825102009-11-12 09:35:36 +00004315 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004316 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004317 } else {
4318 if (item_end < new_size)
4319 break;
4320 if (found_key.offset >= new_size)
4321 del_item = 1;
4322 else
4323 del_item = 0;
4324 }
Chris Mason39279cc2007-06-12 06:35:45 -04004325 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004326 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004327 if (found_type != BTRFS_EXTENT_DATA_KEY)
4328 goto delete;
4329
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004330 if (del_item)
4331 last_size = found_key.offset;
4332 else
4333 last_size = new_size;
4334
Chris Mason179e29e2007-11-01 11:28:41 -04004335 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004336 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004337 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004338 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004339 u64 orig_num_bytes =
4340 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004341 extent_num_bytes = ALIGN(new_size -
4342 found_key.offset,
4343 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004344 btrfs_set_file_extent_num_bytes(leaf, fi,
4345 extent_num_bytes);
4346 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004347 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004348 if (test_bit(BTRFS_ROOT_REF_COWS,
4349 &root->state) &&
4350 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004351 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004352 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004353 } else {
Chris Masondb945352007-10-15 16:15:53 -04004354 extent_num_bytes =
4355 btrfs_file_extent_disk_num_bytes(leaf,
4356 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004357 extent_offset = found_key.offset -
4358 btrfs_file_extent_offset(leaf, fi);
4359
Chris Mason39279cc2007-06-12 06:35:45 -04004360 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004361 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004362 if (extent_start != 0) {
4363 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004364 if (test_bit(BTRFS_ROOT_REF_COWS,
4365 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004366 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004367 }
4368 }
Chris Mason90692182008-02-08 13:49:28 -05004369 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004370 /*
4371 * we can't truncate inline items that have had
4372 * special encodings
4373 */
4374 if (!del_item &&
4375 btrfs_file_extent_compression(leaf, fi) == 0 &&
4376 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4377 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004378 u32 size = new_size - found_key.offset;
4379
Miao Xie27cdeb72014-04-02 19:51:05 +08004380 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004381 inode_sub_bytes(inode, item_end + 1 -
4382 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004383
4384 /*
4385 * update the ram bytes to properly reflect
4386 * the new size of our item
4387 */
4388 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004389 size =
4390 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004391 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004392 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4393 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004394 inode_sub_bytes(inode, item_end + 1 -
4395 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004396 }
Chris Mason39279cc2007-06-12 06:35:45 -04004397 }
Chris Mason179e29e2007-11-01 11:28:41 -04004398delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004399 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004400 if (!pending_del_nr) {
4401 /* no pending yet, add ourselves */
4402 pending_del_slot = path->slots[0];
4403 pending_del_nr = 1;
4404 } else if (pending_del_nr &&
4405 path->slots[0] + 1 == pending_del_slot) {
4406 /* hop on the pending chunk */
4407 pending_del_nr++;
4408 pending_del_slot = path->slots[0];
4409 } else {
Chris Masond3977122009-01-05 21:25:51 -05004410 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004411 }
Chris Mason39279cc2007-06-12 06:35:45 -04004412 } else {
4413 break;
4414 }
Chris Mason28f75a02015-02-04 06:59:29 -08004415 should_throttle = 0;
4416
Miao Xie27cdeb72014-04-02 19:51:05 +08004417 if (found_extent &&
4418 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4419 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004420 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004421 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004422 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004423 extent_num_bytes, 0,
4424 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004425 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004426 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004427 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004428 btrfs_async_run_delayed_refs(root,
4429 trans->delayed_ref_updates * 2, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004430 if (be_nice) {
4431 if (truncate_space_check(trans, root,
4432 extent_num_bytes)) {
4433 should_end = 1;
4434 }
4435 if (btrfs_should_throttle_delayed_refs(trans,
4436 root)) {
4437 should_throttle = 1;
4438 }
4439 }
Chris Mason39279cc2007-06-12 06:35:45 -04004440 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004441
Yan, Zheng80825102009-11-12 09:35:36 +00004442 if (found_type == BTRFS_INODE_ITEM_KEY)
4443 break;
4444
4445 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004446 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004447 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004448 if (pending_del_nr) {
4449 ret = btrfs_del_items(trans, root, path,
4450 pending_del_slot,
4451 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004452 if (ret) {
4453 btrfs_abort_transaction(trans,
4454 root, ret);
4455 goto error;
4456 }
Yan, Zheng80825102009-11-12 09:35:36 +00004457 pending_del_nr = 0;
4458 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004459 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004460 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004461 unsigned long updates = trans->delayed_ref_updates;
4462 if (updates) {
4463 trans->delayed_ref_updates = 0;
4464 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4465 if (ret && !err)
4466 err = ret;
4467 }
4468 }
Chris Mason28f75a02015-02-04 06:59:29 -08004469 /*
4470 * if we failed to refill our space rsv, bail out
4471 * and let the transaction restart
4472 */
4473 if (should_end) {
4474 err = -EAGAIN;
4475 goto error;
4476 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004477 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004478 } else {
4479 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004480 }
Chris Mason39279cc2007-06-12 06:35:45 -04004481 }
Yan, Zheng80825102009-11-12 09:35:36 +00004482out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004483 if (pending_del_nr) {
4484 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4485 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004486 if (ret)
4487 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004488 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004489error:
Filipe Mananadac57052014-08-29 20:54:26 +01004490 if (last_size != (u64)-1 &&
4491 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004492 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004493
Chris Mason39279cc2007-06-12 06:35:45 -04004494 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004495
Chris Mason28f75a02015-02-04 06:59:29 -08004496 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
Chris Mason28ed1342014-12-17 09:41:04 -08004497 unsigned long updates = trans->delayed_ref_updates;
4498 if (updates) {
4499 trans->delayed_ref_updates = 0;
4500 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4501 if (ret && !err)
4502 err = ret;
4503 }
4504 }
Yan, Zheng80825102009-11-12 09:35:36 +00004505 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004506}
4507
Chris Masona52d9a82007-08-27 16:49:44 -04004508/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004509 * btrfs_truncate_page - read, zero a chunk and write a page
4510 * @inode - inode that we're zeroing
4511 * @from - the offset to start zeroing
4512 * @len - the length to zero, 0 to zero the entire range respective to the
4513 * offset
4514 * @front - zero up to the offset instead of from the offset on
4515 *
4516 * This will find the page for the "from" offset and cow the page and zero the
4517 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004518 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004519int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4520 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004521{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004522 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004523 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004524 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4525 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004526 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004527 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004528 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004529 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4530 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4531 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004532 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004533 int ret = 0;
4534 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004535 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004536
Josef Bacik2aaa6652012-08-29 14:27:18 -04004537 if ((offset & (blocksize - 1)) == 0 &&
4538 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004539 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004540 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004541 if (ret)
4542 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004543
Chris Mason211c17f2008-05-15 09:13:45 -04004544again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004545 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004546 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004547 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004548 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004549 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004550 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004551
4552 page_start = page_offset(page);
4553 page_end = page_start + PAGE_CACHE_SIZE - 1;
4554
Chris Masona52d9a82007-08-27 16:49:44 -04004555 if (!PageUptodate(page)) {
4556 ret = btrfs_readpage(NULL, page);
4557 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004558 if (page->mapping != mapping) {
4559 unlock_page(page);
4560 page_cache_release(page);
4561 goto again;
4562 }
Chris Masona52d9a82007-08-27 16:49:44 -04004563 if (!PageUptodate(page)) {
4564 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004565 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004566 }
4567 }
Chris Mason211c17f2008-05-15 09:13:45 -04004568 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004569
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004570 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004571 set_page_extent_mapped(page);
4572
4573 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4574 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004575 unlock_extent_cached(io_tree, page_start, page_end,
4576 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004577 unlock_page(page);
4578 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004579 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004580 btrfs_put_ordered_extent(ordered);
4581 goto again;
4582 }
4583
Josef Bacik2ac55d42010-02-03 19:33:23 +00004584 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004585 EXTENT_DIRTY | EXTENT_DELALLOC |
4586 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004587 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004588
Josef Bacik2ac55d42010-02-03 19:33:23 +00004589 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4590 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004591 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004592 unlock_extent_cached(io_tree, page_start, page_end,
4593 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004594 goto out_unlock;
4595 }
4596
Chris Masone6dcd2d2008-07-17 12:53:50 -04004597 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004598 if (!len)
4599 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004600 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004601 if (front)
4602 memset(kaddr, 0, offset);
4603 else
4604 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004605 flush_dcache_page(page);
4606 kunmap(page);
4607 }
Chris Mason247e7432008-07-17 12:53:51 -04004608 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004609 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004610 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4611 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004612
Chris Mason89642222008-07-24 09:41:53 -04004613out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004614 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004615 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004616 unlock_page(page);
4617 page_cache_release(page);
4618out:
4619 return ret;
4620}
4621
Josef Bacik16e75492013-10-22 12:18:51 -04004622static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4623 u64 offset, u64 len)
4624{
4625 struct btrfs_trans_handle *trans;
4626 int ret;
4627
4628 /*
4629 * Still need to make sure the inode looks like it's been updated so
4630 * that any holes get logged if we fsync.
4631 */
4632 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4633 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4634 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4635 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4636 return 0;
4637 }
4638
4639 /*
4640 * 1 - for the one we're dropping
4641 * 1 - for the one we're adding
4642 * 1 - for updating the inode.
4643 */
4644 trans = btrfs_start_transaction(root, 3);
4645 if (IS_ERR(trans))
4646 return PTR_ERR(trans);
4647
4648 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4649 if (ret) {
4650 btrfs_abort_transaction(trans, root, ret);
4651 btrfs_end_transaction(trans, root);
4652 return ret;
4653 }
4654
4655 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4656 0, 0, len, 0, len, 0, 0, 0);
4657 if (ret)
4658 btrfs_abort_transaction(trans, root, ret);
4659 else
4660 btrfs_update_inode(trans, root, inode);
4661 btrfs_end_transaction(trans, root);
4662 return ret;
4663}
4664
Josef Bacik695a0d02011-03-04 15:46:53 -05004665/*
4666 * This function puts in dummy file extents for the area we're creating a hole
4667 * for. So if we are truncating this file to a larger size we need to insert
4668 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4669 * the range between oldsize and size
4670 */
Josef Bacika41ad392011-01-31 15:30:16 -05004671int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004672{
Yan Zheng9036c102008-10-30 14:19:41 -04004673 struct btrfs_root *root = BTRFS_I(inode)->root;
4674 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004675 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004676 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004677 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004678 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4679 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004680 u64 last_byte;
4681 u64 cur_offset;
4682 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004683 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004684
Josef Bacika71754f2013-06-17 17:14:39 -04004685 /*
4686 * If our size started in the middle of a page we need to zero out the
4687 * rest of the page before we expand the i_size, otherwise we could
4688 * expose stale data.
4689 */
4690 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4691 if (err)
4692 return err;
4693
Yan Zheng9036c102008-10-30 14:19:41 -04004694 if (size <= hole_start)
4695 return 0;
4696
Yan Zheng9036c102008-10-30 14:19:41 -04004697 while (1) {
4698 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004699
Josef Bacik2ac55d42010-02-03 19:33:23 +00004700 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004701 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004702 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4703 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004704 if (!ordered)
4705 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004706 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4707 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004708 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004709 btrfs_put_ordered_extent(ordered);
4710 }
4711
Yan Zheng9036c102008-10-30 14:19:41 -04004712 cur_offset = hole_start;
4713 while (1) {
4714 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4715 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004716 if (IS_ERR(em)) {
4717 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004718 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004719 break;
4720 }
Yan Zheng9036c102008-10-30 14:19:41 -04004721 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004722 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004723 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004724 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004725 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004726
Josef Bacik16e75492013-10-22 12:18:51 -04004727 err = maybe_insert_hole(root, inode, cur_offset,
4728 hole_size);
4729 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004730 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004731 btrfs_drop_extent_cache(inode, cur_offset,
4732 cur_offset + hole_size - 1, 0);
4733 hole_em = alloc_extent_map();
4734 if (!hole_em) {
4735 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4736 &BTRFS_I(inode)->runtime_flags);
4737 goto next;
4738 }
4739 hole_em->start = cur_offset;
4740 hole_em->len = hole_size;
4741 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004742
Josef Bacik5dc562c2012-08-17 13:14:17 -04004743 hole_em->block_start = EXTENT_MAP_HOLE;
4744 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004745 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004746 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004747 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4748 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004749 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004750
4751 while (1) {
4752 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004753 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004754 write_unlock(&em_tree->lock);
4755 if (err != -EEXIST)
4756 break;
4757 btrfs_drop_extent_cache(inode, cur_offset,
4758 cur_offset +
4759 hole_size - 1, 0);
4760 }
4761 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004762 }
Josef Bacik16e75492013-10-22 12:18:51 -04004763next:
Yan Zheng9036c102008-10-30 14:19:41 -04004764 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004765 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004766 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004767 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004768 break;
4769 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004770 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004771 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4772 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004773 return err;
4774}
4775
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004776static int wait_snapshoting_atomic_t(atomic_t *a)
4777{
4778 schedule();
4779 return 0;
4780}
4781
4782static void wait_for_snapshot_creation(struct btrfs_root *root)
4783{
4784 while (true) {
4785 int ret;
4786
4787 ret = btrfs_start_write_no_snapshoting(root);
4788 if (ret)
4789 break;
4790 wait_on_atomic_t(&root->will_be_snapshoted,
4791 wait_snapshoting_atomic_t,
4792 TASK_UNINTERRUPTIBLE);
4793 }
4794}
4795
Eric Sandeen3972f262013-01-12 02:57:22 +00004796static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004797{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004798 struct btrfs_root *root = BTRFS_I(inode)->root;
4799 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004800 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004801 loff_t newsize = attr->ia_size;
4802 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004803 int ret;
4804
Eric Sandeen3972f262013-01-12 02:57:22 +00004805 /*
4806 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4807 * special case where we need to update the times despite not having
4808 * these flags set. For all other operations the VFS set these flags
4809 * explicitly if it wants a timestamp update.
4810 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004811 if (newsize != oldsize) {
4812 inode_inc_iversion(inode);
4813 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4814 inode->i_ctime = inode->i_mtime =
4815 current_fs_time(inode->i_sb);
4816 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004817
Josef Bacika41ad392011-01-31 15:30:16 -05004818 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004819 truncate_pagecache(inode, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004820 /*
4821 * Don't do an expanding truncate while snapshoting is ongoing.
4822 * This is to ensure the snapshot captures a fully consistent
4823 * state of this file - if the snapshot captures this expanding
4824 * truncation, it must capture all writes that happened before
4825 * this truncation.
4826 */
4827 wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004828 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004829 if (ret) {
4830 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004831 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004832 }
Yan, Zheng80825102009-11-12 09:35:36 +00004833
Miao Xief4a2f4c2011-12-14 20:12:01 -05004834 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004835 if (IS_ERR(trans)) {
4836 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004837 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004838 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004839
4840 i_size_write(inode, newsize);
4841 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4842 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004843 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004844 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004845 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004846
Josef Bacika41ad392011-01-31 15:30:16 -05004847 /*
4848 * We're truncating a file that used to have good data down to
4849 * zero. Make sure it gets into the ordered flush list so that
4850 * any new writes get down to disk quickly.
4851 */
4852 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004853 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4854 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004855
Josef Bacikf3fe8202013-01-07 17:03:21 -05004856 /*
4857 * 1 for the orphan item we're going to add
4858 * 1 for the orphan item deletion.
4859 */
4860 trans = btrfs_start_transaction(root, 2);
4861 if (IS_ERR(trans))
4862 return PTR_ERR(trans);
4863
4864 /*
4865 * We need to do this in case we fail at _any_ point during the
4866 * actual truncate. Once we do the truncate_setsize we could
4867 * invalidate pages which forces any outstanding ordered io to
4868 * be instantly completed which will give us extents that need
4869 * to be truncated. If we fail to get an orphan inode down we
4870 * could have left over extents that were never meant to live,
4871 * so we need to garuntee from this point on that everything
4872 * will be consistent.
4873 */
4874 ret = btrfs_orphan_add(trans, inode);
4875 btrfs_end_transaction(trans, root);
4876 if (ret)
4877 return ret;
4878
Josef Bacika41ad392011-01-31 15:30:16 -05004879 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4880 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004881
4882 /* Disable nonlocked read DIO to avoid the end less truncate */
4883 btrfs_inode_block_unlocked_dio(inode);
4884 inode_dio_wait(inode);
4885 btrfs_inode_resume_unlocked_dio(inode);
4886
Josef Bacika41ad392011-01-31 15:30:16 -05004887 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004888 if (ret && inode->i_nlink) {
4889 int err;
4890
4891 /*
4892 * failed to truncate, disk_i_size is only adjusted down
4893 * as we remove extents, so it should represent the true
4894 * size of the inode, so reset the in memory size and
4895 * delete our orphan entry.
4896 */
4897 trans = btrfs_join_transaction(root);
4898 if (IS_ERR(trans)) {
4899 btrfs_orphan_del(NULL, inode);
4900 return ret;
4901 }
4902 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4903 err = btrfs_orphan_del(trans, inode);
4904 if (err)
4905 btrfs_abort_transaction(trans, root, err);
4906 btrfs_end_transaction(trans, root);
4907 }
Yan, Zheng80825102009-11-12 09:35:36 +00004908 }
4909
Josef Bacika41ad392011-01-31 15:30:16 -05004910 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004911}
4912
Chris Mason39279cc2007-06-12 06:35:45 -04004913static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4914{
4915 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004916 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004917 int err;
4918
Li Zefanb83cc962010-12-20 16:04:08 +08004919 if (btrfs_root_readonly(root))
4920 return -EROFS;
4921
Chris Mason39279cc2007-06-12 06:35:45 -04004922 err = inode_change_ok(inode, attr);
4923 if (err)
4924 return err;
4925
Chris Mason5a3f23d2009-03-31 13:27:11 -04004926 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004927 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004928 if (err)
4929 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004930 }
Yan Zheng9036c102008-10-30 14:19:41 -04004931
Christoph Hellwig10257742010-06-04 11:30:02 +02004932 if (attr->ia_valid) {
4933 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004934 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004935 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004936
Josef Bacik22c44fe2011-11-30 10:45:38 -05004937 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004938 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004939 }
4940
Chris Mason39279cc2007-06-12 06:35:45 -04004941 return err;
4942}
Chris Mason61295eb2008-01-14 16:24:38 -05004943
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004944/*
4945 * While truncating the inode pages during eviction, we get the VFS calling
4946 * btrfs_invalidatepage() against each page of the inode. This is slow because
4947 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4948 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4949 * extent_state structures over and over, wasting lots of time.
4950 *
4951 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4952 * those expensive operations on a per page basis and do only the ordered io
4953 * finishing, while we release here the extent_map and extent_state structures,
4954 * without the excessive merging and splitting.
4955 */
4956static void evict_inode_truncate_pages(struct inode *inode)
4957{
4958 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4959 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4960 struct rb_node *node;
4961
4962 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004963 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004964
4965 write_lock(&map_tree->lock);
4966 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4967 struct extent_map *em;
4968
4969 node = rb_first(&map_tree->map);
4970 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004971 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4972 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004973 remove_extent_mapping(map_tree, em);
4974 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004975 if (need_resched()) {
4976 write_unlock(&map_tree->lock);
4977 cond_resched();
4978 write_lock(&map_tree->lock);
4979 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004980 }
4981 write_unlock(&map_tree->lock);
4982
4983 spin_lock(&io_tree->lock);
4984 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4985 struct extent_state *state;
4986 struct extent_state *cached_state = NULL;
4987
4988 node = rb_first(&io_tree->state);
4989 state = rb_entry(node, struct extent_state, rb_node);
4990 atomic_inc(&state->refs);
4991 spin_unlock(&io_tree->lock);
4992
4993 lock_extent_bits(io_tree, state->start, state->end,
4994 0, &cached_state);
4995 clear_extent_bit(io_tree, state->start, state->end,
4996 EXTENT_LOCKED | EXTENT_DIRTY |
4997 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4998 EXTENT_DEFRAG, 1, 1,
4999 &cached_state, GFP_NOFS);
5000 free_extent_state(state);
5001
Filipe Manana7064dd52014-08-08 02:47:05 +01005002 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005003 spin_lock(&io_tree->lock);
5004 }
5005 spin_unlock(&io_tree->lock);
5006}
5007
Al Virobd555972010-06-07 11:35:40 -04005008void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005009{
5010 struct btrfs_trans_handle *trans;
5011 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005012 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005013 int steal_from_global = 0;
Josef Bacik07127182011-08-19 10:29:59 -04005014 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04005015 int ret;
5016
liubo1abe9b82011-03-24 11:18:59 +00005017 trace_btrfs_inode_evict(inode);
5018
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005019 evict_inode_truncate_pages(inode);
5020
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005021 if (inode->i_nlink &&
5022 ((btrfs_root_refs(&root->root_item) != 0 &&
5023 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5024 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005025 goto no_delete;
5026
Chris Mason39279cc2007-06-12 06:35:45 -04005027 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005028 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005029 goto no_delete;
5030 }
Al Virobd555972010-06-07 11:35:40 -04005031 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04005032 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005033
Miao Xief6124962014-09-12 18:44:04 +08005034 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5035
Yan, Zhengc71bf092009-11-12 09:34:40 +00005036 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06005037 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005038 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005039 goto no_delete;
5040 }
5041
Yan, Zheng76dda932009-09-21 16:00:26 -04005042 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005043 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5044 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005045 goto no_delete;
5046 }
5047
Miao Xie0e8c36a2012-12-19 06:59:51 +00005048 ret = btrfs_commit_inode_delayed_inode(inode);
5049 if (ret) {
5050 btrfs_orphan_del(NULL, inode);
5051 goto no_delete;
5052 }
5053
Miao Xie66d8f3d2012-09-06 04:02:28 -06005054 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005055 if (!rsv) {
5056 btrfs_orphan_del(NULL, inode);
5057 goto no_delete;
5058 }
Josef Bacik4a338542011-08-29 11:01:31 -04005059 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005060 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005061 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005062
Chris Masondbe674a2008-07-17 12:54:05 -04005063 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005064
Josef Bacik4289a662011-08-05 13:22:24 -04005065 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005066 * This is a bit simpler than btrfs_truncate since we've already
5067 * reserved our space for our orphan item in the unlink, so we just
5068 * need to reserve some slack space in case we add bytes and update
5069 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005070 */
Yan, Zheng80825102009-11-12 09:35:36 +00005071 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005072 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5073 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005074
Josef Bacik726c35f2011-09-26 15:46:06 -04005075 /*
5076 * Try and steal from the global reserve since we will
5077 * likely not use this space anyway, we want to try as
5078 * hard as possible to get this to work.
5079 */
5080 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005081 steal_from_global++;
5082 else
5083 steal_from_global = 0;
5084 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005085
Josef Bacik3bce8762015-02-24 12:35:51 -08005086 /*
5087 * steal_from_global == 0: we reserved stuff, hooray!
5088 * steal_from_global == 1: we didn't reserve stuff, boo!
5089 * steal_from_global == 2: we've committed, still not a lot of
5090 * room but maybe we'll have room in the global reserve this
5091 * time.
5092 * steal_from_global == 3: abandon all hope!
5093 */
5094 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005095 btrfs_warn(root->fs_info,
5096 "Could not get space for a delete, will truncate on mount %d",
5097 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005098 btrfs_orphan_del(NULL, inode);
5099 btrfs_free_block_rsv(root, rsv);
5100 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005101 }
5102
Miao Xie0e8c36a2012-12-19 06:59:51 +00005103 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005104 if (IS_ERR(trans)) {
5105 btrfs_orphan_del(NULL, inode);
5106 btrfs_free_block_rsv(root, rsv);
5107 goto no_delete;
5108 }
5109
Josef Bacik3bce8762015-02-24 12:35:51 -08005110 /*
5111 * We can't just steal from the global reserve, we need tomake
5112 * sure there is room to do it, if not we need to commit and try
5113 * again.
5114 */
5115 if (steal_from_global) {
5116 if (!btrfs_check_space_for_delayed_refs(trans, root))
5117 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5118 min_size);
5119 else
5120 ret = -ENOSPC;
5121 }
5122
5123 /*
5124 * Couldn't steal from the global reserve, we have too much
5125 * pending stuff built up, commit the transaction and try it
5126 * again.
5127 */
5128 if (ret) {
5129 ret = btrfs_commit_transaction(trans, root);
5130 if (ret) {
5131 btrfs_orphan_del(NULL, inode);
5132 btrfs_free_block_rsv(root, rsv);
5133 goto no_delete;
5134 }
5135 continue;
5136 } else {
5137 steal_from_global = 0;
5138 }
5139
Josef Bacik4289a662011-08-05 13:22:24 -04005140 trans->block_rsv = rsv;
5141
Yan, Zhengd68fc572010-05-16 10:49:58 -04005142 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005143 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005144 break;
5145
Miao Xie8407aa42012-09-07 01:43:32 -06005146 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005147 btrfs_end_transaction(trans, root);
5148 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005149 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005150 }
5151
Josef Bacik4289a662011-08-05 13:22:24 -04005152 btrfs_free_block_rsv(root, rsv);
5153
Josef Bacik4ef31a42013-08-13 14:10:08 -04005154 /*
5155 * Errors here aren't a big deal, it just means we leave orphan items
5156 * in the tree. They will be cleaned up on the next mount.
5157 */
Yan, Zheng80825102009-11-12 09:35:36 +00005158 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005159 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005160 btrfs_orphan_del(trans, inode);
5161 } else {
5162 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005163 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005164
Josef Bacik4289a662011-08-05 13:22:24 -04005165 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005166 if (!(root == root->fs_info->tree_root ||
5167 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005168 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005169
Chris Mason54aa1f42007-06-22 14:16:25 -04005170 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005171 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005172no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005173 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005174 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005175 return;
Chris Mason39279cc2007-06-12 06:35:45 -04005176}
5177
5178/*
5179 * this returns the key found in the dir entry in the location pointer.
5180 * If no dir entries were found, location->objectid is 0.
5181 */
5182static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5183 struct btrfs_key *location)
5184{
5185 const char *name = dentry->d_name.name;
5186 int namelen = dentry->d_name.len;
5187 struct btrfs_dir_item *di;
5188 struct btrfs_path *path;
5189 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005190 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005191
5192 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005193 if (!path)
5194 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005195
Li Zefan33345d012011-04-20 10:31:50 +08005196 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005197 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005198 if (IS_ERR(di))
5199 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005200
David Sterbac7040052011-04-19 18:00:01 +02005201 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005202 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005203
Chris Mason5f39d392007-10-15 16:14:19 -04005204 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005205out:
Chris Mason39279cc2007-06-12 06:35:45 -04005206 btrfs_free_path(path);
5207 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005208out_err:
5209 location->objectid = 0;
5210 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005211}
5212
5213/*
5214 * when we hit a tree root in a directory, the btrfs part of the inode
5215 * needs to be changed to reflect the root directory of the tree root. This
5216 * is kind of like crossing a mount point.
5217 */
5218static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005219 struct inode *dir,
5220 struct dentry *dentry,
5221 struct btrfs_key *location,
5222 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005223{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005224 struct btrfs_path *path;
5225 struct btrfs_root *new_root;
5226 struct btrfs_root_ref *ref;
5227 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005228 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005229 int ret;
5230 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005231
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005232 path = btrfs_alloc_path();
5233 if (!path) {
5234 err = -ENOMEM;
5235 goto out;
5236 }
Chris Mason39279cc2007-06-12 06:35:45 -04005237
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005238 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005239 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5240 key.type = BTRFS_ROOT_REF_KEY;
5241 key.offset = location->objectid;
5242
5243 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5244 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005245 if (ret) {
5246 if (ret < 0)
5247 err = ret;
5248 goto out;
5249 }
Chris Mason39279cc2007-06-12 06:35:45 -04005250
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005251 leaf = path->nodes[0];
5252 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005253 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005254 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5255 goto out;
5256
5257 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5258 (unsigned long)(ref + 1),
5259 dentry->d_name.len);
5260 if (ret)
5261 goto out;
5262
David Sterbab3b4aa72011-04-21 01:20:15 +02005263 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005264
5265 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5266 if (IS_ERR(new_root)) {
5267 err = PTR_ERR(new_root);
5268 goto out;
5269 }
5270
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005271 *sub_root = new_root;
5272 location->objectid = btrfs_root_dirid(&new_root->root_item);
5273 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005274 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005275 err = 0;
5276out:
5277 btrfs_free_path(path);
5278 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005279}
5280
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005281static void inode_tree_add(struct inode *inode)
5282{
5283 struct btrfs_root *root = BTRFS_I(inode)->root;
5284 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005285 struct rb_node **p;
5286 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005287 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005288 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005289
Al Viro1d3382cb2010-10-23 15:19:20 -04005290 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005291 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005292 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005293 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005294 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005295 while (*p) {
5296 parent = *p;
5297 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5298
Li Zefan33345d012011-04-20 10:31:50 +08005299 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005300 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005301 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005302 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005303 else {
5304 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005305 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005306 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005307 RB_CLEAR_NODE(parent);
5308 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005309 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005310 }
5311 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005312 rb_link_node(new, parent, p);
5313 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005314 spin_unlock(&root->inode_lock);
5315}
5316
5317static void inode_tree_del(struct inode *inode)
5318{
5319 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005320 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005321
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005322 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005323 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005324 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005325 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005326 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005327 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005328 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005329
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005330 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005331 synchronize_srcu(&root->fs_info->subvol_srcu);
5332 spin_lock(&root->inode_lock);
5333 empty = RB_EMPTY_ROOT(&root->inode_tree);
5334 spin_unlock(&root->inode_lock);
5335 if (empty)
5336 btrfs_add_dead_root(root);
5337 }
5338}
5339
Jeff Mahoney143bede2012-03-01 14:56:26 +01005340void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005341{
5342 struct rb_node *node;
5343 struct rb_node *prev;
5344 struct btrfs_inode *entry;
5345 struct inode *inode;
5346 u64 objectid = 0;
5347
Liu Bo7813b3d2014-02-10 17:37:25 +08005348 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5349 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005350
5351 spin_lock(&root->inode_lock);
5352again:
5353 node = root->inode_tree.rb_node;
5354 prev = NULL;
5355 while (node) {
5356 prev = node;
5357 entry = rb_entry(node, struct btrfs_inode, rb_node);
5358
Li Zefan33345d012011-04-20 10:31:50 +08005359 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005360 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005361 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005362 node = node->rb_right;
5363 else
5364 break;
5365 }
5366 if (!node) {
5367 while (prev) {
5368 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005369 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005370 node = prev;
5371 break;
5372 }
5373 prev = rb_next(prev);
5374 }
5375 }
5376 while (node) {
5377 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005378 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005379 inode = igrab(&entry->vfs_inode);
5380 if (inode) {
5381 spin_unlock(&root->inode_lock);
5382 if (atomic_read(&inode->i_count) > 1)
5383 d_prune_aliases(inode);
5384 /*
Al Viro45321ac2010-06-07 13:43:19 -04005385 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005386 * the inode cache when its usage count
5387 * hits zero.
5388 */
5389 iput(inode);
5390 cond_resched();
5391 spin_lock(&root->inode_lock);
5392 goto again;
5393 }
5394
5395 if (cond_resched_lock(&root->inode_lock))
5396 goto again;
5397
5398 node = rb_next(node);
5399 }
5400 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005401}
5402
Chris Masone02119d2008-09-05 16:13:11 -04005403static int btrfs_init_locked_inode(struct inode *inode, void *p)
5404{
5405 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005406 inode->i_ino = args->location->objectid;
5407 memcpy(&BTRFS_I(inode)->location, args->location,
5408 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005409 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005410 return 0;
5411}
5412
5413static int btrfs_find_actor(struct inode *inode, void *opaque)
5414{
5415 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005416 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005417 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005418}
5419
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005420static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005421 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005422 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005423{
5424 struct inode *inode;
5425 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005426 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005427
Chris Mason90d3e592014-01-09 17:28:00 -08005428 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005429 args.root = root;
5430
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005431 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005432 btrfs_init_locked_inode,
5433 (void *)&args);
5434 return inode;
5435}
5436
Balaji Rao1a54ef82008-07-21 02:01:04 +05305437/* Get an inode object given its location and corresponding root.
5438 * Returns in *is_new if the inode was read from disk
5439 */
5440struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005441 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305442{
5443 struct inode *inode;
5444
Chris Mason90d3e592014-01-09 17:28:00 -08005445 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305446 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005447 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305448
5449 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305450 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005451 if (!is_bad_inode(inode)) {
5452 inode_tree_add(inode);
5453 unlock_new_inode(inode);
5454 if (new)
5455 *new = 1;
5456 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005457 unlock_new_inode(inode);
5458 iput(inode);
5459 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005460 }
5461 }
5462
Balaji Rao1a54ef82008-07-21 02:01:04 +05305463 return inode;
5464}
5465
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005466static struct inode *new_simple_dir(struct super_block *s,
5467 struct btrfs_key *key,
5468 struct btrfs_root *root)
5469{
5470 struct inode *inode = new_inode(s);
5471
5472 if (!inode)
5473 return ERR_PTR(-ENOMEM);
5474
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005475 BTRFS_I(inode)->root = root;
5476 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005477 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005478
5479 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005480 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005481 inode->i_fop = &simple_dir_operations;
5482 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
chandan r9cc97d62012-07-04 12:48:07 +05305483 inode->i_mtime = CURRENT_TIME;
5484 inode->i_atime = inode->i_mtime;
5485 inode->i_ctime = inode->i_mtime;
5486 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005487
5488 return inode;
5489}
5490
Chris Mason3de45862008-11-17 21:02:50 -05005491struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005492{
Chris Masond3977122009-01-05 21:25:51 -05005493 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005494 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005495 struct btrfs_root *sub_root = root;
5496 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005497 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005498 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005499
5500 if (dentry->d_name.len > BTRFS_NAME_LEN)
5501 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005502
Jeff Layton39e3c952012-11-28 11:30:53 -05005503 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005504 if (ret < 0)
5505 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005506
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005507 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005508 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005509
5510 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005511 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005512 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005513 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005514
5515 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5516
Yan, Zheng76dda932009-09-21 16:00:26 -04005517 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005518 ret = fixup_tree_root_location(root, dir, dentry,
5519 &location, &sub_root);
5520 if (ret < 0) {
5521 if (ret != -ENOENT)
5522 inode = ERR_PTR(ret);
5523 else
5524 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5525 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005526 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005527 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005528 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5529
Julia Lawall34d19ba2011-01-24 19:55:19 +00005530 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005531 down_read(&root->fs_info->cleanup_work_sem);
5532 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005533 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005534 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005535 if (ret) {
5536 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005537 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005538 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005539 }
5540
Chris Mason3de45862008-11-17 21:02:50 -05005541 return inode;
5542}
5543
Nick Pigginfe15ce42011-01-07 17:49:23 +11005544static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005545{
5546 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005547 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005548
Li Zefan848cce02012-02-21 17:04:28 +08005549 if (!inode && !IS_ROOT(dentry))
5550 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005551
Li Zefan848cce02012-02-21 17:04:28 +08005552 if (inode) {
5553 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005554 if (btrfs_root_refs(&root->root_item) == 0)
5555 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005556
5557 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5558 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005559 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005560 return 0;
5561}
5562
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005563static void btrfs_dentry_release(struct dentry *dentry)
5564{
Daeseok Youn944a4512014-04-14 15:37:02 +09005565 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005566}
5567
Chris Mason3de45862008-11-17 21:02:50 -05005568static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005569 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005570{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005571 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005572
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005573 inode = btrfs_lookup_dentry(dir, dentry);
5574 if (IS_ERR(inode)) {
5575 if (PTR_ERR(inode) == -ENOENT)
5576 inode = NULL;
5577 else
5578 return ERR_CAST(inode);
5579 }
5580
Al Viro41d28bc2014-10-12 22:24:21 -04005581 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005582}
5583
Miao Xie16cdcec2011-04-22 18:12:22 +08005584unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005585 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5586};
5587
Al Viro9cdda8d2013-05-22 16:48:09 -04005588static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005589{
Al Viro9cdda8d2013-05-22 16:48:09 -04005590 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005591 struct btrfs_root *root = BTRFS_I(inode)->root;
5592 struct btrfs_item *item;
5593 struct btrfs_dir_item *di;
5594 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005595 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005596 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005597 struct list_head ins_list;
5598 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005599 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005600 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005601 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005602 unsigned char d_type;
5603 int over = 0;
5604 u32 di_cur;
5605 u32 di_total;
5606 u32 di_len;
5607 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005608 char tmp_name[32];
5609 char *name_ptr;
5610 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005611 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005612
5613 /* FIXME, use a real flag for deciding about the key type */
5614 if (root->fs_info->tree_root == root)
5615 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005616
Al Viro9cdda8d2013-05-22 16:48:09 -04005617 if (!dir_emit_dots(file, ctx))
5618 return 0;
5619
David Woodhouse49593bf2008-08-17 17:08:36 +01005620 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005621 if (!path)
5622 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005623
Josef Bacik026fd312011-05-13 10:32:11 -04005624 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005625
Miao Xie16cdcec2011-04-22 18:12:22 +08005626 if (key_type == BTRFS_DIR_INDEX_KEY) {
5627 INIT_LIST_HEAD(&ins_list);
5628 INIT_LIST_HEAD(&del_list);
5629 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5630 }
5631
David Sterba962a2982014-06-04 18:41:45 +02005632 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005633 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005634 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005635
Chris Mason39279cc2007-06-12 06:35:45 -04005636 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5637 if (ret < 0)
5638 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005639
5640 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005641 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005642 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005643 if (slot >= btrfs_header_nritems(leaf)) {
5644 ret = btrfs_next_leaf(root, path);
5645 if (ret < 0)
5646 goto err;
5647 else if (ret > 0)
5648 break;
5649 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005650 }
Chris Mason3de45862008-11-17 21:02:50 -05005651
Ross Kirkdd3cc162013-09-16 15:58:09 +01005652 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005653 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5654
5655 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005656 break;
David Sterba962a2982014-06-04 18:41:45 +02005657 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005658 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005659 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005660 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005661 if (key_type == BTRFS_DIR_INDEX_KEY &&
5662 btrfs_should_delete_dir_index(&del_list,
5663 found_key.offset))
5664 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005665
Al Viro9cdda8d2013-05-22 16:48:09 -04005666 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005667 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005668
Chris Mason39279cc2007-06-12 06:35:45 -04005669 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5670 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005671 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005672
5673 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005674 struct btrfs_key location;
5675
Josef Bacik22a94d42011-03-16 16:47:17 -04005676 if (verify_dir_item(root, leaf, di))
5677 break;
5678
Chris Mason5f39d392007-10-15 16:14:19 -04005679 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005680 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005681 name_ptr = tmp_name;
5682 } else {
5683 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005684 if (!name_ptr) {
5685 ret = -ENOMEM;
5686 goto err;
5687 }
Chris Mason5f39d392007-10-15 16:14:19 -04005688 }
5689 read_extent_buffer(leaf, name_ptr,
5690 (unsigned long)(di + 1), name_len);
5691
5692 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5693 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005694
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005695
Chris Mason3de45862008-11-17 21:02:50 -05005696 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005697 * skip it.
5698 *
5699 * In contrast to old kernels, we insert the snapshot's
5700 * dir item and dir index after it has been created, so
5701 * we won't find a reference to our own snapshot. We
5702 * still keep the following code for backward
5703 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005704 */
5705 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5706 location.objectid == root->root_key.objectid) {
5707 over = 0;
5708 goto skip;
5709 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005710 over = !dir_emit(ctx, name_ptr, name_len,
5711 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005712
Chris Mason3de45862008-11-17 21:02:50 -05005713skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005714 if (name_ptr != tmp_name)
5715 kfree(name_ptr);
5716
Chris Mason39279cc2007-06-12 06:35:45 -04005717 if (over)
5718 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005719 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005720 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005721 di_cur += di_len;
5722 di = (struct btrfs_dir_item *)((char *)di + di_len);
5723 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005724next:
5725 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005726 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005727
Miao Xie16cdcec2011-04-22 18:12:22 +08005728 if (key_type == BTRFS_DIR_INDEX_KEY) {
5729 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005730 ctx->pos++;
5731 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005732 if (ret)
5733 goto nopos;
5734 }
5735
David Woodhouse49593bf2008-08-17 17:08:36 +01005736 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005737 ctx->pos++;
5738
5739 /*
5740 * Stop new entries from being returned after we return the last
5741 * entry.
5742 *
5743 * New directory entries are assigned a strictly increasing
5744 * offset. This means that new entries created during readdir
5745 * are *guaranteed* to be seen in the future by that readdir.
5746 * This has broken buggy programs which operate on names as
5747 * they're returned by readdir. Until we re-use freed offsets
5748 * we have this hack to stop new entries from being returned
5749 * under the assumption that they'll never reach this huge
5750 * offset.
5751 *
5752 * This is being careful not to overflow 32bit loff_t unless the
5753 * last entry requires it because doing so has broken 32bit apps
5754 * in the past.
5755 */
5756 if (key_type == BTRFS_DIR_INDEX_KEY) {
5757 if (ctx->pos >= INT_MAX)
5758 ctx->pos = LLONG_MAX;
5759 else
5760 ctx->pos = INT_MAX;
5761 }
Chris Mason39279cc2007-06-12 06:35:45 -04005762nopos:
5763 ret = 0;
5764err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005765 if (key_type == BTRFS_DIR_INDEX_KEY)
5766 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005767 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005768 return ret;
5769}
5770
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005771int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005772{
5773 struct btrfs_root *root = BTRFS_I(inode)->root;
5774 struct btrfs_trans_handle *trans;
5775 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005776 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005777
Josef Bacik72ac3c02012-05-23 14:13:11 -04005778 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005779 return 0;
5780
Liu Bo83eea1f2012-07-10 05:28:39 -06005781 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005782 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005783
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005784 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005785 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005786 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005787 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005788 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005789 if (IS_ERR(trans))
5790 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005791 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005792 }
5793 return ret;
5794}
5795
5796/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005797 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005798 * inode changes. But, it is most likely to find the inode in cache.
5799 * FIXME, needs more benchmarking...there are no reasons other than performance
5800 * to keep or drop this code.
5801 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005802static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005803{
5804 struct btrfs_root *root = BTRFS_I(inode)->root;
5805 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005806 int ret;
5807
Josef Bacik72ac3c02012-05-23 14:13:11 -04005808 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005809 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005810
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005811 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005812 if (IS_ERR(trans))
5813 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005814
5815 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005816 if (ret && ret == -ENOSPC) {
5817 /* whoops, lets try again with the full transaction */
5818 btrfs_end_transaction(trans, root);
5819 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005820 if (IS_ERR(trans))
5821 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005822
Chris Mason94b60442010-05-26 11:02:00 -04005823 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005824 }
Chris Mason39279cc2007-06-12 06:35:45 -04005825 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005826 if (BTRFS_I(inode)->delayed_node)
5827 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005828
5829 return ret;
5830}
5831
5832/*
5833 * This is a copy of file_update_time. We need this so we can return error on
5834 * ENOSPC for updating the inode in the case of file write and mmap writes.
5835 */
Josef Bacike41f9412012-03-26 09:46:47 -04005836static int btrfs_update_time(struct inode *inode, struct timespec *now,
5837 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005838{
Alexander Block2bc5565282012-06-15 09:49:33 +02005839 struct btrfs_root *root = BTRFS_I(inode)->root;
5840
5841 if (btrfs_root_readonly(root))
5842 return -EROFS;
5843
Josef Bacike41f9412012-03-26 09:46:47 -04005844 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005845 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005846 if (flags & S_CTIME)
5847 inode->i_ctime = *now;
5848 if (flags & S_MTIME)
5849 inode->i_mtime = *now;
5850 if (flags & S_ATIME)
5851 inode->i_atime = *now;
5852 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005853}
5854
Chris Masond352ac62008-09-29 15:18:18 -04005855/*
5856 * find the highest existing sequence number in a directory
5857 * and then set the in-memory index_cnt variable to reflect
5858 * free sequence numbers
5859 */
Josef Bacikaec74772008-07-24 12:12:38 -04005860static int btrfs_set_inode_index_count(struct inode *inode)
5861{
5862 struct btrfs_root *root = BTRFS_I(inode)->root;
5863 struct btrfs_key key, found_key;
5864 struct btrfs_path *path;
5865 struct extent_buffer *leaf;
5866 int ret;
5867
Li Zefan33345d012011-04-20 10:31:50 +08005868 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005869 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005870 key.offset = (u64)-1;
5871
5872 path = btrfs_alloc_path();
5873 if (!path)
5874 return -ENOMEM;
5875
5876 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5877 if (ret < 0)
5878 goto out;
5879 /* FIXME: we should be able to handle this */
5880 if (ret == 0)
5881 goto out;
5882 ret = 0;
5883
5884 /*
5885 * MAGIC NUMBER EXPLANATION:
5886 * since we search a directory based on f_pos we have to start at 2
5887 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5888 * else has to start at 2
5889 */
5890 if (path->slots[0] == 0) {
5891 BTRFS_I(inode)->index_cnt = 2;
5892 goto out;
5893 }
5894
5895 path->slots[0]--;
5896
5897 leaf = path->nodes[0];
5898 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5899
Li Zefan33345d012011-04-20 10:31:50 +08005900 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005901 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005902 BTRFS_I(inode)->index_cnt = 2;
5903 goto out;
5904 }
5905
5906 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5907out:
5908 btrfs_free_path(path);
5909 return ret;
5910}
5911
Chris Masond352ac62008-09-29 15:18:18 -04005912/*
5913 * helper to find a free sequence number in a given directory. This current
5914 * code is very simple, later versions will do smarter things in the btree
5915 */
Chris Mason3de45862008-11-17 21:02:50 -05005916int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005917{
5918 int ret = 0;
5919
5920 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005921 ret = btrfs_inode_delayed_dir_index_count(dir);
5922 if (ret) {
5923 ret = btrfs_set_inode_index_count(dir);
5924 if (ret)
5925 return ret;
5926 }
Josef Bacikaec74772008-07-24 12:12:38 -04005927 }
5928
Chris Mason00e4e6b2008-08-05 11:18:09 -04005929 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005930 BTRFS_I(dir)->index_cnt++;
5931
5932 return ret;
5933}
5934
Chris Masonb0d5d102014-09-08 13:08:51 -07005935static int btrfs_insert_inode_locked(struct inode *inode)
5936{
5937 struct btrfs_iget_args args;
5938 args.location = &BTRFS_I(inode)->location;
5939 args.root = BTRFS_I(inode)->root;
5940
5941 return insert_inode_locked4(inode,
5942 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5943 btrfs_find_actor, &args);
5944}
5945
Chris Mason39279cc2007-06-12 06:35:45 -04005946static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5947 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005948 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005949 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005950 u64 ref_objectid, u64 objectid,
5951 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005952{
5953 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005954 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005955 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005956 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005957 struct btrfs_inode_ref *ref;
5958 struct btrfs_key key[2];
5959 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005960 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005961 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005962 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005963
Chris Mason5f39d392007-10-15 16:14:19 -04005964 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005965 if (!path)
5966 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005967
Chris Mason39279cc2007-06-12 06:35:45 -04005968 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005969 if (!inode) {
5970 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005971 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005972 }
Chris Mason39279cc2007-06-12 06:35:45 -04005973
Li Zefan581bb052011-04-20 10:06:11 +08005974 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005975 * O_TMPFILE, set link count to 0, so that after this point,
5976 * we fill in an inode item with the correct link count.
5977 */
5978 if (!name)
5979 set_nlink(inode, 0);
5980
5981 /*
Li Zefan581bb052011-04-20 10:06:11 +08005982 * we have to initialize this early, so we can reclaim the inode
5983 * number if we fail afterwards in this function.
5984 */
5985 inode->i_ino = objectid;
5986
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005987 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005988 trace_btrfs_inode_request(dir);
5989
Chris Mason3de45862008-11-17 21:02:50 -05005990 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005991 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005992 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005993 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005994 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005995 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005996 } else if (dir) {
5997 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005998 }
5999 /*
6000 * index_cnt is ignored for everything but a dir,
6001 * btrfs_get_inode_index_count has an explanation for the magic
6002 * number
6003 */
6004 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006005 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006006 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006007 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006008 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006009
Josef Bacik5dc562c2012-08-17 13:14:17 -04006010 /*
6011 * We could have gotten an inode number from somebody who was fsynced
6012 * and then removed in this same transaction, so let's just set full
6013 * sync since it will be a full sync anyway and this will blow away the
6014 * old info in the log.
6015 */
6016 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6017
Chris Mason9c583092008-01-29 15:15:18 -05006018 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006019 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006020 key[0].offset = 0;
6021
Chris Mason9c583092008-01-29 15:15:18 -05006022 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006023
6024 if (name) {
6025 /*
6026 * Start new inodes with an inode_ref. This is slightly more
6027 * efficient for small numbers of hard links since they will
6028 * be packed into one item. Extended refs will kick in if we
6029 * add more hard links than can fit in the ref item.
6030 */
6031 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006032 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006033 key[1].offset = ref_objectid;
6034
6035 sizes[1] = name_len + sizeof(*ref);
6036 }
Chris Mason9c583092008-01-29 15:15:18 -05006037
Chris Masonb0d5d102014-09-08 13:08:51 -07006038 location = &BTRFS_I(inode)->location;
6039 location->objectid = objectid;
6040 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006041 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006042
6043 ret = btrfs_insert_inode_locked(inode);
6044 if (ret < 0)
6045 goto fail;
6046
Chris Masonb9473432009-03-13 11:00:37 -04006047 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006048 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006049 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006050 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006051
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006052 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006053 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306054
6055 inode->i_mtime = CURRENT_TIME;
6056 inode->i_atime = inode->i_mtime;
6057 inode->i_ctime = inode->i_mtime;
6058 BTRFS_I(inode)->i_otime = inode->i_mtime;
6059
Chris Mason5f39d392007-10-15 16:14:19 -04006060 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6061 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006062 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6063 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006064 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006065
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006066 if (name) {
6067 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6068 struct btrfs_inode_ref);
6069 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6070 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6071 ptr = (unsigned long)(ref + 1);
6072 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6073 }
Chris Mason9c583092008-01-29 15:15:18 -05006074
Chris Mason5f39d392007-10-15 16:14:19 -04006075 btrfs_mark_buffer_dirty(path->nodes[0]);
6076 btrfs_free_path(path);
6077
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006078 btrfs_inherit_iflags(inode, dir);
6079
Al Viro569254b2011-07-24 17:08:40 -04006080 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04006081 if (btrfs_test_opt(root, NODATASUM))
6082 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06006083 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006084 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6085 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006086 }
6087
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006088 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006089
6090 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006091 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006092
Alexander Block8ea05e32012-07-25 17:35:53 +02006093 btrfs_update_root_times(trans, root);
6094
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006095 ret = btrfs_inode_inherit_props(trans, inode, dir);
6096 if (ret)
6097 btrfs_err(root->fs_info,
6098 "error inheriting props for ino %llu (root %llu): %d",
6099 btrfs_ino(inode), root->root_key.objectid, ret);
6100
Chris Mason39279cc2007-06-12 06:35:45 -04006101 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006102
6103fail_unlock:
6104 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006105fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006106 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006107 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006108 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006109 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006110 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006111}
6112
6113static inline u8 btrfs_inode_type(struct inode *inode)
6114{
6115 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6116}
6117
Chris Masond352ac62008-09-29 15:18:18 -04006118/*
6119 * utility function to add 'inode' into 'parent_inode' with
6120 * a give name and a given sequence number.
6121 * if 'add_backref' is true, also insert a backref from the
6122 * inode to the parent directory.
6123 */
Chris Masone02119d2008-09-05 16:13:11 -04006124int btrfs_add_link(struct btrfs_trans_handle *trans,
6125 struct inode *parent_inode, struct inode *inode,
6126 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006127{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006128 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006129 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006130 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006131 u64 ino = btrfs_ino(inode);
6132 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006133
Li Zefan33345d012011-04-20 10:31:50 +08006134 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006135 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6136 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006137 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006138 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006139 key.offset = 0;
6140 }
Chris Mason39279cc2007-06-12 06:35:45 -04006141
Li Zefan33345d012011-04-20 10:31:50 +08006142 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006143 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6144 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006145 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006146 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006147 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6148 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006149 }
6150
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006151 /* Nothing to clean up yet */
6152 if (ret)
6153 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006154
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006155 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6156 parent_inode, &key,
6157 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006158 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006159 goto fail_dir_item;
6160 else if (ret) {
6161 btrfs_abort_transaction(trans, root, ret);
6162 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006163 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006164
6165 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6166 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006167 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006168 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
6169 ret = btrfs_update_inode(trans, root, parent_inode);
6170 if (ret)
6171 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006172 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006173
6174fail_dir_item:
6175 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6176 u64 local_index;
6177 int err;
6178 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6179 key.objectid, root->root_key.objectid,
6180 parent_ino, &local_index, name, name_len);
6181
6182 } else if (add_backref) {
6183 u64 local_index;
6184 int err;
6185
6186 err = btrfs_del_inode_ref(trans, root, name, name_len,
6187 ino, parent_ino, &local_index);
6188 }
6189 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006190}
6191
6192static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006193 struct inode *dir, struct dentry *dentry,
6194 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006195{
Josef Bacika1b075d2010-11-19 20:36:11 +00006196 int err = btrfs_add_link(trans, dir, inode,
6197 dentry->d_name.name, dentry->d_name.len,
6198 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006199 if (err > 0)
6200 err = -EEXIST;
6201 return err;
6202}
6203
Josef Bacik618e21d2007-07-11 10:18:17 -04006204static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006205 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006206{
6207 struct btrfs_trans_handle *trans;
6208 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006209 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006210 int err;
6211 int drop_inode = 0;
6212 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006213 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006214
6215 if (!new_valid_dev(rdev))
6216 return -EINVAL;
6217
Josef Bacik9ed74f22009-09-11 16:12:44 -04006218 /*
6219 * 2 for inode item and ref
6220 * 2 for dir items
6221 * 1 for xattr if selinux is on
6222 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006223 trans = btrfs_start_transaction(root, 5);
6224 if (IS_ERR(trans))
6225 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006226
Li Zefan581bb052011-04-20 10:06:11 +08006227 err = btrfs_find_free_ino(root, &objectid);
6228 if (err)
6229 goto out_unlock;
6230
Josef Bacikaec74772008-07-24 12:12:38 -04006231 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006232 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006233 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006234 if (IS_ERR(inode)) {
6235 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006236 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006237 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006238
Casey Schauflerad19db72011-12-15 10:09:07 -05006239 /*
6240 * If the active LSM wants to access the inode during
6241 * d_instantiate it needs these. Smack checks to see
6242 * if the filesystem supports xattrs by looking at the
6243 * ops vector.
6244 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006245 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006246 init_special_inode(inode, inode->i_mode, rdev);
6247
6248 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006249 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006250 goto out_unlock_inode;
6251
6252 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6253 if (err) {
6254 goto out_unlock_inode;
6255 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006256 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006257 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006258 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006259 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006260
Josef Bacik618e21d2007-07-11 10:18:17 -04006261out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006262 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006263 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006264 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006265 if (drop_inode) {
6266 inode_dec_link_count(inode);
6267 iput(inode);
6268 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006269 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006270
6271out_unlock_inode:
6272 drop_inode = 1;
6273 unlock_new_inode(inode);
6274 goto out_unlock;
6275
Josef Bacik618e21d2007-07-11 10:18:17 -04006276}
6277
Chris Mason39279cc2007-06-12 06:35:45 -04006278static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006279 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006280{
6281 struct btrfs_trans_handle *trans;
6282 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006283 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006284 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006285 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006286 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006287 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006288
Josef Bacik9ed74f22009-09-11 16:12:44 -04006289 /*
6290 * 2 for inode item and ref
6291 * 2 for dir items
6292 * 1 for xattr if selinux is on
6293 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006294 trans = btrfs_start_transaction(root, 5);
6295 if (IS_ERR(trans))
6296 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006297
Li Zefan581bb052011-04-20 10:06:11 +08006298 err = btrfs_find_free_ino(root, &objectid);
6299 if (err)
6300 goto out_unlock;
6301
Josef Bacikaec74772008-07-24 12:12:38 -04006302 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006303 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006304 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006305 if (IS_ERR(inode)) {
6306 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006307 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006308 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006309 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006310 /*
6311 * If the active LSM wants to access the inode during
6312 * d_instantiate it needs these. Smack checks to see
6313 * if the filesystem supports xattrs by looking at the
6314 * ops vector.
6315 */
6316 inode->i_fop = &btrfs_file_operations;
6317 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006318 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006319
6320 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6321 if (err)
6322 goto out_unlock_inode;
6323
6324 err = btrfs_update_inode(trans, root, inode);
6325 if (err)
6326 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006327
Josef Bacika1b075d2010-11-19 20:36:11 +00006328 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006329 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006330 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006331
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006332 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006333 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006334 d_instantiate(dentry, inode);
6335
Chris Mason39279cc2007-06-12 06:35:45 -04006336out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006337 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006338 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006339 inode_dec_link_count(inode);
6340 iput(inode);
6341 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006342 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006343 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006344 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006345
6346out_unlock_inode:
6347 unlock_new_inode(inode);
6348 goto out_unlock;
6349
Chris Mason39279cc2007-06-12 06:35:45 -04006350}
6351
6352static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6353 struct dentry *dentry)
6354{
6355 struct btrfs_trans_handle *trans;
6356 struct btrfs_root *root = BTRFS_I(dir)->root;
6357 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006358 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006359 int err;
6360 int drop_inode = 0;
6361
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006362 /* do not allow sys_link's with other subvols of the same device */
6363 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006364 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006365
Mark Fashehf1863732012-08-08 11:32:27 -07006366 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006367 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006368
Chris Mason3de45862008-11-17 21:02:50 -05006369 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006370 if (err)
6371 goto fail;
6372
Yan, Zhenga22285a2010-05-16 10:48:46 -04006373 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006374 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006375 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006376 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006377 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006378 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006379 if (IS_ERR(trans)) {
6380 err = PTR_ERR(trans);
6381 goto fail;
6382 }
Chris Mason5f39d392007-10-15 16:14:19 -04006383
Miao Xie67de1172013-12-26 13:07:06 +08006384 /* There are several dir indexes for this inode, clear the cache. */
6385 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006386 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006387 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006388 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006389 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006390 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006391
Josef Bacika1b075d2010-11-19 20:36:11 +00006392 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006393
Yan, Zhenga5719522009-09-24 09:17:31 -04006394 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006395 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006396 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006397 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006398 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006399 if (err)
6400 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006401 if (inode->i_nlink == 1) {
6402 /*
6403 * If new hard link count is 1, it's a file created
6404 * with open(2) O_TMPFILE flag.
6405 */
6406 err = btrfs_orphan_del(trans, inode);
6407 if (err)
6408 goto fail;
6409 }
Al Viro08c422c2011-12-23 07:58:13 -05006410 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006411 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006412 }
Chris Mason39279cc2007-06-12 06:35:45 -04006413
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006414 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006415 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006416fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006417 if (drop_inode) {
6418 inode_dec_link_count(inode);
6419 iput(inode);
6420 }
Liu Bob53d3f52012-11-14 14:34:34 +00006421 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006422 return err;
6423}
6424
Al Viro18bb1db2011-07-26 01:41:39 -04006425static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006426{
Chris Masonb9d86662008-05-02 16:13:49 -04006427 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006428 struct btrfs_trans_handle *trans;
6429 struct btrfs_root *root = BTRFS_I(dir)->root;
6430 int err = 0;
6431 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006432 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006433 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006434
Josef Bacik9ed74f22009-09-11 16:12:44 -04006435 /*
6436 * 2 items for inode and ref
6437 * 2 items for dir items
6438 * 1 for xattr if selinux is on
6439 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006440 trans = btrfs_start_transaction(root, 5);
6441 if (IS_ERR(trans))
6442 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006443
Li Zefan581bb052011-04-20 10:06:11 +08006444 err = btrfs_find_free_ino(root, &objectid);
6445 if (err)
6446 goto out_fail;
6447
Josef Bacikaec74772008-07-24 12:12:38 -04006448 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006449 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006450 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006451 if (IS_ERR(inode)) {
6452 err = PTR_ERR(inode);
6453 goto out_fail;
6454 }
Chris Mason5f39d392007-10-15 16:14:19 -04006455
Chris Mason39279cc2007-06-12 06:35:45 -04006456 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006457 /* these must be set before we unlock the inode */
6458 inode->i_op = &btrfs_dir_inode_operations;
6459 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006460
Eric Paris2a7dba32011-02-01 11:05:39 -05006461 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006462 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006463 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006464
Chris Masondbe674a2008-07-17 12:54:05 -04006465 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006466 err = btrfs_update_inode(trans, root, inode);
6467 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006468 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006469
Josef Bacika1b075d2010-11-19 20:36:11 +00006470 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6471 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006472 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006473 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006474
Chris Mason39279cc2007-06-12 06:35:45 -04006475 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006476 /*
6477 * mkdir is special. We're unlocking after we call d_instantiate
6478 * to avoid a race with nfsd calling d_instantiate.
6479 */
6480 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006481 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006482
6483out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006484 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006485 if (drop_on_err) {
6486 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006487 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006488 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006489 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006490 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006491 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006492
6493out_fail_inode:
6494 unlock_new_inode(inode);
6495 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006496}
6497
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006498/* Find next extent map of a given extent map, caller needs to ensure locks */
6499static struct extent_map *next_extent_map(struct extent_map *em)
6500{
6501 struct rb_node *next;
6502
6503 next = rb_next(&em->rb_node);
6504 if (!next)
6505 return NULL;
6506 return container_of(next, struct extent_map, rb_node);
6507}
6508
6509static struct extent_map *prev_extent_map(struct extent_map *em)
6510{
6511 struct rb_node *prev;
6512
6513 prev = rb_prev(&em->rb_node);
6514 if (!prev)
6515 return NULL;
6516 return container_of(prev, struct extent_map, rb_node);
6517}
6518
Chris Masond352ac62008-09-29 15:18:18 -04006519/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006520 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006521 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006522 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006523 */
Chris Mason3b951512008-04-17 11:29:12 -04006524static int merge_extent_mapping(struct extent_map_tree *em_tree,
6525 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006526 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006527 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006528{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006529 struct extent_map *prev;
6530 struct extent_map *next;
6531 u64 start;
6532 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006533 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006534
Chris Masone6dcd2d2008-07-17 12:53:50 -04006535 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006536
6537 if (existing->start > map_start) {
6538 next = existing;
6539 prev = prev_extent_map(next);
6540 } else {
6541 prev = existing;
6542 next = next_extent_map(prev);
6543 }
6544
6545 start = prev ? extent_map_end(prev) : em->start;
6546 start = max_t(u64, start, em->start);
6547 end = next ? next->start : extent_map_end(em);
6548 end = min_t(u64, end, extent_map_end(em));
6549 start_diff = start - em->start;
6550 em->start = start;
6551 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006552 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6553 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006554 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006555 em->block_len -= start_diff;
6556 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006557 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006558}
6559
Chris Masonc8b97812008-10-29 14:49:59 -04006560static noinline int uncompress_inline(struct btrfs_path *path,
6561 struct inode *inode, struct page *page,
6562 size_t pg_offset, u64 extent_offset,
6563 struct btrfs_file_extent_item *item)
6564{
6565 int ret;
6566 struct extent_buffer *leaf = path->nodes[0];
6567 char *tmp;
6568 size_t max_size;
6569 unsigned long inline_size;
6570 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006571 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006572
6573 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006574 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006575 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6576 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006577 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006578 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006579 if (!tmp)
6580 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006581 ptr = btrfs_file_extent_inline_start(item);
6582
6583 read_extent_buffer(leaf, tmp, ptr, inline_size);
6584
Chris Mason5b050f02008-11-11 09:34:41 -05006585 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006586 ret = btrfs_decompress(compress_type, tmp, page,
6587 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006588 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006589 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006590}
6591
Chris Masond352ac62008-09-29 15:18:18 -04006592/*
6593 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006594 * the ugly parts come from merging extents from the disk with the in-ram
6595 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006596 * where the in-ram extents might be locked pending data=ordered completion.
6597 *
6598 * This also copies inline extents directly into the page.
6599 */
Chris Masond3977122009-01-05 21:25:51 -05006600
Chris Masona52d9a82007-08-27 16:49:44 -04006601struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006602 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006603 int create)
6604{
6605 int ret;
6606 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006607 u64 extent_start = 0;
6608 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006609 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006610 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006611 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006612 struct btrfs_root *root = BTRFS_I(inode)->root;
6613 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006614 struct extent_buffer *leaf;
6615 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006616 struct extent_map *em = NULL;
6617 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006618 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006619 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006620 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006621
Chris Masona52d9a82007-08-27 16:49:44 -04006622again:
Chris Mason890871b2009-09-02 16:24:52 -04006623 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006624 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006625 if (em)
6626 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006627 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006628
Chris Masona52d9a82007-08-27 16:49:44 -04006629 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006630 if (em->start > start || em->start + em->len <= start)
6631 free_extent_map(em);
6632 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006633 free_extent_map(em);
6634 else
6635 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006636 }
David Sterba172ddd62011-04-21 00:48:27 +02006637 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006638 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006639 err = -ENOMEM;
6640 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006641 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006642 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006643 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006644 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006645 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006646 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006647
6648 if (!path) {
6649 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006650 if (!path) {
6651 err = -ENOMEM;
6652 goto out;
6653 }
6654 /*
6655 * Chances are we'll be called again, so go ahead and do
6656 * readahead
6657 */
6658 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006659 }
6660
Chris Mason179e29e2007-11-01 11:28:41 -04006661 ret = btrfs_lookup_file_extent(trans, root, path,
6662 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006663 if (ret < 0) {
6664 err = ret;
6665 goto out;
6666 }
6667
6668 if (ret != 0) {
6669 if (path->slots[0] == 0)
6670 goto not_found;
6671 path->slots[0]--;
6672 }
6673
Chris Mason5f39d392007-10-15 16:14:19 -04006674 leaf = path->nodes[0];
6675 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006676 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006677 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006678 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006679 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006680 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006681 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006682 /*
6683 * If we backup past the first extent we want to move forward
6684 * and see if there is an extent in front of us, otherwise we'll
6685 * say there is a hole for our whole search range which can
6686 * cause problems.
6687 */
6688 extent_end = start;
6689 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006690 }
6691
Chris Mason5f39d392007-10-15 16:14:19 -04006692 found_type = btrfs_file_extent_type(leaf, item);
6693 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006694 if (found_type == BTRFS_FILE_EXTENT_REG ||
6695 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006696 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006697 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006698 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6699 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006700 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006701 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006702 }
Josef Bacik25a50342013-10-14 12:08:38 -04006703next:
Yan Zheng9036c102008-10-30 14:19:41 -04006704 if (start >= extent_end) {
6705 path->slots[0]++;
6706 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6707 ret = btrfs_next_leaf(root, path);
6708 if (ret < 0) {
6709 err = ret;
6710 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006711 }
Yan Zheng9036c102008-10-30 14:19:41 -04006712 if (ret > 0)
6713 goto not_found;
6714 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006715 }
Yan Zheng9036c102008-10-30 14:19:41 -04006716 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6717 if (found_key.objectid != objectid ||
6718 found_key.type != BTRFS_EXTENT_DATA_KEY)
6719 goto not_found;
6720 if (start + len <= found_key.offset)
6721 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006722 if (start > found_key.offset)
6723 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006724 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006725 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006726 em->len = found_key.offset - start;
6727 goto not_found_em;
6728 }
6729
Filipe Manana7ffbb592014-06-09 03:48:05 +01006730 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6731
Yan Zhengd899e052008-10-30 14:25:28 -04006732 if (found_type == BTRFS_FILE_EXTENT_REG ||
6733 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006734 goto insert;
6735 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006736 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006737 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006738 size_t size;
6739 size_t extent_offset;
6740 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006741
Filipe Manana7ffbb592014-06-09 03:48:05 +01006742 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006743 goto out;
Yan689f9342007-10-29 11:41:07 -04006744
Chris Mason514ac8a2014-01-03 21:07:00 -08006745 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006746 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006747 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006748 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006749 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006750 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006751 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006752 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006753 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006754 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006755 if (btrfs_file_extent_compression(leaf, item) !=
6756 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006757 ret = uncompress_inline(path, inode, page,
6758 pg_offset,
6759 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006760 if (ret) {
6761 err = ret;
6762 goto out;
6763 }
Chris Masonc8b97812008-10-29 14:49:59 -04006764 } else {
6765 map = kmap(page);
6766 read_extent_buffer(leaf, map + pg_offset, ptr,
6767 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006768 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6769 memset(map + pg_offset + copy_size, 0,
6770 PAGE_CACHE_SIZE - pg_offset -
6771 copy_size);
6772 }
Chris Masonc8b97812008-10-29 14:49:59 -04006773 kunmap(page);
6774 }
Chris Mason179e29e2007-11-01 11:28:41 -04006775 flush_dcache_page(page);
6776 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006777 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006778 if (!trans) {
6779 kunmap(page);
6780 free_extent_map(em);
6781 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006782
David Sterbab3b4aa72011-04-21 01:20:15 +02006783 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006784 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006785
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006786 if (IS_ERR(trans))
6787 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006788 goto again;
6789 }
Chris Masonc8b97812008-10-29 14:49:59 -04006790 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006791 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006792 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006793 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006794 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006795 }
Chris Masond1310b22008-01-24 16:13:08 -05006796 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006797 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006798 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006799 }
6800not_found:
6801 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006802 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006803 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006804not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006805 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006806 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006807insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006808 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006809 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006810 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006811 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006812 err = -EIO;
6813 goto out;
6814 }
Chris Masond1310b22008-01-24 16:13:08 -05006815
6816 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006817 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006818 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006819 /* it is possible that someone inserted the extent into the tree
6820 * while we had the lock dropped. It is also possible that
6821 * an overlapping map exists in the tree
6822 */
Chris Masona52d9a82007-08-27 16:49:44 -04006823 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006824 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006825
6826 ret = 0;
6827
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006828 existing = search_extent_mapping(em_tree, start, len);
6829 /*
6830 * existing will always be non-NULL, since there must be
6831 * extent causing the -EEXIST.
6832 */
6833 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006834 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006835 /*
6836 * The existing extent map is the one nearest to
6837 * the [start, start + len) range which overlaps
6838 */
6839 err = merge_extent_mapping(em_tree, existing,
6840 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006841 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006842 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006843 free_extent_map(em);
6844 em = NULL;
6845 }
6846 } else {
6847 free_extent_map(em);
6848 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006849 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006850 }
Chris Masona52d9a82007-08-27 16:49:44 -04006851 }
Chris Mason890871b2009-09-02 16:24:52 -04006852 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006853out:
liubo1abe9b82011-03-24 11:18:59 +00006854
Steven Rostedt4cd8587c2013-11-14 22:57:29 -05006855 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006856
Chris Masonf4219502008-07-22 11:18:09 -04006857 if (path)
6858 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006859 if (trans) {
6860 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006861 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006862 err = ret;
6863 }
Chris Masona52d9a82007-08-27 16:49:44 -04006864 if (err) {
6865 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006866 return ERR_PTR(err);
6867 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006868 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006869 return em;
6870}
6871
Chris Masonec29ed52011-02-23 16:23:20 -05006872struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6873 size_t pg_offset, u64 start, u64 len,
6874 int create)
6875{
6876 struct extent_map *em;
6877 struct extent_map *hole_em = NULL;
6878 u64 range_start = start;
6879 u64 end;
6880 u64 found;
6881 u64 found_end;
6882 int err = 0;
6883
6884 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6885 if (IS_ERR(em))
6886 return em;
6887 if (em) {
6888 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006889 * if our em maps to
6890 * - a hole or
6891 * - a pre-alloc extent,
6892 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006893 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006894 if (em->block_start != EXTENT_MAP_HOLE &&
6895 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006896 return em;
6897 else
6898 hole_em = em;
6899 }
6900
6901 /* check to see if we've wrapped (len == -1 or similar) */
6902 end = start + len;
6903 if (end < start)
6904 end = (u64)-1;
6905 else
6906 end -= 1;
6907
6908 em = NULL;
6909
6910 /* ok, we didn't find anything, lets look for delalloc */
6911 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6912 end, len, EXTENT_DELALLOC, 1);
6913 found_end = range_start + found;
6914 if (found_end < range_start)
6915 found_end = (u64)-1;
6916
6917 /*
6918 * we didn't find anything useful, return
6919 * the original results from get_extent()
6920 */
6921 if (range_start > end || found_end <= start) {
6922 em = hole_em;
6923 hole_em = NULL;
6924 goto out;
6925 }
6926
6927 /* adjust the range_start to make sure it doesn't
6928 * go backwards from the start they passed in
6929 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306930 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006931 found = found_end - range_start;
6932
6933 if (found > 0) {
6934 u64 hole_start = start;
6935 u64 hole_len = len;
6936
David Sterba172ddd62011-04-21 00:48:27 +02006937 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006938 if (!em) {
6939 err = -ENOMEM;
6940 goto out;
6941 }
6942 /*
6943 * when btrfs_get_extent can't find anything it
6944 * returns one huge hole
6945 *
6946 * make sure what it found really fits our range, and
6947 * adjust to make sure it is based on the start from
6948 * the caller
6949 */
6950 if (hole_em) {
6951 u64 calc_end = extent_map_end(hole_em);
6952
6953 if (calc_end <= start || (hole_em->start > end)) {
6954 free_extent_map(hole_em);
6955 hole_em = NULL;
6956 } else {
6957 hole_start = max(hole_em->start, start);
6958 hole_len = calc_end - hole_start;
6959 }
6960 }
6961 em->bdev = NULL;
6962 if (hole_em && range_start > hole_start) {
6963 /* our hole starts before our delalloc, so we
6964 * have to return just the parts of the hole
6965 * that go until the delalloc starts
6966 */
6967 em->len = min(hole_len,
6968 range_start - hole_start);
6969 em->start = hole_start;
6970 em->orig_start = hole_start;
6971 /*
6972 * don't adjust block start at all,
6973 * it is fixed at EXTENT_MAP_HOLE
6974 */
6975 em->block_start = hole_em->block_start;
6976 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006977 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6978 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006979 } else {
6980 em->start = range_start;
6981 em->len = found;
6982 em->orig_start = range_start;
6983 em->block_start = EXTENT_MAP_DELALLOC;
6984 em->block_len = found;
6985 }
6986 } else if (hole_em) {
6987 return hole_em;
6988 }
6989out:
6990
6991 free_extent_map(hole_em);
6992 if (err) {
6993 free_extent_map(em);
6994 return ERR_PTR(err);
6995 }
6996 return em;
6997}
6998
Josef Bacik4b46fce2010-05-23 11:00:55 -04006999static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7000 u64 start, u64 len)
7001{
7002 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007003 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007004 struct btrfs_key ins;
7005 u64 alloc_hint;
7006 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007007
Josef Bacik4b46fce2010-05-23 11:00:55 -04007008 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04007009 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007010 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007011 if (ret)
7012 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007013
Josef Bacik70c8a912012-10-11 16:54:30 -04007014 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007015 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04007016 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08007017 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007018 return em;
7019 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007020
7021 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
7022 ins.offset, ins.offset, 0);
7023 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08007024 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007025 free_extent_map(em);
7026 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007027 }
Josef Bacik00361582013-08-14 14:02:47 -04007028
Josef Bacik4b46fce2010-05-23 11:00:55 -04007029 return em;
7030}
7031
Chris Mason46bfbb52010-05-26 11:04:10 -04007032/*
7033 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7034 * block must be cow'd
7035 */
Josef Bacik00361582013-08-14 14:02:47 -04007036noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007037 u64 *orig_start, u64 *orig_block_len,
7038 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007039{
Josef Bacik00361582013-08-14 14:02:47 -04007040 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007041 struct btrfs_path *path;
7042 int ret;
7043 struct extent_buffer *leaf;
7044 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007045 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007046 struct btrfs_file_extent_item *fi;
7047 struct btrfs_key key;
7048 u64 disk_bytenr;
7049 u64 backref_offset;
7050 u64 extent_end;
7051 u64 num_bytes;
7052 int slot;
7053 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007054 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007055
Chris Mason46bfbb52010-05-26 11:04:10 -04007056 path = btrfs_alloc_path();
7057 if (!path)
7058 return -ENOMEM;
7059
Josef Bacik00361582013-08-14 14:02:47 -04007060 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007061 offset, 0);
7062 if (ret < 0)
7063 goto out;
7064
7065 slot = path->slots[0];
7066 if (ret == 1) {
7067 if (slot == 0) {
7068 /* can't find the item, must cow */
7069 ret = 0;
7070 goto out;
7071 }
7072 slot--;
7073 }
7074 ret = 0;
7075 leaf = path->nodes[0];
7076 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007077 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007078 key.type != BTRFS_EXTENT_DATA_KEY) {
7079 /* not our file or wrong item type, must cow */
7080 goto out;
7081 }
7082
7083 if (key.offset > offset) {
7084 /* Wrong offset, must cow */
7085 goto out;
7086 }
7087
7088 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7089 found_type = btrfs_file_extent_type(leaf, fi);
7090 if (found_type != BTRFS_FILE_EXTENT_REG &&
7091 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7092 /* not a regular extent, must cow */
7093 goto out;
7094 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007095
7096 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7097 goto out;
7098
Miao Xiee77751a2013-12-27 21:11:50 +08007099 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7100 if (extent_end <= offset)
7101 goto out;
7102
Chris Mason46bfbb52010-05-26 11:04:10 -04007103 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007104 if (disk_bytenr == 0)
7105 goto out;
7106
7107 if (btrfs_file_extent_compression(leaf, fi) ||
7108 btrfs_file_extent_encryption(leaf, fi) ||
7109 btrfs_file_extent_other_encoding(leaf, fi))
7110 goto out;
7111
Chris Mason46bfbb52010-05-26 11:04:10 -04007112 backref_offset = btrfs_file_extent_offset(leaf, fi);
7113
Josef Bacik7ee9e442013-06-21 16:37:03 -04007114 if (orig_start) {
7115 *orig_start = key.offset - backref_offset;
7116 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7117 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7118 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007119
Chris Mason46bfbb52010-05-26 11:04:10 -04007120 if (btrfs_extent_readonly(root, disk_bytenr))
7121 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007122
7123 num_bytes = min(offset + *len, extent_end) - offset;
7124 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7125 u64 range_end;
7126
7127 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7128 ret = test_range_bit(io_tree, offset, range_end,
7129 EXTENT_DELALLOC, 0, NULL);
7130 if (ret) {
7131 ret = -EAGAIN;
7132 goto out;
7133 }
7134 }
7135
Josef Bacik1bda19e2013-10-18 12:10:36 -04007136 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007137
7138 /*
7139 * look for other files referencing this extent, if we
7140 * find any we must cow
7141 */
Josef Bacik00361582013-08-14 14:02:47 -04007142 trans = btrfs_join_transaction(root);
7143 if (IS_ERR(trans)) {
7144 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007145 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007146 }
7147
7148 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7149 key.offset - backref_offset, disk_bytenr);
7150 btrfs_end_transaction(trans, root);
7151 if (ret) {
7152 ret = 0;
7153 goto out;
7154 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007155
7156 /*
7157 * adjust disk_bytenr and num_bytes to cover just the bytes
7158 * in this extent we are about to write. If there
7159 * are any csums in that range we have to cow in order
7160 * to keep the csums correct
7161 */
7162 disk_bytenr += backref_offset;
7163 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007164 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7165 goto out;
7166 /*
7167 * all of the above have passed, it is safe to overwrite this extent
7168 * without cow
7169 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007170 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007171 ret = 1;
7172out:
7173 btrfs_free_path(path);
7174 return ret;
7175}
7176
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007177bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7178{
7179 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7180 int found = false;
7181 void **pagep = NULL;
7182 struct page *page = NULL;
7183 int start_idx;
7184 int end_idx;
7185
7186 start_idx = start >> PAGE_CACHE_SHIFT;
7187
7188 /*
7189 * end is the last byte in the last page. end == start is legal
7190 */
7191 end_idx = end >> PAGE_CACHE_SHIFT;
7192
7193 rcu_read_lock();
7194
7195 /* Most of the code in this while loop is lifted from
7196 * find_get_page. It's been modified to begin searching from a
7197 * page and return just the first page found in that range. If the
7198 * found idx is less than or equal to the end idx then we know that
7199 * a page exists. If no pages are found or if those pages are
7200 * outside of the range then we're fine (yay!) */
7201 while (page == NULL &&
7202 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7203 page = radix_tree_deref_slot(pagep);
7204 if (unlikely(!page))
7205 break;
7206
7207 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007208 if (radix_tree_deref_retry(page)) {
7209 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007210 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007211 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007212 /*
7213 * Otherwise, shmem/tmpfs must be storing a swap entry
7214 * here as an exceptional entry: so return it without
7215 * attempting to raise page count.
7216 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007217 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007218 break; /* TODO: Is this relevant for this use case? */
7219 }
7220
Filipe Manana91405152014-06-05 13:22:24 +01007221 if (!page_cache_get_speculative(page)) {
7222 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007223 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007224 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007225
7226 /*
7227 * Has the page moved?
7228 * This is part of the lockless pagecache protocol. See
7229 * include/linux/pagemap.h for details.
7230 */
7231 if (unlikely(page != *pagep)) {
7232 page_cache_release(page);
7233 page = NULL;
7234 }
7235 }
7236
7237 if (page) {
7238 if (page->index <= end_idx)
7239 found = true;
7240 page_cache_release(page);
7241 }
7242
7243 rcu_read_unlock();
7244 return found;
7245}
7246
Josef Bacikeb838e72012-07-31 16:28:48 -04007247static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7248 struct extent_state **cached_state, int writing)
7249{
7250 struct btrfs_ordered_extent *ordered;
7251 int ret = 0;
7252
7253 while (1) {
7254 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7255 0, cached_state);
7256 /*
7257 * We're concerned with the entire range that we're going to be
7258 * doing DIO to, so we need to make sure theres no ordered
7259 * extents in this range.
7260 */
7261 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7262 lockend - lockstart + 1);
7263
7264 /*
7265 * We need to make sure there are no buffered pages in this
7266 * range either, we could have raced between the invalidate in
7267 * generic_file_direct_write and locking the extent. The
7268 * invalidate needs to happen so that reads after a write do not
7269 * get stale data.
7270 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007271 if (!ordered &&
7272 (!writing ||
7273 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007274 break;
7275
7276 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7277 cached_state, GFP_NOFS);
7278
7279 if (ordered) {
7280 btrfs_start_ordered_extent(inode, ordered, 1);
7281 btrfs_put_ordered_extent(ordered);
7282 } else {
7283 /* Screw you mmap */
Filipe Manana728404d2014-10-10 09:43:11 +01007284 ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
Filipe Manana075bdbd2014-10-09 21:18:55 +01007285 if (ret)
7286 break;
7287 ret = filemap_fdatawait_range(inode->i_mapping,
7288 lockstart,
7289 lockend);
Josef Bacikeb838e72012-07-31 16:28:48 -04007290 if (ret)
7291 break;
7292
7293 /*
7294 * If we found a page that couldn't be invalidated just
7295 * fall back to buffered.
7296 */
7297 ret = invalidate_inode_pages2_range(inode->i_mapping,
7298 lockstart >> PAGE_CACHE_SHIFT,
7299 lockend >> PAGE_CACHE_SHIFT);
7300 if (ret)
7301 break;
7302 }
7303
7304 cond_resched();
7305 }
7306
7307 return ret;
7308}
7309
Josef Bacik69ffb542012-09-11 15:40:07 -04007310static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7311 u64 len, u64 orig_start,
7312 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007313 u64 orig_block_len, u64 ram_bytes,
7314 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007315{
7316 struct extent_map_tree *em_tree;
7317 struct extent_map *em;
7318 struct btrfs_root *root = BTRFS_I(inode)->root;
7319 int ret;
7320
7321 em_tree = &BTRFS_I(inode)->extent_tree;
7322 em = alloc_extent_map();
7323 if (!em)
7324 return ERR_PTR(-ENOMEM);
7325
7326 em->start = start;
7327 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007328 em->mod_start = start;
7329 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007330 em->len = len;
7331 em->block_len = block_len;
7332 em->block_start = block_start;
7333 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007334 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007335 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007336 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007337 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7338 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007339 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007340
7341 do {
7342 btrfs_drop_extent_cache(inode, em->start,
7343 em->start + em->len - 1, 0);
7344 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007345 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007346 write_unlock(&em_tree->lock);
7347 } while (ret == -EEXIST);
7348
7349 if (ret) {
7350 free_extent_map(em);
7351 return ERR_PTR(ret);
7352 }
7353
7354 return em;
7355}
7356
7357
Josef Bacik4b46fce2010-05-23 11:00:55 -04007358static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7359 struct buffer_head *bh_result, int create)
7360{
7361 struct extent_map *em;
7362 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007363 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007364 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007365 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007366 u64 len = bh_result->b_size;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007367 u64 *outstanding_extents = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007368 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007369 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007370
Miao Xie172a5042013-02-21 02:48:22 -07007371 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007372 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007373 else
Josef Bacikc3298612012-08-03 16:49:19 -04007374 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007375
Josef Bacikc3298612012-08-03 16:49:19 -04007376 lockstart = start;
7377 lockend = start + len - 1;
7378
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007379 if (current->journal_info) {
7380 /*
7381 * Need to pull our outstanding extents and set journal_info to NULL so
7382 * that anything that needs to check if there's a transction doesn't get
7383 * confused.
7384 */
7385 outstanding_extents = current->journal_info;
7386 current->journal_info = NULL;
7387 }
7388
Josef Bacikeb838e72012-07-31 16:28:48 -04007389 /*
7390 * If this errors out it's because we couldn't invalidate pagecache for
7391 * this range and we need to fallback to buffered.
7392 */
7393 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7394 return -ENOTBLK;
7395
Josef Bacik4b46fce2010-05-23 11:00:55 -04007396 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007397 if (IS_ERR(em)) {
7398 ret = PTR_ERR(em);
7399 goto unlock_err;
7400 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007401
7402 /*
7403 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7404 * io. INLINE is special, and we could probably kludge it in here, but
7405 * it's still buffered so for safety lets just fall back to the generic
7406 * buffered path.
7407 *
7408 * For COMPRESSED we _have_ to read the entire extent in so we can
7409 * decompress it, so there will be buffering required no matter what we
7410 * do, so go ahead and fallback to buffered.
7411 *
7412 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7413 * to buffered IO. Don't blame me, this is the price we pay for using
7414 * the generic code.
7415 */
7416 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7417 em->block_start == EXTENT_MAP_INLINE) {
7418 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007419 ret = -ENOTBLK;
7420 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007421 }
7422
7423 /* Just a good old fashioned hole, return */
7424 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7425 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7426 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007427 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007428 }
7429
7430 /*
7431 * We don't allocate a new extent in the following cases
7432 *
7433 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7434 * existing extent.
7435 * 2) The extent is marked as PREALLOC. We're good to go here and can
7436 * just use the extent.
7437 *
7438 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007439 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007440 len = min(len, em->len - (start - em->start));
7441 lockstart = start + len;
7442 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007443 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007444
7445 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7446 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7447 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007448 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007449 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007450
7451 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7452 type = BTRFS_ORDERED_PREALLOC;
7453 else
7454 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007455 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007456 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007457
Josef Bacik00361582013-08-14 14:02:47 -04007458 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007459 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007460 if (type == BTRFS_ORDERED_PREALLOC) {
7461 free_extent_map(em);
7462 em = create_pinned_em(inode, start, len,
7463 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007464 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007465 orig_block_len,
7466 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007467 if (IS_ERR(em)) {
7468 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007469 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007470 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007471 }
7472
Chris Mason46bfbb52010-05-26 11:04:10 -04007473 ret = btrfs_add_ordered_extent_dio(inode, start,
7474 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007475 if (ret) {
7476 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007477 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007478 }
7479 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007480 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007481 }
Josef Bacik00361582013-08-14 14:02:47 -04007482
Chris Mason46bfbb52010-05-26 11:04:10 -04007483 /*
7484 * this will cow the extent, reset the len in case we changed
7485 * it above
7486 */
7487 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007488 free_extent_map(em);
7489 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007490 if (IS_ERR(em)) {
7491 ret = PTR_ERR(em);
7492 goto unlock_err;
7493 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007494 len = min(len, em->len - (start - em->start));
7495unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007496 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7497 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007498 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007499 bh_result->b_bdev = em->bdev;
7500 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007501 if (create) {
7502 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7503 set_buffer_new(bh_result);
7504
7505 /*
7506 * Need to update the i_size under the extent lock so buffered
7507 * readers will get the updated i_size when we unlock.
7508 */
7509 if (start + len > i_size_read(inode))
7510 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007511
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007512 /*
7513 * If we have an outstanding_extents count still set then we're
7514 * within our reservation, otherwise we need to adjust our inode
7515 * counter appropriately.
7516 */
7517 if (*outstanding_extents) {
7518 (*outstanding_extents)--;
7519 } else {
Josef Bacik3e05bde2015-02-11 15:08:57 -05007520 spin_lock(&BTRFS_I(inode)->lock);
7521 BTRFS_I(inode)->outstanding_extents++;
7522 spin_unlock(&BTRFS_I(inode)->lock);
7523 }
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007524
7525 current->journal_info = outstanding_extents;
Josef Bacik32667892015-02-11 15:08:58 -05007526 btrfs_free_reserved_data_space(inode, len);
Josef Bacikc3473e82012-06-19 10:59:00 -04007527 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007528
Josef Bacikeb838e72012-07-31 16:28:48 -04007529 /*
7530 * In the case of write we need to clear and unlock the entire range,
7531 * in the case of read we need to unlock only the end area that we
7532 * aren't using if there is any left over space.
7533 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007534 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007535 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7536 lockend, unlock_bits, 1, 0,
7537 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007538 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007539 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007540 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007541
Josef Bacik4b46fce2010-05-23 11:00:55 -04007542 free_extent_map(em);
7543
7544 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007545
7546unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007547 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7548 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007549 if (outstanding_extents)
7550 current->journal_info = outstanding_extents;
Josef Bacikeb838e72012-07-31 16:28:48 -04007551 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007552}
7553
Miao Xie8b110e32014-09-12 18:44:03 +08007554static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7555 int rw, int mirror_num)
7556{
7557 struct btrfs_root *root = BTRFS_I(inode)->root;
7558 int ret;
7559
7560 BUG_ON(rw & REQ_WRITE);
7561
7562 bio_get(bio);
7563
7564 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7565 BTRFS_WQ_ENDIO_DIO_REPAIR);
7566 if (ret)
7567 goto err;
7568
7569 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7570err:
7571 bio_put(bio);
7572 return ret;
7573}
7574
7575static int btrfs_check_dio_repairable(struct inode *inode,
7576 struct bio *failed_bio,
7577 struct io_failure_record *failrec,
7578 int failed_mirror)
7579{
7580 int num_copies;
7581
7582 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7583 failrec->logical, failrec->len);
7584 if (num_copies == 1) {
7585 /*
7586 * we only have a single copy of the data, so don't bother with
7587 * all the retry and error correction code that follows. no
7588 * matter what the error is, it is very likely to persist.
7589 */
7590 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7591 num_copies, failrec->this_mirror, failed_mirror);
7592 return 0;
7593 }
7594
7595 failrec->failed_mirror = failed_mirror;
7596 failrec->this_mirror++;
7597 if (failrec->this_mirror == failed_mirror)
7598 failrec->this_mirror++;
7599
7600 if (failrec->this_mirror > num_copies) {
7601 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7602 num_copies, failrec->this_mirror, failed_mirror);
7603 return 0;
7604 }
7605
7606 return 1;
7607}
7608
7609static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7610 struct page *page, u64 start, u64 end,
7611 int failed_mirror, bio_end_io_t *repair_endio,
7612 void *repair_arg)
7613{
7614 struct io_failure_record *failrec;
7615 struct bio *bio;
7616 int isector;
7617 int read_mode;
7618 int ret;
7619
7620 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7621
7622 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7623 if (ret)
7624 return ret;
7625
7626 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7627 failed_mirror);
7628 if (!ret) {
7629 free_io_failure(inode, failrec);
7630 return -EIO;
7631 }
7632
7633 if (failed_bio->bi_vcnt > 1)
7634 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7635 else
7636 read_mode = READ_SYNC;
7637
7638 isector = start - btrfs_io_bio(failed_bio)->logical;
7639 isector >>= inode->i_sb->s_blocksize_bits;
7640 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7641 0, isector, repair_endio, repair_arg);
7642 if (!bio) {
7643 free_io_failure(inode, failrec);
7644 return -EIO;
7645 }
7646
7647 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7648 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7649 read_mode, failrec->this_mirror, failrec->in_validation);
7650
7651 ret = submit_dio_repair_bio(inode, bio, read_mode,
7652 failrec->this_mirror);
7653 if (ret) {
7654 free_io_failure(inode, failrec);
7655 bio_put(bio);
7656 }
7657
7658 return ret;
7659}
7660
7661struct btrfs_retry_complete {
7662 struct completion done;
7663 struct inode *inode;
7664 u64 start;
7665 int uptodate;
7666};
7667
7668static void btrfs_retry_endio_nocsum(struct bio *bio, int err)
7669{
7670 struct btrfs_retry_complete *done = bio->bi_private;
7671 struct bio_vec *bvec;
7672 int i;
7673
7674 if (err)
7675 goto end;
7676
7677 done->uptodate = 1;
7678 bio_for_each_segment_all(bvec, bio, i)
7679 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7680end:
7681 complete(&done->done);
7682 bio_put(bio);
7683}
7684
7685static int __btrfs_correct_data_nocsum(struct inode *inode,
7686 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007687{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007688 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007689 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007690 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007691 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007692 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007693
Miao Xiec1dc0892014-09-12 18:43:56 +08007694 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007695 done.inode = inode;
7696
7697 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7698try_again:
7699 done.uptodate = 0;
7700 done.start = start;
7701 init_completion(&done.done);
7702
7703 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7704 start + bvec->bv_len - 1,
7705 io_bio->mirror_num,
7706 btrfs_retry_endio_nocsum, &done);
7707 if (ret)
7708 return ret;
7709
7710 wait_for_completion(&done.done);
7711
7712 if (!done.uptodate) {
7713 /* We might have another mirror, so try again */
7714 goto try_again;
7715 }
7716
7717 start += bvec->bv_len;
7718 }
7719
7720 return 0;
7721}
7722
7723static void btrfs_retry_endio(struct bio *bio, int err)
7724{
7725 struct btrfs_retry_complete *done = bio->bi_private;
7726 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7727 struct bio_vec *bvec;
7728 int uptodate;
7729 int ret;
7730 int i;
7731
7732 if (err)
7733 goto end;
7734
7735 uptodate = 1;
7736 bio_for_each_segment_all(bvec, bio, i) {
7737 ret = __readpage_endio_check(done->inode, io_bio, i,
7738 bvec->bv_page, 0,
7739 done->start, bvec->bv_len);
7740 if (!ret)
7741 clean_io_failure(done->inode, done->start,
7742 bvec->bv_page, 0);
7743 else
7744 uptodate = 0;
7745 }
7746
7747 done->uptodate = uptodate;
7748end:
7749 complete(&done->done);
7750 bio_put(bio);
7751}
7752
7753static int __btrfs_subio_endio_read(struct inode *inode,
7754 struct btrfs_io_bio *io_bio, int err)
7755{
7756 struct bio_vec *bvec;
7757 struct btrfs_retry_complete done;
7758 u64 start;
7759 u64 offset = 0;
7760 int i;
7761 int ret;
7762
7763 err = 0;
7764 start = io_bio->logical;
7765 done.inode = inode;
7766
Miao Xiec1dc0892014-09-12 18:43:56 +08007767 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007768 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7769 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007770 if (likely(!ret))
7771 goto next;
7772try_again:
7773 done.uptodate = 0;
7774 done.start = start;
7775 init_completion(&done.done);
7776
7777 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7778 start + bvec->bv_len - 1,
7779 io_bio->mirror_num,
7780 btrfs_retry_endio, &done);
7781 if (ret) {
7782 err = ret;
7783 goto next;
7784 }
7785
7786 wait_for_completion(&done.done);
7787
7788 if (!done.uptodate) {
7789 /* We might have another mirror, so try again */
7790 goto try_again;
7791 }
7792next:
7793 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007794 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007795 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007796
7797 return err;
7798}
7799
Miao Xie8b110e32014-09-12 18:44:03 +08007800static int btrfs_subio_endio_read(struct inode *inode,
7801 struct btrfs_io_bio *io_bio, int err)
7802{
7803 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7804
7805 if (skip_csum) {
7806 if (unlikely(err))
7807 return __btrfs_correct_data_nocsum(inode, io_bio);
7808 else
7809 return 0;
7810 } else {
7811 return __btrfs_subio_endio_read(inode, io_bio, err);
7812 }
7813}
7814
Miao Xiec1dc0892014-09-12 18:43:56 +08007815static void btrfs_endio_direct_read(struct bio *bio, int err)
7816{
7817 struct btrfs_dio_private *dip = bio->bi_private;
7818 struct inode *inode = dip->inode;
7819 struct bio *dio_bio;
7820 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7821
Miao Xie8b110e32014-09-12 18:44:03 +08007822 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7823 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007824
Josef Bacik4b46fce2010-05-23 11:00:55 -04007825 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007826 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007827 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007828
Josef Bacik4b46fce2010-05-23 11:00:55 -04007829 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007830
7831 /* If we had a csum failure make sure to clear the uptodate flag */
7832 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007833 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7834 dio_end_io(dio_bio, err);
Miao Xie23ea8e52014-09-12 18:43:54 +08007835
7836 if (io_bio->end_io)
7837 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007838 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007839}
7840
7841static void btrfs_endio_direct_write(struct bio *bio, int err)
7842{
7843 struct btrfs_dio_private *dip = bio->bi_private;
7844 struct inode *inode = dip->inode;
7845 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007846 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007847 u64 ordered_offset = dip->logical_offset;
7848 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007849 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007850 int ret;
7851
7852 if (err)
7853 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007854again:
7855 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7856 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007857 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007858 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007859 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007860
Liu Bo9e0af232014-08-15 23:36:53 +08007861 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7862 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007863 btrfs_queue_work(root->fs_info->endio_write_workers,
7864 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007865out_test:
7866 /*
7867 * our bio might span multiple ordered extents. If we haven't
7868 * completed the accounting for the whole dio, go back and try again
7869 */
7870 if (ordered_offset < dip->logical_offset + dip->bytes) {
7871 ordered_bytes = dip->logical_offset + dip->bytes -
7872 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007873 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007874 goto again;
7875 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007876out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007877 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007878
Josef Bacik4b46fce2010-05-23 11:00:55 -04007879 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007880
7881 /* If we had an error make sure to clear the uptodate flag */
7882 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007883 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7884 dio_end_io(dio_bio, err);
7885 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007886}
7887
Chris Masoneaf25d92010-05-25 09:48:28 -04007888static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7889 struct bio *bio, int mirror_num,
7890 unsigned long bio_flags, u64 offset)
7891{
7892 int ret;
7893 struct btrfs_root *root = BTRFS_I(inode)->root;
7894 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007895 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007896 return 0;
7897}
7898
Miao Xiee65e1532010-11-22 03:04:43 +00007899static void btrfs_end_dio_bio(struct bio *bio, int err)
7900{
7901 struct btrfs_dio_private *dip = bio->bi_private;
7902
Miao Xie8b110e32014-09-12 18:44:03 +08007903 if (err)
7904 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
7905 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7906 btrfs_ino(dip->inode), bio->bi_rw,
7907 (unsigned long long)bio->bi_iter.bi_sector,
7908 bio->bi_iter.bi_size, err);
7909
7910 if (dip->subio_endio)
7911 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007912
7913 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007914 dip->errors = 1;
7915
7916 /*
7917 * before atomic variable goto zero, we must make sure
7918 * dip->errors is perceived to be set.
7919 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007920 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007921 }
7922
7923 /* if there are more bios still pending for this dio, just exit */
7924 if (!atomic_dec_and_test(&dip->pending_bios))
7925 goto out;
7926
Chris Mason9be33952013-05-17 18:30:14 -04007927 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007928 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007929 } else {
7930 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007931 bio_endio(dip->orig_bio, 0);
7932 }
7933out:
7934 bio_put(bio);
7935}
7936
7937static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7938 u64 first_sector, gfp_t gfp_flags)
7939{
7940 int nr_vecs = bio_get_nr_vecs(bdev);
7941 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7942}
7943
Miao Xiec1dc0892014-09-12 18:43:56 +08007944static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
7945 struct inode *inode,
7946 struct btrfs_dio_private *dip,
7947 struct bio *bio,
7948 u64 file_offset)
7949{
7950 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7951 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
7952 int ret;
7953
7954 /*
7955 * We load all the csum data we need when we submit
7956 * the first bio to reduce the csum tree search and
7957 * contention.
7958 */
7959 if (dip->logical_offset == file_offset) {
7960 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
7961 file_offset);
7962 if (ret)
7963 return ret;
7964 }
7965
7966 if (bio == dip->orig_bio)
7967 return 0;
7968
7969 file_offset -= dip->logical_offset;
7970 file_offset >>= inode->i_sb->s_blocksize_bits;
7971 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
7972
7973 return 0;
7974}
7975
Miao Xiee65e1532010-11-22 03:04:43 +00007976static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7977 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007978 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007979{
Miao Xiefacc8a222013-07-25 19:22:34 +08007980 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007981 int write = rw & REQ_WRITE;
7982 struct btrfs_root *root = BTRFS_I(inode)->root;
7983 int ret;
7984
Josef Bacikb812ce22012-11-16 13:56:32 -05007985 if (async_submit)
7986 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7987
Miao Xiee65e1532010-11-22 03:04:43 +00007988 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007989
7990 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02007991 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7992 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007993 if (ret)
7994 goto err;
7995 }
Miao Xiee65e1532010-11-22 03:04:43 +00007996
Josef Bacik1ae39932011-04-06 14:41:34 -04007997 if (skip_sum)
7998 goto map;
7999
8000 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008001 ret = btrfs_wq_submit_bio(root->fs_info,
8002 inode, rw, bio, 0, 0,
8003 file_offset,
8004 __btrfs_submit_bio_start_direct_io,
8005 __btrfs_submit_bio_done);
8006 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008007 } else if (write) {
8008 /*
8009 * If we aren't doing async submit, calculate the csum of the
8010 * bio now.
8011 */
8012 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8013 if (ret)
8014 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008015 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008016 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8017 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008018 if (ret)
8019 goto err;
8020 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008021map:
8022 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008023err:
8024 bio_put(bio);
8025 return ret;
8026}
8027
8028static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8029 int skip_sum)
8030{
8031 struct inode *inode = dip->inode;
8032 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008033 struct bio *bio;
8034 struct bio *orig_bio = dip->orig_bio;
8035 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008036 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008037 u64 file_offset = dip->logical_offset;
8038 u64 submit_len = 0;
8039 u64 map_length;
8040 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08008041 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04008042 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008043
Kent Overstreet4f024f32013-10-11 15:44:27 -07008044 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008045 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008046 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008047 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008048 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008049
Kent Overstreet4f024f32013-10-11 15:44:27 -07008050 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008051 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008052 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008053 goto submit;
8054 }
8055
David Woodhouse53b381b2013-01-29 18:40:14 -05008056 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008057 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008058 async_submit = 0;
8059 else
8060 async_submit = 1;
8061
Josef Bacik02f57c72011-04-06 14:25:44 -04008062 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8063 if (!bio)
8064 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008065
Josef Bacik02f57c72011-04-06 14:25:44 -04008066 bio->bi_private = dip;
8067 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008068 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008069 atomic_inc(&dip->pending_bios);
8070
Miao Xiee65e1532010-11-22 03:04:43 +00008071 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02008072 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00008073 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02008074 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00008075 /*
8076 * inc the count before we submit the bio so
8077 * we know the end IO handler won't happen before
8078 * we inc the count. Otherwise, the dip might get freed
8079 * before we're done setting it up
8080 */
8081 atomic_inc(&dip->pending_bios);
8082 ret = __btrfs_submit_dio_bio(bio, inode, rw,
8083 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008084 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008085 if (ret) {
8086 bio_put(bio);
8087 atomic_dec(&dip->pending_bios);
8088 goto out_err;
8089 }
8090
Miao Xiee65e1532010-11-22 03:04:43 +00008091 start_sector += submit_len >> 9;
8092 file_offset += submit_len;
8093
8094 submit_len = 0;
8095 nr_pages = 0;
8096
8097 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8098 start_sector, GFP_NOFS);
8099 if (!bio)
8100 goto out_err;
8101 bio->bi_private = dip;
8102 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008103 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008104
Kent Overstreet4f024f32013-10-11 15:44:27 -07008105 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008106 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008107 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008108 &map_length, NULL, 0);
8109 if (ret) {
8110 bio_put(bio);
8111 goto out_err;
8112 }
8113 } else {
8114 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308115 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00008116 bvec++;
8117 }
8118 }
8119
Josef Bacik02f57c72011-04-06 14:25:44 -04008120submit:
Miao Xiee65e1532010-11-22 03:04:43 +00008121 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008122 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008123 if (!ret)
8124 return 0;
8125
8126 bio_put(bio);
8127out_err:
8128 dip->errors = 1;
8129 /*
8130 * before atomic variable goto zero, we must
8131 * make sure dip->errors is perceived to be set.
8132 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008133 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008134 if (atomic_dec_and_test(&dip->pending_bios))
8135 bio_io_error(dip->orig_bio);
8136
8137 /* bio_end_io() will handle error, so we needn't return it */
8138 return 0;
8139}
8140
Chris Mason9be33952013-05-17 18:30:14 -04008141static void btrfs_submit_direct(int rw, struct bio *dio_bio,
8142 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008143{
8144 struct btrfs_root *root = BTRFS_I(inode)->root;
8145 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04008146 struct bio *io_bio;
Miao Xie23ea8e52014-09-12 18:43:54 +08008147 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008148 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02008149 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008150 int ret = 0;
8151
8152 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8153
Chris Mason9be33952013-05-17 18:30:14 -04008154 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008155 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008156 ret = -ENOMEM;
8157 goto free_ordered;
8158 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008159
Miao Xiec1dc0892014-09-12 18:43:56 +08008160 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008161 if (!dip) {
8162 ret = -ENOMEM;
8163 goto free_io_bio;
8164 }
8165
8166 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008167 dip->inode = inode;
8168 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008169 dip->bytes = dio_bio->bi_iter.bi_size;
8170 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008171 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008172 dip->orig_bio = io_bio;
8173 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008174 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008175 btrfs_bio = btrfs_io_bio(io_bio);
8176 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008177
Miao Xiec1dc0892014-09-12 18:43:56 +08008178 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008179 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008180 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008181 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008182 dip->subio_endio = btrfs_subio_endio_read;
8183 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008184
Miao Xiee65e1532010-11-22 03:04:43 +00008185 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8186 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008187 return;
Chris Mason9be33952013-05-17 18:30:14 -04008188
Miao Xie23ea8e52014-09-12 18:43:54 +08008189 if (btrfs_bio->end_io)
8190 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008191free_io_bio:
8192 bio_put(io_bio);
8193
Josef Bacik4b46fce2010-05-23 11:00:55 -04008194free_ordered:
8195 /*
8196 * If this is a write, we need to clean up the reserved space and kill
8197 * the ordered extent.
8198 */
8199 if (write) {
8200 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05008201 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008202 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
8203 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
8204 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08008205 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008206 btrfs_put_ordered_extent(ordered);
8207 btrfs_put_ordered_extent(ordered);
8208 }
Chris Mason9be33952013-05-17 18:30:14 -04008209 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008210}
8211
Chris Mason5a5f79b2010-05-26 21:33:37 -04008212static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008213 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008214{
8215 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008216 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008217 unsigned blocksize_mask = root->sectorsize - 1;
8218 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008219
8220 if (offset & blocksize_mask)
8221 goto out;
8222
Al Viro28060d52014-03-22 05:15:17 -04008223 if (iov_iter_alignment(iter) & blocksize_mask)
8224 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008225
Al Viro28060d52014-03-22 05:15:17 -04008226 /* If this is a write we don't need to check anymore */
8227 if (rw & WRITE)
8228 return 0;
8229 /*
8230 * Check to make sure we don't have duplicate iov_base's in this
8231 * iovec, if so return EINVAL, otherwise we'll get csum errors
8232 * when reading back.
8233 */
8234 for (seg = 0; seg < iter->nr_segs; seg++) {
8235 for (i = seg + 1; i < iter->nr_segs; i++) {
8236 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008237 goto out;
8238 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008239 }
8240 retval = 0;
8241out:
8242 return retval;
8243}
Josef Bacikeb838e72012-07-31 16:28:48 -04008244
Chris Mason16432982008-04-10 10:23:21 -04008245static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05008246 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04008247{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008248 struct file *file = iocb->ki_filp;
8249 struct inode *inode = file->f_mapping->host;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008250 u64 outstanding_extents = 0;
Miao Xie09348562013-02-07 10:12:07 +00008251 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008252 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008253 bool wakeup = true;
8254 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008255 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008256
Al Viro28060d52014-03-22 05:15:17 -04008257 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008258 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008259
Miao Xie38851cc2013-02-08 07:04:11 +00008260 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008261 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008262
Josef Bacik0e267c42013-07-02 10:38:02 -04008263 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008264 * The generic stuff only does filemap_write_and_wait_range, which
8265 * isn't enough if we've written compressed pages to this area, so
8266 * we need to flush the dirty pages again to make absolutely sure
8267 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008268 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008269 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008270 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8271 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008272 filemap_fdatawrite_range(inode->i_mapping, offset,
8273 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008274
Miao Xie09348562013-02-07 10:12:07 +00008275 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008276 /*
8277 * If the write DIO is beyond the EOF, we need update
8278 * the isize, but it is protected by i_mutex. So we can
8279 * not unlock the i_mutex at this case.
8280 */
8281 if (offset + count <= inode->i_size) {
8282 mutex_unlock(&inode->i_mutex);
8283 relock = true;
8284 }
Miao Xie09348562013-02-07 10:12:07 +00008285 ret = btrfs_delalloc_reserve_space(inode, count);
8286 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008287 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008288 outstanding_extents = div64_u64(count +
8289 BTRFS_MAX_EXTENT_SIZE - 1,
8290 BTRFS_MAX_EXTENT_SIZE);
8291
8292 /*
8293 * We need to know how many extents we reserved so that we can
8294 * do the accounting properly if we go over the number we
8295 * originally calculated. Abuse current->journal_info for this.
8296 */
8297 current->journal_info = &outstanding_extents;
David Sterbaee39b432014-09-30 01:33:33 +02008298 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8299 &BTRFS_I(inode)->runtime_flags)) {
Miao Xie38851cc2013-02-08 07:04:11 +00008300 inode_dio_done(inode);
8301 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8302 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008303 }
8304
8305 ret = __blockdev_direct_IO(rw, iocb, inode,
8306 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05008307 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00008308 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00008309 if (rw & WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008310 current->journal_info = NULL;
Miao Xie09348562013-02-07 10:12:07 +00008311 if (ret < 0 && ret != -EIOCBQUEUED)
8312 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07008313 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00008314 btrfs_delalloc_release_space(inode,
8315 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008316 }
Miao Xie38851cc2013-02-08 07:04:11 +00008317out:
Miao Xie2e60a512013-02-08 07:01:08 +00008318 if (wakeup)
8319 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008320 if (relock)
8321 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008322
8323 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008324}
8325
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008326#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8327
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008328static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8329 __u64 start, __u64 len)
8330{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008331 int ret;
8332
8333 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8334 if (ret)
8335 return ret;
8336
Chris Masonec29ed52011-02-23 16:23:20 -05008337 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008338}
8339
Chris Mason9ebefb182007-06-15 13:50:00 -04008340int btrfs_readpage(struct file *file, struct page *page)
8341{
Chris Masond1310b22008-01-24 16:13:08 -05008342 struct extent_io_tree *tree;
8343 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008344 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008345}
Chris Mason1832a6d2007-12-21 16:27:21 -05008346
Chris Mason39279cc2007-06-12 06:35:45 -04008347static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8348{
Chris Masond1310b22008-01-24 16:13:08 -05008349 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04008350
8351
8352 if (current->flags & PF_MEMALLOC) {
8353 redirty_page_for_writepage(wbc, page);
8354 unlock_page(page);
8355 return 0;
8356 }
Chris Masond1310b22008-01-24 16:13:08 -05008357 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008358 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8359}
Chris Mason39279cc2007-06-12 06:35:45 -04008360
Eric Sandeen48a3b632013-04-25 20:41:01 +00008361static int btrfs_writepages(struct address_space *mapping,
8362 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008363{
Chris Masond1310b22008-01-24 16:13:08 -05008364 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008365
Chris Masond1310b22008-01-24 16:13:08 -05008366 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008367 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8368}
8369
Chris Mason3ab2fb52007-11-08 10:59:22 -05008370static int
8371btrfs_readpages(struct file *file, struct address_space *mapping,
8372 struct list_head *pages, unsigned nr_pages)
8373{
Chris Masond1310b22008-01-24 16:13:08 -05008374 struct extent_io_tree *tree;
8375 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008376 return extent_readpages(tree, mapping, pages, nr_pages,
8377 btrfs_get_extent);
8378}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008379static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008380{
Chris Masond1310b22008-01-24 16:13:08 -05008381 struct extent_io_tree *tree;
8382 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008383 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008384
Chris Masond1310b22008-01-24 16:13:08 -05008385 tree = &BTRFS_I(page->mapping->host)->io_tree;
8386 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008387 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008388 if (ret == 1) {
8389 ClearPagePrivate(page);
8390 set_page_private(page, 0);
8391 page_cache_release(page);
8392 }
8393 return ret;
8394}
Chris Mason39279cc2007-06-12 06:35:45 -04008395
Chris Masone6dcd2d2008-07-17 12:53:50 -04008396static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8397{
Chris Mason98509cfc2008-09-11 15:51:43 -04008398 if (PageWriteback(page) || PageDirty(page))
8399 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008400 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008401}
8402
Lukas Czernerd47992f2013-05-21 23:17:23 -04008403static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8404 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008405{
Josef Bacik5fd02042012-05-02 14:00:54 -04008406 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008407 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008408 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008409 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008410 u64 page_start = page_offset(page);
8411 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008412 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008413
Chris Mason8b62b722009-09-02 16:53:46 -04008414 /*
8415 * we have the page locked, so new writeback can't start,
8416 * and the dirty bit won't be cleared while we are here.
8417 *
8418 * Wait for IO on this page so that we can safely clear
8419 * the PagePrivate2 bit and do ordered accounting
8420 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008421 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008422
Josef Bacik5fd02042012-05-02 14:00:54 -04008423 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008424 if (offset) {
8425 btrfs_releasepage(page, GFP_NOFS);
8426 return;
8427 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008428
8429 if (!inode_evicting)
8430 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8431 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008432 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008433 /*
8434 * IO on this page will never be started, so we need
8435 * to account for any ordered extents now
8436 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008437 if (!inode_evicting)
8438 clear_extent_bit(tree, page_start, page_end,
8439 EXTENT_DIRTY | EXTENT_DELALLOC |
8440 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8441 EXTENT_DEFRAG, 1, 0, &cached_state,
8442 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008443 /*
8444 * whoever cleared the private bit is responsible
8445 * for the finish_ordered_io
8446 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008447 if (TestClearPagePrivate2(page)) {
8448 struct btrfs_ordered_inode_tree *tree;
8449 u64 new_len;
8450
8451 tree = &BTRFS_I(inode)->ordered_tree;
8452
8453 spin_lock_irq(&tree->lock);
8454 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8455 new_len = page_start - ordered->file_offset;
8456 if (new_len < ordered->truncated_len)
8457 ordered->truncated_len = new_len;
8458 spin_unlock_irq(&tree->lock);
8459
8460 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8461 page_start,
8462 PAGE_CACHE_SIZE, 1))
8463 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008464 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008465 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008466 if (!inode_evicting) {
8467 cached_state = NULL;
8468 lock_extent_bits(tree, page_start, page_end, 0,
8469 &cached_state);
8470 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008471 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008472
8473 if (!inode_evicting) {
8474 clear_extent_bit(tree, page_start, page_end,
8475 EXTENT_LOCKED | EXTENT_DIRTY |
8476 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8477 EXTENT_DEFRAG, 1, 1,
8478 &cached_state, GFP_NOFS);
8479
8480 __btrfs_releasepage(page, GFP_NOFS);
8481 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008482
Chris Mason4a096752008-07-21 10:29:44 -04008483 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008484 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008485 ClearPagePrivate(page);
8486 set_page_private(page, 0);
8487 page_cache_release(page);
8488 }
Chris Mason39279cc2007-06-12 06:35:45 -04008489}
8490
Chris Mason9ebefb182007-06-15 13:50:00 -04008491/*
8492 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8493 * called from a page fault handler when a page is first dirtied. Hence we must
8494 * be careful to check for EOF conditions here. We set the page up correctly
8495 * for a written page which means we get ENOSPC checking when writing into
8496 * holes and correct delalloc and unwritten extent mapping on filesystems that
8497 * support these features.
8498 *
8499 * We are not allowed to take the i_mutex here so we have to play games to
8500 * protect against truncate races as the page could now be beyond EOF. Because
8501 * vmtruncate() writes the inode size before removing pages, once we have the
8502 * page lock we can determine safely if the page is beyond EOF. If it is not
8503 * beyond EOF, then the page is guaranteed safe against truncation until we
8504 * unlock the page.
8505 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008506int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008507{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008508 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008509 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008510 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008511 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8512 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008513 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008514 char *kaddr;
8515 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008516 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008517 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008518 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008519 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008520 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008521
Jan Karab2b5ef52012-06-12 16:20:45 +02008522 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04008523 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008524 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008525 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008526 reserved = 1;
8527 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008528 if (ret) {
8529 if (ret == -ENOMEM)
8530 ret = VM_FAULT_OOM;
8531 else /* -ENOSPC, -EIO, etc */
8532 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008533 if (reserved)
8534 goto out;
8535 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008536 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008537
Nick Piggin56a76f82009-03-31 15:23:23 -07008538 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008539again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008540 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008541 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008542 page_start = page_offset(page);
8543 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04008544
Chris Mason9ebefb182007-06-15 13:50:00 -04008545 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008546 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008547 /* page got truncated out from underneath us */
8548 goto out_unlock;
8549 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008550 wait_on_page_writeback(page);
8551
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008552 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008553 set_page_extent_mapped(page);
8554
Chris Masoneb84ae02008-07-17 13:53:27 -04008555 /*
8556 * we can't set the delalloc bits if there are pending ordered
8557 * extents. Drop our locks and wait for them to finish
8558 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008559 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8560 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008561 unlock_extent_cached(io_tree, page_start, page_end,
8562 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008563 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008564 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008565 btrfs_put_ordered_extent(ordered);
8566 goto again;
8567 }
8568
Josef Bacikfbf19082009-10-01 17:10:23 -04008569 /*
8570 * XXX - page_mkwrite gets called every time the page is dirtied, even
8571 * if it was already dirty, so for space accounting reasons we need to
8572 * clear any delalloc bits for the range we are fixing to save. There
8573 * is probably a better way to do this, but for now keep consistent with
8574 * prepare_pages in the normal write path.
8575 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008576 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008577 EXTENT_DIRTY | EXTENT_DELALLOC |
8578 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008579 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008580
Josef Bacik2ac55d42010-02-03 19:33:23 +00008581 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8582 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008583 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008584 unlock_extent_cached(io_tree, page_start, page_end,
8585 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008586 ret = VM_FAULT_SIGBUS;
8587 goto out_unlock;
8588 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008589 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008590
8591 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008592 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008593 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008594 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008595 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008596
Chris Masone6dcd2d2008-07-17 12:53:50 -04008597 if (zero_start != PAGE_CACHE_SIZE) {
8598 kaddr = kmap(page);
8599 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8600 flush_dcache_page(page);
8601 kunmap(page);
8602 }
Chris Mason247e7432008-07-17 12:53:51 -04008603 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008604 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008605 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008606
Chris Mason257c62e2009-10-13 13:21:08 -04008607 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8608 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008609 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008610
Josef Bacik2ac55d42010-02-03 19:33:23 +00008611 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008612
8613out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008614 if (!ret) {
8615 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008616 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008617 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008618 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008619out:
Josef Bacikec39e182012-01-12 19:10:12 -05008620 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008621out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008622 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008623 return ret;
8624}
8625
Josef Bacika41ad392011-01-31 15:30:16 -05008626static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008627{
8628 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008629 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008630 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008631 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008632 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008633 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008634 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008635
Josef Bacik0ef8b722013-10-25 16:13:35 -04008636 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8637 (u64)-1);
8638 if (ret)
8639 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008640
Josef Bacikfcb80c22011-05-03 10:40:22 -04008641 /*
8642 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8643 * 3 things going on here
8644 *
8645 * 1) We need to reserve space for our orphan item and the space to
8646 * delete our orphan item. Lord knows we don't want to have a dangling
8647 * orphan item because we didn't reserve space to remove it.
8648 *
8649 * 2) We need to reserve space to update our inode.
8650 *
8651 * 3) We need to have something to cache all the space that is going to
8652 * be free'd up by the truncate operation, but also have some slack
8653 * space reserved in case it uses space during the truncate (thank you
8654 * very much snapshotting).
8655 *
8656 * And we need these to all be seperate. The fact is we can use alot of
8657 * space doing the truncate, and we have no earthly idea how much space
8658 * we will use, so we need the truncate reservation to be seperate so it
8659 * doesn't end up using space reserved for updating the inode or
8660 * removing the orphan item. We also need to be able to stop the
8661 * transaction and start a new one, which means we need to be able to
8662 * update the inode several times, and we have no idea of knowing how
8663 * many times that will be, so we can't just reserve 1 item for the
8664 * entirety of the opration, so that has to be done seperately as well.
8665 * Then there is the orphan item, which does indeed need to be held on
8666 * to for the whole operation, and we need nobody to touch this reserved
8667 * space except the orphan code.
8668 *
8669 * So that leaves us with
8670 *
8671 * 1) root->orphan_block_rsv - for the orphan deletion.
8672 * 2) rsv - for the truncate reservation, which we will steal from the
8673 * transaction reservation.
8674 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8675 * updating the inode.
8676 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008677 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008678 if (!rsv)
8679 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008680 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008681 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008682
Josef Bacik907cbce2011-08-08 13:46:15 -04008683 /*
Josef Bacik07127182011-08-19 10:29:59 -04008684 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008685 * 1 for updating the inode.
8686 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008687 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008688 if (IS_ERR(trans)) {
8689 err = PTR_ERR(trans);
8690 goto out;
8691 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008692
Josef Bacik907cbce2011-08-08 13:46:15 -04008693 /* Migrate the slack space for the truncate to our reserve */
8694 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8695 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008696 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008697
Chris Mason5a3f23d2009-03-31 13:27:11 -04008698 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008699 * So if we truncate and then write and fsync we normally would just
8700 * write the extents that changed, which is a problem if we need to
8701 * first truncate that entire inode. So set this flag so we write out
8702 * all of the extents in the inode to the sync log so we're completely
8703 * safe.
8704 */
8705 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008706 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008707
Yan, Zheng80825102009-11-12 09:35:36 +00008708 while (1) {
8709 ret = btrfs_truncate_inode_items(trans, root, inode,
8710 inode->i_size,
8711 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08008712 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05008713 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008714 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008715 }
Chris Mason39279cc2007-06-12 06:35:45 -04008716
Josef Bacikfcb80c22011-05-03 10:40:22 -04008717 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008718 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008719 if (ret) {
8720 err = ret;
8721 break;
8722 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008723
Yan, Zheng80825102009-11-12 09:35:36 +00008724 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008725 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008726
8727 trans = btrfs_start_transaction(root, 2);
8728 if (IS_ERR(trans)) {
8729 ret = err = PTR_ERR(trans);
8730 trans = NULL;
8731 break;
8732 }
8733
8734 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8735 rsv, min_size);
8736 BUG_ON(ret); /* shouldn't happen */
8737 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008738 }
8739
8740 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008741 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008742 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008743 if (ret)
8744 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008745 }
8746
Chris Mason917c16b2011-11-08 14:49:59 -05008747 if (trans) {
8748 trans->block_rsv = &root->fs_info->trans_block_rsv;
8749 ret = btrfs_update_inode(trans, root, inode);
8750 if (ret && !err)
8751 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008752
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008753 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008754 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008755 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008756
8757out:
8758 btrfs_free_block_rsv(root, rsv);
8759
Josef Bacik3893e332011-01-31 16:03:11 -05008760 if (ret && !err)
8761 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008762
Josef Bacik3893e332011-01-31 16:03:11 -05008763 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008764}
8765
Sven Wegener3b963622008-06-09 21:57:42 -04008766/*
Chris Masond352ac62008-09-29 15:18:18 -04008767 * create a new subvolume directory/inode (helper for the ioctl).
8768 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008769int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008770 struct btrfs_root *new_root,
8771 struct btrfs_root *parent_root,
8772 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008773{
Chris Mason39279cc2007-06-12 06:35:45 -04008774 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008775 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008776 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008777
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008778 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8779 new_dirid, new_dirid,
8780 S_IFDIR | (~current_umask() & S_IRWXUGO),
8781 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008782 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008783 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008784 inode->i_op = &btrfs_dir_inode_operations;
8785 inode->i_fop = &btrfs_dir_file_operations;
8786
Miklos Szeredibfe86842011-10-28 14:13:29 +02008787 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008788 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008789 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008790
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008791 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8792 if (err)
8793 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008794 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008795 new_root->root_key.objectid, err);
8796
Yan, Zheng76dda932009-09-21 16:00:26 -04008797 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008798
Yan, Zheng76dda932009-09-21 16:00:26 -04008799 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008800 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008801}
8802
Chris Mason39279cc2007-06-12 06:35:45 -04008803struct inode *btrfs_alloc_inode(struct super_block *sb)
8804{
8805 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008806 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008807
8808 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8809 if (!ei)
8810 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008811
8812 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008813 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008814 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008815 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008816 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008817 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008818 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008819 ei->disk_i_size = 0;
8820 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008821 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008822 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008823 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008824 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008825 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008826
Josef Bacik9e0baf62011-07-15 15:16:44 +00008827 spin_lock_init(&ei->lock);
8828 ei->outstanding_extents = 0;
8829 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008830
Josef Bacik72ac3c02012-05-23 14:13:11 -04008831 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008832 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008833
Miao Xie16cdcec2011-04-22 18:12:22 +08008834 ei->delayed_node = NULL;
8835
chandan r9cc97d62012-07-04 12:48:07 +05308836 ei->i_otime.tv_sec = 0;
8837 ei->i_otime.tv_nsec = 0;
8838
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008839 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008840 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008841 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8842 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008843 ei->io_tree.track_uptodate = 1;
8844 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008845 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008846 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008847 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008848 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008849 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008850 RB_CLEAR_NODE(&ei->rb_node);
8851
8852 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008853}
8854
Josef Bacikaaedb552013-10-11 14:44:09 -04008855#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8856void btrfs_test_destroy_inode(struct inode *inode)
8857{
8858 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8859 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8860}
8861#endif
8862
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008863static void btrfs_i_callback(struct rcu_head *head)
8864{
8865 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008866 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8867}
8868
Chris Mason39279cc2007-06-12 06:35:45 -04008869void btrfs_destroy_inode(struct inode *inode)
8870{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008871 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008872 struct btrfs_root *root = BTRFS_I(inode)->root;
8873
Al Virob3d9b7a2012-06-09 13:51:19 -04008874 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008875 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008876 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8877 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008878 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8879 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008880 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008881
Chris Mason5a3f23d2009-03-31 13:27:11 -04008882 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008883 * This can happen where we create an inode, but somebody else also
8884 * created the same inode and we need to destroy the one we already
8885 * created.
8886 */
8887 if (!root)
8888 goto free;
8889
Josef Bacik8a35d952012-05-23 14:26:42 -04008890 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8891 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008892 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008893 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008894 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008895 }
Josef Bacik7b128762008-07-24 12:17:14 -04008896
Chris Masond3977122009-01-05 21:25:51 -05008897 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008898 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8899 if (!ordered)
8900 break;
8901 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008902 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008903 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008904 btrfs_remove_ordered_extent(inode, ordered);
8905 btrfs_put_ordered_extent(ordered);
8906 btrfs_put_ordered_extent(ordered);
8907 }
8908 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008909 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008910 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008911free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008912 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008913}
8914
Al Viro45321ac2010-06-07 13:43:19 -04008915int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008916{
8917 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008918
Naohiro Aota6379ef92013-06-06 09:56:34 +00008919 if (root == NULL)
8920 return 1;
8921
Liu Bofa6ac872013-02-20 14:10:23 +00008922 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008923 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008924 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008925 else
Al Viro45321ac2010-06-07 13:43:19 -04008926 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008927}
8928
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008929static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008930{
8931 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8932
8933 inode_init_once(&ei->vfs_inode);
8934}
8935
8936void btrfs_destroy_cachep(void)
8937{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008938 /*
8939 * Make sure all delayed rcu free inodes are flushed before we
8940 * destroy cache.
8941 */
8942 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008943 if (btrfs_inode_cachep)
8944 kmem_cache_destroy(btrfs_inode_cachep);
8945 if (btrfs_trans_handle_cachep)
8946 kmem_cache_destroy(btrfs_trans_handle_cachep);
8947 if (btrfs_transaction_cachep)
8948 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008949 if (btrfs_path_cachep)
8950 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008951 if (btrfs_free_space_cachep)
8952 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008953 if (btrfs_delalloc_work_cachep)
8954 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008955}
8956
8957int btrfs_init_cachep(void)
8958{
David Sterba837e1972012-09-07 03:00:48 -06008959 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008960 sizeof(struct btrfs_inode), 0,
8961 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008962 if (!btrfs_inode_cachep)
8963 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008964
David Sterba837e1972012-09-07 03:00:48 -06008965 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008966 sizeof(struct btrfs_trans_handle), 0,
8967 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008968 if (!btrfs_trans_handle_cachep)
8969 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008970
David Sterba837e1972012-09-07 03:00:48 -06008971 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008972 sizeof(struct btrfs_transaction), 0,
8973 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008974 if (!btrfs_transaction_cachep)
8975 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008976
David Sterba837e1972012-09-07 03:00:48 -06008977 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008978 sizeof(struct btrfs_path), 0,
8979 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008980 if (!btrfs_path_cachep)
8981 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008982
David Sterba837e1972012-09-07 03:00:48 -06008983 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008984 sizeof(struct btrfs_free_space), 0,
8985 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8986 if (!btrfs_free_space_cachep)
8987 goto fail;
8988
Miao Xie8ccf6f192012-10-25 09:28:04 +00008989 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8990 sizeof(struct btrfs_delalloc_work), 0,
8991 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8992 NULL);
8993 if (!btrfs_delalloc_work_cachep)
8994 goto fail;
8995
Chris Mason39279cc2007-06-12 06:35:45 -04008996 return 0;
8997fail:
8998 btrfs_destroy_cachep();
8999 return -ENOMEM;
9000}
9001
9002static int btrfs_getattr(struct vfsmount *mnt,
9003 struct dentry *dentry, struct kstat *stat)
9004{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009005 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04009006 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05009007 u32 blocksize = inode->i_sb->s_blocksize;
9008
Chris Mason39279cc2007-06-12 06:35:45 -04009009 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009010 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05009011 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009012
9013 spin_lock(&BTRFS_I(inode)->lock);
9014 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9015 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009016 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009017 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009018 return 0;
9019}
9020
Chris Masond3977122009-01-05 21:25:51 -05009021static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9022 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009023{
9024 struct btrfs_trans_handle *trans;
9025 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009026 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04009027 struct inode *new_inode = new_dentry->d_inode;
9028 struct inode *old_inode = old_dentry->d_inode;
9029 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009030 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009031 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009032 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009033 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009034
Li Zefan33345d012011-04-20 10:31:50 +08009035 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009036 return -EPERM;
9037
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009038 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009039 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009040 return -EXDEV;
9041
Li Zefan33345d012011-04-20 10:31:50 +08009042 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9043 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009044 return -ENOTEMPTY;
9045
Chris Mason39279cc2007-06-12 06:35:45 -04009046 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009047 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009048 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009049
9050
9051 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009052 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009053 new_dentry->d_name.name,
9054 new_dentry->d_name.len);
9055
9056 if (ret) {
9057 if (ret == -EEXIST) {
9058 /* we shouldn't get
9059 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309060 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009061 return ret;
9062 }
9063 } else {
9064 /* maybe -EOVERFLOW */
9065 return ret;
9066 }
9067 }
9068 ret = 0;
9069
Chris Mason5a3f23d2009-03-31 13:27:11 -04009070 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009071 * we're using rename to replace one file with another. Start IO on it
9072 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009073 */
Chris Mason8d875f92014-08-12 10:47:42 -07009074 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009075 filemap_flush(old_inode->i_mapping);
9076
Yan, Zheng76dda932009-09-21 16:00:26 -04009077 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009078 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009079 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009080 /*
9081 * We want to reserve the absolute worst case amount of items. So if
9082 * both inodes are subvols and we need to unlink them then that would
9083 * require 4 item modifications, but if they are both normal inodes it
9084 * would require 5 item modifications, so we'll assume their normal
9085 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9086 * should cover the worst case number of items we'll modify.
9087 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04009088 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009089 if (IS_ERR(trans)) {
9090 ret = PTR_ERR(trans);
9091 goto out_notrans;
9092 }
Chris Mason5f39d392007-10-15 16:14:19 -04009093
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009094 if (dest != root)
9095 btrfs_record_root_in_trans(trans, dest);
9096
Yan, Zhenga5719522009-09-24 09:17:31 -04009097 ret = btrfs_set_inode_index(new_dir, &index);
9098 if (ret)
9099 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009100
Miao Xie67de1172013-12-26 13:07:06 +08009101 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009102 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009103 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009104 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009105 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009106 ret = btrfs_insert_inode_ref(trans, dest,
9107 new_dentry->d_name.name,
9108 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009109 old_ino,
9110 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009111 if (ret)
9112 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009113 /*
9114 * this is an ugly little race, but the rename is required
9115 * to make sure that if we crash, the inode is either at the
9116 * old name or the new one. pinning the log transaction lets
9117 * us make sure we don't allow a log commit to come in after
9118 * we unlink the name but before we add the new name back in.
9119 */
9120 btrfs_pin_log_trans(root);
9121 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009122
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009123 inode_inc_iversion(old_dir);
9124 inode_inc_iversion(new_dir);
9125 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009126 old_dir->i_ctime = old_dir->i_mtime = ctime;
9127 new_dir->i_ctime = new_dir->i_mtime = ctime;
9128 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04009129
Chris Mason12fcfd22009-03-24 10:24:20 -04009130 if (old_dentry->d_parent != new_dentry->d_parent)
9131 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9132
Li Zefan33345d012011-04-20 10:31:50 +08009133 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009134 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9135 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9136 old_dentry->d_name.name,
9137 old_dentry->d_name.len);
9138 } else {
Al Viro92986792011-03-04 17:14:37 +00009139 ret = __btrfs_unlink_inode(trans, root, old_dir,
9140 old_dentry->d_inode,
9141 old_dentry->d_name.name,
9142 old_dentry->d_name.len);
9143 if (!ret)
9144 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009145 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009146 if (ret) {
9147 btrfs_abort_transaction(trans, root, ret);
9148 goto out_fail;
9149 }
Chris Mason39279cc2007-06-12 06:35:45 -04009150
9151 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009152 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009153 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08009154 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009155 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9156 root_objectid = BTRFS_I(new_inode)->location.objectid;
9157 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9158 root_objectid,
9159 new_dentry->d_name.name,
9160 new_dentry->d_name.len);
9161 BUG_ON(new_inode->i_nlink == 0);
9162 } else {
9163 ret = btrfs_unlink_inode(trans, dest, new_dir,
9164 new_dentry->d_inode,
9165 new_dentry->d_name.name,
9166 new_dentry->d_name.len);
9167 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009168 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04009169 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009170 if (ret) {
9171 btrfs_abort_transaction(trans, root, ret);
9172 goto out_fail;
9173 }
Chris Mason39279cc2007-06-12 06:35:45 -04009174 }
Josef Bacikaec74772008-07-24 12:12:38 -04009175
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009176 ret = btrfs_add_link(trans, new_dir, old_inode,
9177 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009178 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009179 if (ret) {
9180 btrfs_abort_transaction(trans, root, ret);
9181 goto out_fail;
9182 }
Chris Mason39279cc2007-06-12 06:35:45 -04009183
Miao Xie67de1172013-12-26 13:07:06 +08009184 if (old_inode->i_nlink == 1)
9185 BTRFS_I(old_inode)->dir_index = index;
9186
Li Zefan33345d012011-04-20 10:31:50 +08009187 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04009188 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00009189 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009190 btrfs_end_log_trans(root);
9191 }
Chris Mason39279cc2007-06-12 06:35:45 -04009192out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009193 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009194out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009195 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009196 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009197
Chris Mason39279cc2007-06-12 06:35:45 -04009198 return ret;
9199}
9200
Miklos Szeredi80ace852014-07-23 15:15:32 +02009201static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9202 struct inode *new_dir, struct dentry *new_dentry,
9203 unsigned int flags)
9204{
9205 if (flags & ~RENAME_NOREPLACE)
9206 return -EINVAL;
9207
9208 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
9209}
9210
Miao Xie8ccf6f192012-10-25 09:28:04 +00009211static void btrfs_run_delalloc_work(struct btrfs_work *work)
9212{
9213 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009214 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009215
9216 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9217 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009218 inode = delalloc_work->inode;
9219 if (delalloc_work->wait) {
9220 btrfs_wait_ordered_range(inode, 0, (u64)-1);
9221 } else {
9222 filemap_flush(inode->i_mapping);
9223 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9224 &BTRFS_I(inode)->runtime_flags))
9225 filemap_flush(inode->i_mapping);
9226 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00009227
9228 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009229 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009230 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009231 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009232 complete(&delalloc_work->completion);
9233}
9234
9235struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
9236 int wait, int delay_iput)
9237{
9238 struct btrfs_delalloc_work *work;
9239
9240 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
9241 if (!work)
9242 return NULL;
9243
9244 init_completion(&work->completion);
9245 INIT_LIST_HEAD(&work->list);
9246 work->inode = inode;
9247 work->wait = wait;
9248 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009249 WARN_ON_ONCE(!inode);
9250 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9251 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009252
9253 return work;
9254}
9255
9256void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9257{
9258 wait_for_completion(&work->completion);
9259 kmem_cache_free(btrfs_delalloc_work_cachep, work);
9260}
9261
Chris Masond352ac62008-09-29 15:18:18 -04009262/*
9263 * some fairly slow code that needs optimization. This walks the list
9264 * of all the inodes with pending delalloc and forces them to disk.
9265 */
Miao Xie6c255e62014-03-06 13:55:01 +08009266static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9267 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009268{
Chris Masonea8c2812008-08-04 23:17:27 -04009269 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009270 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009271 struct btrfs_delalloc_work *work, *next;
9272 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009273 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009274 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009275
Miao Xie8ccf6f192012-10-25 09:28:04 +00009276 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009277 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009278
Miao Xie573bfb72014-03-06 13:55:03 +08009279 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009280 spin_lock(&root->delalloc_lock);
9281 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009282 while (!list_empty(&splice)) {
9283 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009284 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009285
Miao Xieeb73c1b2013-05-15 07:48:22 +00009286 list_move_tail(&binode->delalloc_inodes,
9287 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009288 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009289 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009290 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009291 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009292 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009293 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009294
9295 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009296 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009297 if (delay_iput)
9298 btrfs_add_delayed_iput(inode);
9299 else
9300 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009301 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009302 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009303 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009304 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009305 btrfs_queue_work(root->fs_info->flush_workers,
9306 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009307 ret++;
9308 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009309 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009310 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009311 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009312 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009313 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009314
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009315out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009316 list_for_each_entry_safe(work, next, &works, list) {
9317 list_del_init(&work->list);
9318 btrfs_wait_and_free_delalloc_work(work);
9319 }
9320
Miao Xieeb73c1b2013-05-15 07:48:22 +00009321 if (!list_empty_careful(&splice)) {
9322 spin_lock(&root->delalloc_lock);
9323 list_splice_tail(&splice, &root->delalloc_inodes);
9324 spin_unlock(&root->delalloc_lock);
9325 }
Miao Xie573bfb72014-03-06 13:55:03 +08009326 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009327 return ret;
9328}
9329
9330int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9331{
9332 int ret;
9333
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009334 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009335 return -EROFS;
9336
Miao Xie6c255e62014-03-06 13:55:01 +08009337 ret = __start_delalloc_inodes(root, delay_iput, -1);
9338 if (ret > 0)
9339 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009340 /*
9341 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009342 * we have to make sure the IO is actually started and that
9343 * ordered extents get created before we return
9344 */
9345 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009346 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009347 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009348 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009349 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9350 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009351 }
9352 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009353 return ret;
9354}
9355
Miao Xie6c255e62014-03-06 13:55:01 +08009356int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9357 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009358{
9359 struct btrfs_root *root;
9360 struct list_head splice;
9361 int ret;
9362
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009363 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009364 return -EROFS;
9365
9366 INIT_LIST_HEAD(&splice);
9367
Miao Xie573bfb72014-03-06 13:55:03 +08009368 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009369 spin_lock(&fs_info->delalloc_root_lock);
9370 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009371 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009372 root = list_first_entry(&splice, struct btrfs_root,
9373 delalloc_root);
9374 root = btrfs_grab_fs_root(root);
9375 BUG_ON(!root);
9376 list_move_tail(&root->delalloc_root,
9377 &fs_info->delalloc_roots);
9378 spin_unlock(&fs_info->delalloc_root_lock);
9379
Miao Xie6c255e62014-03-06 13:55:01 +08009380 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009381 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009382 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009383 goto out;
9384
Miao Xie6c255e62014-03-06 13:55:01 +08009385 if (nr != -1) {
9386 nr -= ret;
9387 WARN_ON(nr < 0);
9388 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009389 spin_lock(&fs_info->delalloc_root_lock);
9390 }
9391 spin_unlock(&fs_info->delalloc_root_lock);
9392
Miao Xie6c255e62014-03-06 13:55:01 +08009393 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009394 atomic_inc(&fs_info->async_submit_draining);
9395 while (atomic_read(&fs_info->nr_async_submits) ||
9396 atomic_read(&fs_info->async_delalloc_pages)) {
9397 wait_event(fs_info->async_submit_wait,
9398 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9399 atomic_read(&fs_info->async_delalloc_pages) == 0));
9400 }
9401 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009402out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009403 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009404 spin_lock(&fs_info->delalloc_root_lock);
9405 list_splice_tail(&splice, &fs_info->delalloc_roots);
9406 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009407 }
Miao Xie573bfb72014-03-06 13:55:03 +08009408 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009409 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009410}
9411
Chris Mason39279cc2007-06-12 06:35:45 -04009412static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9413 const char *symname)
9414{
9415 struct btrfs_trans_handle *trans;
9416 struct btrfs_root *root = BTRFS_I(dir)->root;
9417 struct btrfs_path *path;
9418 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009419 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009420 int err;
9421 int drop_inode = 0;
9422 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309423 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009424 int name_len;
9425 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009426 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009427 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009428 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009429
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009430 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009431 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9432 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009433
Josef Bacik9ed74f22009-09-11 16:12:44 -04009434 /*
9435 * 2 items for inode item and ref
9436 * 2 items for dir items
9437 * 1 item for xattr if selinux is on
9438 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009439 trans = btrfs_start_transaction(root, 5);
9440 if (IS_ERR(trans))
9441 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009442
Li Zefan581bb052011-04-20 10:06:11 +08009443 err = btrfs_find_free_ino(root, &objectid);
9444 if (err)
9445 goto out_unlock;
9446
Josef Bacikaec74772008-07-24 12:12:38 -04009447 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009448 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04009449 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009450 if (IS_ERR(inode)) {
9451 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009452 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009453 }
Chris Mason39279cc2007-06-12 06:35:45 -04009454
Casey Schauflerad19db72011-12-15 10:09:07 -05009455 /*
9456 * If the active LSM wants to access the inode during
9457 * d_instantiate it needs these. Smack checks to see
9458 * if the filesystem supports xattrs by looking at the
9459 * ops vector.
9460 */
9461 inode->i_fop = &btrfs_file_operations;
9462 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009463 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009464 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9465
9466 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9467 if (err)
9468 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009469
Josef Bacika1b075d2010-11-19 20:36:11 +00009470 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009471 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009472 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009473
9474 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009475 if (!path) {
9476 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009477 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009478 }
Li Zefan33345d012011-04-20 10:31:50 +08009479 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009480 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009481 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009482 datasize = btrfs_file_extent_calc_inline_size(name_len);
9483 err = btrfs_insert_empty_item(trans, root, path, &key,
9484 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009485 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009486 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009487 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009488 }
Chris Mason5f39d392007-10-15 16:14:19 -04009489 leaf = path->nodes[0];
9490 ei = btrfs_item_ptr(leaf, path->slots[0],
9491 struct btrfs_file_extent_item);
9492 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9493 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009494 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009495 btrfs_set_file_extent_encryption(leaf, ei, 0);
9496 btrfs_set_file_extent_compression(leaf, ei, 0);
9497 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9498 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9499
Chris Mason39279cc2007-06-12 06:35:45 -04009500 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009501 write_extent_buffer(leaf, symname, ptr, name_len);
9502 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009503 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009504
Chris Mason39279cc2007-06-12 06:35:45 -04009505 inode->i_op = &btrfs_symlink_inode_operations;
9506 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -04009507 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009508 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009509 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009510 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009511 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009512 goto out_unlock_inode;
9513 }
9514
9515 unlock_new_inode(inode);
9516 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009517
9518out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009519 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009520 if (drop_inode) {
9521 inode_dec_link_count(inode);
9522 iput(inode);
9523 }
Liu Bob53d3f52012-11-14 14:34:34 +00009524 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009525 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009526
9527out_unlock_inode:
9528 drop_inode = 1;
9529 unlock_new_inode(inode);
9530 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009531}
Chris Mason16432982008-04-10 10:23:21 -04009532
Josef Bacik0af3d002010-06-21 14:48:16 -04009533static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9534 u64 start, u64 num_bytes, u64 min_size,
9535 loff_t actual_len, u64 *alloc_hint,
9536 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009537{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009538 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9539 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009540 struct btrfs_root *root = BTRFS_I(inode)->root;
9541 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009542 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009543 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009544 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009545 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009546 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009547
Josef Bacik0af3d002010-06-21 14:48:16 -04009548 if (trans)
9549 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009550 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009551 if (own_trans) {
9552 trans = btrfs_start_transaction(root, 3);
9553 if (IS_ERR(trans)) {
9554 ret = PTR_ERR(trans);
9555 break;
9556 }
Yan Zhengd899e052008-10-30 14:25:28 -04009557 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009558
Chris Mason154ea282013-03-05 11:11:26 -05009559 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9560 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009561 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009562 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009563 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009564 if (own_trans)
9565 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009566 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009567 }
9568
Yan Zhengd899e052008-10-30 14:25:28 -04009569 ret = insert_reserved_file_extent(trans, inode,
9570 cur_offset, ins.objectid,
9571 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009572 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009573 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009574 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009575 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009576 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009577 btrfs_abort_transaction(trans, root, ret);
9578 if (own_trans)
9579 btrfs_end_transaction(trans, root);
9580 break;
9581 }
Chris Masona1ed8352009-09-11 12:27:37 -04009582 btrfs_drop_extent_cache(inode, cur_offset,
9583 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009584
Josef Bacik5dc562c2012-08-17 13:14:17 -04009585 em = alloc_extent_map();
9586 if (!em) {
9587 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9588 &BTRFS_I(inode)->runtime_flags);
9589 goto next;
9590 }
9591
9592 em->start = cur_offset;
9593 em->orig_start = cur_offset;
9594 em->len = ins.offset;
9595 em->block_start = ins.objectid;
9596 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009597 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009598 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009599 em->bdev = root->fs_info->fs_devices->latest_bdev;
9600 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9601 em->generation = trans->transid;
9602
9603 while (1) {
9604 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009605 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009606 write_unlock(&em_tree->lock);
9607 if (ret != -EEXIST)
9608 break;
9609 btrfs_drop_extent_cache(inode, cur_offset,
9610 cur_offset + ins.offset - 1,
9611 0);
9612 }
9613 free_extent_map(em);
9614next:
Yan Zhengd899e052008-10-30 14:25:28 -04009615 num_bytes -= ins.offset;
9616 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009617 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009618
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009619 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009620 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009621 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009622 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009623 (actual_len > inode->i_size) &&
9624 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009625 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009626 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009627 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009628 i_size = cur_offset;
9629 i_size_write(inode, i_size);
9630 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009631 }
9632
Yan Zhengd899e052008-10-30 14:25:28 -04009633 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009634
9635 if (ret) {
9636 btrfs_abort_transaction(trans, root, ret);
9637 if (own_trans)
9638 btrfs_end_transaction(trans, root);
9639 break;
9640 }
Yan Zhengd899e052008-10-30 14:25:28 -04009641
Josef Bacik0af3d002010-06-21 14:48:16 -04009642 if (own_trans)
9643 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009644 }
Yan Zhengd899e052008-10-30 14:25:28 -04009645 return ret;
9646}
9647
Josef Bacik0af3d002010-06-21 14:48:16 -04009648int btrfs_prealloc_file_range(struct inode *inode, int mode,
9649 u64 start, u64 num_bytes, u64 min_size,
9650 loff_t actual_len, u64 *alloc_hint)
9651{
9652 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9653 min_size, actual_len, alloc_hint,
9654 NULL);
9655}
9656
9657int btrfs_prealloc_file_range_trans(struct inode *inode,
9658 struct btrfs_trans_handle *trans, int mode,
9659 u64 start, u64 num_bytes, u64 min_size,
9660 loff_t actual_len, u64 *alloc_hint)
9661{
9662 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9663 min_size, actual_len, alloc_hint, trans);
9664}
9665
Chris Masone6dcd2d2008-07-17 12:53:50 -04009666static int btrfs_set_page_dirty(struct page *page)
9667{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009668 return __set_page_dirty_nobuffers(page);
9669}
9670
Al Viro10556cb2011-06-20 19:28:19 -04009671static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009672{
Li Zefanb83cc962010-12-20 16:04:08 +08009673 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009674 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009675
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009676 if (mask & MAY_WRITE &&
9677 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9678 if (btrfs_root_readonly(root))
9679 return -EROFS;
9680 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9681 return -EACCES;
9682 }
Al Viro2830ba72011-06-20 19:16:29 -04009683 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009684}
Chris Mason39279cc2007-06-12 06:35:45 -04009685
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009686static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9687{
9688 struct btrfs_trans_handle *trans;
9689 struct btrfs_root *root = BTRFS_I(dir)->root;
9690 struct inode *inode = NULL;
9691 u64 objectid;
9692 u64 index;
9693 int ret = 0;
9694
9695 /*
9696 * 5 units required for adding orphan entry
9697 */
9698 trans = btrfs_start_transaction(root, 5);
9699 if (IS_ERR(trans))
9700 return PTR_ERR(trans);
9701
9702 ret = btrfs_find_free_ino(root, &objectid);
9703 if (ret)
9704 goto out;
9705
9706 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9707 btrfs_ino(dir), objectid, mode, &index);
9708 if (IS_ERR(inode)) {
9709 ret = PTR_ERR(inode);
9710 inode = NULL;
9711 goto out;
9712 }
9713
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009714 inode->i_fop = &btrfs_file_operations;
9715 inode->i_op = &btrfs_file_inode_operations;
9716
9717 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009718 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9719
Chris Masonb0d5d102014-09-08 13:08:51 -07009720 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9721 if (ret)
9722 goto out_inode;
9723
9724 ret = btrfs_update_inode(trans, root, inode);
9725 if (ret)
9726 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009727 ret = btrfs_orphan_add(trans, inode);
9728 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009729 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009730
Filipe Manana5762b5c2014-08-01 00:10:32 +01009731 /*
9732 * We set number of links to 0 in btrfs_new_inode(), and here we set
9733 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9734 * through:
9735 *
9736 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9737 */
9738 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009739 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009740 d_tmpfile(dentry, inode);
9741 mark_inode_dirty(inode);
9742
9743out:
9744 btrfs_end_transaction(trans, root);
9745 if (ret)
9746 iput(inode);
9747 btrfs_balance_delayed_items(root);
9748 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009749 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009750
9751out_inode:
9752 unlock_new_inode(inode);
9753 goto out;
9754
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009755}
9756
Filipe Mananab38ef712014-11-13 17:01:45 +00009757/* Inspired by filemap_check_errors() */
9758int btrfs_inode_check_errors(struct inode *inode)
9759{
9760 int ret = 0;
9761
9762 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
9763 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
9764 ret = -ENOSPC;
9765 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
9766 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
9767 ret = -EIO;
9768
9769 return ret;
9770}
9771
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009772static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009773 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009774 .lookup = btrfs_lookup,
9775 .create = btrfs_create,
9776 .unlink = btrfs_unlink,
9777 .link = btrfs_link,
9778 .mkdir = btrfs_mkdir,
9779 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009780 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009781 .symlink = btrfs_symlink,
9782 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009783 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009784 .setxattr = btrfs_setxattr,
9785 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009786 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009787 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009788 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009789 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009790 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009791 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009792 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009793};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009794static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009795 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009796 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009797 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009798 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009799 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009800};
Yan, Zheng76dda932009-09-21 16:00:26 -04009801
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009802static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009803 .llseek = generic_file_llseek,
9804 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009805 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009806 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009807#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009808 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009809#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009810 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009811 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009812};
9813
Chris Masond1310b22008-01-24 16:13:08 -05009814static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009815 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009816 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009817 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009818 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009819 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009820 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009821 .set_bit_hook = btrfs_set_bit_hook,
9822 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009823 .merge_extent_hook = btrfs_merge_extent_hook,
9824 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009825};
9826
Chris Mason35054392009-01-21 13:11:13 -05009827/*
9828 * btrfs doesn't support the bmap operation because swapfiles
9829 * use bmap to make a mapping of extents in the file. They assume
9830 * these extents won't change over the life of the file and they
9831 * use the bmap result to do IO directly to the drive.
9832 *
9833 * the btrfs bmap call would return logical addresses that aren't
9834 * suitable for IO and they also will change frequently as COW
9835 * operations happen. So, swapfile + btrfs == corruption.
9836 *
9837 * For now we're avoiding this by dropping bmap.
9838 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009839static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009840 .readpage = btrfs_readpage,
9841 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04009842 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009843 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009844 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009845 .invalidatepage = btrfs_invalidatepage,
9846 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009847 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009848 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009849};
9850
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009851static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009852 .readpage = btrfs_readpage,
9853 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009854 .invalidatepage = btrfs_invalidatepage,
9855 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009856};
9857
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009858static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009859 .getattr = btrfs_getattr,
9860 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009861 .setxattr = btrfs_setxattr,
9862 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009863 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009864 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009865 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009866 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009867 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009868 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009869 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009870};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009871static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009872 .getattr = btrfs_getattr,
9873 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009874 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009875 .setxattr = btrfs_setxattr,
9876 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009877 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009878 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009879 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009880 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009881 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009882};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009883static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009884 .readlink = generic_readlink,
9885 .follow_link = page_follow_link_light,
9886 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009887 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009888 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009889 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009890 .setxattr = btrfs_setxattr,
9891 .getxattr = btrfs_getxattr,
9892 .listxattr = btrfs_listxattr,
9893 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009894 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009895};
Yan, Zheng76dda932009-09-21 16:00:26 -04009896
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009897const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009898 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009899 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009900};