blob: 018c2a6108f2a1a44b46ace3ba28669af94e8d3f [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050043#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040044#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080045#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080062#include "qgroup.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Filipe Mananaf28a4922015-12-08 19:23:20 +000069struct btrfs_dio_data {
70 u64 outstanding_extents;
71 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
74};
75
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070076static const struct inode_operations btrfs_dir_inode_operations;
77static const struct inode_operations btrfs_symlink_inode_operations;
78static const struct inode_operations btrfs_dir_ro_inode_operations;
79static const struct inode_operations btrfs_special_inode_operations;
80static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070081static const struct address_space_operations btrfs_aops;
82static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070083static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050084static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040085
86static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000087static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040088struct kmem_cache *btrfs_trans_handle_cachep;
89struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040090struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050091struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040092
93#define S_SHIFT 12
94static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
95 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
102};
103
Eric Sandeen3972f262013-01-12 02:57:22 +0000104static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500105static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400106static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500107static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
109 u64 start, u64 end, int *page_started,
110 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400111static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
112 u64 len, u64 orig_start,
113 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400114 u64 orig_block_len, u64 ram_bytes,
115 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400116
Eric Sandeen48a3b632013-04-25 20:41:01 +0000117static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400118
Josef Bacik6a3891c2015-03-16 17:38:52 -0400119#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
120void btrfs_test_inode_set_ops(struct inode *inode)
121{
122 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
123}
124#endif
125
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000126static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500127 struct inode *inode, struct inode *dir,
128 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500129{
130 int err;
131
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000132 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500133 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500134 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500135 return err;
136}
137
Chris Masond352ac62008-09-29 15:18:18 -0400138/*
Chris Masonc8b97812008-10-29 14:49:59 -0400139 * this does all the hard work for inserting an inline extent into
140 * the btree. The caller should have done a btrfs_drop_extents so that
141 * no overlapping inline items exist in the btree
142 */
Chris Mason40f76582014-05-21 13:35:51 -0700143static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000144 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400145 struct btrfs_root *root, struct inode *inode,
146 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000147 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400148 struct page **compressed_pages)
149{
Chris Masonc8b97812008-10-29 14:49:59 -0400150 struct extent_buffer *leaf;
151 struct page *page = NULL;
152 char *kaddr;
153 unsigned long ptr;
154 struct btrfs_file_extent_item *ei;
155 int err = 0;
156 int ret;
157 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400158 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400159
Li Zefanfe3f5662011-03-28 08:30:38 +0000160 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400161 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400162
Chris Masonc8b97812008-10-29 14:49:59 -0400163 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000164
165 if (!extent_inserted) {
166 struct btrfs_key key;
167 size_t datasize;
168
169 key.objectid = btrfs_ino(inode);
170 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200171 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000172
173 datasize = btrfs_file_extent_calc_inline_size(cur_size);
174 path->leave_spinning = 1;
175 ret = btrfs_insert_empty_item(trans, root, path, &key,
176 datasize);
177 if (ret) {
178 err = ret;
179 goto fail;
180 }
Chris Masonc8b97812008-10-29 14:49:59 -0400181 }
182 leaf = path->nodes[0];
183 ei = btrfs_item_ptr(leaf, path->slots[0],
184 struct btrfs_file_extent_item);
185 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
186 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
187 btrfs_set_file_extent_encryption(leaf, ei, 0);
188 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
189 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
190 ptr = btrfs_file_extent_inline_start(ei);
191
Li Zefan261507a02010-12-17 14:21:50 +0800192 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400193 struct page *cpage;
194 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500195 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400196 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500197 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400198 PAGE_CACHE_SIZE);
199
Cong Wang7ac687d2011-11-25 23:14:28 +0800200 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400201 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800202 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400203
204 i++;
205 ptr += cur_size;
206 compressed_size -= cur_size;
207 }
208 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800209 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400210 } else {
211 page = find_get_page(inode->i_mapping,
212 start >> PAGE_CACHE_SHIFT);
213 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800214 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400215 offset = start & (PAGE_CACHE_SIZE - 1);
216 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800217 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400218 page_cache_release(page);
219 }
220 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000221 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400222
Yan, Zhengc2167752009-11-12 09:34:21 +0000223 /*
224 * we're an inline extent, so nobody can
225 * extend the file past i_size without locking
226 * a page we already have locked.
227 *
228 * We must do any isize and inode updates
229 * before we unlock the pages. Otherwise we
230 * could end up racing with unlink.
231 */
Chris Masonc8b97812008-10-29 14:49:59 -0400232 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100233 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000234
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100235 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400236fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400237 return err;
238}
239
240
241/*
242 * conditionally insert an inline extent into the file. This
243 * does the checks required to make sure the data is small enough
244 * to fit as an inline extent.
245 */
Josef Bacik00361582013-08-14 14:02:47 -0400246static noinline int cow_file_range_inline(struct btrfs_root *root,
247 struct inode *inode, u64 start,
248 u64 end, size_t compressed_size,
249 int compress_type,
250 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400251{
Josef Bacik00361582013-08-14 14:02:47 -0400252 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400253 u64 isize = i_size_read(inode);
254 u64 actual_end = min(end + 1, isize);
255 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000256 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400257 u64 data_len = inline_len;
258 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000259 struct btrfs_path *path;
260 int extent_inserted = 0;
261 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400262
263 if (compressed_size)
264 data_len = compressed_size;
265
266 if (start > 0 ||
Wang Shilong354877b2014-07-17 11:44:11 +0800267 actual_end > PAGE_CACHE_SIZE ||
268 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400269 (!compressed_size &&
270 (actual_end & (root->sectorsize - 1)) == 0) ||
271 end + 1 < isize ||
272 data_len > root->fs_info->max_inline) {
273 return 1;
274 }
275
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000276 path = btrfs_alloc_path();
277 if (!path)
278 return -ENOMEM;
279
Josef Bacik00361582013-08-14 14:02:47 -0400280 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000281 if (IS_ERR(trans)) {
282 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400283 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000284 }
Josef Bacik00361582013-08-14 14:02:47 -0400285 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
286
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000287 if (compressed_size && compressed_pages)
288 extent_item_size = btrfs_file_extent_calc_inline_size(
289 compressed_size);
290 else
291 extent_item_size = btrfs_file_extent_calc_inline_size(
292 inline_len);
293
294 ret = __btrfs_drop_extents(trans, root, inode, path,
295 start, aligned_end, NULL,
296 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400297 if (ret) {
298 btrfs_abort_transaction(trans, root, ret);
299 goto out;
300 }
Chris Masonc8b97812008-10-29 14:49:59 -0400301
302 if (isize > actual_end)
303 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000304 ret = insert_inline_extent(trans, path, extent_inserted,
305 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400306 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000307 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400308 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100309 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400310 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400311 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400312 ret = 1;
313 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100314 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400315
Josef Bacikbdc20e62013-02-28 13:23:38 -0500316 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400317 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400318 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400319out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800320 /*
321 * Don't forget to free the reserved space, as for inlined extent
322 * it won't count as data extent, free them directly here.
323 * And at reserve time, it's always aligned to page size, so
324 * just free one page here.
325 */
326 btrfs_qgroup_free_data(inode, 0, PAGE_CACHE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000327 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400328 btrfs_end_transaction(trans, root);
329 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400330}
331
Chris Mason771ed682008-11-06 22:02:51 -0500332struct async_extent {
333 u64 start;
334 u64 ram_size;
335 u64 compressed_size;
336 struct page **pages;
337 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800338 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500339 struct list_head list;
340};
341
342struct async_cow {
343 struct inode *inode;
344 struct btrfs_root *root;
345 struct page *locked_page;
346 u64 start;
347 u64 end;
348 struct list_head extents;
349 struct btrfs_work work;
350};
351
352static noinline int add_async_extent(struct async_cow *cow,
353 u64 start, u64 ram_size,
354 u64 compressed_size,
355 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800356 unsigned long nr_pages,
357 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500358{
359 struct async_extent *async_extent;
360
361 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100362 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500363 async_extent->start = start;
364 async_extent->ram_size = ram_size;
365 async_extent->compressed_size = compressed_size;
366 async_extent->pages = pages;
367 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800368 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500369 list_add_tail(&async_extent->list, &cow->extents);
370 return 0;
371}
372
Wang Shilongf79707b2014-07-17 11:44:09 +0800373static inline int inode_need_compress(struct inode *inode)
374{
375 struct btrfs_root *root = BTRFS_I(inode)->root;
376
377 /* force compress */
378 if (btrfs_test_opt(root, FORCE_COMPRESS))
379 return 1;
380 /* bad compression ratios */
381 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
382 return 0;
383 if (btrfs_test_opt(root, COMPRESS) ||
384 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
385 BTRFS_I(inode)->force_compress)
386 return 1;
387 return 0;
388}
389
Chris Masonc8b97812008-10-29 14:49:59 -0400390/*
Chris Mason771ed682008-11-06 22:02:51 -0500391 * we create compressed extents in two phases. The first
392 * phase compresses a range of pages that have already been
393 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400394 *
Chris Mason771ed682008-11-06 22:02:51 -0500395 * This is done inside an ordered work queue, and the compression
396 * is spread across many cpus. The actual IO submission is step
397 * two, and the ordered work queue takes care of making sure that
398 * happens in the same order things were put onto the queue by
399 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400400 *
Chris Mason771ed682008-11-06 22:02:51 -0500401 * If this code finds it can't get good compression, it puts an
402 * entry onto the work queue to write the uncompressed bytes. This
403 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300404 * are written in the same order that the flusher thread sent them
405 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400406 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100407static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500408 struct page *locked_page,
409 u64 start, u64 end,
410 struct async_cow *async_cow,
411 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400412{
413 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400414 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400415 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400416 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500417 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400418 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400419 struct page **pages = NULL;
420 unsigned long nr_pages;
421 unsigned long nr_pages_ret = 0;
422 unsigned long total_compressed = 0;
423 unsigned long total_in = 0;
424 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500425 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400426 int i;
427 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800428 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400429 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400430
Liu Bo4cb13e52012-03-29 09:57:45 -0400431 /* if this is a small write inside eof, kick off a defrag */
432 if ((end - start + 1) < 16 * 1024 &&
433 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400434 btrfs_add_inode_defrag(NULL, inode);
435
Chris Mason42dc7ba2008-12-15 11:44:56 -0500436 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400437again:
438 will_compress = 0;
439 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
440 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
441
Chris Masonf03d9302009-02-04 09:31:06 -0500442 /*
443 * we don't want to send crud past the end of i_size through
444 * compression, that's just a waste of CPU time. So, if the
445 * end of the file is before the start of our current
446 * requested range of bytes, we bail out to the uncompressed
447 * cleanup code that can deal with all of this.
448 *
449 * It isn't really the fastest way to fix things, but this is a
450 * very uncommon corner.
451 */
452 if (actual_end <= start)
453 goto cleanup_and_bail_uncompressed;
454
Chris Masonc8b97812008-10-29 14:49:59 -0400455 total_compressed = actual_end - start;
456
Shilong Wang4bcbb332014-10-07 18:44:35 -0400457 /*
458 * skip compression for a small file range(<=blocksize) that
459 * isn't an inline extent, since it dosen't save disk space at all.
460 */
461 if (total_compressed <= blocksize &&
462 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
463 goto cleanup_and_bail_uncompressed;
464
Chris Masonc8b97812008-10-29 14:49:59 -0400465 /* we want to make sure that amount of ram required to uncompress
466 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500467 * of a compressed extent to 128k. This is a crucial number
468 * because it also controls how easily we can spread reads across
469 * cpus for decompression.
470 *
471 * We also want to make sure the amount of IO required to do
472 * a random read is reasonably small, so we limit the size of
473 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400474 */
475 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000476 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500477 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400478 total_in = 0;
479 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400480
Chris Mason771ed682008-11-06 22:02:51 -0500481 /*
482 * we do compression for mount -o compress and when the
483 * inode has not been flagged as nocompress. This flag can
484 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400485 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800486 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400487 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100488 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800489 if (!pages) {
490 /* just bail out to the uncompressed code */
491 goto cont;
492 }
Chris Mason179e29e2007-11-01 11:28:41 -0400493
Li Zefan261507a02010-12-17 14:21:50 +0800494 if (BTRFS_I(inode)->force_compress)
495 compress_type = BTRFS_I(inode)->force_compress;
496
Chris Mason4adaa612013-03-26 13:07:00 -0400497 /*
498 * we need to call clear_page_dirty_for_io on each
499 * page in the range. Otherwise applications with the file
500 * mmap'd can wander in and change the page contents while
501 * we are compressing them.
502 *
503 * If the compression fails for any reason, we set the pages
504 * dirty again later on.
505 */
506 extent_range_clear_dirty_for_io(inode, start, end);
507 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800508 ret = btrfs_compress_pages(compress_type,
509 inode->i_mapping, start,
510 total_compressed, pages,
511 nr_pages, &nr_pages_ret,
512 &total_in,
513 &total_compressed,
514 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400515
516 if (!ret) {
517 unsigned long offset = total_compressed &
518 (PAGE_CACHE_SIZE - 1);
519 struct page *page = pages[nr_pages_ret - 1];
520 char *kaddr;
521
522 /* zero the tail end of the last page, we might be
523 * sending it down to disk
524 */
525 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800526 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400527 memset(kaddr + offset, 0,
528 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800529 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400530 }
531 will_compress = 1;
532 }
533 }
Li Zefan560f7d72011-09-08 10:22:01 +0800534cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400535 if (start == 0) {
536 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500537 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400538 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500539 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400540 */
Josef Bacik00361582013-08-14 14:02:47 -0400541 ret = cow_file_range_inline(root, inode, start, end,
542 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400543 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500544 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400545 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000546 total_compressed,
547 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400548 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100549 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400550 unsigned long clear_flags = EXTENT_DELALLOC |
551 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100552 unsigned long page_error_op;
553
Josef Bacik151a41b2013-07-29 13:22:24 -0400554 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100555 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400556
Chris Mason771ed682008-11-06 22:02:51 -0500557 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100558 * inline extent creation worked or returned error,
559 * we don't need to create any more async work items.
560 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500561 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400562 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400563 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400564 PAGE_CLEAR_DIRTY |
565 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100566 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400567 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400568 goto free_pages_out;
569 }
570 }
571
572 if (will_compress) {
573 /*
574 * we aren't doing an inline extent round the compressed size
575 * up to a block size boundary so the allocator does sane
576 * things
577 */
Qu Wenruofda28322013-02-26 08:10:22 +0000578 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400579
580 /*
581 * one last check to make sure the compression is really a
582 * win, compare the page count read with the blocks on disk
583 */
Qu Wenruofda28322013-02-26 08:10:22 +0000584 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400585 if (total_compressed >= total_in) {
586 will_compress = 0;
587 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400588 num_bytes = total_in;
589 }
590 }
591 if (!will_compress && pages) {
592 /*
593 * the compression code ran but failed to make things smaller,
594 * free any pages it allocated and our page pointer array
595 */
596 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400597 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400598 page_cache_release(pages[i]);
599 }
600 kfree(pages);
601 pages = NULL;
602 total_compressed = 0;
603 nr_pages_ret = 0;
604
605 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500606 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
607 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500608 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500609 }
Chris Masonc8b97812008-10-29 14:49:59 -0400610 }
Chris Mason771ed682008-11-06 22:02:51 -0500611 if (will_compress) {
612 *num_added += 1;
613
614 /* the async work queues will take care of doing actual
615 * allocation on disk for these compressed pages,
616 * and will submit them to the elevator.
617 */
618 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800619 total_compressed, pages, nr_pages_ret,
620 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500621
Yan, Zheng24ae6362010-12-06 07:02:36 +0000622 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500623 start += num_bytes;
624 pages = NULL;
625 cond_resched();
626 goto again;
627 }
628 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500629cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500630 /*
631 * No compression, but we still need to write the pages in
632 * the file we've been given so far. redirty the locked
633 * page if it corresponds to our extent and set things up
634 * for the async work queue to run cow_file_range to do
635 * the normal delalloc dance
636 */
637 if (page_offset(locked_page) >= start &&
638 page_offset(locked_page) <= end) {
639 __set_page_dirty_nobuffers(locked_page);
640 /* unlocked later on in the async handlers */
641 }
Chris Mason4adaa612013-03-26 13:07:00 -0400642 if (redirty)
643 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800644 add_async_extent(async_cow, start, end - start + 1,
645 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500646 *num_added += 1;
647 }
648
Filipe Mananac44f6492014-10-09 21:15:44 +0100649 return;
Chris Mason771ed682008-11-06 22:02:51 -0500650
651free_pages_out:
652 for (i = 0; i < nr_pages_ret; i++) {
653 WARN_ON(pages[i]->mapping);
654 page_cache_release(pages[i]);
655 }
Chris Masond3977122009-01-05 21:25:51 -0500656 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500657}
Chris Mason771ed682008-11-06 22:02:51 -0500658
Filipe Manana40ae8372014-10-06 22:14:24 +0100659static void free_async_extent_pages(struct async_extent *async_extent)
660{
661 int i;
662
663 if (!async_extent->pages)
664 return;
665
666 for (i = 0; i < async_extent->nr_pages; i++) {
667 WARN_ON(async_extent->pages[i]->mapping);
668 page_cache_release(async_extent->pages[i]);
669 }
670 kfree(async_extent->pages);
671 async_extent->nr_pages = 0;
672 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500673}
674
675/*
676 * phase two of compressed writeback. This is the ordered portion
677 * of the code, which only gets called in the order the work was
678 * queued. We walk all the async extents created by compress_file_range
679 * and send them down to the disk.
680 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100681static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500682 struct async_cow *async_cow)
683{
684 struct async_extent *async_extent;
685 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500686 struct btrfs_key ins;
687 struct extent_map *em;
688 struct btrfs_root *root = BTRFS_I(inode)->root;
689 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
690 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500691 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500692
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500693again:
Chris Masond3977122009-01-05 21:25:51 -0500694 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500695 async_extent = list_entry(async_cow->extents.next,
696 struct async_extent, list);
697 list_del(&async_extent->list);
698
699 io_tree = &BTRFS_I(inode)->io_tree;
700
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500701retry:
Chris Mason771ed682008-11-06 22:02:51 -0500702 /* did the compression code fall back to uncompressed IO? */
703 if (!async_extent->pages) {
704 int page_started = 0;
705 unsigned long nr_written = 0;
706
707 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000708 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100709 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500710
711 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500712 ret = cow_file_range(inode, async_cow->locked_page,
713 async_extent->start,
714 async_extent->start +
715 async_extent->ram_size - 1,
716 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500717
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100718 /* JDM XXX */
719
Chris Mason771ed682008-11-06 22:02:51 -0500720 /*
721 * if page_started, cow_file_range inserted an
722 * inline extent and took care of all the unlocking
723 * and IO for us. Otherwise, we need to submit
724 * all those pages down to the drive.
725 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500726 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500727 extent_write_locked_range(io_tree,
728 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500729 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500730 async_extent->ram_size - 1,
731 btrfs_get_extent,
732 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500733 else if (ret)
734 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500735 kfree(async_extent);
736 cond_resched();
737 continue;
738 }
739
740 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100741 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500742
Josef Bacik00361582013-08-14 14:02:47 -0400743 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500744 async_extent->compressed_size,
745 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800746 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500747 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100748 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500749
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400750 if (ret == -ENOSPC) {
751 unlock_extent(io_tree, async_extent->start,
752 async_extent->start +
753 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800754
755 /*
756 * we need to redirty the pages if we decide to
757 * fallback to uncompressed IO, otherwise we
758 * will not submit these pages down to lower
759 * layers.
760 */
761 extent_range_redirty_for_io(inode,
762 async_extent->start,
763 async_extent->start +
764 async_extent->ram_size - 1);
765
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100766 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400767 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500768 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500769 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000770 /*
771 * here we're doing allocation and writeback of the
772 * compressed pages
773 */
774 btrfs_drop_extent_cache(inode, async_extent->start,
775 async_extent->start +
776 async_extent->ram_size - 1, 0);
777
David Sterba172ddd62011-04-21 00:48:27 +0200778 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000779 if (!em) {
780 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500781 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000782 }
Chris Mason771ed682008-11-06 22:02:51 -0500783 em->start = async_extent->start;
784 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500785 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400786 em->mod_start = em->start;
787 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500788
789 em->block_start = ins.objectid;
790 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500791 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400792 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500793 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800794 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500795 set_bit(EXTENT_FLAG_PINNED, &em->flags);
796 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400797 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500798
Chris Masond3977122009-01-05 21:25:51 -0500799 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400800 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400801 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400802 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500803 if (ret != -EEXIST) {
804 free_extent_map(em);
805 break;
806 }
807 btrfs_drop_extent_cache(inode, async_extent->start,
808 async_extent->start +
809 async_extent->ram_size - 1, 0);
810 }
811
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500812 if (ret)
813 goto out_free_reserve;
814
Li Zefan261507a02010-12-17 14:21:50 +0800815 ret = btrfs_add_ordered_extent_compress(inode,
816 async_extent->start,
817 ins.objectid,
818 async_extent->ram_size,
819 ins.offset,
820 BTRFS_ORDERED_COMPRESSED,
821 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100822 if (ret) {
823 btrfs_drop_extent_cache(inode, async_extent->start,
824 async_extent->start +
825 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500826 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100827 }
Chris Mason771ed682008-11-06 22:02:51 -0500828
Chris Mason771ed682008-11-06 22:02:51 -0500829 /*
830 * clear dirty, set writeback and unlock the pages.
831 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400832 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400833 async_extent->start +
834 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400835 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
836 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400837 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500838 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500839 async_extent->start,
840 async_extent->ram_size,
841 ins.objectid,
842 ins.offset, async_extent->pages,
843 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100844 if (ret) {
845 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
846 struct page *p = async_extent->pages[0];
847 const u64 start = async_extent->start;
848 const u64 end = start + async_extent->ram_size - 1;
849
850 p->mapping = inode->i_mapping;
851 tree->ops->writepage_end_io_hook(p, start, end,
852 NULL, 0);
853 p->mapping = NULL;
854 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
855 PAGE_END_WRITEBACK |
856 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100857 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100858 }
Chris Mason771ed682008-11-06 22:02:51 -0500859 alloc_hint = ins.objectid + ins.offset;
860 kfree(async_extent);
861 cond_resched();
862 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100863 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500864out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800865 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100866out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400867 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500868 async_extent->start +
869 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400870 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400871 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
872 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100873 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
874 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100875 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100876 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500877 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500878}
879
Josef Bacik4b46fce2010-05-23 11:00:55 -0400880static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
881 u64 num_bytes)
882{
883 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
884 struct extent_map *em;
885 u64 alloc_hint = 0;
886
887 read_lock(&em_tree->lock);
888 em = search_extent_mapping(em_tree, start, num_bytes);
889 if (em) {
890 /*
891 * if block start isn't an actual block number then find the
892 * first block in this inode and use that as a hint. If that
893 * block is also bogus then just don't worry about it.
894 */
895 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
896 free_extent_map(em);
897 em = search_extent_mapping(em_tree, 0, 0);
898 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
899 alloc_hint = em->block_start;
900 if (em)
901 free_extent_map(em);
902 } else {
903 alloc_hint = em->block_start;
904 free_extent_map(em);
905 }
906 }
907 read_unlock(&em_tree->lock);
908
909 return alloc_hint;
910}
911
Chris Mason771ed682008-11-06 22:02:51 -0500912/*
913 * when extent_io.c finds a delayed allocation range in the file,
914 * the call backs end up in this code. The basic idea is to
915 * allocate extents on disk for the range, and create ordered data structs
916 * in ram to track those extents.
917 *
918 * locked_page is the page that writepage had locked already. We use
919 * it to make sure we don't do extra locks or unlocks.
920 *
921 * *page_started is set to one if we unlock locked_page and do everything
922 * required to start IO on it. It may be clean and already done with
923 * IO when we return.
924 */
Josef Bacik00361582013-08-14 14:02:47 -0400925static noinline int cow_file_range(struct inode *inode,
926 struct page *locked_page,
927 u64 start, u64 end, int *page_started,
928 unsigned long *nr_written,
929 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500930{
Josef Bacik00361582013-08-14 14:02:47 -0400931 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500932 u64 alloc_hint = 0;
933 u64 num_bytes;
934 unsigned long ram_size;
935 u64 disk_num_bytes;
936 u64 cur_alloc_size;
937 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500938 struct btrfs_key ins;
939 struct extent_map *em;
940 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
941 int ret = 0;
942
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400943 if (btrfs_is_free_space_inode(inode)) {
944 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500945 ret = -EINVAL;
946 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400947 }
Chris Mason771ed682008-11-06 22:02:51 -0500948
Qu Wenruofda28322013-02-26 08:10:22 +0000949 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500950 num_bytes = max(blocksize, num_bytes);
951 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500952
Chris Mason4cb53002011-05-24 15:35:30 -0400953 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400954 if (num_bytes < 64 * 1024 &&
955 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400956 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400957
Chris Mason771ed682008-11-06 22:02:51 -0500958 if (start == 0) {
959 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400960 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
961 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500962 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400963 extent_clear_unlock_delalloc(inode, start, end, NULL,
964 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400965 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400966 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
967 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000968
Chris Mason771ed682008-11-06 22:02:51 -0500969 *nr_written = *nr_written +
970 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
971 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500972 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100973 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100974 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500975 }
976 }
Chris Masonc8b97812008-10-29 14:49:59 -0400977
978 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200979 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400980
Josef Bacik4b46fce2010-05-23 11:00:55 -0400981 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400982 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400983
Chris Masond3977122009-01-05 21:25:51 -0500984 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400985 unsigned long op;
986
Josef Bacik287a0ab2010-03-19 18:07:23 +0000987 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400988 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500989 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800990 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400991 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100992 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500993
David Sterba172ddd62011-04-21 00:48:27 +0200994 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000995 if (!em) {
996 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000997 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000998 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400999 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -05001000 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -05001001 ram_size = ins.offset;
1002 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001003 em->mod_start = em->start;
1004 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -04001005
Chris Masone6dcd2d2008-07-17 12:53:50 -04001006 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001007 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001008 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001009 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001010 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001011 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001012 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001013
Chris Masond3977122009-01-05 21:25:51 -05001014 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001015 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001016 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001017 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001018 if (ret != -EEXIST) {
1019 free_extent_map(em);
1020 break;
1021 }
1022 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001023 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001024 }
Liu Boace68ba2013-04-22 10:53:47 +00001025 if (ret)
1026 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001027
Chris Mason98d20f62008-04-14 09:46:10 -04001028 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001029 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001030 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001031 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001032 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001033
Yan Zheng17d217f2008-12-12 10:03:38 -05001034 if (root->root_key.objectid ==
1035 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1036 ret = btrfs_reloc_clone_csums(inode, start,
1037 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001038 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001039 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001040 }
1041
Chris Masond3977122009-01-05 21:25:51 -05001042 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001043 break;
Chris Masond3977122009-01-05 21:25:51 -05001044
Chris Masonc8b97812008-10-29 14:49:59 -04001045 /* we're not doing compressed IO, don't unlock the first
1046 * page (which the caller expects to stay locked), don't
1047 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001048 *
1049 * Do set the Private2 bit so we know this page was properly
1050 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001051 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001052 op = unlock ? PAGE_UNLOCK : 0;
1053 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001054
Josef Bacikc2790a22013-07-29 11:20:47 -04001055 extent_clear_unlock_delalloc(inode, start,
1056 start + ram_size - 1, locked_page,
1057 EXTENT_LOCKED | EXTENT_DELALLOC,
1058 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001059 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001060 num_bytes -= cur_alloc_size;
1061 alloc_hint = ins.objectid + ins.offset;
1062 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001063 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001064out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001065 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001066
Filipe Mananad9f85962014-08-25 10:43:00 +01001067out_drop_extent_cache:
1068 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001069out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001070 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001071out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001072 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001073 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1074 EXTENT_DELALLOC | EXTENT_DEFRAG,
1075 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1076 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001077 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001078}
Chris Masonc8b97812008-10-29 14:49:59 -04001079
Chris Mason771ed682008-11-06 22:02:51 -05001080/*
1081 * work queue call back to started compression on a file and pages
1082 */
1083static noinline void async_cow_start(struct btrfs_work *work)
1084{
1085 struct async_cow *async_cow;
1086 int num_added = 0;
1087 async_cow = container_of(work, struct async_cow, work);
1088
1089 compress_file_range(async_cow->inode, async_cow->locked_page,
1090 async_cow->start, async_cow->end, async_cow,
1091 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001092 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001093 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001094 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001095 }
Chris Mason771ed682008-11-06 22:02:51 -05001096}
1097
1098/*
1099 * work queue call back to submit previously compressed pages
1100 */
1101static noinline void async_cow_submit(struct btrfs_work *work)
1102{
1103 struct async_cow *async_cow;
1104 struct btrfs_root *root;
1105 unsigned long nr_pages;
1106
1107 async_cow = container_of(work, struct async_cow, work);
1108
1109 root = async_cow->root;
1110 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1111 PAGE_CACHE_SHIFT;
1112
David Sterbaee863952015-02-16 19:41:40 +01001113 /*
1114 * atomic_sub_return implies a barrier for waitqueue_active
1115 */
Josef Bacik66657b32012-08-01 15:36:24 -04001116 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001117 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001118 waitqueue_active(&root->fs_info->async_submit_wait))
1119 wake_up(&root->fs_info->async_submit_wait);
1120
Chris Masond3977122009-01-05 21:25:51 -05001121 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001122 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001123}
Chris Masonc8b97812008-10-29 14:49:59 -04001124
Chris Mason771ed682008-11-06 22:02:51 -05001125static noinline void async_cow_free(struct btrfs_work *work)
1126{
1127 struct async_cow *async_cow;
1128 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001129 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001130 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001131 kfree(async_cow);
1132}
1133
1134static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1135 u64 start, u64 end, int *page_started,
1136 unsigned long *nr_written)
1137{
1138 struct async_cow *async_cow;
1139 struct btrfs_root *root = BTRFS_I(inode)->root;
1140 unsigned long nr_pages;
1141 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001142 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001143
Chris Masona3429ab2009-10-08 12:30:20 -04001144 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1145 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001146 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001147 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001148 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001149 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001150 async_cow->root = root;
1151 async_cow->locked_page = locked_page;
1152 async_cow->start = start;
1153
Wang Shilongf79707b2014-07-17 11:44:09 +08001154 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1155 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001156 cur_end = end;
1157 else
1158 cur_end = min(end, start + 512 * 1024 - 1);
1159
1160 async_cow->end = cur_end;
1161 INIT_LIST_HEAD(&async_cow->extents);
1162
Liu Bo9e0af232014-08-15 23:36:53 +08001163 btrfs_init_work(&async_cow->work,
1164 btrfs_delalloc_helper,
1165 async_cow_start, async_cow_submit,
1166 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001167
Chris Mason771ed682008-11-06 22:02:51 -05001168 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1169 PAGE_CACHE_SHIFT;
1170 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1171
Qu Wenruoafe3d242014-02-28 10:46:07 +08001172 btrfs_queue_work(root->fs_info->delalloc_workers,
1173 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001174
1175 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1176 wait_event(root->fs_info->async_submit_wait,
1177 (atomic_read(&root->fs_info->async_delalloc_pages) <
1178 limit));
1179 }
1180
Chris Masond3977122009-01-05 21:25:51 -05001181 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001182 atomic_read(&root->fs_info->async_delalloc_pages)) {
1183 wait_event(root->fs_info->async_submit_wait,
1184 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1185 0));
1186 }
1187
1188 *nr_written += nr_pages;
1189 start = cur_end + 1;
1190 }
1191 *page_started = 1;
1192 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001193}
1194
Chris Masond3977122009-01-05 21:25:51 -05001195static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001196 u64 bytenr, u64 num_bytes)
1197{
1198 int ret;
1199 struct btrfs_ordered_sum *sums;
1200 LIST_HEAD(list);
1201
Yan Zheng07d400a2009-01-06 11:42:00 -05001202 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001203 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001204 if (ret == 0 && list_empty(&list))
1205 return 0;
1206
1207 while (!list_empty(&list)) {
1208 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1209 list_del(&sums->list);
1210 kfree(sums);
1211 }
1212 return 1;
1213}
1214
Chris Masond352ac62008-09-29 15:18:18 -04001215/*
1216 * when nowcow writeback call back. This checks for snapshots or COW copies
1217 * of the extents that exist in the file, and COWs the file as required.
1218 *
1219 * If no cow copies or snapshots exist, we write directly to the existing
1220 * blocks on disk
1221 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001222static noinline int run_delalloc_nocow(struct inode *inode,
1223 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001224 u64 start, u64 end, int *page_started, int force,
1225 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001226{
Chris Masonbe20aa92007-12-17 20:14:01 -05001227 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001228 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001229 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001230 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001231 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001232 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001233 u64 cow_start;
1234 u64 cur_offset;
1235 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001236 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001237 u64 disk_bytenr;
1238 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001239 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001240 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001241 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001242 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001243 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001244 int nocow;
1245 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001246 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001247 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001248
1249 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001250 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001251 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1252 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001253 EXTENT_DO_ACCOUNTING |
1254 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001255 PAGE_CLEAR_DIRTY |
1256 PAGE_SET_WRITEBACK |
1257 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001258 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001259 }
Li Zefan82d59022011-04-20 10:33:24 +08001260
Liu Bo83eea1f2012-07-10 05:28:39 -06001261 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001262
1263 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001264 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001265 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001266 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001267
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001268 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001269 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1270 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001271 EXTENT_DO_ACCOUNTING |
1272 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001273 PAGE_CLEAR_DIRTY |
1274 PAGE_SET_WRITEBACK |
1275 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001276 btrfs_free_path(path);
1277 return PTR_ERR(trans);
1278 }
1279
Josef Bacik74b21072011-04-13 12:02:53 -04001280 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001281
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 cow_start = (u64)-1;
1283 cur_offset = start;
1284 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001285 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001287 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001288 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1290 leaf = path->nodes[0];
1291 btrfs_item_key_to_cpu(leaf, &found_key,
1292 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001293 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 found_key.type == BTRFS_EXTENT_DATA_KEY)
1295 path->slots[0]--;
1296 }
1297 check_prev = 0;
1298next_slot:
1299 leaf = path->nodes[0];
1300 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1301 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001302 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001303 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001304 if (ret > 0)
1305 break;
1306 leaf = path->nodes[0];
1307 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001308
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 nocow = 0;
1310 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001311 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001313
Filipe Manana1d512cb2015-11-09 00:33:58 +00001314 if (found_key.objectid > ino)
1315 break;
1316 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1317 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1318 path->slots[0]++;
1319 goto next_slot;
1320 }
1321 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001322 found_key.offset > end)
1323 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001324
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 if (found_key.offset > cur_offset) {
1326 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001327 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001328 goto out_check;
1329 }
Chris Masonc31f8832008-01-08 15:46:31 -05001330
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 fi = btrfs_item_ptr(leaf, path->slots[0],
1332 struct btrfs_file_extent_item);
1333 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001334
Josef Bacikcc95bef2013-04-04 14:31:27 -04001335 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001336 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1337 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001339 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001340 extent_end = found_key.offset +
1341 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001342 disk_num_bytes =
1343 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 if (extent_end <= start) {
1345 path->slots[0]++;
1346 goto next_slot;
1347 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001348 if (disk_bytenr == 0)
1349 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001350 if (btrfs_file_extent_compression(leaf, fi) ||
1351 btrfs_file_extent_encryption(leaf, fi) ||
1352 btrfs_file_extent_other_encoding(leaf, fi))
1353 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001354 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1355 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001356 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001358 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001359 found_key.offset -
1360 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001361 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001362 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001363 disk_bytenr += cur_offset - found_key.offset;
1364 num_bytes = min(end + 1, extent_end) - cur_offset;
1365 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001366 * if there are pending snapshots for this root,
1367 * we fall into common COW way.
1368 */
1369 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001370 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001371 if (!err)
1372 goto out_check;
1373 }
1374 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001375 * force cow if csum exists in the range.
1376 * this ensure that csum for a given extent are
1377 * either valid or do not exist.
1378 */
1379 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1380 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 nocow = 1;
1382 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1383 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001384 btrfs_file_extent_inline_len(leaf,
1385 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001386 extent_end = ALIGN(extent_end, root->sectorsize);
1387 } else {
1388 BUG_ON(1);
1389 }
1390out_check:
1391 if (extent_end <= start) {
1392 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001393 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001394 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001395 goto next_slot;
1396 }
1397 if (!nocow) {
1398 if (cow_start == (u64)-1)
1399 cow_start = cur_offset;
1400 cur_offset = extent_end;
1401 if (cur_offset > end)
1402 break;
1403 path->slots[0]++;
1404 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001405 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001406
David Sterbab3b4aa72011-04-21 01:20:15 +02001407 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001408 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001409 ret = cow_file_range(inode, locked_page,
1410 cow_start, found_key.offset - 1,
1411 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001412 if (ret) {
1413 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001414 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001415 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001416 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001417 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001418 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001419
Yan Zhengd899e052008-10-30 14:25:28 -04001420 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1421 struct extent_map *em;
1422 struct extent_map_tree *em_tree;
1423 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001424 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001425 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001426 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001427 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001428 em->len = num_bytes;
1429 em->block_len = num_bytes;
1430 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001431 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001432 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001433 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001434 em->mod_start = em->start;
1435 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001436 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001437 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001438 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001439 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001440 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001441 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001442 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001443 if (ret != -EEXIST) {
1444 free_extent_map(em);
1445 break;
1446 }
1447 btrfs_drop_extent_cache(inode, em->start,
1448 em->start + em->len - 1, 0);
1449 }
1450 type = BTRFS_ORDERED_PREALLOC;
1451 } else {
1452 type = BTRFS_ORDERED_NOCOW;
1453 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001454
1455 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001456 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001457 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001458
Yan, Zhengefa56462010-05-16 10:49:59 -04001459 if (root->root_key.objectid ==
1460 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1461 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1462 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001463 if (ret) {
1464 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001465 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001466 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001467 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001468 }
1469
Josef Bacikc2790a22013-07-29 11:20:47 -04001470 extent_clear_unlock_delalloc(inode, cur_offset,
1471 cur_offset + num_bytes - 1,
1472 locked_page, EXTENT_LOCKED |
1473 EXTENT_DELALLOC, PAGE_UNLOCK |
1474 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001475 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001476 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001477 cur_offset = extent_end;
1478 if (cur_offset > end)
1479 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001480 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001481 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001482
Josef Bacik17ca04a2012-05-31 15:58:55 -04001483 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001484 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001485 cur_offset = end;
1486 }
1487
Yan Zheng80ff3852008-10-30 14:20:02 -04001488 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001489 ret = cow_file_range(inode, locked_page, cow_start, end,
1490 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001491 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001492 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001493 }
1494
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001495error:
Miao Xiea698d0752012-09-20 01:51:59 -06001496 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001497 if (!ret)
1498 ret = err;
1499
Josef Bacik17ca04a2012-05-31 15:58:55 -04001500 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001501 extent_clear_unlock_delalloc(inode, cur_offset, end,
1502 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001503 EXTENT_DELALLOC | EXTENT_DEFRAG |
1504 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1505 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001506 PAGE_SET_WRITEBACK |
1507 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001508 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001509 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001510}
1511
Wang Shilong47059d92014-07-03 18:22:07 +08001512static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1513{
1514
1515 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1516 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1517 return 0;
1518
1519 /*
1520 * @defrag_bytes is a hint value, no spinlock held here,
1521 * if is not zero, it means the file is defragging.
1522 * Force cow if given extent needs to be defragged.
1523 */
1524 if (BTRFS_I(inode)->defrag_bytes &&
1525 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1526 EXTENT_DEFRAG, 0, NULL))
1527 return 1;
1528
1529 return 0;
1530}
1531
Chris Masond352ac62008-09-29 15:18:18 -04001532/*
1533 * extent_io.c call back to do delayed allocation processing
1534 */
Chris Masonc8b97812008-10-29 14:49:59 -04001535static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001536 u64 start, u64 end, int *page_started,
1537 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001538{
Chris Masonbe20aa92007-12-17 20:14:01 -05001539 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001540 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001541
Wang Shilong47059d92014-07-03 18:22:07 +08001542 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001543 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001544 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001545 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001546 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001547 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001548 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001549 ret = cow_file_range(inode, locked_page, start, end,
1550 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001551 } else {
1552 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1553 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001554 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001555 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001556 }
Chris Masonb888db22007-08-27 16:49:44 -04001557 return ret;
1558}
1559
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001560static void btrfs_split_extent_hook(struct inode *inode,
1561 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001562{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001563 u64 size;
1564
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001565 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001566 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001567 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001568
Josef Bacikdcab6a32015-02-11 15:08:59 -05001569 size = orig->end - orig->start + 1;
1570 if (size > BTRFS_MAX_EXTENT_SIZE) {
1571 u64 num_extents;
1572 u64 new_size;
1573
1574 /*
Josef Bacikba117212015-03-13 15:01:24 -04001575 * See the explanation in btrfs_merge_extent_hook, the same
1576 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001577 */
1578 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001579 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001580 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001581 new_size = split - orig->start;
1582 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1583 BTRFS_MAX_EXTENT_SIZE);
1584 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1585 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001586 return;
1587 }
1588
Josef Bacik9e0baf62011-07-15 15:16:44 +00001589 spin_lock(&BTRFS_I(inode)->lock);
1590 BTRFS_I(inode)->outstanding_extents++;
1591 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001592}
1593
1594/*
1595 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1596 * extents so we can keep track of new extents that are just merged onto old
1597 * extents, such as when we are doing sequential writes, so we can properly
1598 * account for the metadata space we'll need.
1599 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001600static void btrfs_merge_extent_hook(struct inode *inode,
1601 struct extent_state *new,
1602 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001603{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001604 u64 new_size, old_size;
1605 u64 num_extents;
1606
Josef Bacik9ed74f22009-09-11 16:12:44 -04001607 /* not delalloc, ignore it */
1608 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001609 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001610
Josef Bacik8461a3d2015-03-13 15:12:08 -04001611 if (new->start > other->start)
1612 new_size = new->end - other->start + 1;
1613 else
1614 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001615
1616 /* we're not bigger than the max, unreserve the space and go */
1617 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1618 spin_lock(&BTRFS_I(inode)->lock);
1619 BTRFS_I(inode)->outstanding_extents--;
1620 spin_unlock(&BTRFS_I(inode)->lock);
1621 return;
1622 }
1623
1624 /*
Josef Bacikba117212015-03-13 15:01:24 -04001625 * We have to add up either side to figure out how many extents were
1626 * accounted for before we merged into one big extent. If the number of
1627 * extents we accounted for is <= the amount we need for the new range
1628 * then we can return, otherwise drop. Think of it like this
1629 *
1630 * [ 4k][MAX_SIZE]
1631 *
1632 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1633 * need 2 outstanding extents, on one side we have 1 and the other side
1634 * we have 1 so they are == and we can return. But in this case
1635 *
1636 * [MAX_SIZE+4k][MAX_SIZE+4k]
1637 *
1638 * Each range on their own accounts for 2 extents, but merged together
1639 * they are only 3 extents worth of accounting, so we need to drop in
1640 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001641 */
Josef Bacikba117212015-03-13 15:01:24 -04001642 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001643 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1644 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001645 old_size = new->end - new->start + 1;
1646 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1647 BTRFS_MAX_EXTENT_SIZE);
1648
Josef Bacikdcab6a32015-02-11 15:08:59 -05001649 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001650 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001651 return;
1652
Josef Bacik9e0baf62011-07-15 15:16:44 +00001653 spin_lock(&BTRFS_I(inode)->lock);
1654 BTRFS_I(inode)->outstanding_extents--;
1655 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001656}
1657
Miao Xieeb73c1b2013-05-15 07:48:22 +00001658static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1659 struct inode *inode)
1660{
1661 spin_lock(&root->delalloc_lock);
1662 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1663 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1664 &root->delalloc_inodes);
1665 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1666 &BTRFS_I(inode)->runtime_flags);
1667 root->nr_delalloc_inodes++;
1668 if (root->nr_delalloc_inodes == 1) {
1669 spin_lock(&root->fs_info->delalloc_root_lock);
1670 BUG_ON(!list_empty(&root->delalloc_root));
1671 list_add_tail(&root->delalloc_root,
1672 &root->fs_info->delalloc_roots);
1673 spin_unlock(&root->fs_info->delalloc_root_lock);
1674 }
1675 }
1676 spin_unlock(&root->delalloc_lock);
1677}
1678
1679static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1680 struct inode *inode)
1681{
1682 spin_lock(&root->delalloc_lock);
1683 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1684 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1685 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1686 &BTRFS_I(inode)->runtime_flags);
1687 root->nr_delalloc_inodes--;
1688 if (!root->nr_delalloc_inodes) {
1689 spin_lock(&root->fs_info->delalloc_root_lock);
1690 BUG_ON(list_empty(&root->delalloc_root));
1691 list_del_init(&root->delalloc_root);
1692 spin_unlock(&root->fs_info->delalloc_root_lock);
1693 }
1694 }
1695 spin_unlock(&root->delalloc_lock);
1696}
1697
Chris Masond352ac62008-09-29 15:18:18 -04001698/*
1699 * extent_io.c set_bit_hook, used to track delayed allocation
1700 * bytes in this file, and to maintain the list of inodes that
1701 * have pending delalloc work to be done.
1702 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001703static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001704 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001705{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001706
Wang Shilong47059d92014-07-03 18:22:07 +08001707 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1708 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001709 /*
1710 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001711 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001712 * bit, which is only set or cleared with irqs on
1713 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001714 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001715 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001716 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001717 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001718
Josef Bacik9e0baf62011-07-15 15:16:44 +00001719 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001720 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001721 } else {
1722 spin_lock(&BTRFS_I(inode)->lock);
1723 BTRFS_I(inode)->outstanding_extents++;
1724 spin_unlock(&BTRFS_I(inode)->lock);
1725 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001726
Josef Bacik6a3891c2015-03-16 17:38:52 -04001727 /* For sanity tests */
1728 if (btrfs_test_is_dummy_root(root))
1729 return;
1730
Miao Xie963d6782013-01-29 10:10:51 +00001731 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1732 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001733 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001734 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001735 if (*bits & EXTENT_DEFRAG)
1736 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001737 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001738 &BTRFS_I(inode)->runtime_flags))
1739 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001740 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001741 }
Chris Mason291d6732008-01-29 15:55:23 -05001742}
1743
Chris Masond352ac62008-09-29 15:18:18 -04001744/*
1745 * extent_io.c clear_bit_hook, see set_bit_hook for why
1746 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001747static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001748 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001749 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001750{
Wang Shilong47059d92014-07-03 18:22:07 +08001751 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001752 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1753 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001754
1755 spin_lock(&BTRFS_I(inode)->lock);
1756 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1757 BTRFS_I(inode)->defrag_bytes -= len;
1758 spin_unlock(&BTRFS_I(inode)->lock);
1759
Chris Mason75eff682008-12-15 15:54:40 -05001760 /*
1761 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001762 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001763 * bit, which is only set or cleared with irqs on
1764 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001765 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001766 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001767 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001768
Josef Bacik9e0baf62011-07-15 15:16:44 +00001769 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001770 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001771 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1772 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001773 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001774 spin_unlock(&BTRFS_I(inode)->lock);
1775 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001776
Josef Bacikb6d08f02013-09-27 14:57:43 -04001777 /*
1778 * We don't reserve metadata space for space cache inodes so we
1779 * don't need to call dellalloc_release_metadata if there is an
1780 * error.
1781 */
1782 if (*bits & EXTENT_DO_ACCOUNTING &&
1783 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001784 btrfs_delalloc_release_metadata(inode, len);
1785
Josef Bacik6a3891c2015-03-16 17:38:52 -04001786 /* For sanity tests. */
1787 if (btrfs_test_is_dummy_root(root))
1788 return;
1789
Josef Bacik0cb59c92010-07-02 12:14:14 -04001790 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001791 && do_list && !(state->state & EXTENT_NORESERVE))
Qu Wenruo51773be2015-10-08 18:19:37 +08001792 btrfs_free_reserved_data_space_noquota(inode,
1793 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001794
Miao Xie963d6782013-01-29 10:10:51 +00001795 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1796 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001797 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001798 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001799 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001800 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001801 &BTRFS_I(inode)->runtime_flags))
1802 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001803 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001804 }
Chris Mason291d6732008-01-29 15:55:23 -05001805}
1806
Chris Masond352ac62008-09-29 15:18:18 -04001807/*
1808 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1809 * we don't create bios that span stripes or chunks
1810 */
David Woodhouse64a16702009-07-15 23:29:37 +01001811int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001812 size_t size, struct bio *bio,
1813 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001814{
1815 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001816 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001817 u64 length = 0;
1818 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001819 int ret;
1820
Chris Mason771ed682008-11-06 22:02:51 -05001821 if (bio_flags & EXTENT_BIO_COMPRESSED)
1822 return 0;
1823
Kent Overstreet4f024f32013-10-11 15:44:27 -07001824 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001825 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001826 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001827 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001828 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001829 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001830 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001831 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001832 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001833}
1834
Chris Masond352ac62008-09-29 15:18:18 -04001835/*
1836 * in order to insert checksums into the metadata in large chunks,
1837 * we wait until bio submission time. All the pages in the bio are
1838 * checksummed and sums are attached onto the ordered extent record.
1839 *
1840 * At IO completion time the cums attached on the ordered extent record
1841 * are inserted into the btree
1842 */
Chris Masond3977122009-01-05 21:25:51 -05001843static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1844 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001845 unsigned long bio_flags,
1846 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001847{
Chris Mason065631f2008-02-20 12:07:25 -05001848 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001849 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001850
Chris Masond20f7042008-12-08 16:58:54 -05001851 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001852 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001853 return 0;
1854}
Chris Masone0156402008-04-16 11:15:20 -04001855
Chris Mason4a69a412008-11-06 22:03:00 -05001856/*
1857 * in order to insert checksums into the metadata in large chunks,
1858 * we wait until bio submission time. All the pages in the bio are
1859 * checksummed and sums are attached onto the ordered extent record.
1860 *
1861 * At IO completion time the cums attached on the ordered extent record
1862 * are inserted into the btree
1863 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001864static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001865 int mirror_num, unsigned long bio_flags,
1866 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001867{
1868 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001869 int ret;
1870
1871 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001872 if (ret) {
1873 bio->bi_error = ret;
1874 bio_endio(bio);
1875 }
Stefan Behrens61891922012-11-05 18:51:52 +01001876 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001877}
1878
Chris Masond352ac62008-09-29 15:18:18 -04001879/*
Chris Masoncad321a2008-12-17 14:51:42 -05001880 * extent_io.c submission hook. This does the right thing for csum calculation
1881 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001882 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001883static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001884 int mirror_num, unsigned long bio_flags,
1885 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001886{
1887 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301888 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001889 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001890 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001891 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001892
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001893 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001894
Liu Bo83eea1f2012-07-10 05:28:39 -06001895 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301896 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001897
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001898 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001899 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1900 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001901 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001902
Chris Masond20f7042008-12-08 16:58:54 -05001903 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001904 ret = btrfs_submit_compressed_read(inode, bio,
1905 mirror_num,
1906 bio_flags);
1907 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001908 } else if (!skip_sum) {
1909 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1910 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001911 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001912 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001913 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001914 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001915 /* csum items have already been cloned */
1916 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1917 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001918 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001919 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001920 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001921 bio_flags, bio_offset,
1922 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001923 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001924 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001925 } else if (!skip_sum) {
1926 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1927 if (ret)
1928 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001929 }
1930
Chris Mason0b86a832008-03-24 15:01:56 -04001931mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001932 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1933
1934out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001935 if (ret < 0) {
1936 bio->bi_error = ret;
1937 bio_endio(bio);
1938 }
Stefan Behrens61891922012-11-05 18:51:52 +01001939 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001940}
Chris Mason6885f302008-02-20 16:11:05 -05001941
Chris Masond352ac62008-09-29 15:18:18 -04001942/*
1943 * given a list of ordered sums record them in the inode. This happens
1944 * at IO completion time based on sums calculated at bio submission time.
1945 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001946static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001947 struct inode *inode, u64 file_offset,
1948 struct list_head *list)
1949{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001950 struct btrfs_ordered_sum *sum;
1951
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001952 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001953 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001954 btrfs_csum_file_blocks(trans,
1955 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001956 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001957 }
1958 return 0;
1959}
1960
Josef Bacik2ac55d42010-02-03 19:33:23 +00001961int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1962 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001963{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001964 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001965 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001966 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001967}
1968
Chris Masond352ac62008-09-29 15:18:18 -04001969/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001970struct btrfs_writepage_fixup {
1971 struct page *page;
1972 struct btrfs_work work;
1973};
1974
Christoph Hellwigb2950862008-12-02 09:54:17 -05001975static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001976{
1977 struct btrfs_writepage_fixup *fixup;
1978 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001979 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001980 struct page *page;
1981 struct inode *inode;
1982 u64 page_start;
1983 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001984 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001985
1986 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1987 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001988again:
Chris Mason247e7432008-07-17 12:53:51 -04001989 lock_page(page);
1990 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1991 ClearPageChecked(page);
1992 goto out_page;
1993 }
1994
1995 inode = page->mapping->host;
1996 page_start = page_offset(page);
1997 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1998
David Sterbaff13db42015-12-03 14:30:40 +01001999 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002000 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002001
2002 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002003 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002004 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002005
2006 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2007 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002008 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2009 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002010 unlock_page(page);
2011 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002012 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002013 goto again;
2014 }
Chris Mason247e7432008-07-17 12:53:51 -04002015
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002016 ret = btrfs_delalloc_reserve_space(inode, page_start,
2017 PAGE_CACHE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002018 if (ret) {
2019 mapping_set_error(page->mapping, ret);
2020 end_extent_writepage(page, ret, page_start, page_end);
2021 ClearPageChecked(page);
2022 goto out;
2023 }
2024
Josef Bacik2ac55d42010-02-03 19:33:23 +00002025 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002026 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002027 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002028out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002029 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2030 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002031out_page:
2032 unlock_page(page);
2033 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002034 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002035}
2036
2037/*
2038 * There are a few paths in the higher layers of the kernel that directly
2039 * set the page dirty bit without asking the filesystem if it is a
2040 * good idea. This causes problems because we want to make sure COW
2041 * properly happens and the data=ordered rules are followed.
2042 *
Chris Masonc8b97812008-10-29 14:49:59 -04002043 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002044 * hasn't been properly setup for IO. We kick off an async process
2045 * to fix it up. The async helper will wait for ordered extents, set
2046 * the delalloc bit and make it safe to write the page.
2047 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002048static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002049{
2050 struct inode *inode = page->mapping->host;
2051 struct btrfs_writepage_fixup *fixup;
2052 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002053
Chris Mason8b62b722009-09-02 16:53:46 -04002054 /* this page is properly in the ordered list */
2055 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002056 return 0;
2057
2058 if (PageChecked(page))
2059 return -EAGAIN;
2060
2061 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2062 if (!fixup)
2063 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002064
Chris Mason247e7432008-07-17 12:53:51 -04002065 SetPageChecked(page);
2066 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002067 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2068 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002069 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002070 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002071 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002072}
2073
Yan Zhengd899e052008-10-30 14:25:28 -04002074static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2075 struct inode *inode, u64 file_pos,
2076 u64 disk_bytenr, u64 disk_num_bytes,
2077 u64 num_bytes, u64 ram_bytes,
2078 u8 compression, u8 encryption,
2079 u16 other_encoding, int extent_type)
2080{
2081 struct btrfs_root *root = BTRFS_I(inode)->root;
2082 struct btrfs_file_extent_item *fi;
2083 struct btrfs_path *path;
2084 struct extent_buffer *leaf;
2085 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002086 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002087 int ret;
2088
2089 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002090 if (!path)
2091 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002092
Chris Masona1ed8352009-09-11 12:27:37 -04002093 /*
2094 * we may be replacing one extent in the tree with another.
2095 * The new extent is pinned in the extent map, and we don't want
2096 * to drop it from the cache until it is completely in the btree.
2097 *
2098 * So, tell btrfs_drop_extents to leave this extent in the cache.
2099 * the caller is expected to unpin it and allow it to be merged
2100 * with the others.
2101 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002102 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2103 file_pos + num_bytes, NULL, 0,
2104 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002105 if (ret)
2106 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002107
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002108 if (!extent_inserted) {
2109 ins.objectid = btrfs_ino(inode);
2110 ins.offset = file_pos;
2111 ins.type = BTRFS_EXTENT_DATA_KEY;
2112
2113 path->leave_spinning = 1;
2114 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2115 sizeof(*fi));
2116 if (ret)
2117 goto out;
2118 }
Yan Zhengd899e052008-10-30 14:25:28 -04002119 leaf = path->nodes[0];
2120 fi = btrfs_item_ptr(leaf, path->slots[0],
2121 struct btrfs_file_extent_item);
2122 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2123 btrfs_set_file_extent_type(leaf, fi, extent_type);
2124 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2125 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2126 btrfs_set_file_extent_offset(leaf, fi, 0);
2127 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2128 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2129 btrfs_set_file_extent_compression(leaf, fi, compression);
2130 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2131 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002132
Yan Zhengd899e052008-10-30 14:25:28 -04002133 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002134 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002135
2136 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002137
2138 ins.objectid = disk_bytenr;
2139 ins.offset = disk_num_bytes;
2140 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002141 ret = btrfs_alloc_reserved_file_extent(trans, root,
2142 root->root_key.objectid,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002143 btrfs_ino(inode), file_pos,
2144 ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002145 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002146 * Release the reserved range from inode dirty range map, as it is
2147 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002148 */
2149 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002150out:
Yan Zhengd899e052008-10-30 14:25:28 -04002151 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002152
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002153 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002154}
2155
Liu Bo38c227d2013-01-29 03:18:40 +00002156/* snapshot-aware defrag */
2157struct sa_defrag_extent_backref {
2158 struct rb_node node;
2159 struct old_sa_defrag_extent *old;
2160 u64 root_id;
2161 u64 inum;
2162 u64 file_pos;
2163 u64 extent_offset;
2164 u64 num_bytes;
2165 u64 generation;
2166};
2167
2168struct old_sa_defrag_extent {
2169 struct list_head list;
2170 struct new_sa_defrag_extent *new;
2171
2172 u64 extent_offset;
2173 u64 bytenr;
2174 u64 offset;
2175 u64 len;
2176 int count;
2177};
2178
2179struct new_sa_defrag_extent {
2180 struct rb_root root;
2181 struct list_head head;
2182 struct btrfs_path *path;
2183 struct inode *inode;
2184 u64 file_pos;
2185 u64 len;
2186 u64 bytenr;
2187 u64 disk_len;
2188 u8 compress_type;
2189};
2190
2191static int backref_comp(struct sa_defrag_extent_backref *b1,
2192 struct sa_defrag_extent_backref *b2)
2193{
2194 if (b1->root_id < b2->root_id)
2195 return -1;
2196 else if (b1->root_id > b2->root_id)
2197 return 1;
2198
2199 if (b1->inum < b2->inum)
2200 return -1;
2201 else if (b1->inum > b2->inum)
2202 return 1;
2203
2204 if (b1->file_pos < b2->file_pos)
2205 return -1;
2206 else if (b1->file_pos > b2->file_pos)
2207 return 1;
2208
2209 /*
2210 * [------------------------------] ===> (a range of space)
2211 * |<--->| |<---->| =============> (fs/file tree A)
2212 * |<---------------------------->| ===> (fs/file tree B)
2213 *
2214 * A range of space can refer to two file extents in one tree while
2215 * refer to only one file extent in another tree.
2216 *
2217 * So we may process a disk offset more than one time(two extents in A)
2218 * and locate at the same extent(one extent in B), then insert two same
2219 * backrefs(both refer to the extent in B).
2220 */
2221 return 0;
2222}
2223
2224static void backref_insert(struct rb_root *root,
2225 struct sa_defrag_extent_backref *backref)
2226{
2227 struct rb_node **p = &root->rb_node;
2228 struct rb_node *parent = NULL;
2229 struct sa_defrag_extent_backref *entry;
2230 int ret;
2231
2232 while (*p) {
2233 parent = *p;
2234 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2235
2236 ret = backref_comp(backref, entry);
2237 if (ret < 0)
2238 p = &(*p)->rb_left;
2239 else
2240 p = &(*p)->rb_right;
2241 }
2242
2243 rb_link_node(&backref->node, parent, p);
2244 rb_insert_color(&backref->node, root);
2245}
2246
2247/*
2248 * Note the backref might has changed, and in this case we just return 0.
2249 */
2250static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2251 void *ctx)
2252{
2253 struct btrfs_file_extent_item *extent;
2254 struct btrfs_fs_info *fs_info;
2255 struct old_sa_defrag_extent *old = ctx;
2256 struct new_sa_defrag_extent *new = old->new;
2257 struct btrfs_path *path = new->path;
2258 struct btrfs_key key;
2259 struct btrfs_root *root;
2260 struct sa_defrag_extent_backref *backref;
2261 struct extent_buffer *leaf;
2262 struct inode *inode = new->inode;
2263 int slot;
2264 int ret;
2265 u64 extent_offset;
2266 u64 num_bytes;
2267
2268 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2269 inum == btrfs_ino(inode))
2270 return 0;
2271
2272 key.objectid = root_id;
2273 key.type = BTRFS_ROOT_ITEM_KEY;
2274 key.offset = (u64)-1;
2275
2276 fs_info = BTRFS_I(inode)->root->fs_info;
2277 root = btrfs_read_fs_root_no_name(fs_info, &key);
2278 if (IS_ERR(root)) {
2279 if (PTR_ERR(root) == -ENOENT)
2280 return 0;
2281 WARN_ON(1);
2282 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2283 inum, offset, root_id);
2284 return PTR_ERR(root);
2285 }
2286
2287 key.objectid = inum;
2288 key.type = BTRFS_EXTENT_DATA_KEY;
2289 if (offset > (u64)-1 << 32)
2290 key.offset = 0;
2291 else
2292 key.offset = offset;
2293
2294 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302295 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002296 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002297 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002298
2299 while (1) {
2300 cond_resched();
2301
2302 leaf = path->nodes[0];
2303 slot = path->slots[0];
2304
2305 if (slot >= btrfs_header_nritems(leaf)) {
2306 ret = btrfs_next_leaf(root, path);
2307 if (ret < 0) {
2308 goto out;
2309 } else if (ret > 0) {
2310 ret = 0;
2311 goto out;
2312 }
2313 continue;
2314 }
2315
2316 path->slots[0]++;
2317
2318 btrfs_item_key_to_cpu(leaf, &key, slot);
2319
2320 if (key.objectid > inum)
2321 goto out;
2322
2323 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2324 continue;
2325
2326 extent = btrfs_item_ptr(leaf, slot,
2327 struct btrfs_file_extent_item);
2328
2329 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2330 continue;
2331
Liu Boe68afa42013-07-01 22:13:26 +08002332 /*
2333 * 'offset' refers to the exact key.offset,
2334 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2335 * (key.offset - extent_offset).
2336 */
2337 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002338 continue;
2339
Liu Boe68afa42013-07-01 22:13:26 +08002340 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002341 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002342
Liu Bo38c227d2013-01-29 03:18:40 +00002343 if (extent_offset >= old->extent_offset + old->offset +
2344 old->len || extent_offset + num_bytes <=
2345 old->extent_offset + old->offset)
2346 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002347 break;
2348 }
2349
2350 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2351 if (!backref) {
2352 ret = -ENOENT;
2353 goto out;
2354 }
2355
2356 backref->root_id = root_id;
2357 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002358 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002359 backref->num_bytes = num_bytes;
2360 backref->extent_offset = extent_offset;
2361 backref->generation = btrfs_file_extent_generation(leaf, extent);
2362 backref->old = old;
2363 backref_insert(&new->root, backref);
2364 old->count++;
2365out:
2366 btrfs_release_path(path);
2367 WARN_ON(ret);
2368 return ret;
2369}
2370
2371static noinline bool record_extent_backrefs(struct btrfs_path *path,
2372 struct new_sa_defrag_extent *new)
2373{
2374 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2375 struct old_sa_defrag_extent *old, *tmp;
2376 int ret;
2377
2378 new->path = path;
2379
2380 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002381 ret = iterate_inodes_from_logical(old->bytenr +
2382 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002383 path, record_one_backref,
2384 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002385 if (ret < 0 && ret != -ENOENT)
2386 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002387
2388 /* no backref to be processed for this extent */
2389 if (!old->count) {
2390 list_del(&old->list);
2391 kfree(old);
2392 }
2393 }
2394
2395 if (list_empty(&new->head))
2396 return false;
2397
2398 return true;
2399}
2400
2401static int relink_is_mergable(struct extent_buffer *leaf,
2402 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002403 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002404{
Liu Bo116e0022013-08-02 16:30:40 +08002405 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002406 return 0;
2407
2408 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2409 return 0;
2410
Liu Bo116e0022013-08-02 16:30:40 +08002411 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2412 return 0;
2413
2414 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002415 btrfs_file_extent_other_encoding(leaf, fi))
2416 return 0;
2417
2418 return 1;
2419}
2420
2421/*
2422 * Note the backref might has changed, and in this case we just return 0.
2423 */
2424static noinline int relink_extent_backref(struct btrfs_path *path,
2425 struct sa_defrag_extent_backref *prev,
2426 struct sa_defrag_extent_backref *backref)
2427{
2428 struct btrfs_file_extent_item *extent;
2429 struct btrfs_file_extent_item *item;
2430 struct btrfs_ordered_extent *ordered;
2431 struct btrfs_trans_handle *trans;
2432 struct btrfs_fs_info *fs_info;
2433 struct btrfs_root *root;
2434 struct btrfs_key key;
2435 struct extent_buffer *leaf;
2436 struct old_sa_defrag_extent *old = backref->old;
2437 struct new_sa_defrag_extent *new = old->new;
2438 struct inode *src_inode = new->inode;
2439 struct inode *inode;
2440 struct extent_state *cached = NULL;
2441 int ret = 0;
2442 u64 start;
2443 u64 len;
2444 u64 lock_start;
2445 u64 lock_end;
2446 bool merge = false;
2447 int index;
2448
2449 if (prev && prev->root_id == backref->root_id &&
2450 prev->inum == backref->inum &&
2451 prev->file_pos + prev->num_bytes == backref->file_pos)
2452 merge = true;
2453
2454 /* step 1: get root */
2455 key.objectid = backref->root_id;
2456 key.type = BTRFS_ROOT_ITEM_KEY;
2457 key.offset = (u64)-1;
2458
2459 fs_info = BTRFS_I(src_inode)->root->fs_info;
2460 index = srcu_read_lock(&fs_info->subvol_srcu);
2461
2462 root = btrfs_read_fs_root_no_name(fs_info, &key);
2463 if (IS_ERR(root)) {
2464 srcu_read_unlock(&fs_info->subvol_srcu, index);
2465 if (PTR_ERR(root) == -ENOENT)
2466 return 0;
2467 return PTR_ERR(root);
2468 }
Liu Bo38c227d2013-01-29 03:18:40 +00002469
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002470 if (btrfs_root_readonly(root)) {
2471 srcu_read_unlock(&fs_info->subvol_srcu, index);
2472 return 0;
2473 }
2474
Liu Bo38c227d2013-01-29 03:18:40 +00002475 /* step 2: get inode */
2476 key.objectid = backref->inum;
2477 key.type = BTRFS_INODE_ITEM_KEY;
2478 key.offset = 0;
2479
2480 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2481 if (IS_ERR(inode)) {
2482 srcu_read_unlock(&fs_info->subvol_srcu, index);
2483 return 0;
2484 }
2485
2486 srcu_read_unlock(&fs_info->subvol_srcu, index);
2487
2488 /* step 3: relink backref */
2489 lock_start = backref->file_pos;
2490 lock_end = backref->file_pos + backref->num_bytes - 1;
2491 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002492 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002493
2494 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2495 if (ordered) {
2496 btrfs_put_ordered_extent(ordered);
2497 goto out_unlock;
2498 }
2499
2500 trans = btrfs_join_transaction(root);
2501 if (IS_ERR(trans)) {
2502 ret = PTR_ERR(trans);
2503 goto out_unlock;
2504 }
2505
2506 key.objectid = backref->inum;
2507 key.type = BTRFS_EXTENT_DATA_KEY;
2508 key.offset = backref->file_pos;
2509
2510 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2511 if (ret < 0) {
2512 goto out_free_path;
2513 } else if (ret > 0) {
2514 ret = 0;
2515 goto out_free_path;
2516 }
2517
2518 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2519 struct btrfs_file_extent_item);
2520
2521 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2522 backref->generation)
2523 goto out_free_path;
2524
2525 btrfs_release_path(path);
2526
2527 start = backref->file_pos;
2528 if (backref->extent_offset < old->extent_offset + old->offset)
2529 start += old->extent_offset + old->offset -
2530 backref->extent_offset;
2531
2532 len = min(backref->extent_offset + backref->num_bytes,
2533 old->extent_offset + old->offset + old->len);
2534 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2535
2536 ret = btrfs_drop_extents(trans, root, inode, start,
2537 start + len, 1);
2538 if (ret)
2539 goto out_free_path;
2540again:
2541 key.objectid = btrfs_ino(inode);
2542 key.type = BTRFS_EXTENT_DATA_KEY;
2543 key.offset = start;
2544
Liu Boa09a0a72013-03-11 09:20:58 +00002545 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002546 if (merge) {
2547 struct btrfs_file_extent_item *fi;
2548 u64 extent_len;
2549 struct btrfs_key found_key;
2550
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002551 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002552 if (ret < 0)
2553 goto out_free_path;
2554
2555 path->slots[0]--;
2556 leaf = path->nodes[0];
2557 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2558
2559 fi = btrfs_item_ptr(leaf, path->slots[0],
2560 struct btrfs_file_extent_item);
2561 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2562
Liu Bo116e0022013-08-02 16:30:40 +08002563 if (extent_len + found_key.offset == start &&
2564 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002565 btrfs_set_file_extent_num_bytes(leaf, fi,
2566 extent_len + len);
2567 btrfs_mark_buffer_dirty(leaf);
2568 inode_add_bytes(inode, len);
2569
2570 ret = 1;
2571 goto out_free_path;
2572 } else {
2573 merge = false;
2574 btrfs_release_path(path);
2575 goto again;
2576 }
2577 }
2578
2579 ret = btrfs_insert_empty_item(trans, root, path, &key,
2580 sizeof(*extent));
2581 if (ret) {
2582 btrfs_abort_transaction(trans, root, ret);
2583 goto out_free_path;
2584 }
2585
2586 leaf = path->nodes[0];
2587 item = btrfs_item_ptr(leaf, path->slots[0],
2588 struct btrfs_file_extent_item);
2589 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2590 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2591 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2592 btrfs_set_file_extent_num_bytes(leaf, item, len);
2593 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2594 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2595 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2596 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2597 btrfs_set_file_extent_encryption(leaf, item, 0);
2598 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2599
2600 btrfs_mark_buffer_dirty(leaf);
2601 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002602 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002603
2604 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2605 new->disk_len, 0,
2606 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002607 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002608 if (ret) {
2609 btrfs_abort_transaction(trans, root, ret);
2610 goto out_free_path;
2611 }
2612
2613 ret = 1;
2614out_free_path:
2615 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002616 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002617 btrfs_end_transaction(trans, root);
2618out_unlock:
2619 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2620 &cached, GFP_NOFS);
2621 iput(inode);
2622 return ret;
2623}
2624
Liu Bo6f519562013-10-29 10:45:05 +08002625static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2626{
2627 struct old_sa_defrag_extent *old, *tmp;
2628
2629 if (!new)
2630 return;
2631
2632 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002633 kfree(old);
2634 }
2635 kfree(new);
2636}
2637
Liu Bo38c227d2013-01-29 03:18:40 +00002638static void relink_file_extents(struct new_sa_defrag_extent *new)
2639{
2640 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002641 struct sa_defrag_extent_backref *backref;
2642 struct sa_defrag_extent_backref *prev = NULL;
2643 struct inode *inode;
2644 struct btrfs_root *root;
2645 struct rb_node *node;
2646 int ret;
2647
2648 inode = new->inode;
2649 root = BTRFS_I(inode)->root;
2650
2651 path = btrfs_alloc_path();
2652 if (!path)
2653 return;
2654
2655 if (!record_extent_backrefs(path, new)) {
2656 btrfs_free_path(path);
2657 goto out;
2658 }
2659 btrfs_release_path(path);
2660
2661 while (1) {
2662 node = rb_first(&new->root);
2663 if (!node)
2664 break;
2665 rb_erase(node, &new->root);
2666
2667 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2668
2669 ret = relink_extent_backref(path, prev, backref);
2670 WARN_ON(ret < 0);
2671
2672 kfree(prev);
2673
2674 if (ret == 1)
2675 prev = backref;
2676 else
2677 prev = NULL;
2678 cond_resched();
2679 }
2680 kfree(prev);
2681
2682 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002683out:
Liu Bo6f519562013-10-29 10:45:05 +08002684 free_sa_defrag_extent(new);
2685
Liu Bo38c227d2013-01-29 03:18:40 +00002686 atomic_dec(&root->fs_info->defrag_running);
2687 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002688}
2689
2690static struct new_sa_defrag_extent *
2691record_old_file_extents(struct inode *inode,
2692 struct btrfs_ordered_extent *ordered)
2693{
2694 struct btrfs_root *root = BTRFS_I(inode)->root;
2695 struct btrfs_path *path;
2696 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002697 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002698 struct new_sa_defrag_extent *new;
2699 int ret;
2700
2701 new = kmalloc(sizeof(*new), GFP_NOFS);
2702 if (!new)
2703 return NULL;
2704
2705 new->inode = inode;
2706 new->file_pos = ordered->file_offset;
2707 new->len = ordered->len;
2708 new->bytenr = ordered->start;
2709 new->disk_len = ordered->disk_len;
2710 new->compress_type = ordered->compress_type;
2711 new->root = RB_ROOT;
2712 INIT_LIST_HEAD(&new->head);
2713
2714 path = btrfs_alloc_path();
2715 if (!path)
2716 goto out_kfree;
2717
2718 key.objectid = btrfs_ino(inode);
2719 key.type = BTRFS_EXTENT_DATA_KEY;
2720 key.offset = new->file_pos;
2721
2722 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2723 if (ret < 0)
2724 goto out_free_path;
2725 if (ret > 0 && path->slots[0] > 0)
2726 path->slots[0]--;
2727
2728 /* find out all the old extents for the file range */
2729 while (1) {
2730 struct btrfs_file_extent_item *extent;
2731 struct extent_buffer *l;
2732 int slot;
2733 u64 num_bytes;
2734 u64 offset;
2735 u64 end;
2736 u64 disk_bytenr;
2737 u64 extent_offset;
2738
2739 l = path->nodes[0];
2740 slot = path->slots[0];
2741
2742 if (slot >= btrfs_header_nritems(l)) {
2743 ret = btrfs_next_leaf(root, path);
2744 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002745 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002746 else if (ret > 0)
2747 break;
2748 continue;
2749 }
2750
2751 btrfs_item_key_to_cpu(l, &key, slot);
2752
2753 if (key.objectid != btrfs_ino(inode))
2754 break;
2755 if (key.type != BTRFS_EXTENT_DATA_KEY)
2756 break;
2757 if (key.offset >= new->file_pos + new->len)
2758 break;
2759
2760 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2761
2762 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2763 if (key.offset + num_bytes < new->file_pos)
2764 goto next;
2765
2766 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2767 if (!disk_bytenr)
2768 goto next;
2769
2770 extent_offset = btrfs_file_extent_offset(l, extent);
2771
2772 old = kmalloc(sizeof(*old), GFP_NOFS);
2773 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002774 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002775
2776 offset = max(new->file_pos, key.offset);
2777 end = min(new->file_pos + new->len, key.offset + num_bytes);
2778
2779 old->bytenr = disk_bytenr;
2780 old->extent_offset = extent_offset;
2781 old->offset = offset - key.offset;
2782 old->len = end - offset;
2783 old->new = new;
2784 old->count = 0;
2785 list_add_tail(&old->list, &new->head);
2786next:
2787 path->slots[0]++;
2788 cond_resched();
2789 }
2790
2791 btrfs_free_path(path);
2792 atomic_inc(&root->fs_info->defrag_running);
2793
2794 return new;
2795
Liu Bo38c227d2013-01-29 03:18:40 +00002796out_free_path:
2797 btrfs_free_path(path);
2798out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002799 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002800 return NULL;
2801}
2802
Miao Xiee570fd22014-06-19 10:42:50 +08002803static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2804 u64 start, u64 len)
2805{
2806 struct btrfs_block_group_cache *cache;
2807
2808 cache = btrfs_lookup_block_group(root->fs_info, start);
2809 ASSERT(cache);
2810
2811 spin_lock(&cache->lock);
2812 cache->delalloc_bytes -= len;
2813 spin_unlock(&cache->lock);
2814
2815 btrfs_put_block_group(cache);
2816}
2817
Chris Masond352ac62008-09-29 15:18:18 -04002818/* as ordered data IO finishes, this gets called so we can finish
2819 * an ordered extent if the range of bytes in the file it covers are
2820 * fully written.
2821 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002822static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002823{
Josef Bacik5fd02042012-05-02 14:00:54 -04002824 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002825 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002826 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002827 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002828 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002829 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002830 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002831 int ret = 0;
2832 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002833 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002834 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002835
Liu Bo83eea1f2012-07-10 05:28:39 -06002836 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002837
Josef Bacik5fd02042012-05-02 14:00:54 -04002838 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2839 ret = -EIO;
2840 goto out;
2841 }
2842
Miao Xief6124962014-09-12 18:44:04 +08002843 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2844 ordered_extent->file_offset +
2845 ordered_extent->len - 1);
2846
Josef Bacik77cef2e2013-08-29 13:57:21 -04002847 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2848 truncated = true;
2849 logical_len = ordered_extent->truncated_len;
2850 /* Truncated the entire extent, don't bother adding */
2851 if (!logical_len)
2852 goto out;
2853 }
2854
Yan, Zhengc2167752009-11-12 09:34:21 +00002855 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002856 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002857
2858 /*
2859 * For mwrite(mmap + memset to write) case, we still reserve
2860 * space for NOCOW range.
2861 * As NOCOW won't cause a new delayed ref, just free the space
2862 */
2863 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2864 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002865 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2866 if (nolock)
2867 trans = btrfs_join_transaction_nolock(root);
2868 else
2869 trans = btrfs_join_transaction(root);
2870 if (IS_ERR(trans)) {
2871 ret = PTR_ERR(trans);
2872 trans = NULL;
2873 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002874 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002875 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2876 ret = btrfs_update_inode_fallback(trans, root, inode);
2877 if (ret) /* -ENOMEM or corruption */
2878 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002879 goto out;
2880 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002881
Josef Bacik2ac55d42010-02-03 19:33:23 +00002882 lock_extent_bits(io_tree, ordered_extent->file_offset,
2883 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002884 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002885
Liu Bo38c227d2013-01-29 03:18:40 +00002886 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2887 ordered_extent->file_offset + ordered_extent->len - 1,
2888 EXTENT_DEFRAG, 1, cached_state);
2889 if (ret) {
2890 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002891 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002892 /* the inode is shared */
2893 new = record_old_file_extents(inode, ordered_extent);
2894
2895 clear_extent_bit(io_tree, ordered_extent->file_offset,
2896 ordered_extent->file_offset + ordered_extent->len - 1,
2897 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2898 }
2899
Josef Bacik0cb59c92010-07-02 12:14:14 -04002900 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002901 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002902 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002903 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002904 if (IS_ERR(trans)) {
2905 ret = PTR_ERR(trans);
2906 trans = NULL;
2907 goto out_unlock;
2908 }
Chris Masona79b7d42014-05-22 16:18:52 -07002909
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002910 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002911
Chris Masonc8b97812008-10-29 14:49:59 -04002912 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002913 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002914 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002915 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002916 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002917 ordered_extent->file_offset,
2918 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002919 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002920 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002921 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002922 ret = insert_reserved_file_extent(trans, inode,
2923 ordered_extent->file_offset,
2924 ordered_extent->start,
2925 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002926 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002927 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002928 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002929 if (!ret)
2930 btrfs_release_delalloc_bytes(root,
2931 ordered_extent->start,
2932 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002933 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002934 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2935 ordered_extent->file_offset, ordered_extent->len,
2936 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002937 if (ret < 0) {
2938 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002939 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002940 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002941
Chris Masone6dcd2d2008-07-17 12:53:50 -04002942 add_pending_csums(trans, inode, ordered_extent->file_offset,
2943 &ordered_extent->list);
2944
Josef Bacik6c760c02012-11-09 10:53:21 -05002945 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2946 ret = btrfs_update_inode_fallback(trans, root, inode);
2947 if (ret) { /* -ENOMEM or corruption */
2948 btrfs_abort_transaction(trans, root, ret);
2949 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002950 }
2951 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002952out_unlock:
2953 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2954 ordered_extent->file_offset +
2955 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002956out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002957 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002958 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002959 if (trans)
2960 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002961
Josef Bacik77cef2e2013-08-29 13:57:21 -04002962 if (ret || truncated) {
2963 u64 start, end;
2964
2965 if (truncated)
2966 start = ordered_extent->file_offset + logical_len;
2967 else
2968 start = ordered_extent->file_offset;
2969 end = ordered_extent->file_offset + ordered_extent->len - 1;
2970 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2971
2972 /* Drop the cache for the part of the extent we didn't write. */
2973 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002974
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002975 /*
2976 * If the ordered extent had an IOERR or something else went
2977 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002978 * back to the allocator. We only free the extent in the
2979 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002980 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002981 if ((ret || !logical_len) &&
2982 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002983 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2984 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002985 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002986 }
2987
2988
Josef Bacik5fd02042012-05-02 14:00:54 -04002989 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002990 * This needs to be done to make sure anybody waiting knows we are done
2991 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002992 */
2993 btrfs_remove_ordered_extent(inode, ordered_extent);
2994
Liu Bo38c227d2013-01-29 03:18:40 +00002995 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002996 if (new) {
2997 if (ret) {
2998 free_sa_defrag_extent(new);
2999 atomic_dec(&root->fs_info->defrag_running);
3000 } else {
3001 relink_file_extents(new);
3002 }
3003 }
Liu Bo38c227d2013-01-29 03:18:40 +00003004
Chris Masone6dcd2d2008-07-17 12:53:50 -04003005 /* once for us */
3006 btrfs_put_ordered_extent(ordered_extent);
3007 /* once for the tree */
3008 btrfs_put_ordered_extent(ordered_extent);
3009
Josef Bacik5fd02042012-05-02 14:00:54 -04003010 return ret;
3011}
3012
3013static void finish_ordered_fn(struct btrfs_work *work)
3014{
3015 struct btrfs_ordered_extent *ordered_extent;
3016 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3017 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003018}
3019
Christoph Hellwigb2950862008-12-02 09:54:17 -05003020static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003021 struct extent_state *state, int uptodate)
3022{
Josef Bacik5fd02042012-05-02 14:00:54 -04003023 struct inode *inode = page->mapping->host;
3024 struct btrfs_root *root = BTRFS_I(inode)->root;
3025 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003026 struct btrfs_workqueue *wq;
3027 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003028
liubo1abe9b82011-03-24 11:18:59 +00003029 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3030
Chris Mason8b62b722009-09-02 16:53:46 -04003031 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003032 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3033 end - start + 1, uptodate))
3034 return 0;
3035
Liu Bo9e0af232014-08-15 23:36:53 +08003036 if (btrfs_is_free_space_inode(inode)) {
3037 wq = root->fs_info->endio_freespace_worker;
3038 func = btrfs_freespace_write_helper;
3039 } else {
3040 wq = root->fs_info->endio_write_workers;
3041 func = btrfs_endio_write_helper;
3042 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003043
Liu Bo9e0af232014-08-15 23:36:53 +08003044 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3045 NULL);
3046 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003047
3048 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003049}
3050
Miao Xiedc380ae2014-09-12 18:43:55 +08003051static int __readpage_endio_check(struct inode *inode,
3052 struct btrfs_io_bio *io_bio,
3053 int icsum, struct page *page,
3054 int pgoff, u64 start, size_t len)
3055{
3056 char *kaddr;
3057 u32 csum_expected;
3058 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003059
3060 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3061
3062 kaddr = kmap_atomic(page);
3063 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3064 btrfs_csum_final(csum, (char *)&csum);
3065 if (csum != csum_expected)
3066 goto zeroit;
3067
3068 kunmap_atomic(kaddr);
3069 return 0;
3070zeroit:
David Sterba94647322015-10-08 11:01:36 +02003071 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3072 "csum failed ino %llu off %llu csum %u expected csum %u",
Miao Xiedc380ae2014-09-12 18:43:55 +08003073 btrfs_ino(inode), start, csum, csum_expected);
3074 memset(kaddr + pgoff, 1, len);
3075 flush_dcache_page(page);
3076 kunmap_atomic(kaddr);
3077 if (csum_expected == 0)
3078 return 0;
3079 return -EIO;
3080}
3081
Chris Masond352ac62008-09-29 15:18:18 -04003082/*
Chris Masond352ac62008-09-29 15:18:18 -04003083 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003084 * if there's a match, we allow the bio to finish. If not, the code in
3085 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003086 */
Miao Xiefacc8a22013-07-25 19:22:34 +08003087static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3088 u64 phy_offset, struct page *page,
3089 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003090{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003091 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003092 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003093 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003094 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003095
Chris Masond20f7042008-12-08 16:58:54 -05003096 if (PageChecked(page)) {
3097 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003098 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003099 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003100
3101 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003102 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003103
Yan Zheng17d217f2008-12-12 10:03:38 -05003104 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003105 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05003106 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
3107 GFP_NOFS);
3108 return 0;
3109 }
3110
Miao Xiefacc8a22013-07-25 19:22:34 +08003111 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003112 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3113 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003114}
Chris Masonb888db22007-08-27 16:49:44 -04003115
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003116struct delayed_iput {
3117 struct list_head list;
3118 struct inode *inode;
3119};
3120
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003121/* JDM: If this is fs-wide, why can't we add a pointer to
3122 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003123void btrfs_add_delayed_iput(struct inode *inode)
3124{
3125 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3126 struct delayed_iput *delayed;
3127
3128 if (atomic_add_unless(&inode->i_count, -1, 1))
3129 return;
3130
3131 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3132 delayed->inode = inode;
3133
3134 spin_lock(&fs_info->delayed_iput_lock);
3135 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3136 spin_unlock(&fs_info->delayed_iput_lock);
3137}
3138
3139void btrfs_run_delayed_iputs(struct btrfs_root *root)
3140{
3141 LIST_HEAD(list);
3142 struct btrfs_fs_info *fs_info = root->fs_info;
3143 struct delayed_iput *delayed;
3144 int empty;
3145
3146 spin_lock(&fs_info->delayed_iput_lock);
3147 empty = list_empty(&fs_info->delayed_iputs);
3148 spin_unlock(&fs_info->delayed_iput_lock);
3149 if (empty)
3150 return;
3151
Zhao Leid7c15172015-02-26 10:49:20 +08003152 down_read(&fs_info->delayed_iput_sem);
3153
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003154 spin_lock(&fs_info->delayed_iput_lock);
3155 list_splice_init(&fs_info->delayed_iputs, &list);
3156 spin_unlock(&fs_info->delayed_iput_lock);
3157
3158 while (!list_empty(&list)) {
3159 delayed = list_entry(list.next, struct delayed_iput, list);
3160 list_del(&delayed->list);
3161 iput(delayed->inode);
3162 kfree(delayed);
3163 }
Zhao Leid7c15172015-02-26 10:49:20 +08003164
3165 up_read(&root->fs_info->delayed_iput_sem);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003166}
3167
Yan, Zhengd68fc572010-05-16 10:49:58 -04003168/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003169 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003170 * files in the subvolume, it removes orphan item and frees block_rsv
3171 * structure.
3172 */
3173void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3174 struct btrfs_root *root)
3175{
Josef Bacik90290e12011-12-02 15:44:12 -05003176 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003177 int ret;
3178
Josef Bacik8a35d952012-05-23 14:26:42 -04003179 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003180 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3181 return;
3182
Josef Bacik90290e12011-12-02 15:44:12 -05003183 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003184 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003185 spin_unlock(&root->orphan_lock);
3186 return;
3187 }
3188
3189 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3190 spin_unlock(&root->orphan_lock);
3191 return;
3192 }
3193
3194 block_rsv = root->orphan_block_rsv;
3195 root->orphan_block_rsv = NULL;
3196 spin_unlock(&root->orphan_lock);
3197
Miao Xie27cdeb72014-04-02 19:51:05 +08003198 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003199 btrfs_root_refs(&root->root_item) > 0) {
3200 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3201 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003202 if (ret)
3203 btrfs_abort_transaction(trans, root, ret);
3204 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003205 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3206 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003207 }
3208
Josef Bacik90290e12011-12-02 15:44:12 -05003209 if (block_rsv) {
3210 WARN_ON(block_rsv->size > 0);
3211 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003212 }
3213}
3214
3215/*
Josef Bacik7b128762008-07-24 12:17:14 -04003216 * This creates an orphan entry for the given inode in case something goes
3217 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003218 *
3219 * NOTE: caller of this function should reserve 5 units of metadata for
3220 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003221 */
3222int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3223{
3224 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003225 struct btrfs_block_rsv *block_rsv = NULL;
3226 int reserve = 0;
3227 int insert = 0;
3228 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003229
Yan, Zhengd68fc572010-05-16 10:49:58 -04003230 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003231 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003232 if (!block_rsv)
3233 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003234 }
3235
Yan, Zhengd68fc572010-05-16 10:49:58 -04003236 spin_lock(&root->orphan_lock);
3237 if (!root->orphan_block_rsv) {
3238 root->orphan_block_rsv = block_rsv;
3239 } else if (block_rsv) {
3240 btrfs_free_block_rsv(root, block_rsv);
3241 block_rsv = NULL;
3242 }
Josef Bacik7b128762008-07-24 12:17:14 -04003243
Josef Bacik8a35d952012-05-23 14:26:42 -04003244 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3245 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003246#if 0
3247 /*
3248 * For proper ENOSPC handling, we should do orphan
3249 * cleanup when mounting. But this introduces backward
3250 * compatibility issue.
3251 */
3252 if (!xchg(&root->orphan_item_inserted, 1))
3253 insert = 2;
3254 else
3255 insert = 1;
3256#endif
3257 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003258 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003259 }
Josef Bacik7b128762008-07-24 12:17:14 -04003260
Josef Bacik72ac3c02012-05-23 14:13:11 -04003261 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3262 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003263 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003264 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003265
Yan, Zhengd68fc572010-05-16 10:49:58 -04003266 /* grab metadata reservation from transaction handle */
3267 if (reserve) {
3268 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003269 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003270 }
3271
3272 /* insert an orphan item to track this unlinked/truncated file */
3273 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003274 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003275 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003276 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003277 if (reserve) {
3278 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3279 &BTRFS_I(inode)->runtime_flags);
3280 btrfs_orphan_release_metadata(inode);
3281 }
3282 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003283 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3284 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003285 btrfs_abort_transaction(trans, root, ret);
3286 return ret;
3287 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003288 }
3289 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003290 }
3291
3292 /* insert an orphan item to track subvolume contains orphan files */
3293 if (insert >= 2) {
3294 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3295 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003296 if (ret && ret != -EEXIST) {
3297 btrfs_abort_transaction(trans, root, ret);
3298 return ret;
3299 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003300 }
3301 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003302}
3303
3304/*
3305 * We have done the truncate/delete so we can go ahead and remove the orphan
3306 * item for this particular inode.
3307 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003308static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3309 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003310{
3311 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003312 int delete_item = 0;
3313 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003314 int ret = 0;
3315
Yan, Zhengd68fc572010-05-16 10:49:58 -04003316 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003317 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3318 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003319 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003320
Josef Bacik72ac3c02012-05-23 14:13:11 -04003321 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3322 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003323 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003324 spin_unlock(&root->orphan_lock);
3325
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003326 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003327 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003328 if (trans)
3329 ret = btrfs_del_orphan_item(trans, root,
3330 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003331 }
Josef Bacik7b128762008-07-24 12:17:14 -04003332
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003333 if (release_rsv)
3334 btrfs_orphan_release_metadata(inode);
3335
Josef Bacik4ef31a42013-08-13 14:10:08 -04003336 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003337}
3338
3339/*
3340 * this cleans up any orphans that may be left on the list from the last use
3341 * of this root.
3342 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003343int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003344{
3345 struct btrfs_path *path;
3346 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003347 struct btrfs_key key, found_key;
3348 struct btrfs_trans_handle *trans;
3349 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003350 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003351 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3352
Yan, Zhengd68fc572010-05-16 10:49:58 -04003353 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003354 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003355
3356 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003357 if (!path) {
3358 ret = -ENOMEM;
3359 goto out;
3360 }
Josef Bacik7b128762008-07-24 12:17:14 -04003361 path->reada = -1;
3362
3363 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003364 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003365 key.offset = (u64)-1;
3366
Josef Bacik7b128762008-07-24 12:17:14 -04003367 while (1) {
3368 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003369 if (ret < 0)
3370 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003371
3372 /*
3373 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003374 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003375 * find the key and see if we have stuff that matches
3376 */
3377 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003378 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003379 if (path->slots[0] == 0)
3380 break;
3381 path->slots[0]--;
3382 }
3383
3384 /* pull out the item */
3385 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003386 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3387
3388 /* make sure the item matches what we want */
3389 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3390 break;
David Sterba962a2982014-06-04 18:41:45 +02003391 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003392 break;
3393
3394 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003395 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003396
3397 /*
3398 * this is where we are basically btrfs_lookup, without the
3399 * crossing root thing. we store the inode number in the
3400 * offset of the orphan item.
3401 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003402
3403 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003404 btrfs_err(root->fs_info,
3405 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003406 ret = -EINVAL;
3407 goto out;
3408 }
3409
3410 last_objectid = found_key.offset;
3411
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003412 found_key.objectid = found_key.offset;
3413 found_key.type = BTRFS_INODE_ITEM_KEY;
3414 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003415 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303416 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003417 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003418 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003419
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003420 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3421 struct btrfs_root *dead_root;
3422 struct btrfs_fs_info *fs_info = root->fs_info;
3423 int is_dead_root = 0;
3424
3425 /*
3426 * this is an orphan in the tree root. Currently these
3427 * could come from 2 sources:
3428 * a) a snapshot deletion in progress
3429 * b) a free space cache inode
3430 * We need to distinguish those two, as the snapshot
3431 * orphan must not get deleted.
3432 * find_dead_roots already ran before us, so if this
3433 * is a snapshot deletion, we should find the root
3434 * in the dead_roots list
3435 */
3436 spin_lock(&fs_info->trans_lock);
3437 list_for_each_entry(dead_root, &fs_info->dead_roots,
3438 root_list) {
3439 if (dead_root->root_key.objectid ==
3440 found_key.objectid) {
3441 is_dead_root = 1;
3442 break;
3443 }
3444 }
3445 spin_unlock(&fs_info->trans_lock);
3446 if (is_dead_root) {
3447 /* prevent this orphan from being found again */
3448 key.offset = found_key.objectid - 1;
3449 continue;
3450 }
3451 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003452 /*
3453 * Inode is already gone but the orphan item is still there,
3454 * kill the orphan item.
3455 */
3456 if (ret == -ESTALE) {
3457 trans = btrfs_start_transaction(root, 1);
3458 if (IS_ERR(trans)) {
3459 ret = PTR_ERR(trans);
3460 goto out;
3461 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003462 btrfs_debug(root->fs_info, "auto deleting %Lu",
3463 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003464 ret = btrfs_del_orphan_item(trans, root,
3465 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003466 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003467 if (ret)
3468 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003469 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003470 }
Josef Bacik7b128762008-07-24 12:17:14 -04003471
Josef Bacik7b128762008-07-24 12:17:14 -04003472 /*
3473 * add this inode to the orphan list so btrfs_orphan_del does
3474 * the proper thing when we hit it
3475 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003476 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3477 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003478 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003479
Josef Bacik7b128762008-07-24 12:17:14 -04003480 /* if we have links, this was a truncate, lets do that */
3481 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303482 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003483 iput(inode);
3484 continue;
3485 }
Josef Bacik7b128762008-07-24 12:17:14 -04003486 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003487
3488 /* 1 for the orphan item deletion. */
3489 trans = btrfs_start_transaction(root, 1);
3490 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003491 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003492 ret = PTR_ERR(trans);
3493 goto out;
3494 }
3495 ret = btrfs_orphan_add(trans, inode);
3496 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003497 if (ret) {
3498 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003499 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003500 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003501
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003502 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003503 if (ret)
3504 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003505 } else {
3506 nr_unlink++;
3507 }
3508
3509 /* this will do delete_inode and everything for us */
3510 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003511 if (ret)
3512 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003513 }
Miao Xie3254c872011-11-10 20:45:05 -05003514 /* release the path since we're done with it */
3515 btrfs_release_path(path);
3516
Yan, Zhengd68fc572010-05-16 10:49:58 -04003517 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3518
3519 if (root->orphan_block_rsv)
3520 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3521 (u64)-1);
3522
Miao Xie27cdeb72014-04-02 19:51:05 +08003523 if (root->orphan_block_rsv ||
3524 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003525 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003526 if (!IS_ERR(trans))
3527 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003528 }
Josef Bacik7b128762008-07-24 12:17:14 -04003529
3530 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003531 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003532 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003533 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003534
3535out:
3536 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003537 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003538 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003539 btrfs_free_path(path);
3540 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003541}
3542
Chris Masond352ac62008-09-29 15:18:18 -04003543/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003544 * very simple check to peek ahead in the leaf looking for xattrs. If we
3545 * don't find any xattrs, we know there can't be any acls.
3546 *
3547 * slot is the slot the inode is in, objectid is the objectid of the inode
3548 */
3549static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003550 int slot, u64 objectid,
3551 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003552{
3553 u32 nritems = btrfs_header_nritems(leaf);
3554 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003555 static u64 xattr_access = 0;
3556 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003557 int scanned = 0;
3558
Josef Bacikf23b5a52013-06-19 10:16:26 -04003559 if (!xattr_access) {
3560 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3561 strlen(POSIX_ACL_XATTR_ACCESS));
3562 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3563 strlen(POSIX_ACL_XATTR_DEFAULT));
3564 }
3565
Chris Mason46a53cc2009-04-27 11:47:50 -04003566 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003567 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003568 while (slot < nritems) {
3569 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3570
3571 /* we found a different objectid, there must not be acls */
3572 if (found_key.objectid != objectid)
3573 return 0;
3574
3575 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003576 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003577 if (*first_xattr_slot == -1)
3578 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003579 if (found_key.offset == xattr_access ||
3580 found_key.offset == xattr_default)
3581 return 1;
3582 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003583
3584 /*
3585 * we found a key greater than an xattr key, there can't
3586 * be any acls later on
3587 */
3588 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3589 return 0;
3590
3591 slot++;
3592 scanned++;
3593
3594 /*
3595 * it goes inode, inode backrefs, xattrs, extents,
3596 * so if there are a ton of hard links to an inode there can
3597 * be a lot of backrefs. Don't waste time searching too hard,
3598 * this is just an optimization
3599 */
3600 if (scanned >= 8)
3601 break;
3602 }
3603 /* we hit the end of the leaf before we found an xattr or
3604 * something larger than an xattr. We have to assume the inode
3605 * has acls
3606 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003607 if (*first_xattr_slot == -1)
3608 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003609 return 1;
3610}
3611
3612/*
Chris Masond352ac62008-09-29 15:18:18 -04003613 * read an inode from the btree into the in-memory inode
3614 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003615static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003616{
3617 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003618 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003619 struct btrfs_inode_item *inode_item;
3620 struct btrfs_root *root = BTRFS_I(inode)->root;
3621 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003622 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003623 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003624 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003625 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003626 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003627 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003628
3629 ret = btrfs_fill_inode(inode, &rdev);
3630 if (!ret)
3631 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003632
3633 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003634 if (!path)
3635 goto make_bad;
3636
Chris Mason39279cc2007-06-12 06:35:45 -04003637 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003638
Chris Mason39279cc2007-06-12 06:35:45 -04003639 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003640 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003641 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003642
Chris Mason5f39d392007-10-15 16:14:19 -04003643 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003644
3645 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003646 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003647
Chris Mason5f39d392007-10-15 16:14:19 -04003648 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3649 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003650 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003651 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003652 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3653 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003654 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003655
David Sterbaa937b972014-12-12 17:39:12 +01003656 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3657 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003658
David Sterbaa937b972014-12-12 17:39:12 +01003659 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3660 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003661
David Sterbaa937b972014-12-12 17:39:12 +01003662 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3663 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003664
chandan r9cc97d62012-07-04 12:48:07 +05303665 BTRFS_I(inode)->i_otime.tv_sec =
3666 btrfs_timespec_sec(leaf, &inode_item->otime);
3667 BTRFS_I(inode)->i_otime.tv_nsec =
3668 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003669
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003670 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003671 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003672 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3673
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003674 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003675 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003676 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003677 rdev = btrfs_inode_rdev(leaf, inode_item);
3678
Josef Bacikaec74772008-07-24 12:12:38 -04003679 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003680 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003681
3682cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003683 /*
3684 * If we were modified in the current generation and evicted from memory
3685 * and then re-read we need to do a full sync since we don't have any
3686 * idea about which extents were modified before we were evicted from
3687 * cache.
3688 *
3689 * This is required for both inode re-read from disk and delayed inode
3690 * in delayed_nodes_tree.
3691 */
3692 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3693 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3694 &BTRFS_I(inode)->runtime_flags);
3695
Filipe Mananabde6c242015-07-24 00:00:19 +01003696 /*
3697 * We don't persist the id of the transaction where an unlink operation
3698 * against the inode was last made. So here we assume the inode might
3699 * have been evicted, and therefore the exact value of last_unlink_trans
3700 * lost, and set it to last_trans to avoid metadata inconsistencies
3701 * between the inode and its parent if the inode is fsync'ed and the log
3702 * replayed. For example, in the scenario:
3703 *
3704 * touch mydir/foo
3705 * ln mydir/foo mydir/bar
3706 * sync
3707 * unlink mydir/bar
3708 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3709 * xfs_io -c fsync mydir/foo
3710 * <power failure>
3711 * mount fs, triggers fsync log replay
3712 *
3713 * We must make sure that when we fsync our inode foo we also log its
3714 * parent inode, otherwise after log replay the parent still has the
3715 * dentry with the "bar" name but our inode foo has a link count of 1
3716 * and doesn't have an inode ref with the name "bar" anymore.
3717 *
3718 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3719 * but it guarantees correctness at the expense of ocassional full
3720 * transaction commits on fsync if our inode is a directory, or if our
3721 * inode is not a directory, logging its parent unnecessarily.
3722 */
3723 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3724
Miao Xie67de1172013-12-26 13:07:06 +08003725 path->slots[0]++;
3726 if (inode->i_nlink != 1 ||
3727 path->slots[0] >= btrfs_header_nritems(leaf))
3728 goto cache_acl;
3729
3730 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3731 if (location.objectid != btrfs_ino(inode))
3732 goto cache_acl;
3733
3734 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3735 if (location.type == BTRFS_INODE_REF_KEY) {
3736 struct btrfs_inode_ref *ref;
3737
3738 ref = (struct btrfs_inode_ref *)ptr;
3739 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3740 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3741 struct btrfs_inode_extref *extref;
3742
3743 extref = (struct btrfs_inode_extref *)ptr;
3744 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3745 extref);
3746 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003747cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003748 /*
3749 * try to precache a NULL acl entry for files that don't have
3750 * any xattrs or acls
3751 */
Li Zefan33345d012011-04-20 10:31:50 +08003752 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003753 btrfs_ino(inode), &first_xattr_slot);
3754 if (first_xattr_slot != -1) {
3755 path->slots[0] = first_xattr_slot;
3756 ret = btrfs_load_inode_props(inode, path);
3757 if (ret)
3758 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003759 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003760 btrfs_ino(inode),
3761 root->root_key.objectid, ret);
3762 }
3763 btrfs_free_path(path);
3764
Al Viro72c04902009-06-24 16:58:48 -04003765 if (!maybe_acls)
3766 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003767
Chris Mason39279cc2007-06-12 06:35:45 -04003768 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003769 case S_IFREG:
3770 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003771 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003772 inode->i_fop = &btrfs_file_operations;
3773 inode->i_op = &btrfs_file_inode_operations;
3774 break;
3775 case S_IFDIR:
3776 inode->i_fop = &btrfs_dir_file_operations;
3777 if (root == root->fs_info->tree_root)
3778 inode->i_op = &btrfs_dir_ro_inode_operations;
3779 else
3780 inode->i_op = &btrfs_dir_inode_operations;
3781 break;
3782 case S_IFLNK:
3783 inode->i_op = &btrfs_symlink_inode_operations;
3784 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3785 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003786 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003787 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003788 init_special_inode(inode, inode->i_mode, rdev);
3789 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003790 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003791
3792 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003793 return;
3794
3795make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003796 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003797 make_bad_inode(inode);
3798}
3799
Chris Masond352ac62008-09-29 15:18:18 -04003800/*
3801 * given a leaf and an inode, copy the inode fields into the leaf
3802 */
Chris Masone02119d2008-09-05 16:13:11 -04003803static void fill_inode_item(struct btrfs_trans_handle *trans,
3804 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003805 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003806 struct inode *inode)
3807{
Liu Bo51fab692012-12-27 09:01:21 +00003808 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003809
Liu Bo51fab692012-12-27 09:01:21 +00003810 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003811
Liu Bo51fab692012-12-27 09:01:21 +00003812 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3813 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3814 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3815 &token);
3816 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3817 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003818
David Sterbaa937b972014-12-12 17:39:12 +01003819 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003820 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003821 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003822 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003823
David Sterbaa937b972014-12-12 17:39:12 +01003824 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003825 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003826 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003827 inode->i_mtime.tv_nsec, &token);
3828
David Sterbaa937b972014-12-12 17:39:12 +01003829 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003830 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003831 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003832 inode->i_ctime.tv_nsec, &token);
3833
chandan r9cc97d62012-07-04 12:48:07 +05303834 btrfs_set_token_timespec_sec(leaf, &item->otime,
3835 BTRFS_I(inode)->i_otime.tv_sec, &token);
3836 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3837 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3838
Liu Bo51fab692012-12-27 09:01:21 +00003839 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3840 &token);
3841 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3842 &token);
3843 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3844 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3845 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3846 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3847 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003848}
3849
Chris Masond352ac62008-09-29 15:18:18 -04003850/*
3851 * copy everything in the in-memory inode into the btree.
3852 */
Chris Mason21151332011-11-10 20:39:08 -05003853static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003854 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003855{
3856 struct btrfs_inode_item *inode_item;
3857 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003858 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003859 int ret;
3860
3861 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003862 if (!path)
3863 return -ENOMEM;
3864
Chris Masonb9473432009-03-13 11:00:37 -04003865 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003866 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3867 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003868 if (ret) {
3869 if (ret > 0)
3870 ret = -ENOENT;
3871 goto failed;
3872 }
3873
Chris Mason5f39d392007-10-15 16:14:19 -04003874 leaf = path->nodes[0];
3875 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003876 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003877
Chris Masone02119d2008-09-05 16:13:11 -04003878 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003879 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003880 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003881 ret = 0;
3882failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003883 btrfs_free_path(path);
3884 return ret;
3885}
3886
Chris Masond352ac62008-09-29 15:18:18 -04003887/*
Chris Mason21151332011-11-10 20:39:08 -05003888 * copy everything in the in-memory inode into the btree.
3889 */
3890noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3891 struct btrfs_root *root, struct inode *inode)
3892{
3893 int ret;
3894
3895 /*
3896 * If the inode is a free space inode, we can deadlock during commit
3897 * if we put it into the delayed code.
3898 *
3899 * The data relocation inode should also be directly updated
3900 * without delay
3901 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003902 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003903 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3904 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003905 btrfs_update_root_times(trans, root);
3906
Chris Mason21151332011-11-10 20:39:08 -05003907 ret = btrfs_delayed_update_inode(trans, root, inode);
3908 if (!ret)
3909 btrfs_set_inode_last_trans(trans, inode);
3910 return ret;
3911 }
3912
3913 return btrfs_update_inode_item(trans, root, inode);
3914}
3915
Josef Bacikbe6aef62012-10-22 15:43:12 -04003916noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3917 struct btrfs_root *root,
3918 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003919{
3920 int ret;
3921
3922 ret = btrfs_update_inode(trans, root, inode);
3923 if (ret == -ENOSPC)
3924 return btrfs_update_inode_item(trans, root, inode);
3925 return ret;
3926}
3927
3928/*
Chris Masond352ac62008-09-29 15:18:18 -04003929 * unlink helper that gets used here in inode.c and in the tree logging
3930 * recovery code. It remove a link in a directory with a given name, and
3931 * also drops the back refs in the inode to the directory
3932 */
Al Viro92986792011-03-04 17:14:37 +00003933static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3934 struct btrfs_root *root,
3935 struct inode *dir, struct inode *inode,
3936 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003937{
3938 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003939 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003940 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003941 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003942 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003943 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003944 u64 ino = btrfs_ino(inode);
3945 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003946
3947 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003948 if (!path) {
3949 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003950 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003951 }
3952
Chris Masonb9473432009-03-13 11:00:37 -04003953 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003954 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003955 name, name_len, -1);
3956 if (IS_ERR(di)) {
3957 ret = PTR_ERR(di);
3958 goto err;
3959 }
3960 if (!di) {
3961 ret = -ENOENT;
3962 goto err;
3963 }
Chris Mason5f39d392007-10-15 16:14:19 -04003964 leaf = path->nodes[0];
3965 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003966 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003967 if (ret)
3968 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003969 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003970
Miao Xie67de1172013-12-26 13:07:06 +08003971 /*
3972 * If we don't have dir index, we have to get it by looking up
3973 * the inode ref, since we get the inode ref, remove it directly,
3974 * it is unnecessary to do delayed deletion.
3975 *
3976 * But if we have dir index, needn't search inode ref to get it.
3977 * Since the inode ref is close to the inode item, it is better
3978 * that we delay to delete it, and just do this deletion when
3979 * we update the inode item.
3980 */
3981 if (BTRFS_I(inode)->dir_index) {
3982 ret = btrfs_delayed_delete_inode_ref(inode);
3983 if (!ret) {
3984 index = BTRFS_I(inode)->dir_index;
3985 goto skip_backref;
3986 }
3987 }
3988
Li Zefan33345d012011-04-20 10:31:50 +08003989 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3990 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003991 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003992 btrfs_info(root->fs_info,
3993 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003994 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003995 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003996 goto err;
3997 }
Miao Xie67de1172013-12-26 13:07:06 +08003998skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003999 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004000 if (ret) {
4001 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004002 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004003 }
Chris Mason39279cc2007-06-12 06:35:45 -04004004
Chris Masone02119d2008-09-05 16:13:11 -04004005 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08004006 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004007 if (ret != 0 && ret != -ENOENT) {
4008 btrfs_abort_transaction(trans, root, ret);
4009 goto err;
4010 }
Chris Masone02119d2008-09-05 16:13:11 -04004011
4012 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
4013 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04004014 if (ret == -ENOENT)
4015 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004016 else if (ret)
4017 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004018err:
4019 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004020 if (ret)
4021 goto out;
4022
4023 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004024 inode_inc_iversion(inode);
4025 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04004026 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06004027 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004028out:
Chris Mason39279cc2007-06-12 06:35:45 -04004029 return ret;
4030}
4031
Al Viro92986792011-03-04 17:14:37 +00004032int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4033 struct btrfs_root *root,
4034 struct inode *dir, struct inode *inode,
4035 const char *name, int name_len)
4036{
4037 int ret;
4038 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4039 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004040 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004041 ret = btrfs_update_inode(trans, root, inode);
4042 }
4043 return ret;
4044}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004045
4046/*
4047 * helper to start transaction for unlink and rmdir.
4048 *
Josef Bacikd52be812013-05-29 14:54:47 -04004049 * unlink and rmdir are special in btrfs, they do not always free space, so
4050 * if we cannot make our reservations the normal way try and see if there is
4051 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4052 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004053 */
Josef Bacikd52be812013-05-29 14:54:47 -04004054static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004055{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004056 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004057
Josef Bacike70bea52011-10-11 14:18:24 -04004058 /*
4059 * 1 for the possible orphan item
4060 * 1 for the dir item
4061 * 1 for the dir index
4062 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004063 * 1 for the inode
4064 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004065 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004066}
4067
Chris Mason39279cc2007-06-12 06:35:45 -04004068static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4069{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004070 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004071 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004072 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004073 int ret;
4074
Josef Bacikd52be812013-05-29 14:54:47 -04004075 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004076 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004077 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004078
David Howells2b0143b2015-03-17 22:25:59 +00004079 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004080
David Howells2b0143b2015-03-17 22:25:59 +00004081 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004082 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004083 if (ret)
4084 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004085
Yan, Zhenga22285a2010-05-16 10:48:46 -04004086 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004087 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004088 if (ret)
4089 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004090 }
Josef Bacik7b128762008-07-24 12:17:14 -04004091
Tsutomu Itohb5324022011-07-19 07:27:20 +00004092out:
Josef Bacikd52be812013-05-29 14:54:47 -04004093 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004094 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004095 return ret;
4096}
4097
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004098int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4099 struct btrfs_root *root,
4100 struct inode *dir, u64 objectid,
4101 const char *name, int name_len)
4102{
4103 struct btrfs_path *path;
4104 struct extent_buffer *leaf;
4105 struct btrfs_dir_item *di;
4106 struct btrfs_key key;
4107 u64 index;
4108 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004109 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004110
4111 path = btrfs_alloc_path();
4112 if (!path)
4113 return -ENOMEM;
4114
Li Zefan33345d012011-04-20 10:31:50 +08004115 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004116 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004117 if (IS_ERR_OR_NULL(di)) {
4118 if (!di)
4119 ret = -ENOENT;
4120 else
4121 ret = PTR_ERR(di);
4122 goto out;
4123 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004124
4125 leaf = path->nodes[0];
4126 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4127 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4128 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004129 if (ret) {
4130 btrfs_abort_transaction(trans, root, ret);
4131 goto out;
4132 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004133 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004134
4135 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4136 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004137 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004138 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004139 if (ret != -ENOENT) {
4140 btrfs_abort_transaction(trans, root, ret);
4141 goto out;
4142 }
Li Zefan33345d012011-04-20 10:31:50 +08004143 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004144 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004145 if (IS_ERR_OR_NULL(di)) {
4146 if (!di)
4147 ret = -ENOENT;
4148 else
4149 ret = PTR_ERR(di);
4150 btrfs_abort_transaction(trans, root, ret);
4151 goto out;
4152 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004153
4154 leaf = path->nodes[0];
4155 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004156 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004157 index = key.offset;
4158 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004159 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004160
Miao Xie16cdcec2011-04-22 18:12:22 +08004161 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004162 if (ret) {
4163 btrfs_abort_transaction(trans, root, ret);
4164 goto out;
4165 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004166
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004167 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004168 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004169 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004170 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004171 if (ret)
4172 btrfs_abort_transaction(trans, root, ret);
4173out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004174 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004175 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004176}
4177
Chris Mason39279cc2007-06-12 06:35:45 -04004178static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4179{
David Howells2b0143b2015-03-17 22:25:59 +00004180 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004181 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004182 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004183 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004184
David Sterbab3ae2442012-09-13 16:04:34 -06004185 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004186 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004187 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4188 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004189
Josef Bacikd52be812013-05-29 14:54:47 -04004190 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004191 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004192 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004193
Li Zefan33345d012011-04-20 10:31:50 +08004194 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004195 err = btrfs_unlink_subvol(trans, root, dir,
4196 BTRFS_I(inode)->location.objectid,
4197 dentry->d_name.name,
4198 dentry->d_name.len);
4199 goto out;
4200 }
4201
Josef Bacik7b128762008-07-24 12:17:14 -04004202 err = btrfs_orphan_add(trans, inode);
4203 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004204 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004205
Chris Mason39279cc2007-06-12 06:35:45 -04004206 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004207 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004208 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004209 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004210 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004211out:
Josef Bacikd52be812013-05-29 14:54:47 -04004212 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004213 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004214
Chris Mason39279cc2007-06-12 06:35:45 -04004215 return err;
4216}
4217
Chris Mason28f75a02015-02-04 06:59:29 -08004218static int truncate_space_check(struct btrfs_trans_handle *trans,
4219 struct btrfs_root *root,
4220 u64 bytes_deleted)
4221{
4222 int ret;
4223
4224 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
4225 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4226 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4227 if (!ret)
4228 trans->bytes_reserved += bytes_deleted;
4229 return ret;
4230
4231}
4232
Filipe Manana0305cd52015-10-16 12:34:25 +01004233static int truncate_inline_extent(struct inode *inode,
4234 struct btrfs_path *path,
4235 struct btrfs_key *found_key,
4236 const u64 item_end,
4237 const u64 new_size)
4238{
4239 struct extent_buffer *leaf = path->nodes[0];
4240 int slot = path->slots[0];
4241 struct btrfs_file_extent_item *fi;
4242 u32 size = (u32)(new_size - found_key->offset);
4243 struct btrfs_root *root = BTRFS_I(inode)->root;
4244
4245 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4246
4247 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4248 loff_t offset = new_size;
4249 loff_t page_end = ALIGN(offset, PAGE_CACHE_SIZE);
4250
4251 /*
4252 * Zero out the remaining of the last page of our inline extent,
4253 * instead of directly truncating our inline extent here - that
4254 * would be much more complex (decompressing all the data, then
4255 * compressing the truncated data, which might be bigger than
4256 * the size of the inline extent, resize the extent, etc).
4257 * We release the path because to get the page we might need to
4258 * read the extent item from disk (data not in the page cache).
4259 */
4260 btrfs_release_path(path);
4261 return btrfs_truncate_page(inode, offset, page_end - offset, 0);
4262 }
4263
4264 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4265 size = btrfs_file_extent_calc_inline_size(size);
4266 btrfs_truncate_item(root, path, size, 1);
4267
4268 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4269 inode_sub_bytes(inode, item_end + 1 - new_size);
4270
4271 return 0;
4272}
4273
Chris Mason323ac952008-10-01 19:05:46 -04004274/*
Chris Mason39279cc2007-06-12 06:35:45 -04004275 * this can truncate away extent items, csum items and directory items.
4276 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004277 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004278 *
4279 * csum items that cross the new i_size are truncated to the new size
4280 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004281 *
4282 * min_type is the minimum key type to truncate down to. If set to 0, this
4283 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004284 */
Yan, Zheng80825102009-11-12 09:35:36 +00004285int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4286 struct btrfs_root *root,
4287 struct inode *inode,
4288 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004289{
Chris Mason39279cc2007-06-12 06:35:45 -04004290 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004291 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004292 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004293 struct btrfs_key key;
4294 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004295 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004296 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004297 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004298 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004299 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004300 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004301 int found_extent;
4302 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004303 int pending_del_nr = 0;
4304 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004305 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004306 int ret;
4307 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004308 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004309 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004310 bool be_nice = 0;
4311 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004312 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004313
4314 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004315
Chris Mason28ed1342014-12-17 09:41:04 -08004316 /*
4317 * for non-free space inodes and ref cows, we want to back off from
4318 * time to time
4319 */
4320 if (!btrfs_is_free_space_inode(inode) &&
4321 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4322 be_nice = 1;
4323
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004324 path = btrfs_alloc_path();
4325 if (!path)
4326 return -ENOMEM;
4327 path->reada = -1;
4328
Josef Bacik5dc562c2012-08-17 13:14:17 -04004329 /*
4330 * We want to drop from the next block forward in case this new size is
4331 * not block aligned since we will be keeping the last block of the
4332 * extent just the way it is.
4333 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004334 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4335 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004336 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4337 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004338
Miao Xie16cdcec2011-04-22 18:12:22 +08004339 /*
4340 * This function is also used to drop the items in the log tree before
4341 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4342 * it is used to drop the loged items. So we shouldn't kill the delayed
4343 * items.
4344 */
4345 if (min_type == 0 && root == BTRFS_I(inode)->root)
4346 btrfs_kill_delayed_inode_items(inode);
4347
Li Zefan33345d012011-04-20 10:31:50 +08004348 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004349 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004350 key.type = (u8)-1;
4351
Chris Mason85e21ba2008-01-29 15:11:36 -05004352search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004353 /*
4354 * with a 16K leaf size and 128MB extents, you can actually queue
4355 * up a huge file in a single leaf. Most of the time that
4356 * bytes_deleted is > 0, it will be huge by the time we get here
4357 */
4358 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
4359 if (btrfs_should_end_transaction(trans, root)) {
4360 err = -EAGAIN;
4361 goto error;
4362 }
4363 }
4364
4365
Chris Masonb9473432009-03-13 11:00:37 -04004366 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004367 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004368 if (ret < 0) {
4369 err = ret;
4370 goto out;
4371 }
Chris Masond3977122009-01-05 21:25:51 -05004372
Chris Mason85e21ba2008-01-29 15:11:36 -05004373 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004374 /* there are no items in the tree for us to truncate, we're
4375 * done
4376 */
Yan, Zheng80825102009-11-12 09:35:36 +00004377 if (path->slots[0] == 0)
4378 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004379 path->slots[0]--;
4380 }
4381
Chris Masond3977122009-01-05 21:25:51 -05004382 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004383 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004384 leaf = path->nodes[0];
4385 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004386 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004387
Li Zefan33345d012011-04-20 10:31:50 +08004388 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004389 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004390
Chris Mason85e21ba2008-01-29 15:11:36 -05004391 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004392 break;
4393
Chris Mason5f39d392007-10-15 16:14:19 -04004394 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004395 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004396 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004397 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004398 extent_type = btrfs_file_extent_type(leaf, fi);
4399 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004400 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004401 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004402 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004403 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004404 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004405 }
Yan008630c2007-11-07 13:31:09 -05004406 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004407 }
Yan, Zheng80825102009-11-12 09:35:36 +00004408 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004409 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004410 } else {
4411 if (item_end < new_size)
4412 break;
4413 if (found_key.offset >= new_size)
4414 del_item = 1;
4415 else
4416 del_item = 0;
4417 }
Chris Mason39279cc2007-06-12 06:35:45 -04004418 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004419 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004420 if (found_type != BTRFS_EXTENT_DATA_KEY)
4421 goto delete;
4422
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004423 if (del_item)
4424 last_size = found_key.offset;
4425 else
4426 last_size = new_size;
4427
Chris Mason179e29e2007-11-01 11:28:41 -04004428 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004429 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004430 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004431 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004432 u64 orig_num_bytes =
4433 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004434 extent_num_bytes = ALIGN(new_size -
4435 found_key.offset,
4436 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004437 btrfs_set_file_extent_num_bytes(leaf, fi,
4438 extent_num_bytes);
4439 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004440 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004441 if (test_bit(BTRFS_ROOT_REF_COWS,
4442 &root->state) &&
4443 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004444 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004445 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004446 } else {
Chris Masondb945352007-10-15 16:15:53 -04004447 extent_num_bytes =
4448 btrfs_file_extent_disk_num_bytes(leaf,
4449 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004450 extent_offset = found_key.offset -
4451 btrfs_file_extent_offset(leaf, fi);
4452
Chris Mason39279cc2007-06-12 06:35:45 -04004453 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004454 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004455 if (extent_start != 0) {
4456 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004457 if (test_bit(BTRFS_ROOT_REF_COWS,
4458 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004459 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004460 }
4461 }
Chris Mason90692182008-02-08 13:49:28 -05004462 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004463 /*
4464 * we can't truncate inline items that have had
4465 * special encodings
4466 */
4467 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004468 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4469 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004470
4471 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004472 * Need to release path in order to truncate a
4473 * compressed extent. So delete any accumulated
4474 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004475 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004476 if (btrfs_file_extent_compression(leaf, fi) !=
4477 BTRFS_COMPRESS_NONE && pending_del_nr) {
4478 err = btrfs_del_items(trans, root, path,
4479 pending_del_slot,
4480 pending_del_nr);
4481 if (err) {
4482 btrfs_abort_transaction(trans,
4483 root,
4484 err);
4485 goto error;
4486 }
4487 pending_del_nr = 0;
4488 }
4489
4490 err = truncate_inline_extent(inode, path,
4491 &found_key,
4492 item_end,
4493 new_size);
4494 if (err) {
4495 btrfs_abort_transaction(trans,
4496 root, err);
4497 goto error;
4498 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004499 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4500 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004501 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004502 }
Chris Mason39279cc2007-06-12 06:35:45 -04004503 }
Chris Mason179e29e2007-11-01 11:28:41 -04004504delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004505 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004506 if (!pending_del_nr) {
4507 /* no pending yet, add ourselves */
4508 pending_del_slot = path->slots[0];
4509 pending_del_nr = 1;
4510 } else if (pending_del_nr &&
4511 path->slots[0] + 1 == pending_del_slot) {
4512 /* hop on the pending chunk */
4513 pending_del_nr++;
4514 pending_del_slot = path->slots[0];
4515 } else {
Chris Masond3977122009-01-05 21:25:51 -05004516 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004517 }
Chris Mason39279cc2007-06-12 06:35:45 -04004518 } else {
4519 break;
4520 }
Chris Mason28f75a02015-02-04 06:59:29 -08004521 should_throttle = 0;
4522
Miao Xie27cdeb72014-04-02 19:51:05 +08004523 if (found_extent &&
4524 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4525 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004526 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004527 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004528 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004529 extent_num_bytes, 0,
4530 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004531 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004532 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004533 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004534 btrfs_async_run_delayed_refs(root,
4535 trans->delayed_ref_updates * 2, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004536 if (be_nice) {
4537 if (truncate_space_check(trans, root,
4538 extent_num_bytes)) {
4539 should_end = 1;
4540 }
4541 if (btrfs_should_throttle_delayed_refs(trans,
4542 root)) {
4543 should_throttle = 1;
4544 }
4545 }
Chris Mason39279cc2007-06-12 06:35:45 -04004546 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004547
Yan, Zheng80825102009-11-12 09:35:36 +00004548 if (found_type == BTRFS_INODE_ITEM_KEY)
4549 break;
4550
4551 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004552 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004553 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004554 if (pending_del_nr) {
4555 ret = btrfs_del_items(trans, root, path,
4556 pending_del_slot,
4557 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004558 if (ret) {
4559 btrfs_abort_transaction(trans,
4560 root, ret);
4561 goto error;
4562 }
Yan, Zheng80825102009-11-12 09:35:36 +00004563 pending_del_nr = 0;
4564 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004565 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004566 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004567 unsigned long updates = trans->delayed_ref_updates;
4568 if (updates) {
4569 trans->delayed_ref_updates = 0;
4570 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4571 if (ret && !err)
4572 err = ret;
4573 }
4574 }
Chris Mason28f75a02015-02-04 06:59:29 -08004575 /*
4576 * if we failed to refill our space rsv, bail out
4577 * and let the transaction restart
4578 */
4579 if (should_end) {
4580 err = -EAGAIN;
4581 goto error;
4582 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004583 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004584 } else {
4585 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004586 }
Chris Mason39279cc2007-06-12 06:35:45 -04004587 }
Yan, Zheng80825102009-11-12 09:35:36 +00004588out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004589 if (pending_del_nr) {
4590 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4591 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004592 if (ret)
4593 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004594 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004595error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004596 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004597 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004598
Chris Mason39279cc2007-06-12 06:35:45 -04004599 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004600
Chris Mason28f75a02015-02-04 06:59:29 -08004601 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
Chris Mason28ed1342014-12-17 09:41:04 -08004602 unsigned long updates = trans->delayed_ref_updates;
4603 if (updates) {
4604 trans->delayed_ref_updates = 0;
4605 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4606 if (ret && !err)
4607 err = ret;
4608 }
4609 }
Yan, Zheng80825102009-11-12 09:35:36 +00004610 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004611}
4612
Chris Masona52d9a82007-08-27 16:49:44 -04004613/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004614 * btrfs_truncate_page - read, zero a chunk and write a page
4615 * @inode - inode that we're zeroing
4616 * @from - the offset to start zeroing
4617 * @len - the length to zero, 0 to zero the entire range respective to the
4618 * offset
4619 * @front - zero up to the offset instead of from the offset on
4620 *
4621 * This will find the page for the "from" offset and cow the page and zero the
4622 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004623 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004624int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4625 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004626{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004627 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004628 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004629 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4630 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004631 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004632 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004633 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004634 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4635 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4636 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004637 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004638 int ret = 0;
4639 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004640 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004641
Josef Bacik2aaa6652012-08-29 14:27:18 -04004642 if ((offset & (blocksize - 1)) == 0 &&
4643 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004644 goto out;
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004645 ret = btrfs_delalloc_reserve_space(inode,
Qu Wenruodf480632015-09-08 17:25:54 +08004646 round_down(from, PAGE_CACHE_SIZE), PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004647 if (ret)
4648 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004649
Chris Mason211c17f2008-05-15 09:13:45 -04004650again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004651 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004652 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004653 btrfs_delalloc_release_space(inode,
Qu Wenruodf480632015-09-08 17:25:54 +08004654 round_down(from, PAGE_CACHE_SIZE),
4655 PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004656 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004657 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004658 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004659
4660 page_start = page_offset(page);
4661 page_end = page_start + PAGE_CACHE_SIZE - 1;
4662
Chris Masona52d9a82007-08-27 16:49:44 -04004663 if (!PageUptodate(page)) {
4664 ret = btrfs_readpage(NULL, page);
4665 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004666 if (page->mapping != mapping) {
4667 unlock_page(page);
4668 page_cache_release(page);
4669 goto again;
4670 }
Chris Masona52d9a82007-08-27 16:49:44 -04004671 if (!PageUptodate(page)) {
4672 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004673 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004674 }
4675 }
Chris Mason211c17f2008-05-15 09:13:45 -04004676 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004677
David Sterbaff13db42015-12-03 14:30:40 +01004678 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004679 set_page_extent_mapped(page);
4680
4681 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4682 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004683 unlock_extent_cached(io_tree, page_start, page_end,
4684 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004685 unlock_page(page);
4686 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004687 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004688 btrfs_put_ordered_extent(ordered);
4689 goto again;
4690 }
4691
Josef Bacik2ac55d42010-02-03 19:33:23 +00004692 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004693 EXTENT_DIRTY | EXTENT_DELALLOC |
4694 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004695 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004696
Josef Bacik2ac55d42010-02-03 19:33:23 +00004697 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4698 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004699 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004700 unlock_extent_cached(io_tree, page_start, page_end,
4701 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004702 goto out_unlock;
4703 }
4704
Chris Masone6dcd2d2008-07-17 12:53:50 -04004705 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004706 if (!len)
4707 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004708 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004709 if (front)
4710 memset(kaddr, 0, offset);
4711 else
4712 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004713 flush_dcache_page(page);
4714 kunmap(page);
4715 }
Chris Mason247e7432008-07-17 12:53:51 -04004716 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004717 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004718 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4719 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004720
Chris Mason89642222008-07-24 09:41:53 -04004721out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004722 if (ret)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004723 btrfs_delalloc_release_space(inode, page_start,
4724 PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004725 unlock_page(page);
4726 page_cache_release(page);
4727out:
4728 return ret;
4729}
4730
Josef Bacik16e75492013-10-22 12:18:51 -04004731static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4732 u64 offset, u64 len)
4733{
4734 struct btrfs_trans_handle *trans;
4735 int ret;
4736
4737 /*
4738 * Still need to make sure the inode looks like it's been updated so
4739 * that any holes get logged if we fsync.
4740 */
4741 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4742 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4743 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4744 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4745 return 0;
4746 }
4747
4748 /*
4749 * 1 - for the one we're dropping
4750 * 1 - for the one we're adding
4751 * 1 - for updating the inode.
4752 */
4753 trans = btrfs_start_transaction(root, 3);
4754 if (IS_ERR(trans))
4755 return PTR_ERR(trans);
4756
4757 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4758 if (ret) {
4759 btrfs_abort_transaction(trans, root, ret);
4760 btrfs_end_transaction(trans, root);
4761 return ret;
4762 }
4763
4764 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4765 0, 0, len, 0, len, 0, 0, 0);
4766 if (ret)
4767 btrfs_abort_transaction(trans, root, ret);
4768 else
4769 btrfs_update_inode(trans, root, inode);
4770 btrfs_end_transaction(trans, root);
4771 return ret;
4772}
4773
Josef Bacik695a0d02011-03-04 15:46:53 -05004774/*
4775 * This function puts in dummy file extents for the area we're creating a hole
4776 * for. So if we are truncating this file to a larger size we need to insert
4777 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4778 * the range between oldsize and size
4779 */
Josef Bacika41ad392011-01-31 15:30:16 -05004780int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004781{
Yan Zheng9036c102008-10-30 14:19:41 -04004782 struct btrfs_root *root = BTRFS_I(inode)->root;
4783 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004784 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004785 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004786 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004787 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4788 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004789 u64 last_byte;
4790 u64 cur_offset;
4791 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004792 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004793
Josef Bacika71754f2013-06-17 17:14:39 -04004794 /*
4795 * If our size started in the middle of a page we need to zero out the
4796 * rest of the page before we expand the i_size, otherwise we could
4797 * expose stale data.
4798 */
4799 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4800 if (err)
4801 return err;
4802
Yan Zheng9036c102008-10-30 14:19:41 -04004803 if (size <= hole_start)
4804 return 0;
4805
Yan Zheng9036c102008-10-30 14:19:41 -04004806 while (1) {
4807 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004808
David Sterbaff13db42015-12-03 14:30:40 +01004809 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004810 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004811 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4812 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004813 if (!ordered)
4814 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004815 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4816 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004817 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004818 btrfs_put_ordered_extent(ordered);
4819 }
4820
Yan Zheng9036c102008-10-30 14:19:41 -04004821 cur_offset = hole_start;
4822 while (1) {
4823 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4824 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004825 if (IS_ERR(em)) {
4826 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004827 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004828 break;
4829 }
Yan Zheng9036c102008-10-30 14:19:41 -04004830 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004831 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004832 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004833 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004834 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004835
Josef Bacik16e75492013-10-22 12:18:51 -04004836 err = maybe_insert_hole(root, inode, cur_offset,
4837 hole_size);
4838 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004839 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004840 btrfs_drop_extent_cache(inode, cur_offset,
4841 cur_offset + hole_size - 1, 0);
4842 hole_em = alloc_extent_map();
4843 if (!hole_em) {
4844 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4845 &BTRFS_I(inode)->runtime_flags);
4846 goto next;
4847 }
4848 hole_em->start = cur_offset;
4849 hole_em->len = hole_size;
4850 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004851
Josef Bacik5dc562c2012-08-17 13:14:17 -04004852 hole_em->block_start = EXTENT_MAP_HOLE;
4853 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004854 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004855 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004856 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4857 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004858 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004859
4860 while (1) {
4861 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004862 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004863 write_unlock(&em_tree->lock);
4864 if (err != -EEXIST)
4865 break;
4866 btrfs_drop_extent_cache(inode, cur_offset,
4867 cur_offset +
4868 hole_size - 1, 0);
4869 }
4870 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004871 }
Josef Bacik16e75492013-10-22 12:18:51 -04004872next:
Yan Zheng9036c102008-10-30 14:19:41 -04004873 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004874 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004875 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004876 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004877 break;
4878 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004879 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004880 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4881 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004882 return err;
4883}
4884
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004885static int wait_snapshoting_atomic_t(atomic_t *a)
4886{
4887 schedule();
4888 return 0;
4889}
4890
4891static void wait_for_snapshot_creation(struct btrfs_root *root)
4892{
4893 while (true) {
4894 int ret;
4895
4896 ret = btrfs_start_write_no_snapshoting(root);
4897 if (ret)
4898 break;
4899 wait_on_atomic_t(&root->will_be_snapshoted,
4900 wait_snapshoting_atomic_t,
4901 TASK_UNINTERRUPTIBLE);
4902 }
4903}
4904
Eric Sandeen3972f262013-01-12 02:57:22 +00004905static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004906{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004907 struct btrfs_root *root = BTRFS_I(inode)->root;
4908 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004909 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004910 loff_t newsize = attr->ia_size;
4911 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004912 int ret;
4913
Eric Sandeen3972f262013-01-12 02:57:22 +00004914 /*
4915 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4916 * special case where we need to update the times despite not having
4917 * these flags set. For all other operations the VFS set these flags
4918 * explicitly if it wants a timestamp update.
4919 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004920 if (newsize != oldsize) {
4921 inode_inc_iversion(inode);
4922 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4923 inode->i_ctime = inode->i_mtime =
4924 current_fs_time(inode->i_sb);
4925 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004926
Josef Bacika41ad392011-01-31 15:30:16 -05004927 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004928 truncate_pagecache(inode, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004929 /*
4930 * Don't do an expanding truncate while snapshoting is ongoing.
4931 * This is to ensure the snapshot captures a fully consistent
4932 * state of this file - if the snapshot captures this expanding
4933 * truncation, it must capture all writes that happened before
4934 * this truncation.
4935 */
4936 wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004937 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004938 if (ret) {
4939 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004940 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004941 }
Yan, Zheng80825102009-11-12 09:35:36 +00004942
Miao Xief4a2f4c2011-12-14 20:12:01 -05004943 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004944 if (IS_ERR(trans)) {
4945 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004946 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004947 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004948
4949 i_size_write(inode, newsize);
4950 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4951 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004952 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004953 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004954 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004955
Josef Bacika41ad392011-01-31 15:30:16 -05004956 /*
4957 * We're truncating a file that used to have good data down to
4958 * zero. Make sure it gets into the ordered flush list so that
4959 * any new writes get down to disk quickly.
4960 */
4961 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004962 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4963 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004964
Josef Bacikf3fe8202013-01-07 17:03:21 -05004965 /*
4966 * 1 for the orphan item we're going to add
4967 * 1 for the orphan item deletion.
4968 */
4969 trans = btrfs_start_transaction(root, 2);
4970 if (IS_ERR(trans))
4971 return PTR_ERR(trans);
4972
4973 /*
4974 * We need to do this in case we fail at _any_ point during the
4975 * actual truncate. Once we do the truncate_setsize we could
4976 * invalidate pages which forces any outstanding ordered io to
4977 * be instantly completed which will give us extents that need
4978 * to be truncated. If we fail to get an orphan inode down we
4979 * could have left over extents that were never meant to live,
4980 * so we need to garuntee from this point on that everything
4981 * will be consistent.
4982 */
4983 ret = btrfs_orphan_add(trans, inode);
4984 btrfs_end_transaction(trans, root);
4985 if (ret)
4986 return ret;
4987
Josef Bacika41ad392011-01-31 15:30:16 -05004988 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4989 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004990
4991 /* Disable nonlocked read DIO to avoid the end less truncate */
4992 btrfs_inode_block_unlocked_dio(inode);
4993 inode_dio_wait(inode);
4994 btrfs_inode_resume_unlocked_dio(inode);
4995
Josef Bacika41ad392011-01-31 15:30:16 -05004996 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004997 if (ret && inode->i_nlink) {
4998 int err;
4999
5000 /*
5001 * failed to truncate, disk_i_size is only adjusted down
5002 * as we remove extents, so it should represent the true
5003 * size of the inode, so reset the in memory size and
5004 * delete our orphan entry.
5005 */
5006 trans = btrfs_join_transaction(root);
5007 if (IS_ERR(trans)) {
5008 btrfs_orphan_del(NULL, inode);
5009 return ret;
5010 }
5011 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5012 err = btrfs_orphan_del(trans, inode);
5013 if (err)
5014 btrfs_abort_transaction(trans, root, err);
5015 btrfs_end_transaction(trans, root);
5016 }
Yan, Zheng80825102009-11-12 09:35:36 +00005017 }
5018
Josef Bacika41ad392011-01-31 15:30:16 -05005019 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005020}
5021
Chris Mason39279cc2007-06-12 06:35:45 -04005022static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5023{
David Howells2b0143b2015-03-17 22:25:59 +00005024 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005025 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005026 int err;
5027
Li Zefanb83cc962010-12-20 16:04:08 +08005028 if (btrfs_root_readonly(root))
5029 return -EROFS;
5030
Chris Mason39279cc2007-06-12 06:35:45 -04005031 err = inode_change_ok(inode, attr);
5032 if (err)
5033 return err;
5034
Chris Mason5a3f23d2009-03-31 13:27:11 -04005035 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005036 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005037 if (err)
5038 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005039 }
Yan Zheng9036c102008-10-30 14:19:41 -04005040
Christoph Hellwig10257742010-06-04 11:30:02 +02005041 if (attr->ia_valid) {
5042 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005043 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005044 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005045
Josef Bacik22c44fe2011-11-30 10:45:38 -05005046 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005047 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005048 }
5049
Chris Mason39279cc2007-06-12 06:35:45 -04005050 return err;
5051}
Chris Mason61295eb2008-01-14 16:24:38 -05005052
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005053/*
5054 * While truncating the inode pages during eviction, we get the VFS calling
5055 * btrfs_invalidatepage() against each page of the inode. This is slow because
5056 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5057 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5058 * extent_state structures over and over, wasting lots of time.
5059 *
5060 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5061 * those expensive operations on a per page basis and do only the ordered io
5062 * finishing, while we release here the extent_map and extent_state structures,
5063 * without the excessive merging and splitting.
5064 */
5065static void evict_inode_truncate_pages(struct inode *inode)
5066{
5067 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5068 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5069 struct rb_node *node;
5070
5071 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005072 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005073
5074 write_lock(&map_tree->lock);
5075 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5076 struct extent_map *em;
5077
5078 node = rb_first(&map_tree->map);
5079 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005080 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5081 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005082 remove_extent_mapping(map_tree, em);
5083 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005084 if (need_resched()) {
5085 write_unlock(&map_tree->lock);
5086 cond_resched();
5087 write_lock(&map_tree->lock);
5088 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005089 }
5090 write_unlock(&map_tree->lock);
5091
Filipe Manana6ca07092015-05-26 00:55:42 +01005092 /*
5093 * Keep looping until we have no more ranges in the io tree.
5094 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005095 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5096 * still in progress (unlocked the pages in the bio but did not yet
5097 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005098 * ranges can still be locked and eviction started because before
5099 * submitting those bios, which are executed by a separate task (work
5100 * queue kthread), inode references (inode->i_count) were not taken
5101 * (which would be dropped in the end io callback of each bio).
5102 * Therefore here we effectively end up waiting for those bios and
5103 * anyone else holding locked ranges without having bumped the inode's
5104 * reference count - if we don't do it, when they access the inode's
5105 * io_tree to unlock a range it may be too late, leading to an
5106 * use-after-free issue.
5107 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005108 spin_lock(&io_tree->lock);
5109 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5110 struct extent_state *state;
5111 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005112 u64 start;
5113 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005114
5115 node = rb_first(&io_tree->state);
5116 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005117 start = state->start;
5118 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005119 spin_unlock(&io_tree->lock);
5120
David Sterbaff13db42015-12-03 14:30:40 +01005121 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005122
5123 /*
5124 * If still has DELALLOC flag, the extent didn't reach disk,
5125 * and its reserved space won't be freed by delayed_ref.
5126 * So we need to free its reserved space here.
5127 * (Refer to comment in btrfs_invalidatepage, case 2)
5128 *
5129 * Note, end is the bytenr of last byte, so we need + 1 here.
5130 */
5131 if (state->state & EXTENT_DELALLOC)
5132 btrfs_qgroup_free_data(inode, start, end - start + 1);
5133
Filipe Manana6ca07092015-05-26 00:55:42 +01005134 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005135 EXTENT_LOCKED | EXTENT_DIRTY |
5136 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5137 EXTENT_DEFRAG, 1, 1,
5138 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005139
Filipe Manana7064dd52014-08-08 02:47:05 +01005140 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005141 spin_lock(&io_tree->lock);
5142 }
5143 spin_unlock(&io_tree->lock);
5144}
5145
Al Virobd555972010-06-07 11:35:40 -04005146void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005147{
5148 struct btrfs_trans_handle *trans;
5149 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005150 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005151 int steal_from_global = 0;
Josef Bacik07127182011-08-19 10:29:59 -04005152 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04005153 int ret;
5154
liubo1abe9b82011-03-24 11:18:59 +00005155 trace_btrfs_inode_evict(inode);
5156
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005157 evict_inode_truncate_pages(inode);
5158
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005159 if (inode->i_nlink &&
5160 ((btrfs_root_refs(&root->root_item) != 0 &&
5161 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5162 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005163 goto no_delete;
5164
Chris Mason39279cc2007-06-12 06:35:45 -04005165 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005166 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005167 goto no_delete;
5168 }
Al Virobd555972010-06-07 11:35:40 -04005169 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005170 if (!special_file(inode->i_mode))
5171 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005172
Miao Xief6124962014-09-12 18:44:04 +08005173 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5174
Yan, Zhengc71bf092009-11-12 09:34:40 +00005175 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06005176 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005177 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005178 goto no_delete;
5179 }
5180
Yan, Zheng76dda932009-09-21 16:00:26 -04005181 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005182 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5183 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005184 goto no_delete;
5185 }
5186
Miao Xie0e8c36a2012-12-19 06:59:51 +00005187 ret = btrfs_commit_inode_delayed_inode(inode);
5188 if (ret) {
5189 btrfs_orphan_del(NULL, inode);
5190 goto no_delete;
5191 }
5192
Miao Xie66d8f3d2012-09-06 04:02:28 -06005193 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005194 if (!rsv) {
5195 btrfs_orphan_del(NULL, inode);
5196 goto no_delete;
5197 }
Josef Bacik4a338542011-08-29 11:01:31 -04005198 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005199 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005200 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005201
Chris Masondbe674a2008-07-17 12:54:05 -04005202 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005203
Josef Bacik4289a662011-08-05 13:22:24 -04005204 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005205 * This is a bit simpler than btrfs_truncate since we've already
5206 * reserved our space for our orphan item in the unlink, so we just
5207 * need to reserve some slack space in case we add bytes and update
5208 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005209 */
Yan, Zheng80825102009-11-12 09:35:36 +00005210 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005211 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5212 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005213
Josef Bacik726c35f2011-09-26 15:46:06 -04005214 /*
5215 * Try and steal from the global reserve since we will
5216 * likely not use this space anyway, we want to try as
5217 * hard as possible to get this to work.
5218 */
5219 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005220 steal_from_global++;
5221 else
5222 steal_from_global = 0;
5223 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005224
Josef Bacik3bce8762015-02-24 12:35:51 -08005225 /*
5226 * steal_from_global == 0: we reserved stuff, hooray!
5227 * steal_from_global == 1: we didn't reserve stuff, boo!
5228 * steal_from_global == 2: we've committed, still not a lot of
5229 * room but maybe we'll have room in the global reserve this
5230 * time.
5231 * steal_from_global == 3: abandon all hope!
5232 */
5233 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005234 btrfs_warn(root->fs_info,
5235 "Could not get space for a delete, will truncate on mount %d",
5236 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005237 btrfs_orphan_del(NULL, inode);
5238 btrfs_free_block_rsv(root, rsv);
5239 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005240 }
5241
Miao Xie0e8c36a2012-12-19 06:59:51 +00005242 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005243 if (IS_ERR(trans)) {
5244 btrfs_orphan_del(NULL, inode);
5245 btrfs_free_block_rsv(root, rsv);
5246 goto no_delete;
5247 }
5248
Josef Bacik3bce8762015-02-24 12:35:51 -08005249 /*
5250 * We can't just steal from the global reserve, we need tomake
5251 * sure there is room to do it, if not we need to commit and try
5252 * again.
5253 */
5254 if (steal_from_global) {
5255 if (!btrfs_check_space_for_delayed_refs(trans, root))
5256 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5257 min_size);
5258 else
5259 ret = -ENOSPC;
5260 }
5261
5262 /*
5263 * Couldn't steal from the global reserve, we have too much
5264 * pending stuff built up, commit the transaction and try it
5265 * again.
5266 */
5267 if (ret) {
5268 ret = btrfs_commit_transaction(trans, root);
5269 if (ret) {
5270 btrfs_orphan_del(NULL, inode);
5271 btrfs_free_block_rsv(root, rsv);
5272 goto no_delete;
5273 }
5274 continue;
5275 } else {
5276 steal_from_global = 0;
5277 }
5278
Josef Bacik4289a662011-08-05 13:22:24 -04005279 trans->block_rsv = rsv;
5280
Yan, Zhengd68fc572010-05-16 10:49:58 -04005281 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005282 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005283 break;
5284
Miao Xie8407aa42012-09-07 01:43:32 -06005285 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005286 btrfs_end_transaction(trans, root);
5287 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005288 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005289 }
5290
Josef Bacik4289a662011-08-05 13:22:24 -04005291 btrfs_free_block_rsv(root, rsv);
5292
Josef Bacik4ef31a42013-08-13 14:10:08 -04005293 /*
5294 * Errors here aren't a big deal, it just means we leave orphan items
5295 * in the tree. They will be cleaned up on the next mount.
5296 */
Yan, Zheng80825102009-11-12 09:35:36 +00005297 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005298 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005299 btrfs_orphan_del(trans, inode);
5300 } else {
5301 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005302 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005303
Josef Bacik4289a662011-08-05 13:22:24 -04005304 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005305 if (!(root == root->fs_info->tree_root ||
5306 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005307 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005308
Chris Mason54aa1f42007-06-22 14:16:25 -04005309 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005310 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005311no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005312 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005313 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005314 return;
Chris Mason39279cc2007-06-12 06:35:45 -04005315}
5316
5317/*
5318 * this returns the key found in the dir entry in the location pointer.
5319 * If no dir entries were found, location->objectid is 0.
5320 */
5321static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5322 struct btrfs_key *location)
5323{
5324 const char *name = dentry->d_name.name;
5325 int namelen = dentry->d_name.len;
5326 struct btrfs_dir_item *di;
5327 struct btrfs_path *path;
5328 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005329 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005330
5331 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005332 if (!path)
5333 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005334
Li Zefan33345d012011-04-20 10:31:50 +08005335 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005336 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005337 if (IS_ERR(di))
5338 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005339
David Sterbac7040052011-04-19 18:00:01 +02005340 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005341 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005342
Chris Mason5f39d392007-10-15 16:14:19 -04005343 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005344out:
Chris Mason39279cc2007-06-12 06:35:45 -04005345 btrfs_free_path(path);
5346 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005347out_err:
5348 location->objectid = 0;
5349 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005350}
5351
5352/*
5353 * when we hit a tree root in a directory, the btrfs part of the inode
5354 * needs to be changed to reflect the root directory of the tree root. This
5355 * is kind of like crossing a mount point.
5356 */
5357static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005358 struct inode *dir,
5359 struct dentry *dentry,
5360 struct btrfs_key *location,
5361 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005362{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005363 struct btrfs_path *path;
5364 struct btrfs_root *new_root;
5365 struct btrfs_root_ref *ref;
5366 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005367 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005368 int ret;
5369 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005370
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005371 path = btrfs_alloc_path();
5372 if (!path) {
5373 err = -ENOMEM;
5374 goto out;
5375 }
Chris Mason39279cc2007-06-12 06:35:45 -04005376
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005377 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005378 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5379 key.type = BTRFS_ROOT_REF_KEY;
5380 key.offset = location->objectid;
5381
5382 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5383 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005384 if (ret) {
5385 if (ret < 0)
5386 err = ret;
5387 goto out;
5388 }
Chris Mason39279cc2007-06-12 06:35:45 -04005389
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005390 leaf = path->nodes[0];
5391 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005392 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005393 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5394 goto out;
5395
5396 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5397 (unsigned long)(ref + 1),
5398 dentry->d_name.len);
5399 if (ret)
5400 goto out;
5401
David Sterbab3b4aa72011-04-21 01:20:15 +02005402 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005403
5404 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5405 if (IS_ERR(new_root)) {
5406 err = PTR_ERR(new_root);
5407 goto out;
5408 }
5409
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005410 *sub_root = new_root;
5411 location->objectid = btrfs_root_dirid(&new_root->root_item);
5412 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005413 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005414 err = 0;
5415out:
5416 btrfs_free_path(path);
5417 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005418}
5419
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005420static void inode_tree_add(struct inode *inode)
5421{
5422 struct btrfs_root *root = BTRFS_I(inode)->root;
5423 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005424 struct rb_node **p;
5425 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005426 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005427 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005428
Al Viro1d3382cb2010-10-23 15:19:20 -04005429 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005430 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005431 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005432 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005433 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005434 while (*p) {
5435 parent = *p;
5436 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5437
Li Zefan33345d012011-04-20 10:31:50 +08005438 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005439 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005440 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005441 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005442 else {
5443 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005444 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005445 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005446 RB_CLEAR_NODE(parent);
5447 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005448 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005449 }
5450 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005451 rb_link_node(new, parent, p);
5452 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005453 spin_unlock(&root->inode_lock);
5454}
5455
5456static void inode_tree_del(struct inode *inode)
5457{
5458 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005459 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005460
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005461 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005462 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005463 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005464 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005465 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005466 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005467 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005468
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005469 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005470 synchronize_srcu(&root->fs_info->subvol_srcu);
5471 spin_lock(&root->inode_lock);
5472 empty = RB_EMPTY_ROOT(&root->inode_tree);
5473 spin_unlock(&root->inode_lock);
5474 if (empty)
5475 btrfs_add_dead_root(root);
5476 }
5477}
5478
Jeff Mahoney143bede2012-03-01 14:56:26 +01005479void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005480{
5481 struct rb_node *node;
5482 struct rb_node *prev;
5483 struct btrfs_inode *entry;
5484 struct inode *inode;
5485 u64 objectid = 0;
5486
Liu Bo7813b3d2014-02-10 17:37:25 +08005487 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5488 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005489
5490 spin_lock(&root->inode_lock);
5491again:
5492 node = root->inode_tree.rb_node;
5493 prev = NULL;
5494 while (node) {
5495 prev = node;
5496 entry = rb_entry(node, struct btrfs_inode, rb_node);
5497
Li Zefan33345d012011-04-20 10:31:50 +08005498 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005499 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005500 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005501 node = node->rb_right;
5502 else
5503 break;
5504 }
5505 if (!node) {
5506 while (prev) {
5507 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005508 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005509 node = prev;
5510 break;
5511 }
5512 prev = rb_next(prev);
5513 }
5514 }
5515 while (node) {
5516 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005517 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005518 inode = igrab(&entry->vfs_inode);
5519 if (inode) {
5520 spin_unlock(&root->inode_lock);
5521 if (atomic_read(&inode->i_count) > 1)
5522 d_prune_aliases(inode);
5523 /*
Al Viro45321ac2010-06-07 13:43:19 -04005524 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005525 * the inode cache when its usage count
5526 * hits zero.
5527 */
5528 iput(inode);
5529 cond_resched();
5530 spin_lock(&root->inode_lock);
5531 goto again;
5532 }
5533
5534 if (cond_resched_lock(&root->inode_lock))
5535 goto again;
5536
5537 node = rb_next(node);
5538 }
5539 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005540}
5541
Chris Masone02119d2008-09-05 16:13:11 -04005542static int btrfs_init_locked_inode(struct inode *inode, void *p)
5543{
5544 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005545 inode->i_ino = args->location->objectid;
5546 memcpy(&BTRFS_I(inode)->location, args->location,
5547 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005548 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005549 return 0;
5550}
5551
5552static int btrfs_find_actor(struct inode *inode, void *opaque)
5553{
5554 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005555 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005556 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005557}
5558
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005559static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005560 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005561 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005562{
5563 struct inode *inode;
5564 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005565 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005566
Chris Mason90d3e592014-01-09 17:28:00 -08005567 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005568 args.root = root;
5569
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005570 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005571 btrfs_init_locked_inode,
5572 (void *)&args);
5573 return inode;
5574}
5575
Balaji Rao1a54ef82008-07-21 02:01:04 +05305576/* Get an inode object given its location and corresponding root.
5577 * Returns in *is_new if the inode was read from disk
5578 */
5579struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005580 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305581{
5582 struct inode *inode;
5583
Chris Mason90d3e592014-01-09 17:28:00 -08005584 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305585 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005586 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305587
5588 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305589 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005590 if (!is_bad_inode(inode)) {
5591 inode_tree_add(inode);
5592 unlock_new_inode(inode);
5593 if (new)
5594 *new = 1;
5595 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005596 unlock_new_inode(inode);
5597 iput(inode);
5598 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005599 }
5600 }
5601
Balaji Rao1a54ef82008-07-21 02:01:04 +05305602 return inode;
5603}
5604
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005605static struct inode *new_simple_dir(struct super_block *s,
5606 struct btrfs_key *key,
5607 struct btrfs_root *root)
5608{
5609 struct inode *inode = new_inode(s);
5610
5611 if (!inode)
5612 return ERR_PTR(-ENOMEM);
5613
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005614 BTRFS_I(inode)->root = root;
5615 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005616 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005617
5618 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005619 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005620 inode->i_fop = &simple_dir_operations;
5621 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
chandan r9cc97d62012-07-04 12:48:07 +05305622 inode->i_mtime = CURRENT_TIME;
5623 inode->i_atime = inode->i_mtime;
5624 inode->i_ctime = inode->i_mtime;
5625 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005626
5627 return inode;
5628}
5629
Chris Mason3de45862008-11-17 21:02:50 -05005630struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005631{
Chris Masond3977122009-01-05 21:25:51 -05005632 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005633 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005634 struct btrfs_root *sub_root = root;
5635 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005636 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005637 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005638
5639 if (dentry->d_name.len > BTRFS_NAME_LEN)
5640 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005641
Jeff Layton39e3c952012-11-28 11:30:53 -05005642 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005643 if (ret < 0)
5644 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005645
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005646 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005647 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005648
5649 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005650 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005651 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005652 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005653
5654 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5655
Yan, Zheng76dda932009-09-21 16:00:26 -04005656 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005657 ret = fixup_tree_root_location(root, dir, dentry,
5658 &location, &sub_root);
5659 if (ret < 0) {
5660 if (ret != -ENOENT)
5661 inode = ERR_PTR(ret);
5662 else
5663 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5664 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005665 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005666 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005667 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5668
Julia Lawall34d19ba2011-01-24 19:55:19 +00005669 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005670 down_read(&root->fs_info->cleanup_work_sem);
5671 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005672 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005673 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005674 if (ret) {
5675 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005676 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005677 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005678 }
5679
Chris Mason3de45862008-11-17 21:02:50 -05005680 return inode;
5681}
5682
Nick Pigginfe15ce42011-01-07 17:49:23 +11005683static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005684{
5685 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005686 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005687
Li Zefan848cce02012-02-21 17:04:28 +08005688 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005689 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005690
Li Zefan848cce02012-02-21 17:04:28 +08005691 if (inode) {
5692 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005693 if (btrfs_root_refs(&root->root_item) == 0)
5694 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005695
5696 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5697 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005698 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005699 return 0;
5700}
5701
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005702static void btrfs_dentry_release(struct dentry *dentry)
5703{
Daeseok Youn944a4512014-04-14 15:37:02 +09005704 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005705}
5706
Chris Mason3de45862008-11-17 21:02:50 -05005707static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005708 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005709{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005710 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005711
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005712 inode = btrfs_lookup_dentry(dir, dentry);
5713 if (IS_ERR(inode)) {
5714 if (PTR_ERR(inode) == -ENOENT)
5715 inode = NULL;
5716 else
5717 return ERR_CAST(inode);
5718 }
5719
Al Viro41d28bc2014-10-12 22:24:21 -04005720 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005721}
5722
Miao Xie16cdcec2011-04-22 18:12:22 +08005723unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005724 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5725};
5726
Al Viro9cdda8d2013-05-22 16:48:09 -04005727static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005728{
Al Viro9cdda8d2013-05-22 16:48:09 -04005729 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005730 struct btrfs_root *root = BTRFS_I(inode)->root;
5731 struct btrfs_item *item;
5732 struct btrfs_dir_item *di;
5733 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005734 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005735 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005736 struct list_head ins_list;
5737 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005738 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005739 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005740 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005741 unsigned char d_type;
5742 int over = 0;
5743 u32 di_cur;
5744 u32 di_total;
5745 u32 di_len;
5746 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005747 char tmp_name[32];
5748 char *name_ptr;
5749 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005750 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005751
5752 /* FIXME, use a real flag for deciding about the key type */
5753 if (root->fs_info->tree_root == root)
5754 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005755
Al Viro9cdda8d2013-05-22 16:48:09 -04005756 if (!dir_emit_dots(file, ctx))
5757 return 0;
5758
David Woodhouse49593bf2008-08-17 17:08:36 +01005759 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005760 if (!path)
5761 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005762
Josef Bacik026fd312011-05-13 10:32:11 -04005763 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005764
Miao Xie16cdcec2011-04-22 18:12:22 +08005765 if (key_type == BTRFS_DIR_INDEX_KEY) {
5766 INIT_LIST_HEAD(&ins_list);
5767 INIT_LIST_HEAD(&del_list);
5768 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5769 }
5770
David Sterba962a2982014-06-04 18:41:45 +02005771 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005772 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005773 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005774
Chris Mason39279cc2007-06-12 06:35:45 -04005775 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5776 if (ret < 0)
5777 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005778
5779 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005780 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005781 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005782 if (slot >= btrfs_header_nritems(leaf)) {
5783 ret = btrfs_next_leaf(root, path);
5784 if (ret < 0)
5785 goto err;
5786 else if (ret > 0)
5787 break;
5788 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005789 }
Chris Mason3de45862008-11-17 21:02:50 -05005790
Ross Kirkdd3cc162013-09-16 15:58:09 +01005791 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005792 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5793
5794 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005795 break;
David Sterba962a2982014-06-04 18:41:45 +02005796 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005797 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005798 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005799 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005800 if (key_type == BTRFS_DIR_INDEX_KEY &&
5801 btrfs_should_delete_dir_index(&del_list,
5802 found_key.offset))
5803 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005804
Al Viro9cdda8d2013-05-22 16:48:09 -04005805 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005806 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005807
Chris Mason39279cc2007-06-12 06:35:45 -04005808 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5809 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005810 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005811
5812 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005813 struct btrfs_key location;
5814
Josef Bacik22a94d42011-03-16 16:47:17 -04005815 if (verify_dir_item(root, leaf, di))
5816 break;
5817
Chris Mason5f39d392007-10-15 16:14:19 -04005818 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005819 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005820 name_ptr = tmp_name;
5821 } else {
5822 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005823 if (!name_ptr) {
5824 ret = -ENOMEM;
5825 goto err;
5826 }
Chris Mason5f39d392007-10-15 16:14:19 -04005827 }
5828 read_extent_buffer(leaf, name_ptr,
5829 (unsigned long)(di + 1), name_len);
5830
5831 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5832 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005833
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005834
Chris Mason3de45862008-11-17 21:02:50 -05005835 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005836 * skip it.
5837 *
5838 * In contrast to old kernels, we insert the snapshot's
5839 * dir item and dir index after it has been created, so
5840 * we won't find a reference to our own snapshot. We
5841 * still keep the following code for backward
5842 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005843 */
5844 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5845 location.objectid == root->root_key.objectid) {
5846 over = 0;
5847 goto skip;
5848 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005849 over = !dir_emit(ctx, name_ptr, name_len,
5850 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005851
Chris Mason3de45862008-11-17 21:02:50 -05005852skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005853 if (name_ptr != tmp_name)
5854 kfree(name_ptr);
5855
Chris Mason39279cc2007-06-12 06:35:45 -04005856 if (over)
5857 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005858 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005859 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005860 di_cur += di_len;
5861 di = (struct btrfs_dir_item *)((char *)di + di_len);
5862 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005863next:
5864 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005865 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005866
Miao Xie16cdcec2011-04-22 18:12:22 +08005867 if (key_type == BTRFS_DIR_INDEX_KEY) {
5868 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005869 ctx->pos++;
5870 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005871 if (ret)
5872 goto nopos;
5873 }
5874
David Woodhouse49593bf2008-08-17 17:08:36 +01005875 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005876 ctx->pos++;
5877
5878 /*
5879 * Stop new entries from being returned after we return the last
5880 * entry.
5881 *
5882 * New directory entries are assigned a strictly increasing
5883 * offset. This means that new entries created during readdir
5884 * are *guaranteed* to be seen in the future by that readdir.
5885 * This has broken buggy programs which operate on names as
5886 * they're returned by readdir. Until we re-use freed offsets
5887 * we have this hack to stop new entries from being returned
5888 * under the assumption that they'll never reach this huge
5889 * offset.
5890 *
5891 * This is being careful not to overflow 32bit loff_t unless the
5892 * last entry requires it because doing so has broken 32bit apps
5893 * in the past.
5894 */
5895 if (key_type == BTRFS_DIR_INDEX_KEY) {
5896 if (ctx->pos >= INT_MAX)
5897 ctx->pos = LLONG_MAX;
5898 else
5899 ctx->pos = INT_MAX;
5900 }
Chris Mason39279cc2007-06-12 06:35:45 -04005901nopos:
5902 ret = 0;
5903err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005904 if (key_type == BTRFS_DIR_INDEX_KEY)
5905 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005906 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005907 return ret;
5908}
5909
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005910int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005911{
5912 struct btrfs_root *root = BTRFS_I(inode)->root;
5913 struct btrfs_trans_handle *trans;
5914 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005915 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005916
Josef Bacik72ac3c02012-05-23 14:13:11 -04005917 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005918 return 0;
5919
Liu Bo83eea1f2012-07-10 05:28:39 -06005920 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005921 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005922
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005923 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005924 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005925 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005926 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005927 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005928 if (IS_ERR(trans))
5929 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005930 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005931 }
5932 return ret;
5933}
5934
5935/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005936 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005937 * inode changes. But, it is most likely to find the inode in cache.
5938 * FIXME, needs more benchmarking...there are no reasons other than performance
5939 * to keep or drop this code.
5940 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005941static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005942{
5943 struct btrfs_root *root = BTRFS_I(inode)->root;
5944 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005945 int ret;
5946
Josef Bacik72ac3c02012-05-23 14:13:11 -04005947 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005948 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005949
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005950 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005951 if (IS_ERR(trans))
5952 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005953
5954 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005955 if (ret && ret == -ENOSPC) {
5956 /* whoops, lets try again with the full transaction */
5957 btrfs_end_transaction(trans, root);
5958 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005959 if (IS_ERR(trans))
5960 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005961
Chris Mason94b60442010-05-26 11:02:00 -04005962 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005963 }
Chris Mason39279cc2007-06-12 06:35:45 -04005964 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005965 if (BTRFS_I(inode)->delayed_node)
5966 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005967
5968 return ret;
5969}
5970
5971/*
5972 * This is a copy of file_update_time. We need this so we can return error on
5973 * ENOSPC for updating the inode in the case of file write and mmap writes.
5974 */
Josef Bacike41f9412012-03-26 09:46:47 -04005975static int btrfs_update_time(struct inode *inode, struct timespec *now,
5976 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005977{
Alexander Block2bc5565282012-06-15 09:49:33 +02005978 struct btrfs_root *root = BTRFS_I(inode)->root;
5979
5980 if (btrfs_root_readonly(root))
5981 return -EROFS;
5982
Josef Bacike41f9412012-03-26 09:46:47 -04005983 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005984 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005985 if (flags & S_CTIME)
5986 inode->i_ctime = *now;
5987 if (flags & S_MTIME)
5988 inode->i_mtime = *now;
5989 if (flags & S_ATIME)
5990 inode->i_atime = *now;
5991 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005992}
5993
Chris Masond352ac62008-09-29 15:18:18 -04005994/*
5995 * find the highest existing sequence number in a directory
5996 * and then set the in-memory index_cnt variable to reflect
5997 * free sequence numbers
5998 */
Josef Bacikaec74772008-07-24 12:12:38 -04005999static int btrfs_set_inode_index_count(struct inode *inode)
6000{
6001 struct btrfs_root *root = BTRFS_I(inode)->root;
6002 struct btrfs_key key, found_key;
6003 struct btrfs_path *path;
6004 struct extent_buffer *leaf;
6005 int ret;
6006
Li Zefan33345d012011-04-20 10:31:50 +08006007 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006008 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006009 key.offset = (u64)-1;
6010
6011 path = btrfs_alloc_path();
6012 if (!path)
6013 return -ENOMEM;
6014
6015 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6016 if (ret < 0)
6017 goto out;
6018 /* FIXME: we should be able to handle this */
6019 if (ret == 0)
6020 goto out;
6021 ret = 0;
6022
6023 /*
6024 * MAGIC NUMBER EXPLANATION:
6025 * since we search a directory based on f_pos we have to start at 2
6026 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6027 * else has to start at 2
6028 */
6029 if (path->slots[0] == 0) {
6030 BTRFS_I(inode)->index_cnt = 2;
6031 goto out;
6032 }
6033
6034 path->slots[0]--;
6035
6036 leaf = path->nodes[0];
6037 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6038
Li Zefan33345d012011-04-20 10:31:50 +08006039 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006040 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006041 BTRFS_I(inode)->index_cnt = 2;
6042 goto out;
6043 }
6044
6045 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6046out:
6047 btrfs_free_path(path);
6048 return ret;
6049}
6050
Chris Masond352ac62008-09-29 15:18:18 -04006051/*
6052 * helper to find a free sequence number in a given directory. This current
6053 * code is very simple, later versions will do smarter things in the btree
6054 */
Chris Mason3de45862008-11-17 21:02:50 -05006055int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006056{
6057 int ret = 0;
6058
6059 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006060 ret = btrfs_inode_delayed_dir_index_count(dir);
6061 if (ret) {
6062 ret = btrfs_set_inode_index_count(dir);
6063 if (ret)
6064 return ret;
6065 }
Josef Bacikaec74772008-07-24 12:12:38 -04006066 }
6067
Chris Mason00e4e6b2008-08-05 11:18:09 -04006068 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006069 BTRFS_I(dir)->index_cnt++;
6070
6071 return ret;
6072}
6073
Chris Masonb0d5d102014-09-08 13:08:51 -07006074static int btrfs_insert_inode_locked(struct inode *inode)
6075{
6076 struct btrfs_iget_args args;
6077 args.location = &BTRFS_I(inode)->location;
6078 args.root = BTRFS_I(inode)->root;
6079
6080 return insert_inode_locked4(inode,
6081 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6082 btrfs_find_actor, &args);
6083}
6084
Chris Mason39279cc2007-06-12 06:35:45 -04006085static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6086 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006087 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006088 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006089 u64 ref_objectid, u64 objectid,
6090 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006091{
6092 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006093 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006094 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006095 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006096 struct btrfs_inode_ref *ref;
6097 struct btrfs_key key[2];
6098 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006099 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006100 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006101 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006102
Chris Mason5f39d392007-10-15 16:14:19 -04006103 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006104 if (!path)
6105 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006106
Chris Mason39279cc2007-06-12 06:35:45 -04006107 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006108 if (!inode) {
6109 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006110 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006111 }
Chris Mason39279cc2007-06-12 06:35:45 -04006112
Li Zefan581bb052011-04-20 10:06:11 +08006113 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006114 * O_TMPFILE, set link count to 0, so that after this point,
6115 * we fill in an inode item with the correct link count.
6116 */
6117 if (!name)
6118 set_nlink(inode, 0);
6119
6120 /*
Li Zefan581bb052011-04-20 10:06:11 +08006121 * we have to initialize this early, so we can reclaim the inode
6122 * number if we fail afterwards in this function.
6123 */
6124 inode->i_ino = objectid;
6125
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006126 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006127 trace_btrfs_inode_request(dir);
6128
Chris Mason3de45862008-11-17 21:02:50 -05006129 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006130 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006131 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006132 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006133 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006134 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006135 } else if (dir) {
6136 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006137 }
6138 /*
6139 * index_cnt is ignored for everything but a dir,
6140 * btrfs_get_inode_index_count has an explanation for the magic
6141 * number
6142 */
6143 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006144 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006145 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006146 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006147 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006148
Josef Bacik5dc562c2012-08-17 13:14:17 -04006149 /*
6150 * We could have gotten an inode number from somebody who was fsynced
6151 * and then removed in this same transaction, so let's just set full
6152 * sync since it will be a full sync anyway and this will blow away the
6153 * old info in the log.
6154 */
6155 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6156
Chris Mason9c583092008-01-29 15:15:18 -05006157 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006158 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006159 key[0].offset = 0;
6160
Chris Mason9c583092008-01-29 15:15:18 -05006161 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006162
6163 if (name) {
6164 /*
6165 * Start new inodes with an inode_ref. This is slightly more
6166 * efficient for small numbers of hard links since they will
6167 * be packed into one item. Extended refs will kick in if we
6168 * add more hard links than can fit in the ref item.
6169 */
6170 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006171 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006172 key[1].offset = ref_objectid;
6173
6174 sizes[1] = name_len + sizeof(*ref);
6175 }
Chris Mason9c583092008-01-29 15:15:18 -05006176
Chris Masonb0d5d102014-09-08 13:08:51 -07006177 location = &BTRFS_I(inode)->location;
6178 location->objectid = objectid;
6179 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006180 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006181
6182 ret = btrfs_insert_inode_locked(inode);
6183 if (ret < 0)
6184 goto fail;
6185
Chris Masonb9473432009-03-13 11:00:37 -04006186 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006187 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006188 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006189 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006190
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006191 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006192 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306193
6194 inode->i_mtime = CURRENT_TIME;
6195 inode->i_atime = inode->i_mtime;
6196 inode->i_ctime = inode->i_mtime;
6197 BTRFS_I(inode)->i_otime = inode->i_mtime;
6198
Chris Mason5f39d392007-10-15 16:14:19 -04006199 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6200 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006201 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6202 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006203 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006204
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006205 if (name) {
6206 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6207 struct btrfs_inode_ref);
6208 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6209 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6210 ptr = (unsigned long)(ref + 1);
6211 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6212 }
Chris Mason9c583092008-01-29 15:15:18 -05006213
Chris Mason5f39d392007-10-15 16:14:19 -04006214 btrfs_mark_buffer_dirty(path->nodes[0]);
6215 btrfs_free_path(path);
6216
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006217 btrfs_inherit_iflags(inode, dir);
6218
Al Viro569254b2011-07-24 17:08:40 -04006219 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04006220 if (btrfs_test_opt(root, NODATASUM))
6221 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06006222 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006223 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6224 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006225 }
6226
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006227 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006228
6229 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006230 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006231
Alexander Block8ea05e32012-07-25 17:35:53 +02006232 btrfs_update_root_times(trans, root);
6233
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006234 ret = btrfs_inode_inherit_props(trans, inode, dir);
6235 if (ret)
6236 btrfs_err(root->fs_info,
6237 "error inheriting props for ino %llu (root %llu): %d",
6238 btrfs_ino(inode), root->root_key.objectid, ret);
6239
Chris Mason39279cc2007-06-12 06:35:45 -04006240 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006241
6242fail_unlock:
6243 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006244fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006245 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006246 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006247 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006248 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006249 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006250}
6251
6252static inline u8 btrfs_inode_type(struct inode *inode)
6253{
6254 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6255}
6256
Chris Masond352ac62008-09-29 15:18:18 -04006257/*
6258 * utility function to add 'inode' into 'parent_inode' with
6259 * a give name and a given sequence number.
6260 * if 'add_backref' is true, also insert a backref from the
6261 * inode to the parent directory.
6262 */
Chris Masone02119d2008-09-05 16:13:11 -04006263int btrfs_add_link(struct btrfs_trans_handle *trans,
6264 struct inode *parent_inode, struct inode *inode,
6265 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006266{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006267 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006268 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006269 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006270 u64 ino = btrfs_ino(inode);
6271 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006272
Li Zefan33345d012011-04-20 10:31:50 +08006273 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006274 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6275 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006276 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006277 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006278 key.offset = 0;
6279 }
Chris Mason39279cc2007-06-12 06:35:45 -04006280
Li Zefan33345d012011-04-20 10:31:50 +08006281 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006282 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6283 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006284 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006285 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006286 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6287 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006288 }
6289
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006290 /* Nothing to clean up yet */
6291 if (ret)
6292 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006293
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006294 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6295 parent_inode, &key,
6296 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006297 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006298 goto fail_dir_item;
6299 else if (ret) {
6300 btrfs_abort_transaction(trans, root, ret);
6301 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006302 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006303
6304 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6305 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006306 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006307 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
6308 ret = btrfs_update_inode(trans, root, parent_inode);
6309 if (ret)
6310 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006311 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006312
6313fail_dir_item:
6314 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6315 u64 local_index;
6316 int err;
6317 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6318 key.objectid, root->root_key.objectid,
6319 parent_ino, &local_index, name, name_len);
6320
6321 } else if (add_backref) {
6322 u64 local_index;
6323 int err;
6324
6325 err = btrfs_del_inode_ref(trans, root, name, name_len,
6326 ino, parent_ino, &local_index);
6327 }
6328 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006329}
6330
6331static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006332 struct inode *dir, struct dentry *dentry,
6333 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006334{
Josef Bacika1b075d2010-11-19 20:36:11 +00006335 int err = btrfs_add_link(trans, dir, inode,
6336 dentry->d_name.name, dentry->d_name.len,
6337 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006338 if (err > 0)
6339 err = -EEXIST;
6340 return err;
6341}
6342
Josef Bacik618e21d2007-07-11 10:18:17 -04006343static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006344 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006345{
6346 struct btrfs_trans_handle *trans;
6347 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006348 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006349 int err;
6350 int drop_inode = 0;
6351 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006352 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006353
Josef Bacik9ed74f22009-09-11 16:12:44 -04006354 /*
6355 * 2 for inode item and ref
6356 * 2 for dir items
6357 * 1 for xattr if selinux is on
6358 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006359 trans = btrfs_start_transaction(root, 5);
6360 if (IS_ERR(trans))
6361 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006362
Li Zefan581bb052011-04-20 10:06:11 +08006363 err = btrfs_find_free_ino(root, &objectid);
6364 if (err)
6365 goto out_unlock;
6366
Josef Bacikaec74772008-07-24 12:12:38 -04006367 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006368 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006369 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006370 if (IS_ERR(inode)) {
6371 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006372 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006373 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006374
Casey Schauflerad19db72011-12-15 10:09:07 -05006375 /*
6376 * If the active LSM wants to access the inode during
6377 * d_instantiate it needs these. Smack checks to see
6378 * if the filesystem supports xattrs by looking at the
6379 * ops vector.
6380 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006381 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006382 init_special_inode(inode, inode->i_mode, rdev);
6383
6384 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006385 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006386 goto out_unlock_inode;
6387
6388 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6389 if (err) {
6390 goto out_unlock_inode;
6391 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006392 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006393 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006394 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006395 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006396
Josef Bacik618e21d2007-07-11 10:18:17 -04006397out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006398 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006399 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006400 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006401 if (drop_inode) {
6402 inode_dec_link_count(inode);
6403 iput(inode);
6404 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006405 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006406
6407out_unlock_inode:
6408 drop_inode = 1;
6409 unlock_new_inode(inode);
6410 goto out_unlock;
6411
Josef Bacik618e21d2007-07-11 10:18:17 -04006412}
6413
Chris Mason39279cc2007-06-12 06:35:45 -04006414static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006415 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006416{
6417 struct btrfs_trans_handle *trans;
6418 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006419 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006420 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006421 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006422 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006423 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006424
Josef Bacik9ed74f22009-09-11 16:12:44 -04006425 /*
6426 * 2 for inode item and ref
6427 * 2 for dir items
6428 * 1 for xattr if selinux is on
6429 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006430 trans = btrfs_start_transaction(root, 5);
6431 if (IS_ERR(trans))
6432 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006433
Li Zefan581bb052011-04-20 10:06:11 +08006434 err = btrfs_find_free_ino(root, &objectid);
6435 if (err)
6436 goto out_unlock;
6437
Josef Bacikaec74772008-07-24 12:12:38 -04006438 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006439 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006440 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006441 if (IS_ERR(inode)) {
6442 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006443 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006444 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006445 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006446 /*
6447 * If the active LSM wants to access the inode during
6448 * d_instantiate it needs these. Smack checks to see
6449 * if the filesystem supports xattrs by looking at the
6450 * ops vector.
6451 */
6452 inode->i_fop = &btrfs_file_operations;
6453 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006454 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006455
6456 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6457 if (err)
6458 goto out_unlock_inode;
6459
6460 err = btrfs_update_inode(trans, root, inode);
6461 if (err)
6462 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006463
Josef Bacika1b075d2010-11-19 20:36:11 +00006464 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006465 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006466 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006467
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006468 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006469 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006470 d_instantiate(dentry, inode);
6471
Chris Mason39279cc2007-06-12 06:35:45 -04006472out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006473 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006474 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006475 inode_dec_link_count(inode);
6476 iput(inode);
6477 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006478 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006479 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006480 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006481
6482out_unlock_inode:
6483 unlock_new_inode(inode);
6484 goto out_unlock;
6485
Chris Mason39279cc2007-06-12 06:35:45 -04006486}
6487
6488static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6489 struct dentry *dentry)
6490{
Filipe Manana271dba42016-01-05 16:24:05 +00006491 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006492 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006493 struct inode *inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006494 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006495 int err;
6496 int drop_inode = 0;
6497
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006498 /* do not allow sys_link's with other subvols of the same device */
6499 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006500 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006501
Mark Fashehf1863732012-08-08 11:32:27 -07006502 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006503 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006504
Chris Mason3de45862008-11-17 21:02:50 -05006505 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006506 if (err)
6507 goto fail;
6508
Yan, Zhenga22285a2010-05-16 10:48:46 -04006509 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006510 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006511 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006512 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006513 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006514 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006515 if (IS_ERR(trans)) {
6516 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006517 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006518 goto fail;
6519 }
Chris Mason5f39d392007-10-15 16:14:19 -04006520
Miao Xie67de1172013-12-26 13:07:06 +08006521 /* There are several dir indexes for this inode, clear the cache. */
6522 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006523 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006524 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006525 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006526 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006527 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006528
Josef Bacika1b075d2010-11-19 20:36:11 +00006529 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006530
Yan, Zhenga5719522009-09-24 09:17:31 -04006531 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006532 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006533 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006534 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006535 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006536 if (err)
6537 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006538 if (inode->i_nlink == 1) {
6539 /*
6540 * If new hard link count is 1, it's a file created
6541 * with open(2) O_TMPFILE flag.
6542 */
6543 err = btrfs_orphan_del(trans, inode);
6544 if (err)
6545 goto fail;
6546 }
Al Viro08c422c2011-12-23 07:58:13 -05006547 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006548 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006549 }
Chris Mason39279cc2007-06-12 06:35:45 -04006550
Josef Bacikc581afc2014-02-06 16:06:06 -05006551 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006552fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006553 if (trans)
6554 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006555 if (drop_inode) {
6556 inode_dec_link_count(inode);
6557 iput(inode);
6558 }
Liu Bob53d3f52012-11-14 14:34:34 +00006559 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006560 return err;
6561}
6562
Al Viro18bb1db2011-07-26 01:41:39 -04006563static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006564{
Chris Masonb9d86662008-05-02 16:13:49 -04006565 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006566 struct btrfs_trans_handle *trans;
6567 struct btrfs_root *root = BTRFS_I(dir)->root;
6568 int err = 0;
6569 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006570 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006571 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006572
Josef Bacik9ed74f22009-09-11 16:12:44 -04006573 /*
6574 * 2 items for inode and ref
6575 * 2 items for dir items
6576 * 1 for xattr if selinux is on
6577 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006578 trans = btrfs_start_transaction(root, 5);
6579 if (IS_ERR(trans))
6580 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006581
Li Zefan581bb052011-04-20 10:06:11 +08006582 err = btrfs_find_free_ino(root, &objectid);
6583 if (err)
6584 goto out_fail;
6585
Josef Bacikaec74772008-07-24 12:12:38 -04006586 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006587 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006588 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006589 if (IS_ERR(inode)) {
6590 err = PTR_ERR(inode);
6591 goto out_fail;
6592 }
Chris Mason5f39d392007-10-15 16:14:19 -04006593
Chris Mason39279cc2007-06-12 06:35:45 -04006594 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006595 /* these must be set before we unlock the inode */
6596 inode->i_op = &btrfs_dir_inode_operations;
6597 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006598
Eric Paris2a7dba32011-02-01 11:05:39 -05006599 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006600 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006601 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006602
Chris Masondbe674a2008-07-17 12:54:05 -04006603 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006604 err = btrfs_update_inode(trans, root, inode);
6605 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006606 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006607
Josef Bacika1b075d2010-11-19 20:36:11 +00006608 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6609 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006610 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006611 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006612
Chris Mason39279cc2007-06-12 06:35:45 -04006613 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006614 /*
6615 * mkdir is special. We're unlocking after we call d_instantiate
6616 * to avoid a race with nfsd calling d_instantiate.
6617 */
6618 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006619 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006620
6621out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006622 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006623 if (drop_on_err) {
6624 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006625 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006626 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006627 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006628 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006629 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006630
6631out_fail_inode:
6632 unlock_new_inode(inode);
6633 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006634}
6635
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006636/* Find next extent map of a given extent map, caller needs to ensure locks */
6637static struct extent_map *next_extent_map(struct extent_map *em)
6638{
6639 struct rb_node *next;
6640
6641 next = rb_next(&em->rb_node);
6642 if (!next)
6643 return NULL;
6644 return container_of(next, struct extent_map, rb_node);
6645}
6646
6647static struct extent_map *prev_extent_map(struct extent_map *em)
6648{
6649 struct rb_node *prev;
6650
6651 prev = rb_prev(&em->rb_node);
6652 if (!prev)
6653 return NULL;
6654 return container_of(prev, struct extent_map, rb_node);
6655}
6656
Chris Masond352ac62008-09-29 15:18:18 -04006657/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006658 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006659 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006660 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006661 */
Chris Mason3b951512008-04-17 11:29:12 -04006662static int merge_extent_mapping(struct extent_map_tree *em_tree,
6663 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006664 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006665 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006666{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006667 struct extent_map *prev;
6668 struct extent_map *next;
6669 u64 start;
6670 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006671 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006672
Chris Masone6dcd2d2008-07-17 12:53:50 -04006673 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006674
6675 if (existing->start > map_start) {
6676 next = existing;
6677 prev = prev_extent_map(next);
6678 } else {
6679 prev = existing;
6680 next = next_extent_map(prev);
6681 }
6682
6683 start = prev ? extent_map_end(prev) : em->start;
6684 start = max_t(u64, start, em->start);
6685 end = next ? next->start : extent_map_end(em);
6686 end = min_t(u64, end, extent_map_end(em));
6687 start_diff = start - em->start;
6688 em->start = start;
6689 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006690 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6691 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006692 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006693 em->block_len -= start_diff;
6694 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006695 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006696}
6697
Chris Masonc8b97812008-10-29 14:49:59 -04006698static noinline int uncompress_inline(struct btrfs_path *path,
6699 struct inode *inode, struct page *page,
6700 size_t pg_offset, u64 extent_offset,
6701 struct btrfs_file_extent_item *item)
6702{
6703 int ret;
6704 struct extent_buffer *leaf = path->nodes[0];
6705 char *tmp;
6706 size_t max_size;
6707 unsigned long inline_size;
6708 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006709 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006710
6711 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006712 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006713 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6714 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006715 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006716 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006717 if (!tmp)
6718 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006719 ptr = btrfs_file_extent_inline_start(item);
6720
6721 read_extent_buffer(leaf, tmp, ptr, inline_size);
6722
Chris Mason5b050f02008-11-11 09:34:41 -05006723 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006724 ret = btrfs_decompress(compress_type, tmp, page,
6725 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006726 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006727 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006728}
6729
Chris Masond352ac62008-09-29 15:18:18 -04006730/*
6731 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006732 * the ugly parts come from merging extents from the disk with the in-ram
6733 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006734 * where the in-ram extents might be locked pending data=ordered completion.
6735 *
6736 * This also copies inline extents directly into the page.
6737 */
Chris Masond3977122009-01-05 21:25:51 -05006738
Chris Masona52d9a82007-08-27 16:49:44 -04006739struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006740 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006741 int create)
6742{
6743 int ret;
6744 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006745 u64 extent_start = 0;
6746 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006747 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006748 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006749 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006750 struct btrfs_root *root = BTRFS_I(inode)->root;
6751 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006752 struct extent_buffer *leaf;
6753 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006754 struct extent_map *em = NULL;
6755 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006756 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006757 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006758 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006759
Chris Masona52d9a82007-08-27 16:49:44 -04006760again:
Chris Mason890871b2009-09-02 16:24:52 -04006761 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006762 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006763 if (em)
6764 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006765 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006766
Chris Masona52d9a82007-08-27 16:49:44 -04006767 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006768 if (em->start > start || em->start + em->len <= start)
6769 free_extent_map(em);
6770 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006771 free_extent_map(em);
6772 else
6773 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006774 }
David Sterba172ddd62011-04-21 00:48:27 +02006775 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006776 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006777 err = -ENOMEM;
6778 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006779 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006780 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006781 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006782 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006783 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006784 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006785
6786 if (!path) {
6787 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006788 if (!path) {
6789 err = -ENOMEM;
6790 goto out;
6791 }
6792 /*
6793 * Chances are we'll be called again, so go ahead and do
6794 * readahead
6795 */
6796 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006797 }
6798
Chris Mason179e29e2007-11-01 11:28:41 -04006799 ret = btrfs_lookup_file_extent(trans, root, path,
6800 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006801 if (ret < 0) {
6802 err = ret;
6803 goto out;
6804 }
6805
6806 if (ret != 0) {
6807 if (path->slots[0] == 0)
6808 goto not_found;
6809 path->slots[0]--;
6810 }
6811
Chris Mason5f39d392007-10-15 16:14:19 -04006812 leaf = path->nodes[0];
6813 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006814 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006815 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006816 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006817 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006818 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006819 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006820 /*
6821 * If we backup past the first extent we want to move forward
6822 * and see if there is an extent in front of us, otherwise we'll
6823 * say there is a hole for our whole search range which can
6824 * cause problems.
6825 */
6826 extent_end = start;
6827 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006828 }
6829
Chris Mason5f39d392007-10-15 16:14:19 -04006830 found_type = btrfs_file_extent_type(leaf, item);
6831 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006832 if (found_type == BTRFS_FILE_EXTENT_REG ||
6833 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006834 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006835 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006836 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6837 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006838 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006839 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006840 }
Josef Bacik25a50342013-10-14 12:08:38 -04006841next:
Yan Zheng9036c102008-10-30 14:19:41 -04006842 if (start >= extent_end) {
6843 path->slots[0]++;
6844 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6845 ret = btrfs_next_leaf(root, path);
6846 if (ret < 0) {
6847 err = ret;
6848 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006849 }
Yan Zheng9036c102008-10-30 14:19:41 -04006850 if (ret > 0)
6851 goto not_found;
6852 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006853 }
Yan Zheng9036c102008-10-30 14:19:41 -04006854 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6855 if (found_key.objectid != objectid ||
6856 found_key.type != BTRFS_EXTENT_DATA_KEY)
6857 goto not_found;
6858 if (start + len <= found_key.offset)
6859 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006860 if (start > found_key.offset)
6861 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006862 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006863 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006864 em->len = found_key.offset - start;
6865 goto not_found_em;
6866 }
6867
Filipe Manana7ffbb592014-06-09 03:48:05 +01006868 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6869
Yan Zhengd899e052008-10-30 14:25:28 -04006870 if (found_type == BTRFS_FILE_EXTENT_REG ||
6871 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006872 goto insert;
6873 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006874 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006875 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006876 size_t size;
6877 size_t extent_offset;
6878 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006879
Filipe Manana7ffbb592014-06-09 03:48:05 +01006880 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006881 goto out;
Yan689f9342007-10-29 11:41:07 -04006882
Chris Mason514ac8a2014-01-03 21:07:00 -08006883 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006884 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006885 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006886 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006887 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006888 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006889 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006890 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006891 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006892 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006893 if (btrfs_file_extent_compression(leaf, item) !=
6894 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006895 ret = uncompress_inline(path, inode, page,
6896 pg_offset,
6897 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006898 if (ret) {
6899 err = ret;
6900 goto out;
6901 }
Chris Masonc8b97812008-10-29 14:49:59 -04006902 } else {
6903 map = kmap(page);
6904 read_extent_buffer(leaf, map + pg_offset, ptr,
6905 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006906 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6907 memset(map + pg_offset + copy_size, 0,
6908 PAGE_CACHE_SIZE - pg_offset -
6909 copy_size);
6910 }
Chris Masonc8b97812008-10-29 14:49:59 -04006911 kunmap(page);
6912 }
Chris Mason179e29e2007-11-01 11:28:41 -04006913 flush_dcache_page(page);
6914 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006915 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006916 if (!trans) {
6917 kunmap(page);
6918 free_extent_map(em);
6919 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006920
David Sterbab3b4aa72011-04-21 01:20:15 +02006921 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006922 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006923
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006924 if (IS_ERR(trans))
6925 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006926 goto again;
6927 }
Chris Masonc8b97812008-10-29 14:49:59 -04006928 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006929 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006930 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006931 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006932 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006933 }
Chris Masond1310b22008-01-24 16:13:08 -05006934 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006935 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006936 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006937 }
6938not_found:
6939 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006940 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006941 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006942not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006943 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006944 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006945insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006946 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006947 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006948 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006949 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006950 err = -EIO;
6951 goto out;
6952 }
Chris Masond1310b22008-01-24 16:13:08 -05006953
6954 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006955 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006956 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006957 /* it is possible that someone inserted the extent into the tree
6958 * while we had the lock dropped. It is also possible that
6959 * an overlapping map exists in the tree
6960 */
Chris Masona52d9a82007-08-27 16:49:44 -04006961 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006962 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006963
6964 ret = 0;
6965
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006966 existing = search_extent_mapping(em_tree, start, len);
6967 /*
6968 * existing will always be non-NULL, since there must be
6969 * extent causing the -EEXIST.
6970 */
6971 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006972 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006973 /*
6974 * The existing extent map is the one nearest to
6975 * the [start, start + len) range which overlaps
6976 */
6977 err = merge_extent_mapping(em_tree, existing,
6978 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006979 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006980 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006981 free_extent_map(em);
6982 em = NULL;
6983 }
6984 } else {
6985 free_extent_map(em);
6986 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006987 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006988 }
Chris Masona52d9a82007-08-27 16:49:44 -04006989 }
Chris Mason890871b2009-09-02 16:24:52 -04006990 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006991out:
liubo1abe9b82011-03-24 11:18:59 +00006992
Steven Rostedt4cd85872013-11-14 22:57:29 -05006993 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006994
Tsutomu Itoh527afb42015-08-19 14:55:00 +09006995 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006996 if (trans) {
6997 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006998 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006999 err = ret;
7000 }
Chris Masona52d9a82007-08-27 16:49:44 -04007001 if (err) {
7002 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007003 return ERR_PTR(err);
7004 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007005 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007006 return em;
7007}
7008
Chris Masonec29ed52011-02-23 16:23:20 -05007009struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7010 size_t pg_offset, u64 start, u64 len,
7011 int create)
7012{
7013 struct extent_map *em;
7014 struct extent_map *hole_em = NULL;
7015 u64 range_start = start;
7016 u64 end;
7017 u64 found;
7018 u64 found_end;
7019 int err = 0;
7020
7021 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7022 if (IS_ERR(em))
7023 return em;
7024 if (em) {
7025 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007026 * if our em maps to
7027 * - a hole or
7028 * - a pre-alloc extent,
7029 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007030 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007031 if (em->block_start != EXTENT_MAP_HOLE &&
7032 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007033 return em;
7034 else
7035 hole_em = em;
7036 }
7037
7038 /* check to see if we've wrapped (len == -1 or similar) */
7039 end = start + len;
7040 if (end < start)
7041 end = (u64)-1;
7042 else
7043 end -= 1;
7044
7045 em = NULL;
7046
7047 /* ok, we didn't find anything, lets look for delalloc */
7048 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7049 end, len, EXTENT_DELALLOC, 1);
7050 found_end = range_start + found;
7051 if (found_end < range_start)
7052 found_end = (u64)-1;
7053
7054 /*
7055 * we didn't find anything useful, return
7056 * the original results from get_extent()
7057 */
7058 if (range_start > end || found_end <= start) {
7059 em = hole_em;
7060 hole_em = NULL;
7061 goto out;
7062 }
7063
7064 /* adjust the range_start to make sure it doesn't
7065 * go backwards from the start they passed in
7066 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307067 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007068 found = found_end - range_start;
7069
7070 if (found > 0) {
7071 u64 hole_start = start;
7072 u64 hole_len = len;
7073
David Sterba172ddd62011-04-21 00:48:27 +02007074 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007075 if (!em) {
7076 err = -ENOMEM;
7077 goto out;
7078 }
7079 /*
7080 * when btrfs_get_extent can't find anything it
7081 * returns one huge hole
7082 *
7083 * make sure what it found really fits our range, and
7084 * adjust to make sure it is based on the start from
7085 * the caller
7086 */
7087 if (hole_em) {
7088 u64 calc_end = extent_map_end(hole_em);
7089
7090 if (calc_end <= start || (hole_em->start > end)) {
7091 free_extent_map(hole_em);
7092 hole_em = NULL;
7093 } else {
7094 hole_start = max(hole_em->start, start);
7095 hole_len = calc_end - hole_start;
7096 }
7097 }
7098 em->bdev = NULL;
7099 if (hole_em && range_start > hole_start) {
7100 /* our hole starts before our delalloc, so we
7101 * have to return just the parts of the hole
7102 * that go until the delalloc starts
7103 */
7104 em->len = min(hole_len,
7105 range_start - hole_start);
7106 em->start = hole_start;
7107 em->orig_start = hole_start;
7108 /*
7109 * don't adjust block start at all,
7110 * it is fixed at EXTENT_MAP_HOLE
7111 */
7112 em->block_start = hole_em->block_start;
7113 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007114 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7115 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007116 } else {
7117 em->start = range_start;
7118 em->len = found;
7119 em->orig_start = range_start;
7120 em->block_start = EXTENT_MAP_DELALLOC;
7121 em->block_len = found;
7122 }
7123 } else if (hole_em) {
7124 return hole_em;
7125 }
7126out:
7127
7128 free_extent_map(hole_em);
7129 if (err) {
7130 free_extent_map(em);
7131 return ERR_PTR(err);
7132 }
7133 return em;
7134}
7135
Josef Bacik4b46fce2010-05-23 11:00:55 -04007136static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7137 u64 start, u64 len)
7138{
7139 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007140 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007141 struct btrfs_key ins;
7142 u64 alloc_hint;
7143 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007144
Josef Bacik4b46fce2010-05-23 11:00:55 -04007145 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04007146 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007147 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007148 if (ret)
7149 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007150
Josef Bacik70c8a912012-10-11 16:54:30 -04007151 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007152 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04007153 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08007154 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007155 return em;
7156 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007157
7158 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
7159 ins.offset, ins.offset, 0);
7160 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08007161 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007162 free_extent_map(em);
7163 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007164 }
Josef Bacik00361582013-08-14 14:02:47 -04007165
Josef Bacik4b46fce2010-05-23 11:00:55 -04007166 return em;
7167}
7168
Chris Mason46bfbb52010-05-26 11:04:10 -04007169/*
7170 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7171 * block must be cow'd
7172 */
Josef Bacik00361582013-08-14 14:02:47 -04007173noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007174 u64 *orig_start, u64 *orig_block_len,
7175 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007176{
Josef Bacik00361582013-08-14 14:02:47 -04007177 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007178 struct btrfs_path *path;
7179 int ret;
7180 struct extent_buffer *leaf;
7181 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007182 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007183 struct btrfs_file_extent_item *fi;
7184 struct btrfs_key key;
7185 u64 disk_bytenr;
7186 u64 backref_offset;
7187 u64 extent_end;
7188 u64 num_bytes;
7189 int slot;
7190 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007191 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007192
Chris Mason46bfbb52010-05-26 11:04:10 -04007193 path = btrfs_alloc_path();
7194 if (!path)
7195 return -ENOMEM;
7196
Josef Bacik00361582013-08-14 14:02:47 -04007197 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007198 offset, 0);
7199 if (ret < 0)
7200 goto out;
7201
7202 slot = path->slots[0];
7203 if (ret == 1) {
7204 if (slot == 0) {
7205 /* can't find the item, must cow */
7206 ret = 0;
7207 goto out;
7208 }
7209 slot--;
7210 }
7211 ret = 0;
7212 leaf = path->nodes[0];
7213 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007214 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007215 key.type != BTRFS_EXTENT_DATA_KEY) {
7216 /* not our file or wrong item type, must cow */
7217 goto out;
7218 }
7219
7220 if (key.offset > offset) {
7221 /* Wrong offset, must cow */
7222 goto out;
7223 }
7224
7225 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7226 found_type = btrfs_file_extent_type(leaf, fi);
7227 if (found_type != BTRFS_FILE_EXTENT_REG &&
7228 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7229 /* not a regular extent, must cow */
7230 goto out;
7231 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007232
7233 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7234 goto out;
7235
Miao Xiee77751a2013-12-27 21:11:50 +08007236 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7237 if (extent_end <= offset)
7238 goto out;
7239
Chris Mason46bfbb52010-05-26 11:04:10 -04007240 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007241 if (disk_bytenr == 0)
7242 goto out;
7243
7244 if (btrfs_file_extent_compression(leaf, fi) ||
7245 btrfs_file_extent_encryption(leaf, fi) ||
7246 btrfs_file_extent_other_encoding(leaf, fi))
7247 goto out;
7248
Chris Mason46bfbb52010-05-26 11:04:10 -04007249 backref_offset = btrfs_file_extent_offset(leaf, fi);
7250
Josef Bacik7ee9e442013-06-21 16:37:03 -04007251 if (orig_start) {
7252 *orig_start = key.offset - backref_offset;
7253 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7254 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7255 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007256
Chris Mason46bfbb52010-05-26 11:04:10 -04007257 if (btrfs_extent_readonly(root, disk_bytenr))
7258 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007259
7260 num_bytes = min(offset + *len, extent_end) - offset;
7261 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7262 u64 range_end;
7263
7264 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7265 ret = test_range_bit(io_tree, offset, range_end,
7266 EXTENT_DELALLOC, 0, NULL);
7267 if (ret) {
7268 ret = -EAGAIN;
7269 goto out;
7270 }
7271 }
7272
Josef Bacik1bda19e2013-10-18 12:10:36 -04007273 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007274
7275 /*
7276 * look for other files referencing this extent, if we
7277 * find any we must cow
7278 */
Josef Bacik00361582013-08-14 14:02:47 -04007279 trans = btrfs_join_transaction(root);
7280 if (IS_ERR(trans)) {
7281 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007282 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007283 }
7284
7285 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7286 key.offset - backref_offset, disk_bytenr);
7287 btrfs_end_transaction(trans, root);
7288 if (ret) {
7289 ret = 0;
7290 goto out;
7291 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007292
7293 /*
7294 * adjust disk_bytenr and num_bytes to cover just the bytes
7295 * in this extent we are about to write. If there
7296 * are any csums in that range we have to cow in order
7297 * to keep the csums correct
7298 */
7299 disk_bytenr += backref_offset;
7300 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007301 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7302 goto out;
7303 /*
7304 * all of the above have passed, it is safe to overwrite this extent
7305 * without cow
7306 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007307 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007308 ret = 1;
7309out:
7310 btrfs_free_path(path);
7311 return ret;
7312}
7313
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007314bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7315{
7316 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7317 int found = false;
7318 void **pagep = NULL;
7319 struct page *page = NULL;
7320 int start_idx;
7321 int end_idx;
7322
7323 start_idx = start >> PAGE_CACHE_SHIFT;
7324
7325 /*
7326 * end is the last byte in the last page. end == start is legal
7327 */
7328 end_idx = end >> PAGE_CACHE_SHIFT;
7329
7330 rcu_read_lock();
7331
7332 /* Most of the code in this while loop is lifted from
7333 * find_get_page. It's been modified to begin searching from a
7334 * page and return just the first page found in that range. If the
7335 * found idx is less than or equal to the end idx then we know that
7336 * a page exists. If no pages are found or if those pages are
7337 * outside of the range then we're fine (yay!) */
7338 while (page == NULL &&
7339 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7340 page = radix_tree_deref_slot(pagep);
7341 if (unlikely(!page))
7342 break;
7343
7344 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007345 if (radix_tree_deref_retry(page)) {
7346 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007347 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007348 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007349 /*
7350 * Otherwise, shmem/tmpfs must be storing a swap entry
7351 * here as an exceptional entry: so return it without
7352 * attempting to raise page count.
7353 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007354 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007355 break; /* TODO: Is this relevant for this use case? */
7356 }
7357
Filipe Manana91405152014-06-05 13:22:24 +01007358 if (!page_cache_get_speculative(page)) {
7359 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007360 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007361 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007362
7363 /*
7364 * Has the page moved?
7365 * This is part of the lockless pagecache protocol. See
7366 * include/linux/pagemap.h for details.
7367 */
7368 if (unlikely(page != *pagep)) {
7369 page_cache_release(page);
7370 page = NULL;
7371 }
7372 }
7373
7374 if (page) {
7375 if (page->index <= end_idx)
7376 found = true;
7377 page_cache_release(page);
7378 }
7379
7380 rcu_read_unlock();
7381 return found;
7382}
7383
Josef Bacikeb838e72012-07-31 16:28:48 -04007384static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7385 struct extent_state **cached_state, int writing)
7386{
7387 struct btrfs_ordered_extent *ordered;
7388 int ret = 0;
7389
7390 while (1) {
7391 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007392 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007393 /*
7394 * We're concerned with the entire range that we're going to be
7395 * doing DIO to, so we need to make sure theres no ordered
7396 * extents in this range.
7397 */
7398 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7399 lockend - lockstart + 1);
7400
7401 /*
7402 * We need to make sure there are no buffered pages in this
7403 * range either, we could have raced between the invalidate in
7404 * generic_file_direct_write and locking the extent. The
7405 * invalidate needs to happen so that reads after a write do not
7406 * get stale data.
7407 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007408 if (!ordered &&
7409 (!writing ||
7410 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007411 break;
7412
7413 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7414 cached_state, GFP_NOFS);
7415
7416 if (ordered) {
7417 btrfs_start_ordered_extent(inode, ordered, 1);
7418 btrfs_put_ordered_extent(ordered);
7419 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007420 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007421 * We could trigger writeback for this range (and wait
7422 * for it to complete) and then invalidate the pages for
7423 * this range (through invalidate_inode_pages2_range()),
7424 * but that can lead us to a deadlock with a concurrent
7425 * call to readpages() (a buffered read or a defrag call
7426 * triggered a readahead) on a page lock due to an
7427 * ordered dio extent we created before but did not have
7428 * yet a corresponding bio submitted (whence it can not
7429 * complete), which makes readpages() wait for that
7430 * ordered extent to complete while holding a lock on
7431 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007432 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007433 ret = -ENOTBLK;
7434 break;
Josef Bacikeb838e72012-07-31 16:28:48 -04007435 }
7436
7437 cond_resched();
7438 }
7439
7440 return ret;
7441}
7442
Josef Bacik69ffb542012-09-11 15:40:07 -04007443static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7444 u64 len, u64 orig_start,
7445 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007446 u64 orig_block_len, u64 ram_bytes,
7447 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007448{
7449 struct extent_map_tree *em_tree;
7450 struct extent_map *em;
7451 struct btrfs_root *root = BTRFS_I(inode)->root;
7452 int ret;
7453
7454 em_tree = &BTRFS_I(inode)->extent_tree;
7455 em = alloc_extent_map();
7456 if (!em)
7457 return ERR_PTR(-ENOMEM);
7458
7459 em->start = start;
7460 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007461 em->mod_start = start;
7462 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007463 em->len = len;
7464 em->block_len = block_len;
7465 em->block_start = block_start;
7466 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007467 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007468 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007469 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007470 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7471 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007472 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007473
7474 do {
7475 btrfs_drop_extent_cache(inode, em->start,
7476 em->start + em->len - 1, 0);
7477 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007478 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007479 write_unlock(&em_tree->lock);
7480 } while (ret == -EEXIST);
7481
7482 if (ret) {
7483 free_extent_map(em);
7484 return ERR_PTR(ret);
7485 }
7486
7487 return em;
7488}
7489
Filipe Manana9c9464c2015-11-04 09:52:04 +00007490static void adjust_dio_outstanding_extents(struct inode *inode,
7491 struct btrfs_dio_data *dio_data,
7492 const u64 len)
7493{
7494 unsigned num_extents;
7495
7496 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7497 BTRFS_MAX_EXTENT_SIZE);
7498 /*
7499 * If we have an outstanding_extents count still set then we're
7500 * within our reservation, otherwise we need to adjust our inode
7501 * counter appropriately.
7502 */
7503 if (dio_data->outstanding_extents) {
7504 dio_data->outstanding_extents -= num_extents;
7505 } else {
7506 spin_lock(&BTRFS_I(inode)->lock);
7507 BTRFS_I(inode)->outstanding_extents += num_extents;
7508 spin_unlock(&BTRFS_I(inode)->lock);
7509 }
7510}
7511
Josef Bacik4b46fce2010-05-23 11:00:55 -04007512static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7513 struct buffer_head *bh_result, int create)
7514{
7515 struct extent_map *em;
7516 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007517 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307518 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007519 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007520 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007521 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007522 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007523 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007524
Miao Xie172a5042013-02-21 02:48:22 -07007525 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007526 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007527 else
Josef Bacikc3298612012-08-03 16:49:19 -04007528 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007529
Josef Bacikc3298612012-08-03 16:49:19 -04007530 lockstart = start;
7531 lockend = start + len - 1;
7532
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007533 if (current->journal_info) {
7534 /*
7535 * Need to pull our outstanding extents and set journal_info to NULL so
7536 * that anything that needs to check if there's a transction doesn't get
7537 * confused.
7538 */
chandan50745b02015-08-28 21:10:13 +05307539 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007540 current->journal_info = NULL;
7541 }
7542
Josef Bacikeb838e72012-07-31 16:28:48 -04007543 /*
7544 * If this errors out it's because we couldn't invalidate pagecache for
7545 * this range and we need to fallback to buffered.
7546 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007547 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7548 create)) {
7549 ret = -ENOTBLK;
7550 goto err;
7551 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007552
Josef Bacik4b46fce2010-05-23 11:00:55 -04007553 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007554 if (IS_ERR(em)) {
7555 ret = PTR_ERR(em);
7556 goto unlock_err;
7557 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007558
7559 /*
7560 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7561 * io. INLINE is special, and we could probably kludge it in here, but
7562 * it's still buffered so for safety lets just fall back to the generic
7563 * buffered path.
7564 *
7565 * For COMPRESSED we _have_ to read the entire extent in so we can
7566 * decompress it, so there will be buffering required no matter what we
7567 * do, so go ahead and fallback to buffered.
7568 *
7569 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7570 * to buffered IO. Don't blame me, this is the price we pay for using
7571 * the generic code.
7572 */
7573 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7574 em->block_start == EXTENT_MAP_INLINE) {
7575 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007576 ret = -ENOTBLK;
7577 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007578 }
7579
7580 /* Just a good old fashioned hole, return */
7581 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7582 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7583 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007584 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007585 }
7586
7587 /*
7588 * We don't allocate a new extent in the following cases
7589 *
7590 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7591 * existing extent.
7592 * 2) The extent is marked as PREALLOC. We're good to go here and can
7593 * just use the extent.
7594 *
7595 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007596 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007597 len = min(len, em->len - (start - em->start));
7598 lockstart = start + len;
7599 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007600 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007601
7602 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7603 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7604 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007605 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007606 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007607
7608 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7609 type = BTRFS_ORDERED_PREALLOC;
7610 else
7611 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007612 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007613 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007614
Josef Bacik00361582013-08-14 14:02:47 -04007615 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007616 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007617 if (type == BTRFS_ORDERED_PREALLOC) {
7618 free_extent_map(em);
7619 em = create_pinned_em(inode, start, len,
7620 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007621 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007622 orig_block_len,
7623 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007624 if (IS_ERR(em)) {
7625 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007626 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007627 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007628 }
7629
Chris Mason46bfbb52010-05-26 11:04:10 -04007630 ret = btrfs_add_ordered_extent_dio(inode, start,
7631 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007632 if (ret) {
7633 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007634 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007635 }
7636 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007637 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007638 }
Josef Bacik00361582013-08-14 14:02:47 -04007639
Chris Mason46bfbb52010-05-26 11:04:10 -04007640 /*
7641 * this will cow the extent, reset the len in case we changed
7642 * it above
7643 */
7644 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007645 free_extent_map(em);
7646 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007647 if (IS_ERR(em)) {
7648 ret = PTR_ERR(em);
7649 goto unlock_err;
7650 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007651 len = min(len, em->len - (start - em->start));
7652unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007653 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7654 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007655 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007656 bh_result->b_bdev = em->bdev;
7657 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007658 if (create) {
7659 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7660 set_buffer_new(bh_result);
7661
7662 /*
7663 * Need to update the i_size under the extent lock so buffered
7664 * readers will get the updated i_size when we unlock.
7665 */
7666 if (start + len > i_size_read(inode))
7667 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007668
Filipe Manana9c9464c2015-11-04 09:52:04 +00007669 adjust_dio_outstanding_extents(inode, dio_data, len);
Qu Wenruo7cf5b972015-09-08 17:25:55 +08007670 btrfs_free_reserved_data_space(inode, start, len);
chandan50745b02015-08-28 21:10:13 +05307671 WARN_ON(dio_data->reserve < len);
7672 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007673 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307674 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007675 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007676
Josef Bacikeb838e72012-07-31 16:28:48 -04007677 /*
7678 * In the case of write we need to clear and unlock the entire range,
7679 * in the case of read we need to unlock only the end area that we
7680 * aren't using if there is any left over space.
7681 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007682 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007683 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7684 lockend, unlock_bits, 1, 0,
7685 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007686 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007687 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007688 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007689
Josef Bacik4b46fce2010-05-23 11:00:55 -04007690 free_extent_map(em);
7691
7692 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007693
7694unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007695 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7696 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007697err:
chandan50745b02015-08-28 21:10:13 +05307698 if (dio_data)
7699 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007700 /*
7701 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7702 * write less data then expected, so that we don't underflow our inode's
7703 * outstanding extents counter.
7704 */
7705 if (create && dio_data)
7706 adjust_dio_outstanding_extents(inode, dio_data, len);
7707
Josef Bacikeb838e72012-07-31 16:28:48 -04007708 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007709}
7710
Miao Xie8b110e32014-09-12 18:44:03 +08007711static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7712 int rw, int mirror_num)
7713{
7714 struct btrfs_root *root = BTRFS_I(inode)->root;
7715 int ret;
7716
7717 BUG_ON(rw & REQ_WRITE);
7718
7719 bio_get(bio);
7720
7721 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7722 BTRFS_WQ_ENDIO_DIO_REPAIR);
7723 if (ret)
7724 goto err;
7725
7726 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7727err:
7728 bio_put(bio);
7729 return ret;
7730}
7731
7732static int btrfs_check_dio_repairable(struct inode *inode,
7733 struct bio *failed_bio,
7734 struct io_failure_record *failrec,
7735 int failed_mirror)
7736{
7737 int num_copies;
7738
7739 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7740 failrec->logical, failrec->len);
7741 if (num_copies == 1) {
7742 /*
7743 * we only have a single copy of the data, so don't bother with
7744 * all the retry and error correction code that follows. no
7745 * matter what the error is, it is very likely to persist.
7746 */
7747 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7748 num_copies, failrec->this_mirror, failed_mirror);
7749 return 0;
7750 }
7751
7752 failrec->failed_mirror = failed_mirror;
7753 failrec->this_mirror++;
7754 if (failrec->this_mirror == failed_mirror)
7755 failrec->this_mirror++;
7756
7757 if (failrec->this_mirror > num_copies) {
7758 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7759 num_copies, failrec->this_mirror, failed_mirror);
7760 return 0;
7761 }
7762
7763 return 1;
7764}
7765
7766static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7767 struct page *page, u64 start, u64 end,
7768 int failed_mirror, bio_end_io_t *repair_endio,
7769 void *repair_arg)
7770{
7771 struct io_failure_record *failrec;
7772 struct bio *bio;
7773 int isector;
7774 int read_mode;
7775 int ret;
7776
7777 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7778
7779 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7780 if (ret)
7781 return ret;
7782
7783 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7784 failed_mirror);
7785 if (!ret) {
7786 free_io_failure(inode, failrec);
7787 return -EIO;
7788 }
7789
7790 if (failed_bio->bi_vcnt > 1)
7791 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7792 else
7793 read_mode = READ_SYNC;
7794
7795 isector = start - btrfs_io_bio(failed_bio)->logical;
7796 isector >>= inode->i_sb->s_blocksize_bits;
7797 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7798 0, isector, repair_endio, repair_arg);
7799 if (!bio) {
7800 free_io_failure(inode, failrec);
7801 return -EIO;
7802 }
7803
7804 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7805 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7806 read_mode, failrec->this_mirror, failrec->in_validation);
7807
7808 ret = submit_dio_repair_bio(inode, bio, read_mode,
7809 failrec->this_mirror);
7810 if (ret) {
7811 free_io_failure(inode, failrec);
7812 bio_put(bio);
7813 }
7814
7815 return ret;
7816}
7817
7818struct btrfs_retry_complete {
7819 struct completion done;
7820 struct inode *inode;
7821 u64 start;
7822 int uptodate;
7823};
7824
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007825static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007826{
7827 struct btrfs_retry_complete *done = bio->bi_private;
7828 struct bio_vec *bvec;
7829 int i;
7830
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007831 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007832 goto end;
7833
7834 done->uptodate = 1;
7835 bio_for_each_segment_all(bvec, bio, i)
7836 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7837end:
7838 complete(&done->done);
7839 bio_put(bio);
7840}
7841
7842static int __btrfs_correct_data_nocsum(struct inode *inode,
7843 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007844{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007845 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007846 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007847 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007848 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007849 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007850
Miao Xiec1dc0892014-09-12 18:43:56 +08007851 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007852 done.inode = inode;
7853
7854 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7855try_again:
7856 done.uptodate = 0;
7857 done.start = start;
7858 init_completion(&done.done);
7859
7860 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7861 start + bvec->bv_len - 1,
7862 io_bio->mirror_num,
7863 btrfs_retry_endio_nocsum, &done);
7864 if (ret)
7865 return ret;
7866
7867 wait_for_completion(&done.done);
7868
7869 if (!done.uptodate) {
7870 /* We might have another mirror, so try again */
7871 goto try_again;
7872 }
7873
7874 start += bvec->bv_len;
7875 }
7876
7877 return 0;
7878}
7879
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007880static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007881{
7882 struct btrfs_retry_complete *done = bio->bi_private;
7883 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7884 struct bio_vec *bvec;
7885 int uptodate;
7886 int ret;
7887 int i;
7888
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007889 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007890 goto end;
7891
7892 uptodate = 1;
7893 bio_for_each_segment_all(bvec, bio, i) {
7894 ret = __readpage_endio_check(done->inode, io_bio, i,
7895 bvec->bv_page, 0,
7896 done->start, bvec->bv_len);
7897 if (!ret)
7898 clean_io_failure(done->inode, done->start,
7899 bvec->bv_page, 0);
7900 else
7901 uptodate = 0;
7902 }
7903
7904 done->uptodate = uptodate;
7905end:
7906 complete(&done->done);
7907 bio_put(bio);
7908}
7909
7910static int __btrfs_subio_endio_read(struct inode *inode,
7911 struct btrfs_io_bio *io_bio, int err)
7912{
7913 struct bio_vec *bvec;
7914 struct btrfs_retry_complete done;
7915 u64 start;
7916 u64 offset = 0;
7917 int i;
7918 int ret;
7919
7920 err = 0;
7921 start = io_bio->logical;
7922 done.inode = inode;
7923
Miao Xiec1dc0892014-09-12 18:43:56 +08007924 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007925 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7926 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007927 if (likely(!ret))
7928 goto next;
7929try_again:
7930 done.uptodate = 0;
7931 done.start = start;
7932 init_completion(&done.done);
7933
7934 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7935 start + bvec->bv_len - 1,
7936 io_bio->mirror_num,
7937 btrfs_retry_endio, &done);
7938 if (ret) {
7939 err = ret;
7940 goto next;
7941 }
7942
7943 wait_for_completion(&done.done);
7944
7945 if (!done.uptodate) {
7946 /* We might have another mirror, so try again */
7947 goto try_again;
7948 }
7949next:
7950 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007951 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007952 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007953
7954 return err;
7955}
7956
Miao Xie8b110e32014-09-12 18:44:03 +08007957static int btrfs_subio_endio_read(struct inode *inode,
7958 struct btrfs_io_bio *io_bio, int err)
7959{
7960 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7961
7962 if (skip_csum) {
7963 if (unlikely(err))
7964 return __btrfs_correct_data_nocsum(inode, io_bio);
7965 else
7966 return 0;
7967 } else {
7968 return __btrfs_subio_endio_read(inode, io_bio, err);
7969 }
7970}
7971
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007972static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08007973{
7974 struct btrfs_dio_private *dip = bio->bi_private;
7975 struct inode *inode = dip->inode;
7976 struct bio *dio_bio;
7977 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007978 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08007979
Miao Xie8b110e32014-09-12 18:44:03 +08007980 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7981 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007982
Josef Bacik4b46fce2010-05-23 11:00:55 -04007983 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007984 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007985 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007986
Josef Bacik4b46fce2010-05-23 11:00:55 -04007987 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007988
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007989 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08007990
7991 if (io_bio->end_io)
7992 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007993 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007994}
7995
Filipe Manana14543772015-11-24 16:23:54 +00007996static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
7997 const u64 offset,
7998 const u64 bytes,
7999 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008000{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008001 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008002 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008003 u64 ordered_offset = offset;
8004 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008005 int ret;
8006
Chris Mason163cf092010-11-28 19:56:33 -05008007again:
8008 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8009 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008010 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008011 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008012 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008013 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008014
Liu Bo9e0af232014-08-15 23:36:53 +08008015 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8016 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08008017 btrfs_queue_work(root->fs_info->endio_write_workers,
8018 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008019out_test:
8020 /*
8021 * our bio might span multiple ordered extents. If we haven't
8022 * completed the accounting for the whole dio, go back and try again
8023 */
Filipe Manana14543772015-11-24 16:23:54 +00008024 if (ordered_offset < offset + bytes) {
8025 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008026 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008027 goto again;
8028 }
Filipe Manana14543772015-11-24 16:23:54 +00008029}
8030
8031static void btrfs_endio_direct_write(struct bio *bio)
8032{
8033 struct btrfs_dio_private *dip = bio->bi_private;
8034 struct bio *dio_bio = dip->dio_bio;
8035
8036 btrfs_endio_direct_write_update_ordered(dip->inode,
8037 dip->logical_offset,
8038 dip->bytes,
8039 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008040
Josef Bacik4b46fce2010-05-23 11:00:55 -04008041 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008042
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008043 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008044 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008045}
8046
Chris Masoneaf25d92010-05-25 09:48:28 -04008047static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
8048 struct bio *bio, int mirror_num,
8049 unsigned long bio_flags, u64 offset)
8050{
8051 int ret;
8052 struct btrfs_root *root = BTRFS_I(inode)->root;
8053 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008054 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008055 return 0;
8056}
8057
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008058static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008059{
8060 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008061 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008062
Miao Xie8b110e32014-09-12 18:44:03 +08008063 if (err)
8064 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
8065 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
8066 btrfs_ino(dip->inode), bio->bi_rw,
8067 (unsigned long long)bio->bi_iter.bi_sector,
8068 bio->bi_iter.bi_size, err);
8069
8070 if (dip->subio_endio)
8071 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008072
8073 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008074 dip->errors = 1;
8075
8076 /*
8077 * before atomic variable goto zero, we must make sure
8078 * dip->errors is perceived to be set.
8079 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008080 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008081 }
8082
8083 /* if there are more bios still pending for this dio, just exit */
8084 if (!atomic_dec_and_test(&dip->pending_bios))
8085 goto out;
8086
Chris Mason9be33952013-05-17 18:30:14 -04008087 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008088 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008089 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008090 dip->dio_bio->bi_error = 0;
8091 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008092 }
8093out:
8094 bio_put(bio);
8095}
8096
8097static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8098 u64 first_sector, gfp_t gfp_flags)
8099{
Chris Masonda2f0f72015-07-02 13:57:22 -07008100 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008101 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008102 if (bio)
8103 bio_associate_current(bio);
8104 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008105}
8106
Miao Xiec1dc0892014-09-12 18:43:56 +08008107static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
8108 struct inode *inode,
8109 struct btrfs_dio_private *dip,
8110 struct bio *bio,
8111 u64 file_offset)
8112{
8113 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8114 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8115 int ret;
8116
8117 /*
8118 * We load all the csum data we need when we submit
8119 * the first bio to reduce the csum tree search and
8120 * contention.
8121 */
8122 if (dip->logical_offset == file_offset) {
8123 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
8124 file_offset);
8125 if (ret)
8126 return ret;
8127 }
8128
8129 if (bio == dip->orig_bio)
8130 return 0;
8131
8132 file_offset -= dip->logical_offset;
8133 file_offset >>= inode->i_sb->s_blocksize_bits;
8134 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8135
8136 return 0;
8137}
8138
Miao Xiee65e1532010-11-22 03:04:43 +00008139static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8140 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008141 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008142{
Miao Xiefacc8a22013-07-25 19:22:34 +08008143 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00008144 int write = rw & REQ_WRITE;
8145 struct btrfs_root *root = BTRFS_I(inode)->root;
8146 int ret;
8147
Josef Bacikb812ce22012-11-16 13:56:32 -05008148 if (async_submit)
8149 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8150
Miao Xiee65e1532010-11-22 03:04:43 +00008151 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008152
8153 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02008154 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8155 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008156 if (ret)
8157 goto err;
8158 }
Miao Xiee65e1532010-11-22 03:04:43 +00008159
Josef Bacik1ae39932011-04-06 14:41:34 -04008160 if (skip_sum)
8161 goto map;
8162
8163 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008164 ret = btrfs_wq_submit_bio(root->fs_info,
8165 inode, rw, bio, 0, 0,
8166 file_offset,
8167 __btrfs_submit_bio_start_direct_io,
8168 __btrfs_submit_bio_done);
8169 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008170 } else if (write) {
8171 /*
8172 * If we aren't doing async submit, calculate the csum of the
8173 * bio now.
8174 */
8175 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8176 if (ret)
8177 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008178 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008179 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8180 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008181 if (ret)
8182 goto err;
8183 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008184map:
8185 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008186err:
8187 bio_put(bio);
8188 return ret;
8189}
8190
8191static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8192 int skip_sum)
8193{
8194 struct inode *inode = dip->inode;
8195 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008196 struct bio *bio;
8197 struct bio *orig_bio = dip->orig_bio;
8198 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008199 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008200 u64 file_offset = dip->logical_offset;
8201 u64 submit_len = 0;
8202 u64 map_length;
8203 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08008204 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04008205 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008206
Kent Overstreet4f024f32013-10-11 15:44:27 -07008207 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008208 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008209 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008210 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008211 return -EIO;
Miao Xiefacc8a22013-07-25 19:22:34 +08008212
Kent Overstreet4f024f32013-10-11 15:44:27 -07008213 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008214 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008215 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008216 goto submit;
8217 }
8218
David Woodhouse53b381b2013-01-29 18:40:14 -05008219 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008220 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008221 async_submit = 0;
8222 else
8223 async_submit = 1;
8224
Josef Bacik02f57c72011-04-06 14:25:44 -04008225 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8226 if (!bio)
8227 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008228
Josef Bacik02f57c72011-04-06 14:25:44 -04008229 bio->bi_private = dip;
8230 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008231 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008232 atomic_inc(&dip->pending_bios);
8233
Miao Xiee65e1532010-11-22 03:04:43 +00008234 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02008235 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00008236 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02008237 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00008238 /*
8239 * inc the count before we submit the bio so
8240 * we know the end IO handler won't happen before
8241 * we inc the count. Otherwise, the dip might get freed
8242 * before we're done setting it up
8243 */
8244 atomic_inc(&dip->pending_bios);
8245 ret = __btrfs_submit_dio_bio(bio, inode, rw,
8246 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008247 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008248 if (ret) {
8249 bio_put(bio);
8250 atomic_dec(&dip->pending_bios);
8251 goto out_err;
8252 }
8253
Miao Xiee65e1532010-11-22 03:04:43 +00008254 start_sector += submit_len >> 9;
8255 file_offset += submit_len;
8256
8257 submit_len = 0;
8258 nr_pages = 0;
8259
8260 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8261 start_sector, GFP_NOFS);
8262 if (!bio)
8263 goto out_err;
8264 bio->bi_private = dip;
8265 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008266 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008267
Kent Overstreet4f024f32013-10-11 15:44:27 -07008268 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008269 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008270 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008271 &map_length, NULL, 0);
8272 if (ret) {
8273 bio_put(bio);
8274 goto out_err;
8275 }
8276 } else {
8277 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308278 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00008279 bvec++;
8280 }
8281 }
8282
Josef Bacik02f57c72011-04-06 14:25:44 -04008283submit:
Miao Xiee65e1532010-11-22 03:04:43 +00008284 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008285 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008286 if (!ret)
8287 return 0;
8288
8289 bio_put(bio);
8290out_err:
8291 dip->errors = 1;
8292 /*
8293 * before atomic variable goto zero, we must
8294 * make sure dip->errors is perceived to be set.
8295 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008296 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008297 if (atomic_dec_and_test(&dip->pending_bios))
8298 bio_io_error(dip->orig_bio);
8299
8300 /* bio_end_io() will handle error, so we needn't return it */
8301 return 0;
8302}
8303
Chris Mason9be33952013-05-17 18:30:14 -04008304static void btrfs_submit_direct(int rw, struct bio *dio_bio,
8305 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008306{
Filipe Manana61de7182015-07-01 12:13:10 +01008307 struct btrfs_dio_private *dip = NULL;
8308 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008309 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008310 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02008311 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008312 int ret = 0;
8313
8314 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8315
Chris Mason9be33952013-05-17 18:30:14 -04008316 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008317 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008318 ret = -ENOMEM;
8319 goto free_ordered;
8320 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008321
Miao Xiec1dc0892014-09-12 18:43:56 +08008322 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008323 if (!dip) {
8324 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008325 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008326 }
8327
8328 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008329 dip->inode = inode;
8330 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008331 dip->bytes = dio_bio->bi_iter.bi_size;
8332 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008333 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008334 dip->orig_bio = io_bio;
8335 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008336 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008337 btrfs_bio = btrfs_io_bio(io_bio);
8338 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008339
Miao Xiec1dc0892014-09-12 18:43:56 +08008340 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008341 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008342 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008343 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008344 dip->subio_endio = btrfs_subio_endio_read;
8345 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008346
Filipe Mananaf28a4922015-12-08 19:23:20 +00008347 /*
8348 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8349 * even if we fail to submit a bio, because in such case we do the
8350 * corresponding error handling below and it must not be done a second
8351 * time by btrfs_direct_IO().
8352 */
8353 if (write) {
8354 struct btrfs_dio_data *dio_data = current->journal_info;
8355
8356 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8357 dip->bytes;
8358 dio_data->unsubmitted_oe_range_start =
8359 dio_data->unsubmitted_oe_range_end;
8360 }
8361
Miao Xiee65e1532010-11-22 03:04:43 +00008362 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8363 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008364 return;
Chris Mason9be33952013-05-17 18:30:14 -04008365
Miao Xie23ea8e52014-09-12 18:43:54 +08008366 if (btrfs_bio->end_io)
8367 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008368
Josef Bacik4b46fce2010-05-23 11:00:55 -04008369free_ordered:
8370 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008371 * If we arrived here it means either we failed to submit the dip
8372 * or we either failed to clone the dio_bio or failed to allocate the
8373 * dip. If we cloned the dio_bio and allocated the dip, we can just
8374 * call bio_endio against our io_bio so that we get proper resource
8375 * cleanup if we fail to submit the dip, otherwise, we must do the
8376 * same as btrfs_endio_direct_[write|read] because we can't call these
8377 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008378 */
Filipe Manana61de7182015-07-01 12:13:10 +01008379 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008380 io_bio->bi_error = -EIO;
8381 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008382 /*
8383 * The end io callbacks free our dip, do the final put on io_bio
8384 * and all the cleanup and final put for dio_bio (through
8385 * dio_end_io()).
8386 */
8387 dip = NULL;
8388 io_bio = NULL;
8389 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008390 if (write)
8391 btrfs_endio_direct_write_update_ordered(inode,
8392 file_offset,
8393 dio_bio->bi_iter.bi_size,
8394 0);
8395 else
Filipe Manana61de7182015-07-01 12:13:10 +01008396 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8397 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008398
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008399 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008400 /*
8401 * Releases and cleans up our dio_bio, no need to bio_put()
8402 * nor bio_endio()/bio_io_error() against dio_bio.
8403 */
8404 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008405 }
Filipe Manana61de7182015-07-01 12:13:10 +01008406 if (io_bio)
8407 bio_put(io_bio);
8408 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008409}
8410
Omar Sandoval6f673762015-03-16 04:33:52 -07008411static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008412 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008413{
8414 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008415 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008416 unsigned blocksize_mask = root->sectorsize - 1;
8417 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008418
8419 if (offset & blocksize_mask)
8420 goto out;
8421
Al Viro28060d52014-03-22 05:15:17 -04008422 if (iov_iter_alignment(iter) & blocksize_mask)
8423 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008424
Al Viro28060d52014-03-22 05:15:17 -04008425 /* If this is a write we don't need to check anymore */
Omar Sandoval6f673762015-03-16 04:33:52 -07008426 if (iov_iter_rw(iter) == WRITE)
Al Viro28060d52014-03-22 05:15:17 -04008427 return 0;
8428 /*
8429 * Check to make sure we don't have duplicate iov_base's in this
8430 * iovec, if so return EINVAL, otherwise we'll get csum errors
8431 * when reading back.
8432 */
8433 for (seg = 0; seg < iter->nr_segs; seg++) {
8434 for (i = seg + 1; i < iter->nr_segs; i++) {
8435 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008436 goto out;
8437 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008438 }
8439 retval = 0;
8440out:
8441 return retval;
8442}
Josef Bacikeb838e72012-07-31 16:28:48 -04008443
Omar Sandoval22c61862015-03-16 04:33:53 -07008444static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
8445 loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04008446{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008447 struct file *file = iocb->ki_filp;
8448 struct inode *inode = file->f_mapping->host;
chandan50745b02015-08-28 21:10:13 +05308449 struct btrfs_root *root = BTRFS_I(inode)->root;
8450 struct btrfs_dio_data dio_data = { 0 };
Miao Xie09348562013-02-07 10:12:07 +00008451 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008452 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008453 bool wakeup = true;
8454 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008455 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008456
Omar Sandoval6f673762015-03-16 04:33:52 -07008457 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008458 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008459
Jens Axboefe0f07d2015-04-15 17:05:48 -06008460 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008461 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008462
Josef Bacik0e267c42013-07-02 10:38:02 -04008463 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008464 * The generic stuff only does filemap_write_and_wait_range, which
8465 * isn't enough if we've written compressed pages to this area, so
8466 * we need to flush the dirty pages again to make absolutely sure
8467 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008468 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008469 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008470 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8471 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008472 filemap_fdatawrite_range(inode->i_mapping, offset,
8473 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008474
Omar Sandoval6f673762015-03-16 04:33:52 -07008475 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008476 /*
8477 * If the write DIO is beyond the EOF, we need update
8478 * the isize, but it is protected by i_mutex. So we can
8479 * not unlock the i_mutex at this case.
8480 */
8481 if (offset + count <= inode->i_size) {
8482 mutex_unlock(&inode->i_mutex);
8483 relock = true;
8484 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008485 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008486 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008487 goto out;
chandan50745b02015-08-28 21:10:13 +05308488 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008489 BTRFS_MAX_EXTENT_SIZE - 1,
8490 BTRFS_MAX_EXTENT_SIZE);
8491
8492 /*
8493 * We need to know how many extents we reserved so that we can
8494 * do the accounting properly if we go over the number we
8495 * originally calculated. Abuse current->journal_info for this.
8496 */
chandan50745b02015-08-28 21:10:13 +05308497 dio_data.reserve = round_up(count, root->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008498 dio_data.unsubmitted_oe_range_start = (u64)offset;
8499 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308500 current->journal_info = &dio_data;
David Sterbaee39b432014-09-30 01:33:33 +02008501 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8502 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008503 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008504 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8505 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008506 }
8507
Omar Sandoval17f8c842015-03-16 04:33:50 -07008508 ret = __blockdev_direct_IO(iocb, inode,
8509 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
8510 iter, offset, btrfs_get_blocks_direct, NULL,
8511 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008512 if (iov_iter_rw(iter) == WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008513 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008514 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308515 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008516 btrfs_delalloc_release_space(inode, offset,
8517 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008518 /*
8519 * On error we might have left some ordered extents
8520 * without submitting corresponding bios for them, so
8521 * cleanup them up to avoid other tasks getting them
8522 * and waiting for them to complete forever.
8523 */
8524 if (dio_data.unsubmitted_oe_range_start <
8525 dio_data.unsubmitted_oe_range_end)
8526 btrfs_endio_direct_write_update_ordered(inode,
8527 dio_data.unsubmitted_oe_range_start,
8528 dio_data.unsubmitted_oe_range_end -
8529 dio_data.unsubmitted_oe_range_start,
8530 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008531 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008532 btrfs_delalloc_release_space(inode, offset,
8533 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008534 }
Miao Xie38851cc2013-02-08 07:04:11 +00008535out:
Miao Xie2e60a512013-02-08 07:01:08 +00008536 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008537 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008538 if (relock)
8539 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008540
8541 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008542}
8543
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008544#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8545
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008546static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8547 __u64 start, __u64 len)
8548{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008549 int ret;
8550
8551 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8552 if (ret)
8553 return ret;
8554
Chris Masonec29ed52011-02-23 16:23:20 -05008555 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008556}
8557
Chris Mason9ebefb182007-06-15 13:50:00 -04008558int btrfs_readpage(struct file *file, struct page *page)
8559{
Chris Masond1310b22008-01-24 16:13:08 -05008560 struct extent_io_tree *tree;
8561 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008562 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008563}
Chris Mason1832a6d2007-12-21 16:27:21 -05008564
Chris Mason39279cc2007-06-12 06:35:45 -04008565static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8566{
Chris Masond1310b22008-01-24 16:13:08 -05008567 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04008568
8569
8570 if (current->flags & PF_MEMALLOC) {
8571 redirty_page_for_writepage(wbc, page);
8572 unlock_page(page);
8573 return 0;
8574 }
Chris Masond1310b22008-01-24 16:13:08 -05008575 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008576 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8577}
Chris Mason39279cc2007-06-12 06:35:45 -04008578
Eric Sandeen48a3b632013-04-25 20:41:01 +00008579static int btrfs_writepages(struct address_space *mapping,
8580 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008581{
Chris Masond1310b22008-01-24 16:13:08 -05008582 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008583
Chris Masond1310b22008-01-24 16:13:08 -05008584 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008585 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8586}
8587
Chris Mason3ab2fb52007-11-08 10:59:22 -05008588static int
8589btrfs_readpages(struct file *file, struct address_space *mapping,
8590 struct list_head *pages, unsigned nr_pages)
8591{
Chris Masond1310b22008-01-24 16:13:08 -05008592 struct extent_io_tree *tree;
8593 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008594 return extent_readpages(tree, mapping, pages, nr_pages,
8595 btrfs_get_extent);
8596}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008597static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008598{
Chris Masond1310b22008-01-24 16:13:08 -05008599 struct extent_io_tree *tree;
8600 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008601 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008602
Chris Masond1310b22008-01-24 16:13:08 -05008603 tree = &BTRFS_I(page->mapping->host)->io_tree;
8604 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008605 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008606 if (ret == 1) {
8607 ClearPagePrivate(page);
8608 set_page_private(page, 0);
8609 page_cache_release(page);
8610 }
8611 return ret;
8612}
Chris Mason39279cc2007-06-12 06:35:45 -04008613
Chris Masone6dcd2d2008-07-17 12:53:50 -04008614static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8615{
Chris Mason98509cf2008-09-11 15:51:43 -04008616 if (PageWriteback(page) || PageDirty(page))
8617 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008618 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008619}
8620
Lukas Czernerd47992f2013-05-21 23:17:23 -04008621static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8622 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008623{
Josef Bacik5fd02042012-05-02 14:00:54 -04008624 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008625 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008626 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008627 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008628 u64 page_start = page_offset(page);
8629 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008630 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008631
Chris Mason8b62b722009-09-02 16:53:46 -04008632 /*
8633 * we have the page locked, so new writeback can't start,
8634 * and the dirty bit won't be cleared while we are here.
8635 *
8636 * Wait for IO on this page so that we can safely clear
8637 * the PagePrivate2 bit and do ordered accounting
8638 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008639 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008640
Josef Bacik5fd02042012-05-02 14:00:54 -04008641 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008642 if (offset) {
8643 btrfs_releasepage(page, GFP_NOFS);
8644 return;
8645 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008646
8647 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008648 lock_extent_bits(tree, page_start, page_end, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008649 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008650 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008651 /*
8652 * IO on this page will never be started, so we need
8653 * to account for any ordered extents now
8654 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008655 if (!inode_evicting)
8656 clear_extent_bit(tree, page_start, page_end,
8657 EXTENT_DIRTY | EXTENT_DELALLOC |
8658 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8659 EXTENT_DEFRAG, 1, 0, &cached_state,
8660 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008661 /*
8662 * whoever cleared the private bit is responsible
8663 * for the finish_ordered_io
8664 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008665 if (TestClearPagePrivate2(page)) {
8666 struct btrfs_ordered_inode_tree *tree;
8667 u64 new_len;
8668
8669 tree = &BTRFS_I(inode)->ordered_tree;
8670
8671 spin_lock_irq(&tree->lock);
8672 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8673 new_len = page_start - ordered->file_offset;
8674 if (new_len < ordered->truncated_len)
8675 ordered->truncated_len = new_len;
8676 spin_unlock_irq(&tree->lock);
8677
8678 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8679 page_start,
8680 PAGE_CACHE_SIZE, 1))
8681 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008682 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008683 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008684 if (!inode_evicting) {
8685 cached_state = NULL;
David Sterbaff13db42015-12-03 14:30:40 +01008686 lock_extent_bits(tree, page_start, page_end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008687 &cached_state);
8688 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008689 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008690
Qu Wenruob9d0b382015-09-29 10:35:16 +08008691 /*
8692 * Qgroup reserved space handler
8693 * Page here will be either
8694 * 1) Already written to disk
8695 * In this case, its reserved space is released from data rsv map
8696 * and will be freed by delayed_ref handler finally.
8697 * So even we call qgroup_free_data(), it won't decrease reserved
8698 * space.
8699 * 2) Not written to disk
8700 * This means the reserved space should be freed here.
8701 */
8702 btrfs_qgroup_free_data(inode, page_start, PAGE_CACHE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008703 if (!inode_evicting) {
8704 clear_extent_bit(tree, page_start, page_end,
8705 EXTENT_LOCKED | EXTENT_DIRTY |
8706 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8707 EXTENT_DEFRAG, 1, 1,
8708 &cached_state, GFP_NOFS);
8709
8710 __btrfs_releasepage(page, GFP_NOFS);
8711 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008712
Chris Mason4a096752008-07-21 10:29:44 -04008713 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008714 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008715 ClearPagePrivate(page);
8716 set_page_private(page, 0);
8717 page_cache_release(page);
8718 }
Chris Mason39279cc2007-06-12 06:35:45 -04008719}
8720
Chris Mason9ebefb182007-06-15 13:50:00 -04008721/*
8722 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8723 * called from a page fault handler when a page is first dirtied. Hence we must
8724 * be careful to check for EOF conditions here. We set the page up correctly
8725 * for a written page which means we get ENOSPC checking when writing into
8726 * holes and correct delalloc and unwritten extent mapping on filesystems that
8727 * support these features.
8728 *
8729 * We are not allowed to take the i_mutex here so we have to play games to
8730 * protect against truncate races as the page could now be beyond EOF. Because
8731 * vmtruncate() writes the inode size before removing pages, once we have the
8732 * page lock we can determine safely if the page is beyond EOF. If it is not
8733 * beyond EOF, then the page is guaranteed safe against truncation until we
8734 * unlock the page.
8735 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008736int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008737{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008738 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008739 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008740 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008741 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8742 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008743 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008744 char *kaddr;
8745 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008746 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008747 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008748 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008749 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008750 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008751
Jan Karab2b5ef52012-06-12 16:20:45 +02008752 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008753 page_start = page_offset(page);
8754 page_end = page_start + PAGE_CACHE_SIZE - 1;
8755
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008756 ret = btrfs_delalloc_reserve_space(inode, page_start,
8757 PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008758 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008759 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008760 reserved = 1;
8761 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008762 if (ret) {
8763 if (ret == -ENOMEM)
8764 ret = VM_FAULT_OOM;
8765 else /* -ENOSPC, -EIO, etc */
8766 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008767 if (reserved)
8768 goto out;
8769 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008770 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008771
Nick Piggin56a76f82009-03-31 15:23:23 -07008772 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008773again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008774 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008775 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008776
Chris Mason9ebefb182007-06-15 13:50:00 -04008777 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008778 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008779 /* page got truncated out from underneath us */
8780 goto out_unlock;
8781 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008782 wait_on_page_writeback(page);
8783
David Sterbaff13db42015-12-03 14:30:40 +01008784 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008785 set_page_extent_mapped(page);
8786
Chris Masoneb84ae02008-07-17 13:53:27 -04008787 /*
8788 * we can't set the delalloc bits if there are pending ordered
8789 * extents. Drop our locks and wait for them to finish
8790 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008791 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8792 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008793 unlock_extent_cached(io_tree, page_start, page_end,
8794 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008795 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008796 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008797 btrfs_put_ordered_extent(ordered);
8798 goto again;
8799 }
8800
Josef Bacikfbf19082009-10-01 17:10:23 -04008801 /*
8802 * XXX - page_mkwrite gets called every time the page is dirtied, even
8803 * if it was already dirty, so for space accounting reasons we need to
8804 * clear any delalloc bits for the range we are fixing to save. There
8805 * is probably a better way to do this, but for now keep consistent with
8806 * prepare_pages in the normal write path.
8807 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008808 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008809 EXTENT_DIRTY | EXTENT_DELALLOC |
8810 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008811 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008812
Josef Bacik2ac55d42010-02-03 19:33:23 +00008813 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8814 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008815 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008816 unlock_extent_cached(io_tree, page_start, page_end,
8817 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008818 ret = VM_FAULT_SIGBUS;
8819 goto out_unlock;
8820 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008821 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008822
8823 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008824 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008825 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008826 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008827 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008828
Chris Masone6dcd2d2008-07-17 12:53:50 -04008829 if (zero_start != PAGE_CACHE_SIZE) {
8830 kaddr = kmap(page);
8831 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8832 flush_dcache_page(page);
8833 kunmap(page);
8834 }
Chris Mason247e7432008-07-17 12:53:51 -04008835 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008836 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008837 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008838
Chris Mason257c62e2009-10-13 13:21:08 -04008839 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8840 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008841 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008842
Josef Bacik2ac55d42010-02-03 19:33:23 +00008843 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008844
8845out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008846 if (!ret) {
8847 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008848 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008849 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008850 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008851out:
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008852 btrfs_delalloc_release_space(inode, page_start, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008853out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008854 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008855 return ret;
8856}
8857
Josef Bacika41ad392011-01-31 15:30:16 -05008858static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008859{
8860 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008861 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008862 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008863 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008864 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008865 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008866 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008867
Josef Bacik0ef8b722013-10-25 16:13:35 -04008868 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8869 (u64)-1);
8870 if (ret)
8871 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008872
Josef Bacikfcb80c22011-05-03 10:40:22 -04008873 /*
8874 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8875 * 3 things going on here
8876 *
8877 * 1) We need to reserve space for our orphan item and the space to
8878 * delete our orphan item. Lord knows we don't want to have a dangling
8879 * orphan item because we didn't reserve space to remove it.
8880 *
8881 * 2) We need to reserve space to update our inode.
8882 *
8883 * 3) We need to have something to cache all the space that is going to
8884 * be free'd up by the truncate operation, but also have some slack
8885 * space reserved in case it uses space during the truncate (thank you
8886 * very much snapshotting).
8887 *
8888 * And we need these to all be seperate. The fact is we can use alot of
8889 * space doing the truncate, and we have no earthly idea how much space
8890 * we will use, so we need the truncate reservation to be seperate so it
8891 * doesn't end up using space reserved for updating the inode or
8892 * removing the orphan item. We also need to be able to stop the
8893 * transaction and start a new one, which means we need to be able to
8894 * update the inode several times, and we have no idea of knowing how
8895 * many times that will be, so we can't just reserve 1 item for the
8896 * entirety of the opration, so that has to be done seperately as well.
8897 * Then there is the orphan item, which does indeed need to be held on
8898 * to for the whole operation, and we need nobody to touch this reserved
8899 * space except the orphan code.
8900 *
8901 * So that leaves us with
8902 *
8903 * 1) root->orphan_block_rsv - for the orphan deletion.
8904 * 2) rsv - for the truncate reservation, which we will steal from the
8905 * transaction reservation.
8906 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8907 * updating the inode.
8908 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008909 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008910 if (!rsv)
8911 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008912 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008913 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008914
Josef Bacik907cbce2011-08-08 13:46:15 -04008915 /*
Josef Bacik07127182011-08-19 10:29:59 -04008916 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008917 * 1 for updating the inode.
8918 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008919 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008920 if (IS_ERR(trans)) {
8921 err = PTR_ERR(trans);
8922 goto out;
8923 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008924
Josef Bacik907cbce2011-08-08 13:46:15 -04008925 /* Migrate the slack space for the truncate to our reserve */
8926 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8927 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008928 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008929
Chris Mason5a3f23d2009-03-31 13:27:11 -04008930 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008931 * So if we truncate and then write and fsync we normally would just
8932 * write the extents that changed, which is a problem if we need to
8933 * first truncate that entire inode. So set this flag so we write out
8934 * all of the extents in the inode to the sync log so we're completely
8935 * safe.
8936 */
8937 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008938 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008939
Yan, Zheng80825102009-11-12 09:35:36 +00008940 while (1) {
8941 ret = btrfs_truncate_inode_items(trans, root, inode,
8942 inode->i_size,
8943 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08008944 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05008945 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008946 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008947 }
Chris Mason39279cc2007-06-12 06:35:45 -04008948
Josef Bacikfcb80c22011-05-03 10:40:22 -04008949 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008950 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008951 if (ret) {
8952 err = ret;
8953 break;
8954 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008955
Yan, Zheng80825102009-11-12 09:35:36 +00008956 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008957 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008958
8959 trans = btrfs_start_transaction(root, 2);
8960 if (IS_ERR(trans)) {
8961 ret = err = PTR_ERR(trans);
8962 trans = NULL;
8963 break;
8964 }
8965
8966 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8967 rsv, min_size);
8968 BUG_ON(ret); /* shouldn't happen */
8969 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008970 }
8971
8972 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008973 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008974 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008975 if (ret)
8976 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008977 }
8978
Chris Mason917c16b2011-11-08 14:49:59 -05008979 if (trans) {
8980 trans->block_rsv = &root->fs_info->trans_block_rsv;
8981 ret = btrfs_update_inode(trans, root, inode);
8982 if (ret && !err)
8983 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008984
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008985 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008986 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008987 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008988
8989out:
8990 btrfs_free_block_rsv(root, rsv);
8991
Josef Bacik3893e332011-01-31 16:03:11 -05008992 if (ret && !err)
8993 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008994
Josef Bacik3893e332011-01-31 16:03:11 -05008995 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008996}
8997
Sven Wegener3b963622008-06-09 21:57:42 -04008998/*
Chris Masond352ac62008-09-29 15:18:18 -04008999 * create a new subvolume directory/inode (helper for the ioctl).
9000 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009001int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009002 struct btrfs_root *new_root,
9003 struct btrfs_root *parent_root,
9004 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009005{
Chris Mason39279cc2007-06-12 06:35:45 -04009006 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009007 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009008 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009009
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009010 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9011 new_dirid, new_dirid,
9012 S_IFDIR | (~current_umask() & S_IRWXUGO),
9013 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009014 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009015 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009016 inode->i_op = &btrfs_dir_inode_operations;
9017 inode->i_fop = &btrfs_dir_file_operations;
9018
Miklos Szeredibfe86842011-10-28 14:13:29 +02009019 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009020 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009021 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009022
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009023 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9024 if (err)
9025 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009026 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009027 new_root->root_key.objectid, err);
9028
Yan, Zheng76dda932009-09-21 16:00:26 -04009029 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009030
Yan, Zheng76dda932009-09-21 16:00:26 -04009031 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009032 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009033}
9034
Chris Mason39279cc2007-06-12 06:35:45 -04009035struct inode *btrfs_alloc_inode(struct super_block *sb)
9036{
9037 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009038 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009039
9040 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9041 if (!ei)
9042 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009043
9044 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009045 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009046 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009047 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009048 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009049 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009050 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009051 ei->disk_i_size = 0;
9052 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009053 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009054 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009055 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009056 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009057 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009058
Josef Bacik9e0baf62011-07-15 15:16:44 +00009059 spin_lock_init(&ei->lock);
9060 ei->outstanding_extents = 0;
9061 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009062
Josef Bacik72ac3c02012-05-23 14:13:11 -04009063 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009064 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009065
Miao Xie16cdcec2011-04-22 18:12:22 +08009066 ei->delayed_node = NULL;
9067
chandan r9cc97d62012-07-04 12:48:07 +05309068 ei->i_otime.tv_sec = 0;
9069 ei->i_otime.tv_nsec = 0;
9070
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009071 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009072 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009073 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9074 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009075 ei->io_tree.track_uptodate = 1;
9076 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009077 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009078 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009079 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009080 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009081 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009082 RB_CLEAR_NODE(&ei->rb_node);
9083
9084 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009085}
9086
Josef Bacikaaedb552013-10-11 14:44:09 -04009087#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9088void btrfs_test_destroy_inode(struct inode *inode)
9089{
9090 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9091 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9092}
9093#endif
9094
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009095static void btrfs_i_callback(struct rcu_head *head)
9096{
9097 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009098 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9099}
9100
Chris Mason39279cc2007-06-12 06:35:45 -04009101void btrfs_destroy_inode(struct inode *inode)
9102{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009103 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009104 struct btrfs_root *root = BTRFS_I(inode)->root;
9105
Al Virob3d9b7a2012-06-09 13:51:19 -04009106 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009107 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009108 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9109 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009110 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9111 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009112 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009113
Chris Mason5a3f23d2009-03-31 13:27:11 -04009114 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009115 * This can happen where we create an inode, but somebody else also
9116 * created the same inode and we need to destroy the one we already
9117 * created.
9118 */
9119 if (!root)
9120 goto free;
9121
Josef Bacik8a35d952012-05-23 14:26:42 -04009122 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9123 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009124 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009125 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009126 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009127 }
Josef Bacik7b128762008-07-24 12:17:14 -04009128
Chris Masond3977122009-01-05 21:25:51 -05009129 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009130 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9131 if (!ordered)
9132 break;
9133 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009134 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009135 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009136 btrfs_remove_ordered_extent(inode, ordered);
9137 btrfs_put_ordered_extent(ordered);
9138 btrfs_put_ordered_extent(ordered);
9139 }
9140 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009141 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009142 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009143 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009144free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009145 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009146}
9147
Al Viro45321ac2010-06-07 13:43:19 -04009148int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009149{
9150 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009151
Naohiro Aota6379ef92013-06-06 09:56:34 +00009152 if (root == NULL)
9153 return 1;
9154
Liu Bofa6ac872013-02-20 14:10:23 +00009155 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009156 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009157 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009158 else
Al Viro45321ac2010-06-07 13:43:19 -04009159 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009160}
9161
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009162static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009163{
9164 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9165
9166 inode_init_once(&ei->vfs_inode);
9167}
9168
9169void btrfs_destroy_cachep(void)
9170{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009171 /*
9172 * Make sure all delayed rcu free inodes are flushed before we
9173 * destroy cache.
9174 */
9175 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04009176 if (btrfs_inode_cachep)
9177 kmem_cache_destroy(btrfs_inode_cachep);
9178 if (btrfs_trans_handle_cachep)
9179 kmem_cache_destroy(btrfs_trans_handle_cachep);
9180 if (btrfs_transaction_cachep)
9181 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009182 if (btrfs_path_cachep)
9183 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05009184 if (btrfs_free_space_cachep)
9185 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009186 if (btrfs_delalloc_work_cachep)
9187 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009188}
9189
9190int btrfs_init_cachep(void)
9191{
David Sterba837e1972012-09-07 03:00:48 -06009192 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009193 sizeof(struct btrfs_inode), 0,
9194 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009195 if (!btrfs_inode_cachep)
9196 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009197
David Sterba837e1972012-09-07 03:00:48 -06009198 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009199 sizeof(struct btrfs_trans_handle), 0,
9200 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009201 if (!btrfs_trans_handle_cachep)
9202 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009203
David Sterba837e1972012-09-07 03:00:48 -06009204 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009205 sizeof(struct btrfs_transaction), 0,
9206 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009207 if (!btrfs_transaction_cachep)
9208 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009209
David Sterba837e1972012-09-07 03:00:48 -06009210 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009211 sizeof(struct btrfs_path), 0,
9212 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009213 if (!btrfs_path_cachep)
9214 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009215
David Sterba837e1972012-09-07 03:00:48 -06009216 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009217 sizeof(struct btrfs_free_space), 0,
9218 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
9219 if (!btrfs_free_space_cachep)
9220 goto fail;
9221
Miao Xie8ccf6f192012-10-25 09:28:04 +00009222 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
9223 sizeof(struct btrfs_delalloc_work), 0,
9224 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
9225 NULL);
9226 if (!btrfs_delalloc_work_cachep)
9227 goto fail;
9228
Chris Mason39279cc2007-06-12 06:35:45 -04009229 return 0;
9230fail:
9231 btrfs_destroy_cachep();
9232 return -ENOMEM;
9233}
9234
9235static int btrfs_getattr(struct vfsmount *mnt,
9236 struct dentry *dentry, struct kstat *stat)
9237{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009238 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009239 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009240 u32 blocksize = inode->i_sb->s_blocksize;
9241
Chris Mason39279cc2007-06-12 06:35:45 -04009242 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009243 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05009244 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009245
9246 spin_lock(&BTRFS_I(inode)->lock);
9247 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9248 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009249 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009250 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009251 return 0;
9252}
9253
Chris Masond3977122009-01-05 21:25:51 -05009254static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9255 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009256{
9257 struct btrfs_trans_handle *trans;
9258 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009259 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009260 struct inode *new_inode = d_inode(new_dentry);
9261 struct inode *old_inode = d_inode(old_dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04009262 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009263 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009264 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009265 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009266 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009267
Li Zefan33345d012011-04-20 10:31:50 +08009268 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009269 return -EPERM;
9270
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009271 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009272 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009273 return -EXDEV;
9274
Li Zefan33345d012011-04-20 10:31:50 +08009275 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9276 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009277 return -ENOTEMPTY;
9278
Chris Mason39279cc2007-06-12 06:35:45 -04009279 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009280 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009281 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009282
9283
9284 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009285 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009286 new_dentry->d_name.name,
9287 new_dentry->d_name.len);
9288
9289 if (ret) {
9290 if (ret == -EEXIST) {
9291 /* we shouldn't get
9292 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309293 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009294 return ret;
9295 }
9296 } else {
9297 /* maybe -EOVERFLOW */
9298 return ret;
9299 }
9300 }
9301 ret = 0;
9302
Chris Mason5a3f23d2009-03-31 13:27:11 -04009303 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009304 * we're using rename to replace one file with another. Start IO on it
9305 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009306 */
Chris Mason8d875f92014-08-12 10:47:42 -07009307 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009308 filemap_flush(old_inode->i_mapping);
9309
Yan, Zheng76dda932009-09-21 16:00:26 -04009310 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009311 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009312 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009313 /*
9314 * We want to reserve the absolute worst case amount of items. So if
9315 * both inodes are subvols and we need to unlink them then that would
9316 * require 4 item modifications, but if they are both normal inodes it
9317 * would require 5 item modifications, so we'll assume their normal
9318 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9319 * should cover the worst case number of items we'll modify.
9320 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04009321 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009322 if (IS_ERR(trans)) {
9323 ret = PTR_ERR(trans);
9324 goto out_notrans;
9325 }
Chris Mason5f39d392007-10-15 16:14:19 -04009326
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009327 if (dest != root)
9328 btrfs_record_root_in_trans(trans, dest);
9329
Yan, Zhenga5719522009-09-24 09:17:31 -04009330 ret = btrfs_set_inode_index(new_dir, &index);
9331 if (ret)
9332 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009333
Miao Xie67de1172013-12-26 13:07:06 +08009334 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009335 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009336 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009337 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009338 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009339 ret = btrfs_insert_inode_ref(trans, dest,
9340 new_dentry->d_name.name,
9341 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009342 old_ino,
9343 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009344 if (ret)
9345 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009346 /*
9347 * this is an ugly little race, but the rename is required
9348 * to make sure that if we crash, the inode is either at the
9349 * old name or the new one. pinning the log transaction lets
9350 * us make sure we don't allow a log commit to come in after
9351 * we unlink the name but before we add the new name back in.
9352 */
9353 btrfs_pin_log_trans(root);
9354 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009355
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009356 inode_inc_iversion(old_dir);
9357 inode_inc_iversion(new_dir);
9358 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009359 old_dir->i_ctime = old_dir->i_mtime = ctime;
9360 new_dir->i_ctime = new_dir->i_mtime = ctime;
9361 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04009362
Chris Mason12fcfd22009-03-24 10:24:20 -04009363 if (old_dentry->d_parent != new_dentry->d_parent)
9364 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9365
Li Zefan33345d012011-04-20 10:31:50 +08009366 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009367 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9368 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9369 old_dentry->d_name.name,
9370 old_dentry->d_name.len);
9371 } else {
Al Viro92986792011-03-04 17:14:37 +00009372 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009373 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009374 old_dentry->d_name.name,
9375 old_dentry->d_name.len);
9376 if (!ret)
9377 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009378 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009379 if (ret) {
9380 btrfs_abort_transaction(trans, root, ret);
9381 goto out_fail;
9382 }
Chris Mason39279cc2007-06-12 06:35:45 -04009383
9384 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009385 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009386 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08009387 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009388 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9389 root_objectid = BTRFS_I(new_inode)->location.objectid;
9390 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9391 root_objectid,
9392 new_dentry->d_name.name,
9393 new_dentry->d_name.len);
9394 BUG_ON(new_inode->i_nlink == 0);
9395 } else {
9396 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009397 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009398 new_dentry->d_name.name,
9399 new_dentry->d_name.len);
9400 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009401 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009402 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009403 if (ret) {
9404 btrfs_abort_transaction(trans, root, ret);
9405 goto out_fail;
9406 }
Chris Mason39279cc2007-06-12 06:35:45 -04009407 }
Josef Bacikaec74772008-07-24 12:12:38 -04009408
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009409 ret = btrfs_add_link(trans, new_dir, old_inode,
9410 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009411 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009412 if (ret) {
9413 btrfs_abort_transaction(trans, root, ret);
9414 goto out_fail;
9415 }
Chris Mason39279cc2007-06-12 06:35:45 -04009416
Miao Xie67de1172013-12-26 13:07:06 +08009417 if (old_inode->i_nlink == 1)
9418 BTRFS_I(old_inode)->dir_index = index;
9419
Li Zefan33345d012011-04-20 10:31:50 +08009420 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04009421 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00009422 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009423 btrfs_end_log_trans(root);
9424 }
Chris Mason39279cc2007-06-12 06:35:45 -04009425out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009426 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009427out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009428 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009429 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009430
Chris Mason39279cc2007-06-12 06:35:45 -04009431 return ret;
9432}
9433
Miklos Szeredi80ace852014-07-23 15:15:32 +02009434static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9435 struct inode *new_dir, struct dentry *new_dentry,
9436 unsigned int flags)
9437{
9438 if (flags & ~RENAME_NOREPLACE)
9439 return -EINVAL;
9440
9441 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
9442}
9443
Miao Xie8ccf6f192012-10-25 09:28:04 +00009444static void btrfs_run_delalloc_work(struct btrfs_work *work)
9445{
9446 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009447 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009448
9449 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9450 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009451 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009452 filemap_flush(inode->i_mapping);
9453 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9454 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009455 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009456
9457 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009458 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009459 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009460 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009461 complete(&delalloc_work->completion);
9462}
9463
9464struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +01009465 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009466{
9467 struct btrfs_delalloc_work *work;
9468
9469 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
9470 if (!work)
9471 return NULL;
9472
9473 init_completion(&work->completion);
9474 INIT_LIST_HEAD(&work->list);
9475 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009476 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009477 WARN_ON_ONCE(!inode);
9478 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9479 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009480
9481 return work;
9482}
9483
9484void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9485{
9486 wait_for_completion(&work->completion);
9487 kmem_cache_free(btrfs_delalloc_work_cachep, work);
9488}
9489
Chris Masond352ac62008-09-29 15:18:18 -04009490/*
9491 * some fairly slow code that needs optimization. This walks the list
9492 * of all the inodes with pending delalloc and forces them to disk.
9493 */
Miao Xie6c255e62014-03-06 13:55:01 +08009494static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9495 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009496{
Chris Masonea8c2812008-08-04 23:17:27 -04009497 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009498 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009499 struct btrfs_delalloc_work *work, *next;
9500 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009501 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009502 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009503
Miao Xie8ccf6f192012-10-25 09:28:04 +00009504 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009505 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009506
Miao Xie573bfb72014-03-06 13:55:03 +08009507 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009508 spin_lock(&root->delalloc_lock);
9509 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009510 while (!list_empty(&splice)) {
9511 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009512 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009513
Miao Xieeb73c1b2013-05-15 07:48:22 +00009514 list_move_tail(&binode->delalloc_inodes,
9515 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009516 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009517 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009518 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009519 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009520 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009521 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009522
David Sterba651d4942015-11-27 19:24:16 +01009523 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009524 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009525 if (delay_iput)
9526 btrfs_add_delayed_iput(inode);
9527 else
9528 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009529 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009530 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009531 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009532 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009533 btrfs_queue_work(root->fs_info->flush_workers,
9534 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009535 ret++;
9536 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009537 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009538 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009539 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009540 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009541 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee1d2008-09-29 11:19:10 -04009542
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009543out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009544 list_for_each_entry_safe(work, next, &works, list) {
9545 list_del_init(&work->list);
9546 btrfs_wait_and_free_delalloc_work(work);
9547 }
9548
Miao Xieeb73c1b2013-05-15 07:48:22 +00009549 if (!list_empty_careful(&splice)) {
9550 spin_lock(&root->delalloc_lock);
9551 list_splice_tail(&splice, &root->delalloc_inodes);
9552 spin_unlock(&root->delalloc_lock);
9553 }
Miao Xie573bfb72014-03-06 13:55:03 +08009554 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009555 return ret;
9556}
9557
9558int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9559{
9560 int ret;
9561
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009562 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009563 return -EROFS;
9564
Miao Xie6c255e62014-03-06 13:55:01 +08009565 ret = __start_delalloc_inodes(root, delay_iput, -1);
9566 if (ret > 0)
9567 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009568 /*
9569 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee1d2008-09-29 11:19:10 -04009570 * we have to make sure the IO is actually started and that
9571 * ordered extents get created before we return
9572 */
9573 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009574 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009575 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee1d2008-09-29 11:19:10 -04009576 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009577 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9578 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee1d2008-09-29 11:19:10 -04009579 }
9580 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009581 return ret;
9582}
9583
Miao Xie6c255e62014-03-06 13:55:01 +08009584int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9585 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009586{
9587 struct btrfs_root *root;
9588 struct list_head splice;
9589 int ret;
9590
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009591 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009592 return -EROFS;
9593
9594 INIT_LIST_HEAD(&splice);
9595
Miao Xie573bfb72014-03-06 13:55:03 +08009596 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009597 spin_lock(&fs_info->delalloc_root_lock);
9598 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009599 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009600 root = list_first_entry(&splice, struct btrfs_root,
9601 delalloc_root);
9602 root = btrfs_grab_fs_root(root);
9603 BUG_ON(!root);
9604 list_move_tail(&root->delalloc_root,
9605 &fs_info->delalloc_roots);
9606 spin_unlock(&fs_info->delalloc_root_lock);
9607
Miao Xie6c255e62014-03-06 13:55:01 +08009608 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009609 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009610 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009611 goto out;
9612
Miao Xie6c255e62014-03-06 13:55:01 +08009613 if (nr != -1) {
9614 nr -= ret;
9615 WARN_ON(nr < 0);
9616 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009617 spin_lock(&fs_info->delalloc_root_lock);
9618 }
9619 spin_unlock(&fs_info->delalloc_root_lock);
9620
Miao Xie6c255e62014-03-06 13:55:01 +08009621 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009622 atomic_inc(&fs_info->async_submit_draining);
9623 while (atomic_read(&fs_info->nr_async_submits) ||
9624 atomic_read(&fs_info->async_delalloc_pages)) {
9625 wait_event(fs_info->async_submit_wait,
9626 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9627 atomic_read(&fs_info->async_delalloc_pages) == 0));
9628 }
9629 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009630out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009631 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009632 spin_lock(&fs_info->delalloc_root_lock);
9633 list_splice_tail(&splice, &fs_info->delalloc_roots);
9634 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009635 }
Miao Xie573bfb72014-03-06 13:55:03 +08009636 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009637 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009638}
9639
Chris Mason39279cc2007-06-12 06:35:45 -04009640static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9641 const char *symname)
9642{
9643 struct btrfs_trans_handle *trans;
9644 struct btrfs_root *root = BTRFS_I(dir)->root;
9645 struct btrfs_path *path;
9646 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009647 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009648 int err;
9649 int drop_inode = 0;
9650 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309651 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009652 int name_len;
9653 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009654 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009655 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009656 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009657
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009658 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009659 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9660 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009661
Josef Bacik9ed74f22009-09-11 16:12:44 -04009662 /*
9663 * 2 items for inode item and ref
9664 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009665 * 1 item for updating parent inode item
9666 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009667 * 1 item for xattr if selinux is on
9668 */
Filipe Manana9269d122015-12-31 18:16:29 +00009669 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009670 if (IS_ERR(trans))
9671 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009672
Li Zefan581bb052011-04-20 10:06:11 +08009673 err = btrfs_find_free_ino(root, &objectid);
9674 if (err)
9675 goto out_unlock;
9676
Josef Bacikaec74772008-07-24 12:12:38 -04009677 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009678 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04009679 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009680 if (IS_ERR(inode)) {
9681 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009682 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009683 }
Chris Mason39279cc2007-06-12 06:35:45 -04009684
Casey Schauflerad19db72011-12-15 10:09:07 -05009685 /*
9686 * If the active LSM wants to access the inode during
9687 * d_instantiate it needs these. Smack checks to see
9688 * if the filesystem supports xattrs by looking at the
9689 * ops vector.
9690 */
9691 inode->i_fop = &btrfs_file_operations;
9692 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009693 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009694 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9695
9696 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9697 if (err)
9698 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009699
Chris Mason39279cc2007-06-12 06:35:45 -04009700 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009701 if (!path) {
9702 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009703 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009704 }
Li Zefan33345d012011-04-20 10:31:50 +08009705 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009706 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009707 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009708 datasize = btrfs_file_extent_calc_inline_size(name_len);
9709 err = btrfs_insert_empty_item(trans, root, path, &key,
9710 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009711 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009712 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009713 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009714 }
Chris Mason5f39d392007-10-15 16:14:19 -04009715 leaf = path->nodes[0];
9716 ei = btrfs_item_ptr(leaf, path->slots[0],
9717 struct btrfs_file_extent_item);
9718 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9719 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009720 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009721 btrfs_set_file_extent_encryption(leaf, ei, 0);
9722 btrfs_set_file_extent_compression(leaf, ei, 0);
9723 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9724 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9725
Chris Mason39279cc2007-06-12 06:35:45 -04009726 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009727 write_extent_buffer(leaf, symname, ptr, name_len);
9728 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009729 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009730
Chris Mason39279cc2007-06-12 06:35:45 -04009731 inode->i_op = &btrfs_symlink_inode_operations;
9732 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -04009733 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009734 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009735 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009736 /*
9737 * Last step, add directory indexes for our symlink inode. This is the
9738 * last step to avoid extra cleanup of these indexes if an error happens
9739 * elsewhere above.
9740 */
9741 if (!err)
9742 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07009743 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009744 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009745 goto out_unlock_inode;
9746 }
9747
9748 unlock_new_inode(inode);
9749 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009750
9751out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009752 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009753 if (drop_inode) {
9754 inode_dec_link_count(inode);
9755 iput(inode);
9756 }
Liu Bob53d3f52012-11-14 14:34:34 +00009757 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009758 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009759
9760out_unlock_inode:
9761 drop_inode = 1;
9762 unlock_new_inode(inode);
9763 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009764}
Chris Mason16432982008-04-10 10:23:21 -04009765
Josef Bacik0af3d002010-06-21 14:48:16 -04009766static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9767 u64 start, u64 num_bytes, u64 min_size,
9768 loff_t actual_len, u64 *alloc_hint,
9769 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009770{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009771 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9772 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009773 struct btrfs_root *root = BTRFS_I(inode)->root;
9774 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009775 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009776 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009777 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009778 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009779 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009780 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009781
Josef Bacik0af3d002010-06-21 14:48:16 -04009782 if (trans)
9783 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009784 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009785 if (own_trans) {
9786 trans = btrfs_start_transaction(root, 3);
9787 if (IS_ERR(trans)) {
9788 ret = PTR_ERR(trans);
9789 break;
9790 }
Yan Zhengd899e052008-10-30 14:25:28 -04009791 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009792
Chris Mason154ea282013-03-05 11:11:26 -05009793 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9794 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009795 /*
9796 * If we are severely fragmented we could end up with really
9797 * small allocations, so if the allocator is returning small
9798 * chunks lets make its job easier by only searching for those
9799 * sized chunks.
9800 */
9801 cur_bytes = min(cur_bytes, last_alloc);
Josef Bacik00361582013-08-14 14:02:47 -04009802 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009803 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009804 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009805 if (own_trans)
9806 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009807 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009808 }
9809
Josef Bacik0b670dc2015-09-23 17:11:16 -04009810 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04009811 ret = insert_reserved_file_extent(trans, inode,
9812 cur_offset, ins.objectid,
9813 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009814 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009815 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009816 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009817 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009818 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009819 btrfs_abort_transaction(trans, root, ret);
9820 if (own_trans)
9821 btrfs_end_transaction(trans, root);
9822 break;
9823 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009824
Chris Masona1ed8352009-09-11 12:27:37 -04009825 btrfs_drop_extent_cache(inode, cur_offset,
9826 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009827
Josef Bacik5dc562c2012-08-17 13:14:17 -04009828 em = alloc_extent_map();
9829 if (!em) {
9830 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9831 &BTRFS_I(inode)->runtime_flags);
9832 goto next;
9833 }
9834
9835 em->start = cur_offset;
9836 em->orig_start = cur_offset;
9837 em->len = ins.offset;
9838 em->block_start = ins.objectid;
9839 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009840 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009841 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009842 em->bdev = root->fs_info->fs_devices->latest_bdev;
9843 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9844 em->generation = trans->transid;
9845
9846 while (1) {
9847 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009848 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009849 write_unlock(&em_tree->lock);
9850 if (ret != -EEXIST)
9851 break;
9852 btrfs_drop_extent_cache(inode, cur_offset,
9853 cur_offset + ins.offset - 1,
9854 0);
9855 }
9856 free_extent_map(em);
9857next:
Yan Zhengd899e052008-10-30 14:25:28 -04009858 num_bytes -= ins.offset;
9859 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009860 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009861
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009862 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009863 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009864 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009865 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009866 (actual_len > inode->i_size) &&
9867 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009868 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009869 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009870 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009871 i_size = cur_offset;
9872 i_size_write(inode, i_size);
9873 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009874 }
9875
Yan Zhengd899e052008-10-30 14:25:28 -04009876 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009877
9878 if (ret) {
9879 btrfs_abort_transaction(trans, root, ret);
9880 if (own_trans)
9881 btrfs_end_transaction(trans, root);
9882 break;
9883 }
Yan Zhengd899e052008-10-30 14:25:28 -04009884
Josef Bacik0af3d002010-06-21 14:48:16 -04009885 if (own_trans)
9886 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009887 }
Yan Zhengd899e052008-10-30 14:25:28 -04009888 return ret;
9889}
9890
Josef Bacik0af3d002010-06-21 14:48:16 -04009891int btrfs_prealloc_file_range(struct inode *inode, int mode,
9892 u64 start, u64 num_bytes, u64 min_size,
9893 loff_t actual_len, u64 *alloc_hint)
9894{
9895 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9896 min_size, actual_len, alloc_hint,
9897 NULL);
9898}
9899
9900int btrfs_prealloc_file_range_trans(struct inode *inode,
9901 struct btrfs_trans_handle *trans, int mode,
9902 u64 start, u64 num_bytes, u64 min_size,
9903 loff_t actual_len, u64 *alloc_hint)
9904{
9905 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9906 min_size, actual_len, alloc_hint, trans);
9907}
9908
Chris Masone6dcd2d2008-07-17 12:53:50 -04009909static int btrfs_set_page_dirty(struct page *page)
9910{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009911 return __set_page_dirty_nobuffers(page);
9912}
9913
Al Viro10556cb2011-06-20 19:28:19 -04009914static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009915{
Li Zefanb83cc962010-12-20 16:04:08 +08009916 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009917 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009918
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009919 if (mask & MAY_WRITE &&
9920 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9921 if (btrfs_root_readonly(root))
9922 return -EROFS;
9923 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9924 return -EACCES;
9925 }
Al Viro2830ba72011-06-20 19:16:29 -04009926 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009927}
Chris Mason39279cc2007-06-12 06:35:45 -04009928
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009929static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9930{
9931 struct btrfs_trans_handle *trans;
9932 struct btrfs_root *root = BTRFS_I(dir)->root;
9933 struct inode *inode = NULL;
9934 u64 objectid;
9935 u64 index;
9936 int ret = 0;
9937
9938 /*
9939 * 5 units required for adding orphan entry
9940 */
9941 trans = btrfs_start_transaction(root, 5);
9942 if (IS_ERR(trans))
9943 return PTR_ERR(trans);
9944
9945 ret = btrfs_find_free_ino(root, &objectid);
9946 if (ret)
9947 goto out;
9948
9949 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9950 btrfs_ino(dir), objectid, mode, &index);
9951 if (IS_ERR(inode)) {
9952 ret = PTR_ERR(inode);
9953 inode = NULL;
9954 goto out;
9955 }
9956
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009957 inode->i_fop = &btrfs_file_operations;
9958 inode->i_op = &btrfs_file_inode_operations;
9959
9960 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009961 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9962
Chris Masonb0d5d102014-09-08 13:08:51 -07009963 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9964 if (ret)
9965 goto out_inode;
9966
9967 ret = btrfs_update_inode(trans, root, inode);
9968 if (ret)
9969 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009970 ret = btrfs_orphan_add(trans, inode);
9971 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009972 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009973
Filipe Manana5762b5c2014-08-01 00:10:32 +01009974 /*
9975 * We set number of links to 0 in btrfs_new_inode(), and here we set
9976 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9977 * through:
9978 *
9979 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9980 */
9981 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009982 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009983 d_tmpfile(dentry, inode);
9984 mark_inode_dirty(inode);
9985
9986out:
9987 btrfs_end_transaction(trans, root);
9988 if (ret)
9989 iput(inode);
9990 btrfs_balance_delayed_items(root);
9991 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009992 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009993
9994out_inode:
9995 unlock_new_inode(inode);
9996 goto out;
9997
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009998}
9999
Filipe Mananab38ef712014-11-13 17:01:45 +000010000/* Inspired by filemap_check_errors() */
10001int btrfs_inode_check_errors(struct inode *inode)
10002{
10003 int ret = 0;
10004
10005 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
10006 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
10007 ret = -ENOSPC;
10008 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
10009 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
10010 ret = -EIO;
10011
10012 return ret;
10013}
10014
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010015static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010016 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010017 .lookup = btrfs_lookup,
10018 .create = btrfs_create,
10019 .unlink = btrfs_unlink,
10020 .link = btrfs_link,
10021 .mkdir = btrfs_mkdir,
10022 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +020010023 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010024 .symlink = btrfs_symlink,
10025 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010026 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010027 .setxattr = btrfs_setxattr,
10028 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010029 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010030 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -050010031 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010032 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010033 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010034 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010035 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010036};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010037static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010038 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010039 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010040 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010041 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010042 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010043};
Yan, Zheng76dda932009-09-21 16:00:26 -040010044
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010045static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010046 .llseek = generic_file_llseek,
10047 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -040010048 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010049 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010050#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010051 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010052#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010053 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010054 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010055};
10056
Chris Masond1310b22008-01-24 16:13:08 -050010057static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010058 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010059 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010060 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010061 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010062 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010063 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010064 .set_bit_hook = btrfs_set_bit_hook,
10065 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010066 .merge_extent_hook = btrfs_merge_extent_hook,
10067 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010068};
10069
Chris Mason35054392009-01-21 13:11:13 -050010070/*
10071 * btrfs doesn't support the bmap operation because swapfiles
10072 * use bmap to make a mapping of extents in the file. They assume
10073 * these extents won't change over the life of the file and they
10074 * use the bmap result to do IO directly to the drive.
10075 *
10076 * the btrfs bmap call would return logical addresses that aren't
10077 * suitable for IO and they also will change frequently as COW
10078 * operations happen. So, swapfile + btrfs == corruption.
10079 *
10080 * For now we're avoiding this by dropping bmap.
10081 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010082static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010083 .readpage = btrfs_readpage,
10084 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010085 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010086 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010087 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010088 .invalidatepage = btrfs_invalidatepage,
10089 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010090 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010091 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010092};
10093
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010094static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010095 .readpage = btrfs_readpage,
10096 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010097 .invalidatepage = btrfs_invalidatepage,
10098 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010099};
10100
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010101static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010102 .getattr = btrfs_getattr,
10103 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010104 .setxattr = btrfs_setxattr,
10105 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010106 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010107 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -050010108 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010109 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010110 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010111 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010112 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010113};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010114static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010115 .getattr = btrfs_getattr,
10116 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010117 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010118 .setxattr = btrfs_setxattr,
10119 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -040010120 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010121 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010122 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010123 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010124 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010125};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010126static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010127 .readlink = generic_readlink,
10128 .follow_link = page_follow_link_light,
10129 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +000010130 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010131 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010132 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010133 .setxattr = btrfs_setxattr,
10134 .getxattr = btrfs_getxattr,
10135 .listxattr = btrfs_listxattr,
10136 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010137 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010138};
Yan, Zheng76dda932009-09-21 16:00:26 -040010139
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010140const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010141 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010142 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010143};