blob: 917ab6b6c27741bf551391e78e9a2f1fe3248dd1 [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;
David Sterba20e55062015-11-19 11:42:28 +010084static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040085
86static struct kmem_cache *btrfs_inode_cachep;
87struct kmem_cache *btrfs_trans_handle_cachep;
88struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040089struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050090struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040091
92#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010093static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040094 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
95 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
96 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
97 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
98 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
99 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
100 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
101};
102
Eric Sandeen3972f262013-01-12 02:57:22 +0000103static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500104static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400105static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500106static noinline int cow_file_range(struct inode *inode,
107 struct page *locked_page,
108 u64 start, u64 end, int *page_started,
109 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400110static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
111 u64 len, u64 orig_start,
112 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400113 u64 orig_block_len, u64 ram_bytes,
114 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400115
Eric Sandeen48a3b632013-04-25 20:41:01 +0000116static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400117
Josef Bacik6a3891c2015-03-16 17:38:52 -0400118#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
119void btrfs_test_inode_set_ops(struct inode *inode)
120{
121 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
122}
123#endif
124
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000125static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500126 struct inode *inode, struct inode *dir,
127 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500128{
129 int err;
130
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000131 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500132 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500133 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500134 return err;
135}
136
Chris Masond352ac62008-09-29 15:18:18 -0400137/*
Chris Masonc8b97812008-10-29 14:49:59 -0400138 * this does all the hard work for inserting an inline extent into
139 * the btree. The caller should have done a btrfs_drop_extents so that
140 * no overlapping inline items exist in the btree
141 */
Chris Mason40f76582014-05-21 13:35:51 -0700142static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000143 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400144 struct btrfs_root *root, struct inode *inode,
145 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000146 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400147 struct page **compressed_pages)
148{
Chris Masonc8b97812008-10-29 14:49:59 -0400149 struct extent_buffer *leaf;
150 struct page *page = NULL;
151 char *kaddr;
152 unsigned long ptr;
153 struct btrfs_file_extent_item *ei;
154 int err = 0;
155 int ret;
156 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400157 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400158
Li Zefanfe3f5662011-03-28 08:30:38 +0000159 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400160 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400161
Chris Masonc8b97812008-10-29 14:49:59 -0400162 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000163
164 if (!extent_inserted) {
165 struct btrfs_key key;
166 size_t datasize;
167
168 key.objectid = btrfs_ino(inode);
169 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200170 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000171
172 datasize = btrfs_file_extent_calc_inline_size(cur_size);
173 path->leave_spinning = 1;
174 ret = btrfs_insert_empty_item(trans, root, path, &key,
175 datasize);
176 if (ret) {
177 err = ret;
178 goto fail;
179 }
Chris Masonc8b97812008-10-29 14:49:59 -0400180 }
181 leaf = path->nodes[0];
182 ei = btrfs_item_ptr(leaf, path->slots[0],
183 struct btrfs_file_extent_item);
184 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
185 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
186 btrfs_set_file_extent_encryption(leaf, ei, 0);
187 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
188 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
189 ptr = btrfs_file_extent_inline_start(ei);
190
Li Zefan261507a02010-12-17 14:21:50 +0800191 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400192 struct page *cpage;
193 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500194 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400195 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500196 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400197 PAGE_CACHE_SIZE);
198
Cong Wang7ac687d2011-11-25 23:14:28 +0800199 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400200 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800201 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400202
203 i++;
204 ptr += cur_size;
205 compressed_size -= cur_size;
206 }
207 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800208 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400209 } else {
210 page = find_get_page(inode->i_mapping,
211 start >> PAGE_CACHE_SHIFT);
212 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800213 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400214 offset = start & (PAGE_CACHE_SIZE - 1);
215 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800216 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400217 page_cache_release(page);
218 }
219 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000220 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400221
Yan, Zhengc2167752009-11-12 09:34:21 +0000222 /*
223 * we're an inline extent, so nobody can
224 * extend the file past i_size without locking
225 * a page we already have locked.
226 *
227 * We must do any isize and inode updates
228 * before we unlock the pages. Otherwise we
229 * could end up racing with unlink.
230 */
Chris Masonc8b97812008-10-29 14:49:59 -0400231 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100232 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000233
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100234 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400235fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400236 return err;
237}
238
239
240/*
241 * conditionally insert an inline extent into the file. This
242 * does the checks required to make sure the data is small enough
243 * to fit as an inline extent.
244 */
Josef Bacik00361582013-08-14 14:02:47 -0400245static noinline int cow_file_range_inline(struct btrfs_root *root,
246 struct inode *inode, u64 start,
247 u64 end, size_t compressed_size,
248 int compress_type,
249 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400250{
Josef Bacik00361582013-08-14 14:02:47 -0400251 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400252 u64 isize = i_size_read(inode);
253 u64 actual_end = min(end + 1, isize);
254 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000255 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400256 u64 data_len = inline_len;
257 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000258 struct btrfs_path *path;
259 int extent_inserted = 0;
260 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400261
262 if (compressed_size)
263 data_len = compressed_size;
264
265 if (start > 0 ||
Wang Shilong354877b2014-07-17 11:44:11 +0800266 actual_end > PAGE_CACHE_SIZE ||
267 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400268 (!compressed_size &&
269 (actual_end & (root->sectorsize - 1)) == 0) ||
270 end + 1 < isize ||
271 data_len > root->fs_info->max_inline) {
272 return 1;
273 }
274
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000275 path = btrfs_alloc_path();
276 if (!path)
277 return -ENOMEM;
278
Josef Bacik00361582013-08-14 14:02:47 -0400279 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000280 if (IS_ERR(trans)) {
281 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400282 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000283 }
Josef Bacik00361582013-08-14 14:02:47 -0400284 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
285
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000286 if (compressed_size && compressed_pages)
287 extent_item_size = btrfs_file_extent_calc_inline_size(
288 compressed_size);
289 else
290 extent_item_size = btrfs_file_extent_calc_inline_size(
291 inline_len);
292
293 ret = __btrfs_drop_extents(trans, root, inode, path,
294 start, aligned_end, NULL,
295 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400296 if (ret) {
297 btrfs_abort_transaction(trans, root, ret);
298 goto out;
299 }
Chris Masonc8b97812008-10-29 14:49:59 -0400300
301 if (isize > actual_end)
302 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000303 ret = insert_inline_extent(trans, path, extent_inserted,
304 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400305 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000306 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400307 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100308 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400309 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400310 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400311 ret = 1;
312 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100313 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400314
Josef Bacikbdc20e62013-02-28 13:23:38 -0500315 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400316 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400317 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400318out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800319 /*
320 * Don't forget to free the reserved space, as for inlined extent
321 * it won't count as data extent, free them directly here.
322 * And at reserve time, it's always aligned to page size, so
323 * just free one page here.
324 */
325 btrfs_qgroup_free_data(inode, 0, PAGE_CACHE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000326 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400327 btrfs_end_transaction(trans, root);
328 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400329}
330
Chris Mason771ed682008-11-06 22:02:51 -0500331struct async_extent {
332 u64 start;
333 u64 ram_size;
334 u64 compressed_size;
335 struct page **pages;
336 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800337 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500338 struct list_head list;
339};
340
341struct async_cow {
342 struct inode *inode;
343 struct btrfs_root *root;
344 struct page *locked_page;
345 u64 start;
346 u64 end;
347 struct list_head extents;
348 struct btrfs_work work;
349};
350
351static noinline int add_async_extent(struct async_cow *cow,
352 u64 start, u64 ram_size,
353 u64 compressed_size,
354 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800355 unsigned long nr_pages,
356 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500357{
358 struct async_extent *async_extent;
359
360 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100361 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500362 async_extent->start = start;
363 async_extent->ram_size = ram_size;
364 async_extent->compressed_size = compressed_size;
365 async_extent->pages = pages;
366 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800367 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500368 list_add_tail(&async_extent->list, &cow->extents);
369 return 0;
370}
371
Wang Shilongf79707b2014-07-17 11:44:09 +0800372static inline int inode_need_compress(struct inode *inode)
373{
374 struct btrfs_root *root = BTRFS_I(inode)->root;
375
376 /* force compress */
377 if (btrfs_test_opt(root, FORCE_COMPRESS))
378 return 1;
379 /* bad compression ratios */
380 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
381 return 0;
382 if (btrfs_test_opt(root, COMPRESS) ||
383 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
384 BTRFS_I(inode)->force_compress)
385 return 1;
386 return 0;
387}
388
Chris Masonc8b97812008-10-29 14:49:59 -0400389/*
Chris Mason771ed682008-11-06 22:02:51 -0500390 * we create compressed extents in two phases. The first
391 * phase compresses a range of pages that have already been
392 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400393 *
Chris Mason771ed682008-11-06 22:02:51 -0500394 * This is done inside an ordered work queue, and the compression
395 * is spread across many cpus. The actual IO submission is step
396 * two, and the ordered work queue takes care of making sure that
397 * happens in the same order things were put onto the queue by
398 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400399 *
Chris Mason771ed682008-11-06 22:02:51 -0500400 * If this code finds it can't get good compression, it puts an
401 * entry onto the work queue to write the uncompressed bytes. This
402 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300403 * are written in the same order that the flusher thread sent them
404 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400405 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100406static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500407 struct page *locked_page,
408 u64 start, u64 end,
409 struct async_cow *async_cow,
410 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400411{
412 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400413 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400414 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400415 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500416 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400417 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400418 struct page **pages = NULL;
419 unsigned long nr_pages;
420 unsigned long nr_pages_ret = 0;
421 unsigned long total_compressed = 0;
422 unsigned long total_in = 0;
Byongho Leeee221842015-12-15 01:42:10 +0900423 unsigned long max_compressed = SZ_128K;
424 unsigned long max_uncompressed = SZ_128K;
Chris Masonc8b97812008-10-29 14:49:59 -0400425 int i;
426 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800427 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400428 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400429
Liu Bo4cb13e52012-03-29 09:57:45 -0400430 /* if this is a small write inside eof, kick off a defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900431 if ((end - start + 1) < SZ_16K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400432 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400433 btrfs_add_inode_defrag(NULL, inode);
434
Chris Mason42dc7ba2008-12-15 11:44:56 -0500435 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400436again:
437 will_compress = 0;
438 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
Byongho Leeee221842015-12-15 01:42:10 +0900439 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400440
Chris Masonf03d9302009-02-04 09:31:06 -0500441 /*
442 * we don't want to send crud past the end of i_size through
443 * compression, that's just a waste of CPU time. So, if the
444 * end of the file is before the start of our current
445 * requested range of bytes, we bail out to the uncompressed
446 * cleanup code that can deal with all of this.
447 *
448 * It isn't really the fastest way to fix things, but this is a
449 * very uncommon corner.
450 */
451 if (actual_end <= start)
452 goto cleanup_and_bail_uncompressed;
453
Chris Masonc8b97812008-10-29 14:49:59 -0400454 total_compressed = actual_end - start;
455
Shilong Wang4bcbb332014-10-07 18:44:35 -0400456 /*
457 * skip compression for a small file range(<=blocksize) that
458 * isn't an inline extent, since it dosen't save disk space at all.
459 */
460 if (total_compressed <= blocksize &&
461 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
462 goto cleanup_and_bail_uncompressed;
463
Chris Masonc8b97812008-10-29 14:49:59 -0400464 /* we want to make sure that amount of ram required to uncompress
465 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500466 * of a compressed extent to 128k. This is a crucial number
467 * because it also controls how easily we can spread reads across
468 * cpus for decompression.
469 *
470 * We also want to make sure the amount of IO required to do
471 * a random read is reasonably small, so we limit the size of
472 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400473 */
474 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000475 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500476 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400477 total_in = 0;
478 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400479
Chris Mason771ed682008-11-06 22:02:51 -0500480 /*
481 * we do compression for mount -o compress and when the
482 * inode has not been flagged as nocompress. This flag can
483 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400484 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800485 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400486 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100487 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800488 if (!pages) {
489 /* just bail out to the uncompressed code */
490 goto cont;
491 }
Chris Mason179e29e2007-11-01 11:28:41 -0400492
Li Zefan261507a02010-12-17 14:21:50 +0800493 if (BTRFS_I(inode)->force_compress)
494 compress_type = BTRFS_I(inode)->force_compress;
495
Chris Mason4adaa612013-03-26 13:07:00 -0400496 /*
497 * we need to call clear_page_dirty_for_io on each
498 * page in the range. Otherwise applications with the file
499 * mmap'd can wander in and change the page contents while
500 * we are compressing them.
501 *
502 * If the compression fails for any reason, we set the pages
503 * dirty again later on.
504 */
505 extent_range_clear_dirty_for_io(inode, start, end);
506 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800507 ret = btrfs_compress_pages(compress_type,
508 inode->i_mapping, start,
509 total_compressed, pages,
510 nr_pages, &nr_pages_ret,
511 &total_in,
512 &total_compressed,
513 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400514
515 if (!ret) {
516 unsigned long offset = total_compressed &
517 (PAGE_CACHE_SIZE - 1);
518 struct page *page = pages[nr_pages_ret - 1];
519 char *kaddr;
520
521 /* zero the tail end of the last page, we might be
522 * sending it down to disk
523 */
524 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800525 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400526 memset(kaddr + offset, 0,
527 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800528 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400529 }
530 will_compress = 1;
531 }
532 }
Li Zefan560f7d72011-09-08 10:22:01 +0800533cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400534 if (start == 0) {
535 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500536 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400537 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500538 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400539 */
Josef Bacik00361582013-08-14 14:02:47 -0400540 ret = cow_file_range_inline(root, inode, start, end,
541 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400542 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500543 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400544 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000545 total_compressed,
546 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400547 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100548 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400549 unsigned long clear_flags = EXTENT_DELALLOC |
550 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100551 unsigned long page_error_op;
552
Josef Bacik151a41b2013-07-29 13:22:24 -0400553 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100554 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400555
Chris Mason771ed682008-11-06 22:02:51 -0500556 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100557 * inline extent creation worked or returned error,
558 * we don't need to create any more async work items.
559 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500560 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400561 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400562 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400563 PAGE_CLEAR_DIRTY |
564 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100565 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400566 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400567 goto free_pages_out;
568 }
569 }
570
571 if (will_compress) {
572 /*
573 * we aren't doing an inline extent round the compressed size
574 * up to a block size boundary so the allocator does sane
575 * things
576 */
Qu Wenruofda28322013-02-26 08:10:22 +0000577 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400578
579 /*
580 * one last check to make sure the compression is really a
581 * win, compare the page count read with the blocks on disk
582 */
Qu Wenruofda28322013-02-26 08:10:22 +0000583 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400584 if (total_compressed >= total_in) {
585 will_compress = 0;
586 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400587 num_bytes = total_in;
588 }
589 }
590 if (!will_compress && pages) {
591 /*
592 * the compression code ran but failed to make things smaller,
593 * free any pages it allocated and our page pointer array
594 */
595 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400596 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400597 page_cache_release(pages[i]);
598 }
599 kfree(pages);
600 pages = NULL;
601 total_compressed = 0;
602 nr_pages_ret = 0;
603
604 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500605 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
606 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500607 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500608 }
Chris Masonc8b97812008-10-29 14:49:59 -0400609 }
Chris Mason771ed682008-11-06 22:02:51 -0500610 if (will_compress) {
611 *num_added += 1;
612
613 /* the async work queues will take care of doing actual
614 * allocation on disk for these compressed pages,
615 * and will submit them to the elevator.
616 */
617 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800618 total_compressed, pages, nr_pages_ret,
619 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500620
Yan, Zheng24ae6362010-12-06 07:02:36 +0000621 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500622 start += num_bytes;
623 pages = NULL;
624 cond_resched();
625 goto again;
626 }
627 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500628cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500629 /*
630 * No compression, but we still need to write the pages in
631 * the file we've been given so far. redirty the locked
632 * page if it corresponds to our extent and set things up
633 * for the async work queue to run cow_file_range to do
634 * the normal delalloc dance
635 */
636 if (page_offset(locked_page) >= start &&
637 page_offset(locked_page) <= end) {
638 __set_page_dirty_nobuffers(locked_page);
639 /* unlocked later on in the async handlers */
640 }
Chris Mason4adaa612013-03-26 13:07:00 -0400641 if (redirty)
642 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800643 add_async_extent(async_cow, start, end - start + 1,
644 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500645 *num_added += 1;
646 }
647
Filipe Mananac44f6492014-10-09 21:15:44 +0100648 return;
Chris Mason771ed682008-11-06 22:02:51 -0500649
650free_pages_out:
651 for (i = 0; i < nr_pages_ret; i++) {
652 WARN_ON(pages[i]->mapping);
653 page_cache_release(pages[i]);
654 }
Chris Masond3977122009-01-05 21:25:51 -0500655 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500656}
Chris Mason771ed682008-11-06 22:02:51 -0500657
Filipe Manana40ae8372014-10-06 22:14:24 +0100658static void free_async_extent_pages(struct async_extent *async_extent)
659{
660 int i;
661
662 if (!async_extent->pages)
663 return;
664
665 for (i = 0; i < async_extent->nr_pages; i++) {
666 WARN_ON(async_extent->pages[i]->mapping);
667 page_cache_release(async_extent->pages[i]);
668 }
669 kfree(async_extent->pages);
670 async_extent->nr_pages = 0;
671 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500672}
673
674/*
675 * phase two of compressed writeback. This is the ordered portion
676 * of the code, which only gets called in the order the work was
677 * queued. We walk all the async extents created by compress_file_range
678 * and send them down to the disk.
679 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100680static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500681 struct async_cow *async_cow)
682{
683 struct async_extent *async_extent;
684 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500685 struct btrfs_key ins;
686 struct extent_map *em;
687 struct btrfs_root *root = BTRFS_I(inode)->root;
688 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
689 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500690 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500691
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500692again:
Chris Masond3977122009-01-05 21:25:51 -0500693 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500694 async_extent = list_entry(async_cow->extents.next,
695 struct async_extent, list);
696 list_del(&async_extent->list);
697
698 io_tree = &BTRFS_I(inode)->io_tree;
699
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500700retry:
Chris Mason771ed682008-11-06 22:02:51 -0500701 /* did the compression code fall back to uncompressed IO? */
702 if (!async_extent->pages) {
703 int page_started = 0;
704 unsigned long nr_written = 0;
705
706 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000707 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100708 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500709
710 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500711 ret = cow_file_range(inode, async_cow->locked_page,
712 async_extent->start,
713 async_extent->start +
714 async_extent->ram_size - 1,
715 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500716
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100717 /* JDM XXX */
718
Chris Mason771ed682008-11-06 22:02:51 -0500719 /*
720 * if page_started, cow_file_range inserted an
721 * inline extent and took care of all the unlocking
722 * and IO for us. Otherwise, we need to submit
723 * all those pages down to the drive.
724 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500725 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500726 extent_write_locked_range(io_tree,
727 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500728 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500729 async_extent->ram_size - 1,
730 btrfs_get_extent,
731 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500732 else if (ret)
733 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500734 kfree(async_extent);
735 cond_resched();
736 continue;
737 }
738
739 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100740 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500741
Josef Bacik00361582013-08-14 14:02:47 -0400742 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500743 async_extent->compressed_size,
744 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800745 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500746 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100747 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500748
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400749 if (ret == -ENOSPC) {
750 unlock_extent(io_tree, async_extent->start,
751 async_extent->start +
752 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800753
754 /*
755 * we need to redirty the pages if we decide to
756 * fallback to uncompressed IO, otherwise we
757 * will not submit these pages down to lower
758 * layers.
759 */
760 extent_range_redirty_for_io(inode,
761 async_extent->start,
762 async_extent->start +
763 async_extent->ram_size - 1);
764
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100765 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400766 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500767 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500768 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000769 /*
770 * here we're doing allocation and writeback of the
771 * compressed pages
772 */
773 btrfs_drop_extent_cache(inode, async_extent->start,
774 async_extent->start +
775 async_extent->ram_size - 1, 0);
776
David Sterba172ddd62011-04-21 00:48:27 +0200777 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000778 if (!em) {
779 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500780 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000781 }
Chris Mason771ed682008-11-06 22:02:51 -0500782 em->start = async_extent->start;
783 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500784 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400785 em->mod_start = em->start;
786 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500787
788 em->block_start = ins.objectid;
789 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500790 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400791 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500792 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800793 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500794 set_bit(EXTENT_FLAG_PINNED, &em->flags);
795 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400796 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500797
Chris Masond3977122009-01-05 21:25:51 -0500798 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400799 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400800 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400801 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500802 if (ret != -EEXIST) {
803 free_extent_map(em);
804 break;
805 }
806 btrfs_drop_extent_cache(inode, async_extent->start,
807 async_extent->start +
808 async_extent->ram_size - 1, 0);
809 }
810
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500811 if (ret)
812 goto out_free_reserve;
813
Li Zefan261507a02010-12-17 14:21:50 +0800814 ret = btrfs_add_ordered_extent_compress(inode,
815 async_extent->start,
816 ins.objectid,
817 async_extent->ram_size,
818 ins.offset,
819 BTRFS_ORDERED_COMPRESSED,
820 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100821 if (ret) {
822 btrfs_drop_extent_cache(inode, async_extent->start,
823 async_extent->start +
824 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500825 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100826 }
Chris Mason771ed682008-11-06 22:02:51 -0500827
Chris Mason771ed682008-11-06 22:02:51 -0500828 /*
829 * clear dirty, set writeback and unlock the pages.
830 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400831 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400832 async_extent->start +
833 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400834 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
835 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400836 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500837 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500838 async_extent->start,
839 async_extent->ram_size,
840 ins.objectid,
841 ins.offset, async_extent->pages,
842 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100843 if (ret) {
844 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
845 struct page *p = async_extent->pages[0];
846 const u64 start = async_extent->start;
847 const u64 end = start + async_extent->ram_size - 1;
848
849 p->mapping = inode->i_mapping;
850 tree->ops->writepage_end_io_hook(p, start, end,
851 NULL, 0);
852 p->mapping = NULL;
853 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
854 PAGE_END_WRITEBACK |
855 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100856 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100857 }
Chris Mason771ed682008-11-06 22:02:51 -0500858 alloc_hint = ins.objectid + ins.offset;
859 kfree(async_extent);
860 cond_resched();
861 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100862 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500863out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800864 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100865out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400866 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500867 async_extent->start +
868 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400869 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400870 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
871 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100872 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
873 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100874 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100875 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500876 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500877}
878
Josef Bacik4b46fce2010-05-23 11:00:55 -0400879static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
880 u64 num_bytes)
881{
882 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
883 struct extent_map *em;
884 u64 alloc_hint = 0;
885
886 read_lock(&em_tree->lock);
887 em = search_extent_mapping(em_tree, start, num_bytes);
888 if (em) {
889 /*
890 * if block start isn't an actual block number then find the
891 * first block in this inode and use that as a hint. If that
892 * block is also bogus then just don't worry about it.
893 */
894 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
895 free_extent_map(em);
896 em = search_extent_mapping(em_tree, 0, 0);
897 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
898 alloc_hint = em->block_start;
899 if (em)
900 free_extent_map(em);
901 } else {
902 alloc_hint = em->block_start;
903 free_extent_map(em);
904 }
905 }
906 read_unlock(&em_tree->lock);
907
908 return alloc_hint;
909}
910
Chris Mason771ed682008-11-06 22:02:51 -0500911/*
912 * when extent_io.c finds a delayed allocation range in the file,
913 * the call backs end up in this code. The basic idea is to
914 * allocate extents on disk for the range, and create ordered data structs
915 * in ram to track those extents.
916 *
917 * locked_page is the page that writepage had locked already. We use
918 * it to make sure we don't do extra locks or unlocks.
919 *
920 * *page_started is set to one if we unlock locked_page and do everything
921 * required to start IO on it. It may be clean and already done with
922 * IO when we return.
923 */
Josef Bacik00361582013-08-14 14:02:47 -0400924static noinline int cow_file_range(struct inode *inode,
925 struct page *locked_page,
926 u64 start, u64 end, int *page_started,
927 unsigned long *nr_written,
928 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500929{
Josef Bacik00361582013-08-14 14:02:47 -0400930 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500931 u64 alloc_hint = 0;
932 u64 num_bytes;
933 unsigned long ram_size;
934 u64 disk_num_bytes;
935 u64 cur_alloc_size;
936 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500937 struct btrfs_key ins;
938 struct extent_map *em;
939 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
940 int ret = 0;
941
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400942 if (btrfs_is_free_space_inode(inode)) {
943 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500944 ret = -EINVAL;
945 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400946 }
Chris Mason771ed682008-11-06 22:02:51 -0500947
Qu Wenruofda28322013-02-26 08:10:22 +0000948 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500949 num_bytes = max(blocksize, num_bytes);
950 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500951
Chris Mason4cb53002011-05-24 15:35:30 -0400952 /* if this is a small write inside eof, kick off defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900953 if (num_bytes < SZ_64K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400954 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400955 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400956
Chris Mason771ed682008-11-06 22:02:51 -0500957 if (start == 0) {
958 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400959 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
960 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500961 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400962 extent_clear_unlock_delalloc(inode, start, end, NULL,
963 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400964 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400965 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
966 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000967
Chris Mason771ed682008-11-06 22:02:51 -0500968 *nr_written = *nr_written +
969 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
970 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500971 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100972 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100973 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500974 }
975 }
Chris Masonc8b97812008-10-29 14:49:59 -0400976
977 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200978 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400979
Josef Bacik4b46fce2010-05-23 11:00:55 -0400980 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400981 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400982
Chris Masond3977122009-01-05 21:25:51 -0500983 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400984 unsigned long op;
985
Josef Bacik287a0ab2010-03-19 18:07:23 +0000986 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400987 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500988 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800989 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400990 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100991 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500992
David Sterba172ddd62011-04-21 00:48:27 +0200993 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000994 if (!em) {
995 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000996 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000997 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400998 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500999 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -05001000 ram_size = ins.offset;
1001 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001002 em->mod_start = em->start;
1003 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -04001004
Chris Masone6dcd2d2008-07-17 12:53:50 -04001005 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001006 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001007 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001008 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001009 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001010 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001011 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001012
Chris Masond3977122009-01-05 21:25:51 -05001013 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001014 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001015 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001016 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001017 if (ret != -EEXIST) {
1018 free_extent_map(em);
1019 break;
1020 }
1021 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001022 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001023 }
Liu Boace68ba2013-04-22 10:53:47 +00001024 if (ret)
1025 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001026
Chris Mason98d20f62008-04-14 09:46:10 -04001027 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001028 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001029 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001030 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001031 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001032
Yan Zheng17d217f2008-12-12 10:03:38 -05001033 if (root->root_key.objectid ==
1034 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1035 ret = btrfs_reloc_clone_csums(inode, start,
1036 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001037 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001038 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001039 }
1040
Chris Masond3977122009-01-05 21:25:51 -05001041 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001042 break;
Chris Masond3977122009-01-05 21:25:51 -05001043
Chris Masonc8b97812008-10-29 14:49:59 -04001044 /* we're not doing compressed IO, don't unlock the first
1045 * page (which the caller expects to stay locked), don't
1046 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001047 *
1048 * Do set the Private2 bit so we know this page was properly
1049 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001050 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001051 op = unlock ? PAGE_UNLOCK : 0;
1052 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001053
Josef Bacikc2790a22013-07-29 11:20:47 -04001054 extent_clear_unlock_delalloc(inode, start,
1055 start + ram_size - 1, locked_page,
1056 EXTENT_LOCKED | EXTENT_DELALLOC,
1057 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001058 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001059 num_bytes -= cur_alloc_size;
1060 alloc_hint = ins.objectid + ins.offset;
1061 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001062 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001063out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001064 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001065
Filipe Mananad9f85962014-08-25 10:43:00 +01001066out_drop_extent_cache:
1067 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001068out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001069 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001070out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001071 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001072 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1073 EXTENT_DELALLOC | EXTENT_DEFRAG,
1074 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1075 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001076 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001077}
Chris Masonc8b97812008-10-29 14:49:59 -04001078
Chris Mason771ed682008-11-06 22:02:51 -05001079/*
1080 * work queue call back to started compression on a file and pages
1081 */
1082static noinline void async_cow_start(struct btrfs_work *work)
1083{
1084 struct async_cow *async_cow;
1085 int num_added = 0;
1086 async_cow = container_of(work, struct async_cow, work);
1087
1088 compress_file_range(async_cow->inode, async_cow->locked_page,
1089 async_cow->start, async_cow->end, async_cow,
1090 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001091 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001092 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001093 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001094 }
Chris Mason771ed682008-11-06 22:02:51 -05001095}
1096
1097/*
1098 * work queue call back to submit previously compressed pages
1099 */
1100static noinline void async_cow_submit(struct btrfs_work *work)
1101{
1102 struct async_cow *async_cow;
1103 struct btrfs_root *root;
1104 unsigned long nr_pages;
1105
1106 async_cow = container_of(work, struct async_cow, work);
1107
1108 root = async_cow->root;
1109 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1110 PAGE_CACHE_SHIFT;
1111
David Sterbaee863952015-02-16 19:41:40 +01001112 /*
1113 * atomic_sub_return implies a barrier for waitqueue_active
1114 */
Josef Bacik66657b32012-08-01 15:36:24 -04001115 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001116 5 * SZ_1M &&
Chris Mason771ed682008-11-06 22:02:51 -05001117 waitqueue_active(&root->fs_info->async_submit_wait))
1118 wake_up(&root->fs_info->async_submit_wait);
1119
Chris Masond3977122009-01-05 21:25:51 -05001120 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001121 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001122}
Chris Masonc8b97812008-10-29 14:49:59 -04001123
Chris Mason771ed682008-11-06 22:02:51 -05001124static noinline void async_cow_free(struct btrfs_work *work)
1125{
1126 struct async_cow *async_cow;
1127 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001128 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001129 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001130 kfree(async_cow);
1131}
1132
1133static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1134 u64 start, u64 end, int *page_started,
1135 unsigned long *nr_written)
1136{
1137 struct async_cow *async_cow;
1138 struct btrfs_root *root = BTRFS_I(inode)->root;
1139 unsigned long nr_pages;
1140 u64 cur_end;
Byongho Leeee221842015-12-15 01:42:10 +09001141 int limit = 10 * SZ_1M;
Chris Mason771ed682008-11-06 22:02:51 -05001142
Chris Masona3429ab2009-10-08 12:30:20 -04001143 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1144 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001145 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001146 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001147 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001148 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001149 async_cow->root = root;
1150 async_cow->locked_page = locked_page;
1151 async_cow->start = start;
1152
Wang Shilongf79707b2014-07-17 11:44:09 +08001153 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1154 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001155 cur_end = end;
1156 else
Byongho Leeee221842015-12-15 01:42:10 +09001157 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001158
1159 async_cow->end = cur_end;
1160 INIT_LIST_HEAD(&async_cow->extents);
1161
Liu Bo9e0af232014-08-15 23:36:53 +08001162 btrfs_init_work(&async_cow->work,
1163 btrfs_delalloc_helper,
1164 async_cow_start, async_cow_submit,
1165 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001166
Chris Mason771ed682008-11-06 22:02:51 -05001167 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1168 PAGE_CACHE_SHIFT;
1169 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1170
Qu Wenruoafe3d242014-02-28 10:46:07 +08001171 btrfs_queue_work(root->fs_info->delalloc_workers,
1172 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001173
1174 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1175 wait_event(root->fs_info->async_submit_wait,
1176 (atomic_read(&root->fs_info->async_delalloc_pages) <
1177 limit));
1178 }
1179
Chris Masond3977122009-01-05 21:25:51 -05001180 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001181 atomic_read(&root->fs_info->async_delalloc_pages)) {
1182 wait_event(root->fs_info->async_submit_wait,
1183 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1184 0));
1185 }
1186
1187 *nr_written += nr_pages;
1188 start = cur_end + 1;
1189 }
1190 *page_started = 1;
1191 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001192}
1193
Chris Masond3977122009-01-05 21:25:51 -05001194static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001195 u64 bytenr, u64 num_bytes)
1196{
1197 int ret;
1198 struct btrfs_ordered_sum *sums;
1199 LIST_HEAD(list);
1200
Yan Zheng07d400a2009-01-06 11:42:00 -05001201 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001202 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001203 if (ret == 0 && list_empty(&list))
1204 return 0;
1205
1206 while (!list_empty(&list)) {
1207 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1208 list_del(&sums->list);
1209 kfree(sums);
1210 }
1211 return 1;
1212}
1213
Chris Masond352ac62008-09-29 15:18:18 -04001214/*
1215 * when nowcow writeback call back. This checks for snapshots or COW copies
1216 * of the extents that exist in the file, and COWs the file as required.
1217 *
1218 * If no cow copies or snapshots exist, we write directly to the existing
1219 * blocks on disk
1220 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001221static noinline int run_delalloc_nocow(struct inode *inode,
1222 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001223 u64 start, u64 end, int *page_started, int force,
1224 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001225{
Chris Masonbe20aa92007-12-17 20:14:01 -05001226 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001227 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001228 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001229 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001230 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001231 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001232 u64 cow_start;
1233 u64 cur_offset;
1234 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001235 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001236 u64 disk_bytenr;
1237 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001238 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001239 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001240 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001241 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001242 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001243 int nocow;
1244 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001245 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001246 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001247
1248 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001249 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001250 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1251 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001252 EXTENT_DO_ACCOUNTING |
1253 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001254 PAGE_CLEAR_DIRTY |
1255 PAGE_SET_WRITEBACK |
1256 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001257 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001258 }
Li Zefan82d59022011-04-20 10:33:24 +08001259
Liu Bo83eea1f2012-07-10 05:28:39 -06001260 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001261
1262 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001263 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001264 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001265 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001266
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001267 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001268 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1269 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001270 EXTENT_DO_ACCOUNTING |
1271 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001272 PAGE_CLEAR_DIRTY |
1273 PAGE_SET_WRITEBACK |
1274 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001275 btrfs_free_path(path);
1276 return PTR_ERR(trans);
1277 }
1278
Josef Bacik74b21072011-04-13 12:02:53 -04001279 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001280
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 cow_start = (u64)-1;
1282 cur_offset = start;
1283 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001284 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001286 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001287 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001288 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1289 leaf = path->nodes[0];
1290 btrfs_item_key_to_cpu(leaf, &found_key,
1291 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001292 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 found_key.type == BTRFS_EXTENT_DATA_KEY)
1294 path->slots[0]--;
1295 }
1296 check_prev = 0;
1297next_slot:
1298 leaf = path->nodes[0];
1299 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1300 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001301 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001302 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 if (ret > 0)
1304 break;
1305 leaf = path->nodes[0];
1306 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001307
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 nocow = 0;
1309 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001310 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001311 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001312
Filipe Manana1d512cb2015-11-09 00:33:58 +00001313 if (found_key.objectid > ino)
1314 break;
1315 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1316 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1317 path->slots[0]++;
1318 goto next_slot;
1319 }
1320 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 found_key.offset > end)
1322 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001323
Yan Zheng80ff3852008-10-30 14:20:02 -04001324 if (found_key.offset > cur_offset) {
1325 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001326 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001327 goto out_check;
1328 }
Chris Masonc31f8832008-01-08 15:46:31 -05001329
Yan Zheng80ff3852008-10-30 14:20:02 -04001330 fi = btrfs_item_ptr(leaf, path->slots[0],
1331 struct btrfs_file_extent_item);
1332 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001333
Josef Bacikcc95bef2013-04-04 14:31:27 -04001334 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001335 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1336 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001337 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001338 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 extent_end = found_key.offset +
1340 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001341 disk_num_bytes =
1342 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 if (extent_end <= start) {
1344 path->slots[0]++;
1345 goto next_slot;
1346 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001347 if (disk_bytenr == 0)
1348 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001349 if (btrfs_file_extent_compression(leaf, fi) ||
1350 btrfs_file_extent_encryption(leaf, fi) ||
1351 btrfs_file_extent_other_encoding(leaf, fi))
1352 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001353 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1354 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001355 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001356 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001357 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001358 found_key.offset -
1359 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001360 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001361 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001362 disk_bytenr += cur_offset - found_key.offset;
1363 num_bytes = min(end + 1, extent_end) - cur_offset;
1364 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001365 * if there are pending snapshots for this root,
1366 * we fall into common COW way.
1367 */
1368 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001369 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001370 if (!err)
1371 goto out_check;
1372 }
1373 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001374 * force cow if csum exists in the range.
1375 * this ensure that csum for a given extent are
1376 * either valid or do not exist.
1377 */
1378 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1379 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001380 nocow = 1;
1381 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1382 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001383 btrfs_file_extent_inline_len(leaf,
1384 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001385 extent_end = ALIGN(extent_end, root->sectorsize);
1386 } else {
1387 BUG_ON(1);
1388 }
1389out_check:
1390 if (extent_end <= start) {
1391 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001392 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001393 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001394 goto next_slot;
1395 }
1396 if (!nocow) {
1397 if (cow_start == (u64)-1)
1398 cow_start = cur_offset;
1399 cur_offset = extent_end;
1400 if (cur_offset > end)
1401 break;
1402 path->slots[0]++;
1403 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001404 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001405
David Sterbab3b4aa72011-04-21 01:20:15 +02001406 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001407 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001408 ret = cow_file_range(inode, locked_page,
1409 cow_start, found_key.offset - 1,
1410 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001411 if (ret) {
1412 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001413 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001414 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001415 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001416 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001417 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001418
Yan Zhengd899e052008-10-30 14:25:28 -04001419 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1420 struct extent_map *em;
1421 struct extent_map_tree *em_tree;
1422 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001423 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001424 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001425 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001426 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001427 em->len = num_bytes;
1428 em->block_len = num_bytes;
1429 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001430 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001431 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001432 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001433 em->mod_start = em->start;
1434 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001435 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001436 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001437 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001438 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001439 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001440 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001441 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001442 if (ret != -EEXIST) {
1443 free_extent_map(em);
1444 break;
1445 }
1446 btrfs_drop_extent_cache(inode, em->start,
1447 em->start + em->len - 1, 0);
1448 }
1449 type = BTRFS_ORDERED_PREALLOC;
1450 } else {
1451 type = BTRFS_ORDERED_NOCOW;
1452 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001453
1454 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001455 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001456 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001457
Yan, Zhengefa56462010-05-16 10:49:59 -04001458 if (root->root_key.objectid ==
1459 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1460 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1461 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001462 if (ret) {
1463 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001464 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001465 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001466 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001467 }
1468
Josef Bacikc2790a22013-07-29 11:20:47 -04001469 extent_clear_unlock_delalloc(inode, cur_offset,
1470 cur_offset + num_bytes - 1,
1471 locked_page, EXTENT_LOCKED |
1472 EXTENT_DELALLOC, PAGE_UNLOCK |
1473 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001474 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001475 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001476 cur_offset = extent_end;
1477 if (cur_offset > end)
1478 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001479 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001480 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001481
Josef Bacik17ca04a2012-05-31 15:58:55 -04001482 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001483 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001484 cur_offset = end;
1485 }
1486
Yan Zheng80ff3852008-10-30 14:20:02 -04001487 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001488 ret = cow_file_range(inode, locked_page, cow_start, end,
1489 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001490 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001491 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001492 }
1493
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001494error:
Miao Xiea698d0752012-09-20 01:51:59 -06001495 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001496 if (!ret)
1497 ret = err;
1498
Josef Bacik17ca04a2012-05-31 15:58:55 -04001499 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001500 extent_clear_unlock_delalloc(inode, cur_offset, end,
1501 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001502 EXTENT_DELALLOC | EXTENT_DEFRAG |
1503 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1504 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001505 PAGE_SET_WRITEBACK |
1506 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001507 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001508 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001509}
1510
Wang Shilong47059d92014-07-03 18:22:07 +08001511static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1512{
1513
1514 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1515 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1516 return 0;
1517
1518 /*
1519 * @defrag_bytes is a hint value, no spinlock held here,
1520 * if is not zero, it means the file is defragging.
1521 * Force cow if given extent needs to be defragged.
1522 */
1523 if (BTRFS_I(inode)->defrag_bytes &&
1524 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1525 EXTENT_DEFRAG, 0, NULL))
1526 return 1;
1527
1528 return 0;
1529}
1530
Chris Masond352ac62008-09-29 15:18:18 -04001531/*
1532 * extent_io.c call back to do delayed allocation processing
1533 */
Chris Masonc8b97812008-10-29 14:49:59 -04001534static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001535 u64 start, u64 end, int *page_started,
1536 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001537{
Chris Masonbe20aa92007-12-17 20:14:01 -05001538 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001539 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001540
Wang Shilong47059d92014-07-03 18:22:07 +08001541 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001542 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001543 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001544 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001545 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001546 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001547 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001548 ret = cow_file_range(inode, locked_page, start, end,
1549 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001550 } else {
1551 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1552 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001553 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001554 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001555 }
Chris Masonb888db22007-08-27 16:49:44 -04001556 return ret;
1557}
1558
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001559static void btrfs_split_extent_hook(struct inode *inode,
1560 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001561{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001562 u64 size;
1563
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001564 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001565 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001566 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001567
Josef Bacikdcab6a32015-02-11 15:08:59 -05001568 size = orig->end - orig->start + 1;
1569 if (size > BTRFS_MAX_EXTENT_SIZE) {
1570 u64 num_extents;
1571 u64 new_size;
1572
1573 /*
Josef Bacikba117212015-03-13 15:01:24 -04001574 * See the explanation in btrfs_merge_extent_hook, the same
1575 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001576 */
1577 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001578 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001579 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001580 new_size = split - orig->start;
1581 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1582 BTRFS_MAX_EXTENT_SIZE);
1583 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1584 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001585 return;
1586 }
1587
Josef Bacik9e0baf62011-07-15 15:16:44 +00001588 spin_lock(&BTRFS_I(inode)->lock);
1589 BTRFS_I(inode)->outstanding_extents++;
1590 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001591}
1592
1593/*
1594 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1595 * extents so we can keep track of new extents that are just merged onto old
1596 * extents, such as when we are doing sequential writes, so we can properly
1597 * account for the metadata space we'll need.
1598 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001599static void btrfs_merge_extent_hook(struct inode *inode,
1600 struct extent_state *new,
1601 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001602{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001603 u64 new_size, old_size;
1604 u64 num_extents;
1605
Josef Bacik9ed74f22009-09-11 16:12:44 -04001606 /* not delalloc, ignore it */
1607 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001608 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001609
Josef Bacik8461a3d2015-03-13 15:12:08 -04001610 if (new->start > other->start)
1611 new_size = new->end - other->start + 1;
1612 else
1613 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001614
1615 /* we're not bigger than the max, unreserve the space and go */
1616 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1617 spin_lock(&BTRFS_I(inode)->lock);
1618 BTRFS_I(inode)->outstanding_extents--;
1619 spin_unlock(&BTRFS_I(inode)->lock);
1620 return;
1621 }
1622
1623 /*
Josef Bacikba117212015-03-13 15:01:24 -04001624 * We have to add up either side to figure out how many extents were
1625 * accounted for before we merged into one big extent. If the number of
1626 * extents we accounted for is <= the amount we need for the new range
1627 * then we can return, otherwise drop. Think of it like this
1628 *
1629 * [ 4k][MAX_SIZE]
1630 *
1631 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1632 * need 2 outstanding extents, on one side we have 1 and the other side
1633 * we have 1 so they are == and we can return. But in this case
1634 *
1635 * [MAX_SIZE+4k][MAX_SIZE+4k]
1636 *
1637 * Each range on their own accounts for 2 extents, but merged together
1638 * they are only 3 extents worth of accounting, so we need to drop in
1639 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001640 */
Josef Bacikba117212015-03-13 15:01:24 -04001641 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001642 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1643 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001644 old_size = new->end - new->start + 1;
1645 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1646 BTRFS_MAX_EXTENT_SIZE);
1647
Josef Bacikdcab6a32015-02-11 15:08:59 -05001648 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001649 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001650 return;
1651
Josef Bacik9e0baf62011-07-15 15:16:44 +00001652 spin_lock(&BTRFS_I(inode)->lock);
1653 BTRFS_I(inode)->outstanding_extents--;
1654 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001655}
1656
Miao Xieeb73c1b2013-05-15 07:48:22 +00001657static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1658 struct inode *inode)
1659{
1660 spin_lock(&root->delalloc_lock);
1661 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1662 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1663 &root->delalloc_inodes);
1664 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1665 &BTRFS_I(inode)->runtime_flags);
1666 root->nr_delalloc_inodes++;
1667 if (root->nr_delalloc_inodes == 1) {
1668 spin_lock(&root->fs_info->delalloc_root_lock);
1669 BUG_ON(!list_empty(&root->delalloc_root));
1670 list_add_tail(&root->delalloc_root,
1671 &root->fs_info->delalloc_roots);
1672 spin_unlock(&root->fs_info->delalloc_root_lock);
1673 }
1674 }
1675 spin_unlock(&root->delalloc_lock);
1676}
1677
1678static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1679 struct inode *inode)
1680{
1681 spin_lock(&root->delalloc_lock);
1682 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1683 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1684 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1685 &BTRFS_I(inode)->runtime_flags);
1686 root->nr_delalloc_inodes--;
1687 if (!root->nr_delalloc_inodes) {
1688 spin_lock(&root->fs_info->delalloc_root_lock);
1689 BUG_ON(list_empty(&root->delalloc_root));
1690 list_del_init(&root->delalloc_root);
1691 spin_unlock(&root->fs_info->delalloc_root_lock);
1692 }
1693 }
1694 spin_unlock(&root->delalloc_lock);
1695}
1696
Chris Masond352ac62008-09-29 15:18:18 -04001697/*
1698 * extent_io.c set_bit_hook, used to track delayed allocation
1699 * bytes in this file, and to maintain the list of inodes that
1700 * have pending delalloc work to be done.
1701 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001702static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001703 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001704{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001705
Wang Shilong47059d92014-07-03 18:22:07 +08001706 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1707 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001708 /*
1709 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001710 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001711 * bit, which is only set or cleared with irqs on
1712 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001713 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001714 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001715 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001716 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001717
Josef Bacik9e0baf62011-07-15 15:16:44 +00001718 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001719 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001720 } else {
1721 spin_lock(&BTRFS_I(inode)->lock);
1722 BTRFS_I(inode)->outstanding_extents++;
1723 spin_unlock(&BTRFS_I(inode)->lock);
1724 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001725
Josef Bacik6a3891c2015-03-16 17:38:52 -04001726 /* For sanity tests */
1727 if (btrfs_test_is_dummy_root(root))
1728 return;
1729
Miao Xie963d6782013-01-29 10:10:51 +00001730 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1731 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001732 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001733 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001734 if (*bits & EXTENT_DEFRAG)
1735 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001736 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001737 &BTRFS_I(inode)->runtime_flags))
1738 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001739 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001740 }
Chris Mason291d6732008-01-29 15:55:23 -05001741}
1742
Chris Masond352ac62008-09-29 15:18:18 -04001743/*
1744 * extent_io.c clear_bit_hook, see set_bit_hook for why
1745 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001746static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001747 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001748 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001749{
Wang Shilong47059d92014-07-03 18:22:07 +08001750 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001751 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1752 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001753
1754 spin_lock(&BTRFS_I(inode)->lock);
1755 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1756 BTRFS_I(inode)->defrag_bytes -= len;
1757 spin_unlock(&BTRFS_I(inode)->lock);
1758
Chris Mason75eff682008-12-15 15:54:40 -05001759 /*
1760 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001761 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001762 * bit, which is only set or cleared with irqs on
1763 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001764 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001765 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001766 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001767
Josef Bacik9e0baf62011-07-15 15:16:44 +00001768 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001769 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001770 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1771 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001772 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001773 spin_unlock(&BTRFS_I(inode)->lock);
1774 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001775
Josef Bacikb6d08f02013-09-27 14:57:43 -04001776 /*
1777 * We don't reserve metadata space for space cache inodes so we
1778 * don't need to call dellalloc_release_metadata if there is an
1779 * error.
1780 */
1781 if (*bits & EXTENT_DO_ACCOUNTING &&
1782 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001783 btrfs_delalloc_release_metadata(inode, len);
1784
Josef Bacik6a3891c2015-03-16 17:38:52 -04001785 /* For sanity tests. */
1786 if (btrfs_test_is_dummy_root(root))
1787 return;
1788
Josef Bacik0cb59c92010-07-02 12:14:14 -04001789 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001790 && do_list && !(state->state & EXTENT_NORESERVE))
Qu Wenruo51773be2015-10-08 18:19:37 +08001791 btrfs_free_reserved_data_space_noquota(inode,
1792 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001793
Miao Xie963d6782013-01-29 10:10:51 +00001794 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1795 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001796 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001797 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001798 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001799 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001800 &BTRFS_I(inode)->runtime_flags))
1801 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001802 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001803 }
Chris Mason291d6732008-01-29 15:55:23 -05001804}
1805
Chris Masond352ac62008-09-29 15:18:18 -04001806/*
1807 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1808 * we don't create bios that span stripes or chunks
1809 */
David Woodhouse64a16702009-07-15 23:29:37 +01001810int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001811 size_t size, struct bio *bio,
1812 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001813{
1814 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001815 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001816 u64 length = 0;
1817 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001818 int ret;
1819
Chris Mason771ed682008-11-06 22:02:51 -05001820 if (bio_flags & EXTENT_BIO_COMPRESSED)
1821 return 0;
1822
Kent Overstreet4f024f32013-10-11 15:44:27 -07001823 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001824 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001825 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001826 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001827 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001828 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001829 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001830 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001831 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001832}
1833
Chris Masond352ac62008-09-29 15:18:18 -04001834/*
1835 * in order to insert checksums into the metadata in large chunks,
1836 * we wait until bio submission time. All the pages in the bio are
1837 * checksummed and sums are attached onto the ordered extent record.
1838 *
1839 * At IO completion time the cums attached on the ordered extent record
1840 * are inserted into the btree
1841 */
Chris Masond3977122009-01-05 21:25:51 -05001842static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1843 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001844 unsigned long bio_flags,
1845 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001846{
Chris Mason065631f2008-02-20 12:07:25 -05001847 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001848 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001849
Chris Masond20f7042008-12-08 16:58:54 -05001850 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001851 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001852 return 0;
1853}
Chris Masone0156402008-04-16 11:15:20 -04001854
Chris Mason4a69a412008-11-06 22:03:00 -05001855/*
1856 * in order to insert checksums into the metadata in large chunks,
1857 * we wait until bio submission time. All the pages in the bio are
1858 * checksummed and sums are attached onto the ordered extent record.
1859 *
1860 * At IO completion time the cums attached on the ordered extent record
1861 * are inserted into the btree
1862 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001863static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001864 int mirror_num, unsigned long bio_flags,
1865 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001866{
1867 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001868 int ret;
1869
1870 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001871 if (ret) {
1872 bio->bi_error = ret;
1873 bio_endio(bio);
1874 }
Stefan Behrens61891922012-11-05 18:51:52 +01001875 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001876}
1877
Chris Masond352ac62008-09-29 15:18:18 -04001878/*
Chris Masoncad321a2008-12-17 14:51:42 -05001879 * extent_io.c submission hook. This does the right thing for csum calculation
1880 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001881 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001882static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001883 int mirror_num, unsigned long bio_flags,
1884 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001885{
1886 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301887 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001888 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001889 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001890 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001891
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001892 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001893
Liu Bo83eea1f2012-07-10 05:28:39 -06001894 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301895 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001896
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001897 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001898 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1899 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001900 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001901
Chris Masond20f7042008-12-08 16:58:54 -05001902 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001903 ret = btrfs_submit_compressed_read(inode, bio,
1904 mirror_num,
1905 bio_flags);
1906 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001907 } else if (!skip_sum) {
1908 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1909 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001910 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001911 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001912 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001913 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001914 /* csum items have already been cloned */
1915 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1916 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001917 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001918 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001919 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001920 bio_flags, bio_offset,
1921 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001922 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001923 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001924 } else if (!skip_sum) {
1925 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1926 if (ret)
1927 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001928 }
1929
Chris Mason0b86a832008-03-24 15:01:56 -04001930mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001931 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1932
1933out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001934 if (ret < 0) {
1935 bio->bi_error = ret;
1936 bio_endio(bio);
1937 }
Stefan Behrens61891922012-11-05 18:51:52 +01001938 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001939}
Chris Mason6885f302008-02-20 16:11:05 -05001940
Chris Masond352ac62008-09-29 15:18:18 -04001941/*
1942 * given a list of ordered sums record them in the inode. This happens
1943 * at IO completion time based on sums calculated at bio submission time.
1944 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001945static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001946 struct inode *inode, u64 file_offset,
1947 struct list_head *list)
1948{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001949 struct btrfs_ordered_sum *sum;
1950
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001951 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001952 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001953 btrfs_csum_file_blocks(trans,
1954 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001955 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001956 }
1957 return 0;
1958}
1959
Josef Bacik2ac55d42010-02-03 19:33:23 +00001960int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1961 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001962{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001963 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001964 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001965 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001966}
1967
Chris Masond352ac62008-09-29 15:18:18 -04001968/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001969struct btrfs_writepage_fixup {
1970 struct page *page;
1971 struct btrfs_work work;
1972};
1973
Christoph Hellwigb2950862008-12-02 09:54:17 -05001974static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001975{
1976 struct btrfs_writepage_fixup *fixup;
1977 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001978 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001979 struct page *page;
1980 struct inode *inode;
1981 u64 page_start;
1982 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001983 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001984
1985 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1986 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001987again:
Chris Mason247e7432008-07-17 12:53:51 -04001988 lock_page(page);
1989 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1990 ClearPageChecked(page);
1991 goto out_page;
1992 }
1993
1994 inode = page->mapping->host;
1995 page_start = page_offset(page);
1996 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1997
David Sterbaff13db42015-12-03 14:30:40 +01001998 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001999 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002000
2001 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002002 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002003 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002004
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05302005 ordered = btrfs_lookup_ordered_range(inode, page_start,
2006 PAGE_CACHE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002007 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 +00003116void btrfs_add_delayed_iput(struct inode *inode)
3117{
3118 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
David Sterba8089fe62015-11-19 14:15:51 +01003119 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003120
3121 if (atomic_add_unless(&inode->i_count, -1, 1))
3122 return;
3123
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003124 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003125 if (binode->delayed_iput_count == 0) {
3126 ASSERT(list_empty(&binode->delayed_iput));
3127 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3128 } else {
3129 binode->delayed_iput_count++;
3130 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003131 spin_unlock(&fs_info->delayed_iput_lock);
3132}
3133
3134void btrfs_run_delayed_iputs(struct btrfs_root *root)
3135{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003136 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003137
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003138 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003139 while (!list_empty(&fs_info->delayed_iputs)) {
3140 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003141
David Sterba8089fe62015-11-19 14:15:51 +01003142 inode = list_first_entry(&fs_info->delayed_iputs,
3143 struct btrfs_inode, delayed_iput);
3144 if (inode->delayed_iput_count) {
3145 inode->delayed_iput_count--;
3146 list_move_tail(&inode->delayed_iput,
3147 &fs_info->delayed_iputs);
3148 } else {
3149 list_del_init(&inode->delayed_iput);
3150 }
3151 spin_unlock(&fs_info->delayed_iput_lock);
3152 iput(&inode->vfs_inode);
3153 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003154 }
David Sterba8089fe62015-11-19 14:15:51 +01003155 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003156}
3157
Yan, Zhengd68fc572010-05-16 10:49:58 -04003158/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003159 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003160 * files in the subvolume, it removes orphan item and frees block_rsv
3161 * structure.
3162 */
3163void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3164 struct btrfs_root *root)
3165{
Josef Bacik90290e12011-12-02 15:44:12 -05003166 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003167 int ret;
3168
Josef Bacik8a35d952012-05-23 14:26:42 -04003169 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003170 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3171 return;
3172
Josef Bacik90290e12011-12-02 15:44:12 -05003173 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003174 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003175 spin_unlock(&root->orphan_lock);
3176 return;
3177 }
3178
3179 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3180 spin_unlock(&root->orphan_lock);
3181 return;
3182 }
3183
3184 block_rsv = root->orphan_block_rsv;
3185 root->orphan_block_rsv = NULL;
3186 spin_unlock(&root->orphan_lock);
3187
Miao Xie27cdeb72014-04-02 19:51:05 +08003188 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003189 btrfs_root_refs(&root->root_item) > 0) {
3190 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3191 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003192 if (ret)
3193 btrfs_abort_transaction(trans, root, ret);
3194 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003195 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3196 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003197 }
3198
Josef Bacik90290e12011-12-02 15:44:12 -05003199 if (block_rsv) {
3200 WARN_ON(block_rsv->size > 0);
3201 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003202 }
3203}
3204
3205/*
Josef Bacik7b128762008-07-24 12:17:14 -04003206 * This creates an orphan entry for the given inode in case something goes
3207 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003208 *
3209 * NOTE: caller of this function should reserve 5 units of metadata for
3210 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003211 */
3212int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3213{
3214 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003215 struct btrfs_block_rsv *block_rsv = NULL;
3216 int reserve = 0;
3217 int insert = 0;
3218 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003219
Yan, Zhengd68fc572010-05-16 10:49:58 -04003220 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003221 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003222 if (!block_rsv)
3223 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003224 }
3225
Yan, Zhengd68fc572010-05-16 10:49:58 -04003226 spin_lock(&root->orphan_lock);
3227 if (!root->orphan_block_rsv) {
3228 root->orphan_block_rsv = block_rsv;
3229 } else if (block_rsv) {
3230 btrfs_free_block_rsv(root, block_rsv);
3231 block_rsv = NULL;
3232 }
Josef Bacik7b128762008-07-24 12:17:14 -04003233
Josef Bacik8a35d952012-05-23 14:26:42 -04003234 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3235 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003236#if 0
3237 /*
3238 * For proper ENOSPC handling, we should do orphan
3239 * cleanup when mounting. But this introduces backward
3240 * compatibility issue.
3241 */
3242 if (!xchg(&root->orphan_item_inserted, 1))
3243 insert = 2;
3244 else
3245 insert = 1;
3246#endif
3247 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003248 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003249 }
Josef Bacik7b128762008-07-24 12:17:14 -04003250
Josef Bacik72ac3c02012-05-23 14:13:11 -04003251 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3252 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003253 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003254 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003255
Yan, Zhengd68fc572010-05-16 10:49:58 -04003256 /* grab metadata reservation from transaction handle */
3257 if (reserve) {
3258 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003259 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003260 }
3261
3262 /* insert an orphan item to track this unlinked/truncated file */
3263 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003264 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003265 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003266 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003267 if (reserve) {
3268 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3269 &BTRFS_I(inode)->runtime_flags);
3270 btrfs_orphan_release_metadata(inode);
3271 }
3272 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003273 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3274 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003275 btrfs_abort_transaction(trans, root, ret);
3276 return ret;
3277 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003278 }
3279 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003280 }
3281
3282 /* insert an orphan item to track subvolume contains orphan files */
3283 if (insert >= 2) {
3284 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3285 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003286 if (ret && ret != -EEXIST) {
3287 btrfs_abort_transaction(trans, root, ret);
3288 return ret;
3289 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003290 }
3291 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003292}
3293
3294/*
3295 * We have done the truncate/delete so we can go ahead and remove the orphan
3296 * item for this particular inode.
3297 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003298static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3299 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003300{
3301 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003302 int delete_item = 0;
3303 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003304 int ret = 0;
3305
Yan, Zhengd68fc572010-05-16 10:49:58 -04003306 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003307 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3308 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003309 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003310
Josef Bacik72ac3c02012-05-23 14:13:11 -04003311 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3312 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003313 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003314 spin_unlock(&root->orphan_lock);
3315
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003316 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003317 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003318 if (trans)
3319 ret = btrfs_del_orphan_item(trans, root,
3320 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003321 }
Josef Bacik7b128762008-07-24 12:17:14 -04003322
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003323 if (release_rsv)
3324 btrfs_orphan_release_metadata(inode);
3325
Josef Bacik4ef31a42013-08-13 14:10:08 -04003326 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003327}
3328
3329/*
3330 * this cleans up any orphans that may be left on the list from the last use
3331 * of this root.
3332 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003333int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003334{
3335 struct btrfs_path *path;
3336 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003337 struct btrfs_key key, found_key;
3338 struct btrfs_trans_handle *trans;
3339 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003340 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003341 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3342
Yan, Zhengd68fc572010-05-16 10:49:58 -04003343 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003344 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003345
3346 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003347 if (!path) {
3348 ret = -ENOMEM;
3349 goto out;
3350 }
David Sterbae4058b52015-11-27 16:31:35 +01003351 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003352
3353 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003354 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003355 key.offset = (u64)-1;
3356
Josef Bacik7b128762008-07-24 12:17:14 -04003357 while (1) {
3358 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003359 if (ret < 0)
3360 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003361
3362 /*
3363 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003364 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003365 * find the key and see if we have stuff that matches
3366 */
3367 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003368 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003369 if (path->slots[0] == 0)
3370 break;
3371 path->slots[0]--;
3372 }
3373
3374 /* pull out the item */
3375 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003376 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3377
3378 /* make sure the item matches what we want */
3379 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3380 break;
David Sterba962a2982014-06-04 18:41:45 +02003381 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003382 break;
3383
3384 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003385 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003386
3387 /*
3388 * this is where we are basically btrfs_lookup, without the
3389 * crossing root thing. we store the inode number in the
3390 * offset of the orphan item.
3391 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003392
3393 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003394 btrfs_err(root->fs_info,
3395 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003396 ret = -EINVAL;
3397 goto out;
3398 }
3399
3400 last_objectid = found_key.offset;
3401
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003402 found_key.objectid = found_key.offset;
3403 found_key.type = BTRFS_INODE_ITEM_KEY;
3404 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003405 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303406 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003407 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003408 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003409
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003410 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3411 struct btrfs_root *dead_root;
3412 struct btrfs_fs_info *fs_info = root->fs_info;
3413 int is_dead_root = 0;
3414
3415 /*
3416 * this is an orphan in the tree root. Currently these
3417 * could come from 2 sources:
3418 * a) a snapshot deletion in progress
3419 * b) a free space cache inode
3420 * We need to distinguish those two, as the snapshot
3421 * orphan must not get deleted.
3422 * find_dead_roots already ran before us, so if this
3423 * is a snapshot deletion, we should find the root
3424 * in the dead_roots list
3425 */
3426 spin_lock(&fs_info->trans_lock);
3427 list_for_each_entry(dead_root, &fs_info->dead_roots,
3428 root_list) {
3429 if (dead_root->root_key.objectid ==
3430 found_key.objectid) {
3431 is_dead_root = 1;
3432 break;
3433 }
3434 }
3435 spin_unlock(&fs_info->trans_lock);
3436 if (is_dead_root) {
3437 /* prevent this orphan from being found again */
3438 key.offset = found_key.objectid - 1;
3439 continue;
3440 }
3441 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003442 /*
3443 * Inode is already gone but the orphan item is still there,
3444 * kill the orphan item.
3445 */
3446 if (ret == -ESTALE) {
3447 trans = btrfs_start_transaction(root, 1);
3448 if (IS_ERR(trans)) {
3449 ret = PTR_ERR(trans);
3450 goto out;
3451 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003452 btrfs_debug(root->fs_info, "auto deleting %Lu",
3453 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003454 ret = btrfs_del_orphan_item(trans, root,
3455 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003456 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003457 if (ret)
3458 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003459 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003460 }
Josef Bacik7b128762008-07-24 12:17:14 -04003461
Josef Bacik7b128762008-07-24 12:17:14 -04003462 /*
3463 * add this inode to the orphan list so btrfs_orphan_del does
3464 * the proper thing when we hit it
3465 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003466 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3467 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003468 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003469
Josef Bacik7b128762008-07-24 12:17:14 -04003470 /* if we have links, this was a truncate, lets do that */
3471 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303472 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003473 iput(inode);
3474 continue;
3475 }
Josef Bacik7b128762008-07-24 12:17:14 -04003476 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003477
3478 /* 1 for the orphan item deletion. */
3479 trans = btrfs_start_transaction(root, 1);
3480 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003481 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003482 ret = PTR_ERR(trans);
3483 goto out;
3484 }
3485 ret = btrfs_orphan_add(trans, inode);
3486 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003487 if (ret) {
3488 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003489 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003490 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003491
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003492 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003493 if (ret)
3494 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003495 } else {
3496 nr_unlink++;
3497 }
3498
3499 /* this will do delete_inode and everything for us */
3500 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003501 if (ret)
3502 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003503 }
Miao Xie3254c872011-11-10 20:45:05 -05003504 /* release the path since we're done with it */
3505 btrfs_release_path(path);
3506
Yan, Zhengd68fc572010-05-16 10:49:58 -04003507 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3508
3509 if (root->orphan_block_rsv)
3510 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3511 (u64)-1);
3512
Miao Xie27cdeb72014-04-02 19:51:05 +08003513 if (root->orphan_block_rsv ||
3514 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003515 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003516 if (!IS_ERR(trans))
3517 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003518 }
Josef Bacik7b128762008-07-24 12:17:14 -04003519
3520 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003521 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003522 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003523 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003524
3525out:
3526 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003527 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003528 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003529 btrfs_free_path(path);
3530 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003531}
3532
Chris Masond352ac62008-09-29 15:18:18 -04003533/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003534 * very simple check to peek ahead in the leaf looking for xattrs. If we
3535 * don't find any xattrs, we know there can't be any acls.
3536 *
3537 * slot is the slot the inode is in, objectid is the objectid of the inode
3538 */
3539static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003540 int slot, u64 objectid,
3541 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003542{
3543 u32 nritems = btrfs_header_nritems(leaf);
3544 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003545 static u64 xattr_access = 0;
3546 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003547 int scanned = 0;
3548
Josef Bacikf23b5a52013-06-19 10:16:26 -04003549 if (!xattr_access) {
3550 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3551 strlen(POSIX_ACL_XATTR_ACCESS));
3552 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3553 strlen(POSIX_ACL_XATTR_DEFAULT));
3554 }
3555
Chris Mason46a53cc2009-04-27 11:47:50 -04003556 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003557 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003558 while (slot < nritems) {
3559 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3560
3561 /* we found a different objectid, there must not be acls */
3562 if (found_key.objectid != objectid)
3563 return 0;
3564
3565 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003566 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003567 if (*first_xattr_slot == -1)
3568 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003569 if (found_key.offset == xattr_access ||
3570 found_key.offset == xattr_default)
3571 return 1;
3572 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003573
3574 /*
3575 * we found a key greater than an xattr key, there can't
3576 * be any acls later on
3577 */
3578 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3579 return 0;
3580
3581 slot++;
3582 scanned++;
3583
3584 /*
3585 * it goes inode, inode backrefs, xattrs, extents,
3586 * so if there are a ton of hard links to an inode there can
3587 * be a lot of backrefs. Don't waste time searching too hard,
3588 * this is just an optimization
3589 */
3590 if (scanned >= 8)
3591 break;
3592 }
3593 /* we hit the end of the leaf before we found an xattr or
3594 * something larger than an xattr. We have to assume the inode
3595 * has acls
3596 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003597 if (*first_xattr_slot == -1)
3598 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003599 return 1;
3600}
3601
3602/*
Chris Masond352ac62008-09-29 15:18:18 -04003603 * read an inode from the btree into the in-memory inode
3604 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003605static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003606{
3607 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003608 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003609 struct btrfs_inode_item *inode_item;
3610 struct btrfs_root *root = BTRFS_I(inode)->root;
3611 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003612 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003613 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003614 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003615 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003616 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003617 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003618
3619 ret = btrfs_fill_inode(inode, &rdev);
3620 if (!ret)
3621 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003622
3623 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003624 if (!path)
3625 goto make_bad;
3626
Chris Mason39279cc2007-06-12 06:35:45 -04003627 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003628
Chris Mason39279cc2007-06-12 06:35:45 -04003629 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003630 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003631 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003632
Chris Mason5f39d392007-10-15 16:14:19 -04003633 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003634
3635 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003636 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003637
Chris Mason5f39d392007-10-15 16:14:19 -04003638 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3639 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003640 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003641 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003642 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3643 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003644 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003645
David Sterbaa937b972014-12-12 17:39:12 +01003646 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3647 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003648
David Sterbaa937b972014-12-12 17:39:12 +01003649 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3650 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003651
David Sterbaa937b972014-12-12 17:39:12 +01003652 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3653 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003654
chandan r9cc97d62012-07-04 12:48:07 +05303655 BTRFS_I(inode)->i_otime.tv_sec =
3656 btrfs_timespec_sec(leaf, &inode_item->otime);
3657 BTRFS_I(inode)->i_otime.tv_nsec =
3658 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003659
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003660 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003661 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003662 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3663
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003664 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003665 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003666 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003667 rdev = btrfs_inode_rdev(leaf, inode_item);
3668
Josef Bacikaec74772008-07-24 12:12:38 -04003669 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003670 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003671
3672cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003673 /*
3674 * If we were modified in the current generation and evicted from memory
3675 * and then re-read we need to do a full sync since we don't have any
3676 * idea about which extents were modified before we were evicted from
3677 * cache.
3678 *
3679 * This is required for both inode re-read from disk and delayed inode
3680 * in delayed_nodes_tree.
3681 */
3682 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3683 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3684 &BTRFS_I(inode)->runtime_flags);
3685
Filipe Mananabde6c242015-07-24 00:00:19 +01003686 /*
3687 * We don't persist the id of the transaction where an unlink operation
3688 * against the inode was last made. So here we assume the inode might
3689 * have been evicted, and therefore the exact value of last_unlink_trans
3690 * lost, and set it to last_trans to avoid metadata inconsistencies
3691 * between the inode and its parent if the inode is fsync'ed and the log
3692 * replayed. For example, in the scenario:
3693 *
3694 * touch mydir/foo
3695 * ln mydir/foo mydir/bar
3696 * sync
3697 * unlink mydir/bar
3698 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3699 * xfs_io -c fsync mydir/foo
3700 * <power failure>
3701 * mount fs, triggers fsync log replay
3702 *
3703 * We must make sure that when we fsync our inode foo we also log its
3704 * parent inode, otherwise after log replay the parent still has the
3705 * dentry with the "bar" name but our inode foo has a link count of 1
3706 * and doesn't have an inode ref with the name "bar" anymore.
3707 *
3708 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3709 * but it guarantees correctness at the expense of ocassional full
3710 * transaction commits on fsync if our inode is a directory, or if our
3711 * inode is not a directory, logging its parent unnecessarily.
3712 */
3713 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3714
Miao Xie67de1172013-12-26 13:07:06 +08003715 path->slots[0]++;
3716 if (inode->i_nlink != 1 ||
3717 path->slots[0] >= btrfs_header_nritems(leaf))
3718 goto cache_acl;
3719
3720 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3721 if (location.objectid != btrfs_ino(inode))
3722 goto cache_acl;
3723
3724 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3725 if (location.type == BTRFS_INODE_REF_KEY) {
3726 struct btrfs_inode_ref *ref;
3727
3728 ref = (struct btrfs_inode_ref *)ptr;
3729 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3730 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3731 struct btrfs_inode_extref *extref;
3732
3733 extref = (struct btrfs_inode_extref *)ptr;
3734 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3735 extref);
3736 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003737cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003738 /*
3739 * try to precache a NULL acl entry for files that don't have
3740 * any xattrs or acls
3741 */
Li Zefan33345d012011-04-20 10:31:50 +08003742 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003743 btrfs_ino(inode), &first_xattr_slot);
3744 if (first_xattr_slot != -1) {
3745 path->slots[0] = first_xattr_slot;
3746 ret = btrfs_load_inode_props(inode, path);
3747 if (ret)
3748 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003749 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003750 btrfs_ino(inode),
3751 root->root_key.objectid, ret);
3752 }
3753 btrfs_free_path(path);
3754
Al Viro72c04902009-06-24 16:58:48 -04003755 if (!maybe_acls)
3756 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003757
Chris Mason39279cc2007-06-12 06:35:45 -04003758 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003759 case S_IFREG:
3760 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003761 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003762 inode->i_fop = &btrfs_file_operations;
3763 inode->i_op = &btrfs_file_inode_operations;
3764 break;
3765 case S_IFDIR:
3766 inode->i_fop = &btrfs_dir_file_operations;
3767 if (root == root->fs_info->tree_root)
3768 inode->i_op = &btrfs_dir_ro_inode_operations;
3769 else
3770 inode->i_op = &btrfs_dir_inode_operations;
3771 break;
3772 case S_IFLNK:
3773 inode->i_op = &btrfs_symlink_inode_operations;
3774 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3775 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003776 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003777 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003778 init_special_inode(inode, inode->i_mode, rdev);
3779 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003780 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003781
3782 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003783 return;
3784
3785make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003786 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003787 make_bad_inode(inode);
3788}
3789
Chris Masond352ac62008-09-29 15:18:18 -04003790/*
3791 * given a leaf and an inode, copy the inode fields into the leaf
3792 */
Chris Masone02119d2008-09-05 16:13:11 -04003793static void fill_inode_item(struct btrfs_trans_handle *trans,
3794 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003795 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003796 struct inode *inode)
3797{
Liu Bo51fab692012-12-27 09:01:21 +00003798 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003799
Liu Bo51fab692012-12-27 09:01:21 +00003800 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003801
Liu Bo51fab692012-12-27 09:01:21 +00003802 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3803 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3804 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3805 &token);
3806 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3807 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003808
David Sterbaa937b972014-12-12 17:39:12 +01003809 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003810 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003811 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003812 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003813
David Sterbaa937b972014-12-12 17:39:12 +01003814 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003815 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003816 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003817 inode->i_mtime.tv_nsec, &token);
3818
David Sterbaa937b972014-12-12 17:39:12 +01003819 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003820 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003821 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003822 inode->i_ctime.tv_nsec, &token);
3823
chandan r9cc97d62012-07-04 12:48:07 +05303824 btrfs_set_token_timespec_sec(leaf, &item->otime,
3825 BTRFS_I(inode)->i_otime.tv_sec, &token);
3826 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3827 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3828
Liu Bo51fab692012-12-27 09:01:21 +00003829 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3830 &token);
3831 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3832 &token);
3833 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3834 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3835 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3836 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3837 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003838}
3839
Chris Masond352ac62008-09-29 15:18:18 -04003840/*
3841 * copy everything in the in-memory inode into the btree.
3842 */
Chris Mason21151332011-11-10 20:39:08 -05003843static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003844 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003845{
3846 struct btrfs_inode_item *inode_item;
3847 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003848 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003849 int ret;
3850
3851 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003852 if (!path)
3853 return -ENOMEM;
3854
Chris Masonb9473432009-03-13 11:00:37 -04003855 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003856 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3857 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003858 if (ret) {
3859 if (ret > 0)
3860 ret = -ENOENT;
3861 goto failed;
3862 }
3863
Chris Mason5f39d392007-10-15 16:14:19 -04003864 leaf = path->nodes[0];
3865 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003866 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003867
Chris Masone02119d2008-09-05 16:13:11 -04003868 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003869 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003870 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003871 ret = 0;
3872failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003873 btrfs_free_path(path);
3874 return ret;
3875}
3876
Chris Masond352ac62008-09-29 15:18:18 -04003877/*
Chris Mason21151332011-11-10 20:39:08 -05003878 * copy everything in the in-memory inode into the btree.
3879 */
3880noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3881 struct btrfs_root *root, struct inode *inode)
3882{
3883 int ret;
3884
3885 /*
3886 * If the inode is a free space inode, we can deadlock during commit
3887 * if we put it into the delayed code.
3888 *
3889 * The data relocation inode should also be directly updated
3890 * without delay
3891 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003892 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003893 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3894 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003895 btrfs_update_root_times(trans, root);
3896
Chris Mason21151332011-11-10 20:39:08 -05003897 ret = btrfs_delayed_update_inode(trans, root, inode);
3898 if (!ret)
3899 btrfs_set_inode_last_trans(trans, inode);
3900 return ret;
3901 }
3902
3903 return btrfs_update_inode_item(trans, root, inode);
3904}
3905
Josef Bacikbe6aef62012-10-22 15:43:12 -04003906noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3907 struct btrfs_root *root,
3908 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003909{
3910 int ret;
3911
3912 ret = btrfs_update_inode(trans, root, inode);
3913 if (ret == -ENOSPC)
3914 return btrfs_update_inode_item(trans, root, inode);
3915 return ret;
3916}
3917
3918/*
Chris Masond352ac62008-09-29 15:18:18 -04003919 * unlink helper that gets used here in inode.c and in the tree logging
3920 * recovery code. It remove a link in a directory with a given name, and
3921 * also drops the back refs in the inode to the directory
3922 */
Al Viro92986792011-03-04 17:14:37 +00003923static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3924 struct btrfs_root *root,
3925 struct inode *dir, struct inode *inode,
3926 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003927{
3928 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003929 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003930 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003931 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003932 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003933 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003934 u64 ino = btrfs_ino(inode);
3935 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003936
3937 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003938 if (!path) {
3939 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003940 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003941 }
3942
Chris Masonb9473432009-03-13 11:00:37 -04003943 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003944 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003945 name, name_len, -1);
3946 if (IS_ERR(di)) {
3947 ret = PTR_ERR(di);
3948 goto err;
3949 }
3950 if (!di) {
3951 ret = -ENOENT;
3952 goto err;
3953 }
Chris Mason5f39d392007-10-15 16:14:19 -04003954 leaf = path->nodes[0];
3955 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003956 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003957 if (ret)
3958 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003959 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003960
Miao Xie67de1172013-12-26 13:07:06 +08003961 /*
3962 * If we don't have dir index, we have to get it by looking up
3963 * the inode ref, since we get the inode ref, remove it directly,
3964 * it is unnecessary to do delayed deletion.
3965 *
3966 * But if we have dir index, needn't search inode ref to get it.
3967 * Since the inode ref is close to the inode item, it is better
3968 * that we delay to delete it, and just do this deletion when
3969 * we update the inode item.
3970 */
3971 if (BTRFS_I(inode)->dir_index) {
3972 ret = btrfs_delayed_delete_inode_ref(inode);
3973 if (!ret) {
3974 index = BTRFS_I(inode)->dir_index;
3975 goto skip_backref;
3976 }
3977 }
3978
Li Zefan33345d012011-04-20 10:31:50 +08003979 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3980 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003981 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003982 btrfs_info(root->fs_info,
3983 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003984 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003985 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003986 goto err;
3987 }
Miao Xie67de1172013-12-26 13:07:06 +08003988skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003989 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003990 if (ret) {
3991 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003992 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003993 }
Chris Mason39279cc2007-06-12 06:35:45 -04003994
Chris Masone02119d2008-09-05 16:13:11 -04003995 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003996 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003997 if (ret != 0 && ret != -ENOENT) {
3998 btrfs_abort_transaction(trans, root, ret);
3999 goto err;
4000 }
Chris Masone02119d2008-09-05 16:13:11 -04004001
4002 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
4003 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04004004 if (ret == -ENOENT)
4005 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004006 else if (ret)
4007 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004008err:
4009 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004010 if (ret)
4011 goto out;
4012
4013 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004014 inode_inc_iversion(inode);
4015 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04004016 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06004017 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004018out:
Chris Mason39279cc2007-06-12 06:35:45 -04004019 return ret;
4020}
4021
Al Viro92986792011-03-04 17:14:37 +00004022int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4023 struct btrfs_root *root,
4024 struct inode *dir, struct inode *inode,
4025 const char *name, int name_len)
4026{
4027 int ret;
4028 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4029 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004030 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004031 ret = btrfs_update_inode(trans, root, inode);
4032 }
4033 return ret;
4034}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004035
4036/*
4037 * helper to start transaction for unlink and rmdir.
4038 *
Josef Bacikd52be812013-05-29 14:54:47 -04004039 * unlink and rmdir are special in btrfs, they do not always free space, so
4040 * if we cannot make our reservations the normal way try and see if there is
4041 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4042 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004043 */
Josef Bacikd52be812013-05-29 14:54:47 -04004044static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004045{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004046 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004047
Josef Bacike70bea52011-10-11 14:18:24 -04004048 /*
4049 * 1 for the possible orphan item
4050 * 1 for the dir item
4051 * 1 for the dir index
4052 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004053 * 1 for the inode
4054 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004055 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004056}
4057
Chris Mason39279cc2007-06-12 06:35:45 -04004058static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4059{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004060 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004061 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004062 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004063 int ret;
4064
Josef Bacikd52be812013-05-29 14:54:47 -04004065 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004066 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004067 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004068
David Howells2b0143b2015-03-17 22:25:59 +00004069 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004070
David Howells2b0143b2015-03-17 22:25:59 +00004071 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004072 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004073 if (ret)
4074 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004075
Yan, Zhenga22285a2010-05-16 10:48:46 -04004076 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004077 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004078 if (ret)
4079 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004080 }
Josef Bacik7b128762008-07-24 12:17:14 -04004081
Tsutomu Itohb5324022011-07-19 07:27:20 +00004082out:
Josef Bacikd52be812013-05-29 14:54:47 -04004083 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004084 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004085 return ret;
4086}
4087
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004088int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4089 struct btrfs_root *root,
4090 struct inode *dir, u64 objectid,
4091 const char *name, int name_len)
4092{
4093 struct btrfs_path *path;
4094 struct extent_buffer *leaf;
4095 struct btrfs_dir_item *di;
4096 struct btrfs_key key;
4097 u64 index;
4098 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004099 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004100
4101 path = btrfs_alloc_path();
4102 if (!path)
4103 return -ENOMEM;
4104
Li Zefan33345d012011-04-20 10:31:50 +08004105 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004106 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004107 if (IS_ERR_OR_NULL(di)) {
4108 if (!di)
4109 ret = -ENOENT;
4110 else
4111 ret = PTR_ERR(di);
4112 goto out;
4113 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004114
4115 leaf = path->nodes[0];
4116 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4117 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4118 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004119 if (ret) {
4120 btrfs_abort_transaction(trans, root, ret);
4121 goto out;
4122 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004123 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004124
4125 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4126 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004127 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004128 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004129 if (ret != -ENOENT) {
4130 btrfs_abort_transaction(trans, root, ret);
4131 goto out;
4132 }
Li Zefan33345d012011-04-20 10:31:50 +08004133 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004134 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004135 if (IS_ERR_OR_NULL(di)) {
4136 if (!di)
4137 ret = -ENOENT;
4138 else
4139 ret = PTR_ERR(di);
4140 btrfs_abort_transaction(trans, root, ret);
4141 goto out;
4142 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004143
4144 leaf = path->nodes[0];
4145 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004146 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004147 index = key.offset;
4148 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004149 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004150
Miao Xie16cdcec2011-04-22 18:12:22 +08004151 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004152 if (ret) {
4153 btrfs_abort_transaction(trans, root, ret);
4154 goto out;
4155 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004156
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004157 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004158 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004159 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004160 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004161 if (ret)
4162 btrfs_abort_transaction(trans, root, ret);
4163out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004164 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004165 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004166}
4167
Chris Mason39279cc2007-06-12 06:35:45 -04004168static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4169{
David Howells2b0143b2015-03-17 22:25:59 +00004170 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004171 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004172 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004173 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004174
David Sterbab3ae2442012-09-13 16:04:34 -06004175 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004176 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004177 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4178 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004179
Josef Bacikd52be812013-05-29 14:54:47 -04004180 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004181 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004182 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004183
Li Zefan33345d012011-04-20 10:31:50 +08004184 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004185 err = btrfs_unlink_subvol(trans, root, dir,
4186 BTRFS_I(inode)->location.objectid,
4187 dentry->d_name.name,
4188 dentry->d_name.len);
4189 goto out;
4190 }
4191
Josef Bacik7b128762008-07-24 12:17:14 -04004192 err = btrfs_orphan_add(trans, inode);
4193 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004194 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004195
Chris Mason39279cc2007-06-12 06:35:45 -04004196 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004197 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004198 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004199 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004200 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004201out:
Josef Bacikd52be812013-05-29 14:54:47 -04004202 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004203 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004204
Chris Mason39279cc2007-06-12 06:35:45 -04004205 return err;
4206}
4207
Chris Mason28f75a02015-02-04 06:59:29 -08004208static int truncate_space_check(struct btrfs_trans_handle *trans,
4209 struct btrfs_root *root,
4210 u64 bytes_deleted)
4211{
4212 int ret;
4213
4214 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
4215 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4216 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4217 if (!ret)
4218 trans->bytes_reserved += bytes_deleted;
4219 return ret;
4220
4221}
4222
Filipe Manana0305cd52015-10-16 12:34:25 +01004223static int truncate_inline_extent(struct inode *inode,
4224 struct btrfs_path *path,
4225 struct btrfs_key *found_key,
4226 const u64 item_end,
4227 const u64 new_size)
4228{
4229 struct extent_buffer *leaf = path->nodes[0];
4230 int slot = path->slots[0];
4231 struct btrfs_file_extent_item *fi;
4232 u32 size = (u32)(new_size - found_key->offset);
4233 struct btrfs_root *root = BTRFS_I(inode)->root;
4234
4235 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4236
4237 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4238 loff_t offset = new_size;
4239 loff_t page_end = ALIGN(offset, PAGE_CACHE_SIZE);
4240
4241 /*
4242 * Zero out the remaining of the last page of our inline extent,
4243 * instead of directly truncating our inline extent here - that
4244 * would be much more complex (decompressing all the data, then
4245 * compressing the truncated data, which might be bigger than
4246 * the size of the inline extent, resize the extent, etc).
4247 * We release the path because to get the page we might need to
4248 * read the extent item from disk (data not in the page cache).
4249 */
4250 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304251 return btrfs_truncate_block(inode, offset, page_end - offset,
4252 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004253 }
4254
4255 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4256 size = btrfs_file_extent_calc_inline_size(size);
4257 btrfs_truncate_item(root, path, size, 1);
4258
4259 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4260 inode_sub_bytes(inode, item_end + 1 - new_size);
4261
4262 return 0;
4263}
4264
Chris Mason323ac952008-10-01 19:05:46 -04004265/*
Chris Mason39279cc2007-06-12 06:35:45 -04004266 * this can truncate away extent items, csum items and directory items.
4267 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004268 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004269 *
4270 * csum items that cross the new i_size are truncated to the new size
4271 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004272 *
4273 * min_type is the minimum key type to truncate down to. If set to 0, this
4274 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004275 */
Yan, Zheng80825102009-11-12 09:35:36 +00004276int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4277 struct btrfs_root *root,
4278 struct inode *inode,
4279 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004280{
Chris Mason39279cc2007-06-12 06:35:45 -04004281 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004282 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004283 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004284 struct btrfs_key key;
4285 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004286 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004287 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004288 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004289 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004290 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004291 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004292 int found_extent;
4293 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004294 int pending_del_nr = 0;
4295 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004296 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004297 int ret;
4298 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004299 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004300 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004301 bool be_nice = 0;
4302 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004303 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004304
4305 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004306
Chris Mason28ed1342014-12-17 09:41:04 -08004307 /*
4308 * for non-free space inodes and ref cows, we want to back off from
4309 * time to time
4310 */
4311 if (!btrfs_is_free_space_inode(inode) &&
4312 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4313 be_nice = 1;
4314
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004315 path = btrfs_alloc_path();
4316 if (!path)
4317 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004318 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004319
Josef Bacik5dc562c2012-08-17 13:14:17 -04004320 /*
4321 * We want to drop from the next block forward in case this new size is
4322 * not block aligned since we will be keeping the last block of the
4323 * extent just the way it is.
4324 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004325 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4326 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004327 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4328 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004329
Miao Xie16cdcec2011-04-22 18:12:22 +08004330 /*
4331 * This function is also used to drop the items in the log tree before
4332 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4333 * it is used to drop the loged items. So we shouldn't kill the delayed
4334 * items.
4335 */
4336 if (min_type == 0 && root == BTRFS_I(inode)->root)
4337 btrfs_kill_delayed_inode_items(inode);
4338
Li Zefan33345d012011-04-20 10:31:50 +08004339 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004340 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004341 key.type = (u8)-1;
4342
Chris Mason85e21ba2008-01-29 15:11:36 -05004343search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004344 /*
4345 * with a 16K leaf size and 128MB extents, you can actually queue
4346 * up a huge file in a single leaf. Most of the time that
4347 * bytes_deleted is > 0, it will be huge by the time we get here
4348 */
Byongho Leeee221842015-12-15 01:42:10 +09004349 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004350 if (btrfs_should_end_transaction(trans, root)) {
4351 err = -EAGAIN;
4352 goto error;
4353 }
4354 }
4355
4356
Chris Masonb9473432009-03-13 11:00:37 -04004357 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004358 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004359 if (ret < 0) {
4360 err = ret;
4361 goto out;
4362 }
Chris Masond3977122009-01-05 21:25:51 -05004363
Chris Mason85e21ba2008-01-29 15:11:36 -05004364 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004365 /* there are no items in the tree for us to truncate, we're
4366 * done
4367 */
Yan, Zheng80825102009-11-12 09:35:36 +00004368 if (path->slots[0] == 0)
4369 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004370 path->slots[0]--;
4371 }
4372
Chris Masond3977122009-01-05 21:25:51 -05004373 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004374 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004375 leaf = path->nodes[0];
4376 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004377 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004378
Li Zefan33345d012011-04-20 10:31:50 +08004379 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004380 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004381
Chris Mason85e21ba2008-01-29 15:11:36 -05004382 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004383 break;
4384
Chris Mason5f39d392007-10-15 16:14:19 -04004385 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004386 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004387 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004388 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004389 extent_type = btrfs_file_extent_type(leaf, fi);
4390 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004391 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004392 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004393 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004394 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004395 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004396 }
Yan008630c2007-11-07 13:31:09 -05004397 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004398 }
Yan, Zheng80825102009-11-12 09:35:36 +00004399 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004400 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004401 } else {
4402 if (item_end < new_size)
4403 break;
4404 if (found_key.offset >= new_size)
4405 del_item = 1;
4406 else
4407 del_item = 0;
4408 }
Chris Mason39279cc2007-06-12 06:35:45 -04004409 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004410 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004411 if (found_type != BTRFS_EXTENT_DATA_KEY)
4412 goto delete;
4413
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004414 if (del_item)
4415 last_size = found_key.offset;
4416 else
4417 last_size = new_size;
4418
Chris Mason179e29e2007-11-01 11:28:41 -04004419 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004420 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004421 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004422 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004423 u64 orig_num_bytes =
4424 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004425 extent_num_bytes = ALIGN(new_size -
4426 found_key.offset,
4427 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004428 btrfs_set_file_extent_num_bytes(leaf, fi,
4429 extent_num_bytes);
4430 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004431 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004432 if (test_bit(BTRFS_ROOT_REF_COWS,
4433 &root->state) &&
4434 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004435 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004436 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004437 } else {
Chris Masondb945352007-10-15 16:15:53 -04004438 extent_num_bytes =
4439 btrfs_file_extent_disk_num_bytes(leaf,
4440 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004441 extent_offset = found_key.offset -
4442 btrfs_file_extent_offset(leaf, fi);
4443
Chris Mason39279cc2007-06-12 06:35:45 -04004444 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004445 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004446 if (extent_start != 0) {
4447 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004448 if (test_bit(BTRFS_ROOT_REF_COWS,
4449 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004450 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004451 }
4452 }
Chris Mason90692182008-02-08 13:49:28 -05004453 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004454 /*
4455 * we can't truncate inline items that have had
4456 * special encodings
4457 */
4458 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004459 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4460 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004461
4462 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004463 * Need to release path in order to truncate a
4464 * compressed extent. So delete any accumulated
4465 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004466 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004467 if (btrfs_file_extent_compression(leaf, fi) !=
4468 BTRFS_COMPRESS_NONE && pending_del_nr) {
4469 err = btrfs_del_items(trans, root, path,
4470 pending_del_slot,
4471 pending_del_nr);
4472 if (err) {
4473 btrfs_abort_transaction(trans,
4474 root,
4475 err);
4476 goto error;
4477 }
4478 pending_del_nr = 0;
4479 }
4480
4481 err = truncate_inline_extent(inode, path,
4482 &found_key,
4483 item_end,
4484 new_size);
4485 if (err) {
4486 btrfs_abort_transaction(trans,
4487 root, err);
4488 goto error;
4489 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004490 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4491 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004492 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004493 }
Chris Mason39279cc2007-06-12 06:35:45 -04004494 }
Chris Mason179e29e2007-11-01 11:28:41 -04004495delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004496 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004497 if (!pending_del_nr) {
4498 /* no pending yet, add ourselves */
4499 pending_del_slot = path->slots[0];
4500 pending_del_nr = 1;
4501 } else if (pending_del_nr &&
4502 path->slots[0] + 1 == pending_del_slot) {
4503 /* hop on the pending chunk */
4504 pending_del_nr++;
4505 pending_del_slot = path->slots[0];
4506 } else {
Chris Masond3977122009-01-05 21:25:51 -05004507 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004508 }
Chris Mason39279cc2007-06-12 06:35:45 -04004509 } else {
4510 break;
4511 }
Chris Mason28f75a02015-02-04 06:59:29 -08004512 should_throttle = 0;
4513
Miao Xie27cdeb72014-04-02 19:51:05 +08004514 if (found_extent &&
4515 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4516 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004517 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004518 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004519 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004520 extent_num_bytes, 0,
4521 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004522 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004523 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004524 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004525 btrfs_async_run_delayed_refs(root,
4526 trans->delayed_ref_updates * 2, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004527 if (be_nice) {
4528 if (truncate_space_check(trans, root,
4529 extent_num_bytes)) {
4530 should_end = 1;
4531 }
4532 if (btrfs_should_throttle_delayed_refs(trans,
4533 root)) {
4534 should_throttle = 1;
4535 }
4536 }
Chris Mason39279cc2007-06-12 06:35:45 -04004537 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004538
Yan, Zheng80825102009-11-12 09:35:36 +00004539 if (found_type == BTRFS_INODE_ITEM_KEY)
4540 break;
4541
4542 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004543 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004544 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004545 if (pending_del_nr) {
4546 ret = btrfs_del_items(trans, root, path,
4547 pending_del_slot,
4548 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004549 if (ret) {
4550 btrfs_abort_transaction(trans,
4551 root, ret);
4552 goto error;
4553 }
Yan, Zheng80825102009-11-12 09:35:36 +00004554 pending_del_nr = 0;
4555 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004556 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004557 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004558 unsigned long updates = trans->delayed_ref_updates;
4559 if (updates) {
4560 trans->delayed_ref_updates = 0;
4561 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4562 if (ret && !err)
4563 err = ret;
4564 }
4565 }
Chris Mason28f75a02015-02-04 06:59:29 -08004566 /*
4567 * if we failed to refill our space rsv, bail out
4568 * and let the transaction restart
4569 */
4570 if (should_end) {
4571 err = -EAGAIN;
4572 goto error;
4573 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004574 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004575 } else {
4576 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004577 }
Chris Mason39279cc2007-06-12 06:35:45 -04004578 }
Yan, Zheng80825102009-11-12 09:35:36 +00004579out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004580 if (pending_del_nr) {
4581 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4582 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004583 if (ret)
4584 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004585 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004586error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004587 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004588 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004589
Chris Mason39279cc2007-06-12 06:35:45 -04004590 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004591
Byongho Leeee221842015-12-15 01:42:10 +09004592 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004593 unsigned long updates = trans->delayed_ref_updates;
4594 if (updates) {
4595 trans->delayed_ref_updates = 0;
4596 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4597 if (ret && !err)
4598 err = ret;
4599 }
4600 }
Yan, Zheng80825102009-11-12 09:35:36 +00004601 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004602}
4603
Chris Masona52d9a82007-08-27 16:49:44 -04004604/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304605 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004606 * @inode - inode that we're zeroing
4607 * @from - the offset to start zeroing
4608 * @len - the length to zero, 0 to zero the entire range respective to the
4609 * offset
4610 * @front - zero up to the offset instead of from the offset on
4611 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304612 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004613 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004614 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304615int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004616 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004617{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004618 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004619 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004620 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4621 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004622 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004623 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004624 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004625 pgoff_t index = from >> PAGE_CACHE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304626 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004627 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004628 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004629 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304630 u64 block_start;
4631 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004632
Josef Bacik2aaa6652012-08-29 14:27:18 -04004633 if ((offset & (blocksize - 1)) == 0 &&
4634 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004635 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304636
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004637 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304638 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004639 if (ret)
4640 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004641
Chris Mason211c17f2008-05-15 09:13:45 -04004642again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004643 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004644 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004645 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304646 round_down(from, blocksize),
4647 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004648 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004649 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004650 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004651
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304652 block_start = round_down(from, blocksize);
4653 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004654
Chris Masona52d9a82007-08-27 16:49:44 -04004655 if (!PageUptodate(page)) {
4656 ret = btrfs_readpage(NULL, page);
4657 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004658 if (page->mapping != mapping) {
4659 unlock_page(page);
4660 page_cache_release(page);
4661 goto again;
4662 }
Chris Masona52d9a82007-08-27 16:49:44 -04004663 if (!PageUptodate(page)) {
4664 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004665 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004666 }
4667 }
Chris Mason211c17f2008-05-15 09:13:45 -04004668 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004669
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304670 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004671 set_page_extent_mapped(page);
4672
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304673 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004674 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304675 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004676 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004677 unlock_page(page);
4678 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004679 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004680 btrfs_put_ordered_extent(ordered);
4681 goto again;
4682 }
4683
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304684 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004685 EXTENT_DIRTY | EXTENT_DELALLOC |
4686 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004687 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004688
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304689 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004690 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004691 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304692 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004693 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004694 goto out_unlock;
4695 }
4696
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304697 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004698 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304699 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004700 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004701 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304702 memset(kaddr + (block_start - page_offset(page)),
4703 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004704 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304705 memset(kaddr + (block_start - page_offset(page)) + offset,
4706 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004707 flush_dcache_page(page);
4708 kunmap(page);
4709 }
Chris Mason247e7432008-07-17 12:53:51 -04004710 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004711 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304712 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004713 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004714
Chris Mason89642222008-07-24 09:41:53 -04004715out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004716 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304717 btrfs_delalloc_release_space(inode, block_start,
4718 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004719 unlock_page(page);
4720 page_cache_release(page);
4721out:
4722 return ret;
4723}
4724
Josef Bacik16e75492013-10-22 12:18:51 -04004725static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4726 u64 offset, u64 len)
4727{
4728 struct btrfs_trans_handle *trans;
4729 int ret;
4730
4731 /*
4732 * Still need to make sure the inode looks like it's been updated so
4733 * that any holes get logged if we fsync.
4734 */
4735 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4736 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4737 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4738 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4739 return 0;
4740 }
4741
4742 /*
4743 * 1 - for the one we're dropping
4744 * 1 - for the one we're adding
4745 * 1 - for updating the inode.
4746 */
4747 trans = btrfs_start_transaction(root, 3);
4748 if (IS_ERR(trans))
4749 return PTR_ERR(trans);
4750
4751 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4752 if (ret) {
4753 btrfs_abort_transaction(trans, root, ret);
4754 btrfs_end_transaction(trans, root);
4755 return ret;
4756 }
4757
4758 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4759 0, 0, len, 0, len, 0, 0, 0);
4760 if (ret)
4761 btrfs_abort_transaction(trans, root, ret);
4762 else
4763 btrfs_update_inode(trans, root, inode);
4764 btrfs_end_transaction(trans, root);
4765 return ret;
4766}
4767
Josef Bacik695a0d02011-03-04 15:46:53 -05004768/*
4769 * This function puts in dummy file extents for the area we're creating a hole
4770 * for. So if we are truncating this file to a larger size we need to insert
4771 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4772 * the range between oldsize and size
4773 */
Josef Bacika41ad392011-01-31 15:30:16 -05004774int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004775{
Yan Zheng9036c102008-10-30 14:19:41 -04004776 struct btrfs_root *root = BTRFS_I(inode)->root;
4777 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004778 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004779 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004780 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004781 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4782 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004783 u64 last_byte;
4784 u64 cur_offset;
4785 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004786 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004787
Josef Bacika71754f2013-06-17 17:14:39 -04004788 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304789 * If our size started in the middle of a block we need to zero out the
4790 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004791 * expose stale data.
4792 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304793 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004794 if (err)
4795 return err;
4796
Yan Zheng9036c102008-10-30 14:19:41 -04004797 if (size <= hole_start)
4798 return 0;
4799
Yan Zheng9036c102008-10-30 14:19:41 -04004800 while (1) {
4801 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004802
David Sterbaff13db42015-12-03 14:30:40 +01004803 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004804 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004805 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4806 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004807 if (!ordered)
4808 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004809 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4810 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004811 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004812 btrfs_put_ordered_extent(ordered);
4813 }
4814
Yan Zheng9036c102008-10-30 14:19:41 -04004815 cur_offset = hole_start;
4816 while (1) {
4817 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4818 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004819 if (IS_ERR(em)) {
4820 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004821 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004822 break;
4823 }
Yan Zheng9036c102008-10-30 14:19:41 -04004824 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004825 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004826 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004827 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004828 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004829
Josef Bacik16e75492013-10-22 12:18:51 -04004830 err = maybe_insert_hole(root, inode, cur_offset,
4831 hole_size);
4832 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004833 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004834 btrfs_drop_extent_cache(inode, cur_offset,
4835 cur_offset + hole_size - 1, 0);
4836 hole_em = alloc_extent_map();
4837 if (!hole_em) {
4838 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4839 &BTRFS_I(inode)->runtime_flags);
4840 goto next;
4841 }
4842 hole_em->start = cur_offset;
4843 hole_em->len = hole_size;
4844 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004845
Josef Bacik5dc562c2012-08-17 13:14:17 -04004846 hole_em->block_start = EXTENT_MAP_HOLE;
4847 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004848 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004849 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004850 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4851 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004852 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004853
4854 while (1) {
4855 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004856 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004857 write_unlock(&em_tree->lock);
4858 if (err != -EEXIST)
4859 break;
4860 btrfs_drop_extent_cache(inode, cur_offset,
4861 cur_offset +
4862 hole_size - 1, 0);
4863 }
4864 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004865 }
Josef Bacik16e75492013-10-22 12:18:51 -04004866next:
Yan Zheng9036c102008-10-30 14:19:41 -04004867 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004868 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004869 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004870 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004871 break;
4872 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004873 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004874 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4875 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004876 return err;
4877}
4878
Eric Sandeen3972f262013-01-12 02:57:22 +00004879static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004880{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004881 struct btrfs_root *root = BTRFS_I(inode)->root;
4882 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004883 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004884 loff_t newsize = attr->ia_size;
4885 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004886 int ret;
4887
Eric Sandeen3972f262013-01-12 02:57:22 +00004888 /*
4889 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4890 * special case where we need to update the times despite not having
4891 * these flags set. For all other operations the VFS set these flags
4892 * explicitly if it wants a timestamp update.
4893 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004894 if (newsize != oldsize) {
4895 inode_inc_iversion(inode);
4896 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4897 inode->i_ctime = inode->i_mtime =
4898 current_fs_time(inode->i_sb);
4899 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004900
Josef Bacika41ad392011-01-31 15:30:16 -05004901 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004902 truncate_pagecache(inode, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004903 /*
4904 * Don't do an expanding truncate while snapshoting is ongoing.
4905 * This is to ensure the snapshot captures a fully consistent
4906 * state of this file - if the snapshot captures this expanding
4907 * truncation, it must capture all writes that happened before
4908 * this truncation.
4909 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004910 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004911 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004912 if (ret) {
4913 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004914 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004915 }
Yan, Zheng80825102009-11-12 09:35:36 +00004916
Miao Xief4a2f4c2011-12-14 20:12:01 -05004917 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004918 if (IS_ERR(trans)) {
4919 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004920 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004921 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004922
4923 i_size_write(inode, newsize);
4924 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4925 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004926 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004927 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004928 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004929
Josef Bacika41ad392011-01-31 15:30:16 -05004930 /*
4931 * We're truncating a file that used to have good data down to
4932 * zero. Make sure it gets into the ordered flush list so that
4933 * any new writes get down to disk quickly.
4934 */
4935 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004936 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4937 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004938
Josef Bacikf3fe8202013-01-07 17:03:21 -05004939 /*
4940 * 1 for the orphan item we're going to add
4941 * 1 for the orphan item deletion.
4942 */
4943 trans = btrfs_start_transaction(root, 2);
4944 if (IS_ERR(trans))
4945 return PTR_ERR(trans);
4946
4947 /*
4948 * We need to do this in case we fail at _any_ point during the
4949 * actual truncate. Once we do the truncate_setsize we could
4950 * invalidate pages which forces any outstanding ordered io to
4951 * be instantly completed which will give us extents that need
4952 * to be truncated. If we fail to get an orphan inode down we
4953 * could have left over extents that were never meant to live,
4954 * so we need to garuntee from this point on that everything
4955 * will be consistent.
4956 */
4957 ret = btrfs_orphan_add(trans, inode);
4958 btrfs_end_transaction(trans, root);
4959 if (ret)
4960 return ret;
4961
Josef Bacika41ad392011-01-31 15:30:16 -05004962 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4963 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004964
4965 /* Disable nonlocked read DIO to avoid the end less truncate */
4966 btrfs_inode_block_unlocked_dio(inode);
4967 inode_dio_wait(inode);
4968 btrfs_inode_resume_unlocked_dio(inode);
4969
Josef Bacika41ad392011-01-31 15:30:16 -05004970 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004971 if (ret && inode->i_nlink) {
4972 int err;
4973
4974 /*
4975 * failed to truncate, disk_i_size is only adjusted down
4976 * as we remove extents, so it should represent the true
4977 * size of the inode, so reset the in memory size and
4978 * delete our orphan entry.
4979 */
4980 trans = btrfs_join_transaction(root);
4981 if (IS_ERR(trans)) {
4982 btrfs_orphan_del(NULL, inode);
4983 return ret;
4984 }
4985 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4986 err = btrfs_orphan_del(trans, inode);
4987 if (err)
4988 btrfs_abort_transaction(trans, root, err);
4989 btrfs_end_transaction(trans, root);
4990 }
Yan, Zheng80825102009-11-12 09:35:36 +00004991 }
4992
Josef Bacika41ad392011-01-31 15:30:16 -05004993 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004994}
4995
Chris Mason39279cc2007-06-12 06:35:45 -04004996static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4997{
David Howells2b0143b2015-03-17 22:25:59 +00004998 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004999 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005000 int err;
5001
Li Zefanb83cc962010-12-20 16:04:08 +08005002 if (btrfs_root_readonly(root))
5003 return -EROFS;
5004
Chris Mason39279cc2007-06-12 06:35:45 -04005005 err = inode_change_ok(inode, attr);
5006 if (err)
5007 return err;
5008
Chris Mason5a3f23d2009-03-31 13:27:11 -04005009 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005010 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005011 if (err)
5012 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005013 }
Yan Zheng9036c102008-10-30 14:19:41 -04005014
Christoph Hellwig10257742010-06-04 11:30:02 +02005015 if (attr->ia_valid) {
5016 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005017 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005018 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005019
Josef Bacik22c44fe2011-11-30 10:45:38 -05005020 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005021 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005022 }
5023
Chris Mason39279cc2007-06-12 06:35:45 -04005024 return err;
5025}
Chris Mason61295eb2008-01-14 16:24:38 -05005026
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005027/*
5028 * While truncating the inode pages during eviction, we get the VFS calling
5029 * btrfs_invalidatepage() against each page of the inode. This is slow because
5030 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5031 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5032 * extent_state structures over and over, wasting lots of time.
5033 *
5034 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5035 * those expensive operations on a per page basis and do only the ordered io
5036 * finishing, while we release here the extent_map and extent_state structures,
5037 * without the excessive merging and splitting.
5038 */
5039static void evict_inode_truncate_pages(struct inode *inode)
5040{
5041 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5042 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5043 struct rb_node *node;
5044
5045 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005046 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005047
5048 write_lock(&map_tree->lock);
5049 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5050 struct extent_map *em;
5051
5052 node = rb_first(&map_tree->map);
5053 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005054 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5055 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005056 remove_extent_mapping(map_tree, em);
5057 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005058 if (need_resched()) {
5059 write_unlock(&map_tree->lock);
5060 cond_resched();
5061 write_lock(&map_tree->lock);
5062 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005063 }
5064 write_unlock(&map_tree->lock);
5065
Filipe Manana6ca07092015-05-26 00:55:42 +01005066 /*
5067 * Keep looping until we have no more ranges in the io tree.
5068 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005069 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5070 * still in progress (unlocked the pages in the bio but did not yet
5071 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005072 * ranges can still be locked and eviction started because before
5073 * submitting those bios, which are executed by a separate task (work
5074 * queue kthread), inode references (inode->i_count) were not taken
5075 * (which would be dropped in the end io callback of each bio).
5076 * Therefore here we effectively end up waiting for those bios and
5077 * anyone else holding locked ranges without having bumped the inode's
5078 * reference count - if we don't do it, when they access the inode's
5079 * io_tree to unlock a range it may be too late, leading to an
5080 * use-after-free issue.
5081 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005082 spin_lock(&io_tree->lock);
5083 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5084 struct extent_state *state;
5085 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005086 u64 start;
5087 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005088
5089 node = rb_first(&io_tree->state);
5090 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005091 start = state->start;
5092 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005093 spin_unlock(&io_tree->lock);
5094
David Sterbaff13db42015-12-03 14:30:40 +01005095 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005096
5097 /*
5098 * If still has DELALLOC flag, the extent didn't reach disk,
5099 * and its reserved space won't be freed by delayed_ref.
5100 * So we need to free its reserved space here.
5101 * (Refer to comment in btrfs_invalidatepage, case 2)
5102 *
5103 * Note, end is the bytenr of last byte, so we need + 1 here.
5104 */
5105 if (state->state & EXTENT_DELALLOC)
5106 btrfs_qgroup_free_data(inode, start, end - start + 1);
5107
Filipe Manana6ca07092015-05-26 00:55:42 +01005108 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005109 EXTENT_LOCKED | EXTENT_DIRTY |
5110 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5111 EXTENT_DEFRAG, 1, 1,
5112 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005113
Filipe Manana7064dd52014-08-08 02:47:05 +01005114 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005115 spin_lock(&io_tree->lock);
5116 }
5117 spin_unlock(&io_tree->lock);
5118}
5119
Al Virobd555972010-06-07 11:35:40 -04005120void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005121{
5122 struct btrfs_trans_handle *trans;
5123 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005124 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005125 int steal_from_global = 0;
Josef Bacik07127182011-08-19 10:29:59 -04005126 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04005127 int ret;
5128
liubo1abe9b82011-03-24 11:18:59 +00005129 trace_btrfs_inode_evict(inode);
5130
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005131 evict_inode_truncate_pages(inode);
5132
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005133 if (inode->i_nlink &&
5134 ((btrfs_root_refs(&root->root_item) != 0 &&
5135 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5136 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005137 goto no_delete;
5138
Chris Mason39279cc2007-06-12 06:35:45 -04005139 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005140 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005141 goto no_delete;
5142 }
Al Virobd555972010-06-07 11:35:40 -04005143 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005144 if (!special_file(inode->i_mode))
5145 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005146
Miao Xief6124962014-09-12 18:44:04 +08005147 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5148
Yan, Zhengc71bf092009-11-12 09:34:40 +00005149 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06005150 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005151 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005152 goto no_delete;
5153 }
5154
Yan, Zheng76dda932009-09-21 16:00:26 -04005155 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005156 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5157 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005158 goto no_delete;
5159 }
5160
Miao Xie0e8c36a2012-12-19 06:59:51 +00005161 ret = btrfs_commit_inode_delayed_inode(inode);
5162 if (ret) {
5163 btrfs_orphan_del(NULL, inode);
5164 goto no_delete;
5165 }
5166
Miao Xie66d8f3d2012-09-06 04:02:28 -06005167 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005168 if (!rsv) {
5169 btrfs_orphan_del(NULL, inode);
5170 goto no_delete;
5171 }
Josef Bacik4a338542011-08-29 11:01:31 -04005172 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005173 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005174 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005175
Chris Masondbe674a2008-07-17 12:54:05 -04005176 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005177
Josef Bacik4289a662011-08-05 13:22:24 -04005178 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005179 * This is a bit simpler than btrfs_truncate since we've already
5180 * reserved our space for our orphan item in the unlink, so we just
5181 * need to reserve some slack space in case we add bytes and update
5182 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005183 */
Yan, Zheng80825102009-11-12 09:35:36 +00005184 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005185 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5186 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005187
Josef Bacik726c35f2011-09-26 15:46:06 -04005188 /*
5189 * Try and steal from the global reserve since we will
5190 * likely not use this space anyway, we want to try as
5191 * hard as possible to get this to work.
5192 */
5193 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005194 steal_from_global++;
5195 else
5196 steal_from_global = 0;
5197 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005198
Josef Bacik3bce8762015-02-24 12:35:51 -08005199 /*
5200 * steal_from_global == 0: we reserved stuff, hooray!
5201 * steal_from_global == 1: we didn't reserve stuff, boo!
5202 * steal_from_global == 2: we've committed, still not a lot of
5203 * room but maybe we'll have room in the global reserve this
5204 * time.
5205 * steal_from_global == 3: abandon all hope!
5206 */
5207 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005208 btrfs_warn(root->fs_info,
5209 "Could not get space for a delete, will truncate on mount %d",
5210 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005211 btrfs_orphan_del(NULL, inode);
5212 btrfs_free_block_rsv(root, rsv);
5213 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005214 }
5215
Miao Xie0e8c36a2012-12-19 06:59:51 +00005216 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005217 if (IS_ERR(trans)) {
5218 btrfs_orphan_del(NULL, inode);
5219 btrfs_free_block_rsv(root, rsv);
5220 goto no_delete;
5221 }
5222
Josef Bacik3bce8762015-02-24 12:35:51 -08005223 /*
5224 * We can't just steal from the global reserve, we need tomake
5225 * sure there is room to do it, if not we need to commit and try
5226 * again.
5227 */
5228 if (steal_from_global) {
5229 if (!btrfs_check_space_for_delayed_refs(trans, root))
5230 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5231 min_size);
5232 else
5233 ret = -ENOSPC;
5234 }
5235
5236 /*
5237 * Couldn't steal from the global reserve, we have too much
5238 * pending stuff built up, commit the transaction and try it
5239 * again.
5240 */
5241 if (ret) {
5242 ret = btrfs_commit_transaction(trans, root);
5243 if (ret) {
5244 btrfs_orphan_del(NULL, inode);
5245 btrfs_free_block_rsv(root, rsv);
5246 goto no_delete;
5247 }
5248 continue;
5249 } else {
5250 steal_from_global = 0;
5251 }
5252
Josef Bacik4289a662011-08-05 13:22:24 -04005253 trans->block_rsv = rsv;
5254
Yan, Zhengd68fc572010-05-16 10:49:58 -04005255 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005256 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005257 break;
5258
Miao Xie8407aa42012-09-07 01:43:32 -06005259 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005260 btrfs_end_transaction(trans, root);
5261 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005262 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005263 }
5264
Josef Bacik4289a662011-08-05 13:22:24 -04005265 btrfs_free_block_rsv(root, rsv);
5266
Josef Bacik4ef31a42013-08-13 14:10:08 -04005267 /*
5268 * Errors here aren't a big deal, it just means we leave orphan items
5269 * in the tree. They will be cleaned up on the next mount.
5270 */
Yan, Zheng80825102009-11-12 09:35:36 +00005271 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005272 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005273 btrfs_orphan_del(trans, inode);
5274 } else {
5275 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005276 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005277
Josef Bacik4289a662011-08-05 13:22:24 -04005278 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005279 if (!(root == root->fs_info->tree_root ||
5280 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005281 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005282
Chris Mason54aa1f42007-06-22 14:16:25 -04005283 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005284 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005285no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005286 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005287 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005288}
5289
5290/*
5291 * this returns the key found in the dir entry in the location pointer.
5292 * If no dir entries were found, location->objectid is 0.
5293 */
5294static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5295 struct btrfs_key *location)
5296{
5297 const char *name = dentry->d_name.name;
5298 int namelen = dentry->d_name.len;
5299 struct btrfs_dir_item *di;
5300 struct btrfs_path *path;
5301 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005302 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005303
5304 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005305 if (!path)
5306 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005307
Li Zefan33345d012011-04-20 10:31:50 +08005308 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005309 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005310 if (IS_ERR(di))
5311 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005312
David Sterbac7040052011-04-19 18:00:01 +02005313 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005314 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005315
Chris Mason5f39d392007-10-15 16:14:19 -04005316 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005317out:
Chris Mason39279cc2007-06-12 06:35:45 -04005318 btrfs_free_path(path);
5319 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005320out_err:
5321 location->objectid = 0;
5322 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005323}
5324
5325/*
5326 * when we hit a tree root in a directory, the btrfs part of the inode
5327 * needs to be changed to reflect the root directory of the tree root. This
5328 * is kind of like crossing a mount point.
5329 */
5330static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005331 struct inode *dir,
5332 struct dentry *dentry,
5333 struct btrfs_key *location,
5334 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005335{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005336 struct btrfs_path *path;
5337 struct btrfs_root *new_root;
5338 struct btrfs_root_ref *ref;
5339 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005340 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005341 int ret;
5342 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005343
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005344 path = btrfs_alloc_path();
5345 if (!path) {
5346 err = -ENOMEM;
5347 goto out;
5348 }
Chris Mason39279cc2007-06-12 06:35:45 -04005349
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005350 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005351 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5352 key.type = BTRFS_ROOT_REF_KEY;
5353 key.offset = location->objectid;
5354
5355 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5356 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005357 if (ret) {
5358 if (ret < 0)
5359 err = ret;
5360 goto out;
5361 }
Chris Mason39279cc2007-06-12 06:35:45 -04005362
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005363 leaf = path->nodes[0];
5364 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005365 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005366 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5367 goto out;
5368
5369 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5370 (unsigned long)(ref + 1),
5371 dentry->d_name.len);
5372 if (ret)
5373 goto out;
5374
David Sterbab3b4aa72011-04-21 01:20:15 +02005375 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005376
5377 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5378 if (IS_ERR(new_root)) {
5379 err = PTR_ERR(new_root);
5380 goto out;
5381 }
5382
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005383 *sub_root = new_root;
5384 location->objectid = btrfs_root_dirid(&new_root->root_item);
5385 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005386 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005387 err = 0;
5388out:
5389 btrfs_free_path(path);
5390 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005391}
5392
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005393static void inode_tree_add(struct inode *inode)
5394{
5395 struct btrfs_root *root = BTRFS_I(inode)->root;
5396 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005397 struct rb_node **p;
5398 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005399 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005400 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005401
Al Viro1d3382cb2010-10-23 15:19:20 -04005402 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005403 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005404 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005405 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005406 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005407 while (*p) {
5408 parent = *p;
5409 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5410
Li Zefan33345d012011-04-20 10:31:50 +08005411 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005412 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005413 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005414 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005415 else {
5416 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005417 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005418 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005419 RB_CLEAR_NODE(parent);
5420 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005421 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005422 }
5423 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005424 rb_link_node(new, parent, p);
5425 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005426 spin_unlock(&root->inode_lock);
5427}
5428
5429static void inode_tree_del(struct inode *inode)
5430{
5431 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005432 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005433
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005434 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005435 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005436 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005437 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005438 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005439 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005440 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005441
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005442 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005443 synchronize_srcu(&root->fs_info->subvol_srcu);
5444 spin_lock(&root->inode_lock);
5445 empty = RB_EMPTY_ROOT(&root->inode_tree);
5446 spin_unlock(&root->inode_lock);
5447 if (empty)
5448 btrfs_add_dead_root(root);
5449 }
5450}
5451
Jeff Mahoney143bede2012-03-01 14:56:26 +01005452void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005453{
5454 struct rb_node *node;
5455 struct rb_node *prev;
5456 struct btrfs_inode *entry;
5457 struct inode *inode;
5458 u64 objectid = 0;
5459
Liu Bo7813b3d2014-02-10 17:37:25 +08005460 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5461 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005462
5463 spin_lock(&root->inode_lock);
5464again:
5465 node = root->inode_tree.rb_node;
5466 prev = NULL;
5467 while (node) {
5468 prev = node;
5469 entry = rb_entry(node, struct btrfs_inode, rb_node);
5470
Li Zefan33345d012011-04-20 10:31:50 +08005471 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005472 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005473 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005474 node = node->rb_right;
5475 else
5476 break;
5477 }
5478 if (!node) {
5479 while (prev) {
5480 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005481 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005482 node = prev;
5483 break;
5484 }
5485 prev = rb_next(prev);
5486 }
5487 }
5488 while (node) {
5489 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005490 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005491 inode = igrab(&entry->vfs_inode);
5492 if (inode) {
5493 spin_unlock(&root->inode_lock);
5494 if (atomic_read(&inode->i_count) > 1)
5495 d_prune_aliases(inode);
5496 /*
Al Viro45321ac2010-06-07 13:43:19 -04005497 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005498 * the inode cache when its usage count
5499 * hits zero.
5500 */
5501 iput(inode);
5502 cond_resched();
5503 spin_lock(&root->inode_lock);
5504 goto again;
5505 }
5506
5507 if (cond_resched_lock(&root->inode_lock))
5508 goto again;
5509
5510 node = rb_next(node);
5511 }
5512 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005513}
5514
Chris Masone02119d2008-09-05 16:13:11 -04005515static int btrfs_init_locked_inode(struct inode *inode, void *p)
5516{
5517 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005518 inode->i_ino = args->location->objectid;
5519 memcpy(&BTRFS_I(inode)->location, args->location,
5520 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005521 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005522 return 0;
5523}
5524
5525static int btrfs_find_actor(struct inode *inode, void *opaque)
5526{
5527 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005528 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005529 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005530}
5531
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005532static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005533 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005534 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005535{
5536 struct inode *inode;
5537 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005538 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005539
Chris Mason90d3e592014-01-09 17:28:00 -08005540 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005541 args.root = root;
5542
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005543 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005544 btrfs_init_locked_inode,
5545 (void *)&args);
5546 return inode;
5547}
5548
Balaji Rao1a54ef82008-07-21 02:01:04 +05305549/* Get an inode object given its location and corresponding root.
5550 * Returns in *is_new if the inode was read from disk
5551 */
5552struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005553 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305554{
5555 struct inode *inode;
5556
Chris Mason90d3e592014-01-09 17:28:00 -08005557 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305558 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005559 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305560
5561 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305562 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005563 if (!is_bad_inode(inode)) {
5564 inode_tree_add(inode);
5565 unlock_new_inode(inode);
5566 if (new)
5567 *new = 1;
5568 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005569 unlock_new_inode(inode);
5570 iput(inode);
5571 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005572 }
5573 }
5574
Balaji Rao1a54ef82008-07-21 02:01:04 +05305575 return inode;
5576}
5577
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005578static struct inode *new_simple_dir(struct super_block *s,
5579 struct btrfs_key *key,
5580 struct btrfs_root *root)
5581{
5582 struct inode *inode = new_inode(s);
5583
5584 if (!inode)
5585 return ERR_PTR(-ENOMEM);
5586
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005587 BTRFS_I(inode)->root = root;
5588 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005589 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005590
5591 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005592 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005593 inode->i_fop = &simple_dir_operations;
5594 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
chandan r9cc97d62012-07-04 12:48:07 +05305595 inode->i_mtime = CURRENT_TIME;
5596 inode->i_atime = inode->i_mtime;
5597 inode->i_ctime = inode->i_mtime;
5598 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005599
5600 return inode;
5601}
5602
Chris Mason3de45862008-11-17 21:02:50 -05005603struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005604{
Chris Masond3977122009-01-05 21:25:51 -05005605 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005606 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005607 struct btrfs_root *sub_root = root;
5608 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005609 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005610 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005611
5612 if (dentry->d_name.len > BTRFS_NAME_LEN)
5613 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005614
Jeff Layton39e3c952012-11-28 11:30:53 -05005615 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005616 if (ret < 0)
5617 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005618
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005619 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005620 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005621
5622 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005623 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005624 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005625 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005626
5627 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5628
Yan, Zheng76dda932009-09-21 16:00:26 -04005629 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005630 ret = fixup_tree_root_location(root, dir, dentry,
5631 &location, &sub_root);
5632 if (ret < 0) {
5633 if (ret != -ENOENT)
5634 inode = ERR_PTR(ret);
5635 else
5636 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5637 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005638 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005639 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005640 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5641
Julia Lawall34d19ba2011-01-24 19:55:19 +00005642 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005643 down_read(&root->fs_info->cleanup_work_sem);
5644 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005645 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005646 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005647 if (ret) {
5648 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005649 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005650 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005651 }
5652
Chris Mason3de45862008-11-17 21:02:50 -05005653 return inode;
5654}
5655
Nick Pigginfe15ce42011-01-07 17:49:23 +11005656static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005657{
5658 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005659 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005660
Li Zefan848cce02012-02-21 17:04:28 +08005661 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005662 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005663
Li Zefan848cce02012-02-21 17:04:28 +08005664 if (inode) {
5665 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005666 if (btrfs_root_refs(&root->root_item) == 0)
5667 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005668
5669 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5670 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005671 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005672 return 0;
5673}
5674
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005675static void btrfs_dentry_release(struct dentry *dentry)
5676{
Daeseok Youn944a4512014-04-14 15:37:02 +09005677 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005678}
5679
Chris Mason3de45862008-11-17 21:02:50 -05005680static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005681 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005682{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005683 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005684
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005685 inode = btrfs_lookup_dentry(dir, dentry);
5686 if (IS_ERR(inode)) {
5687 if (PTR_ERR(inode) == -ENOENT)
5688 inode = NULL;
5689 else
5690 return ERR_CAST(inode);
5691 }
5692
Al Viro41d28bc2014-10-12 22:24:21 -04005693 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005694}
5695
Miao Xie16cdcec2011-04-22 18:12:22 +08005696unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005697 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5698};
5699
Al Viro9cdda8d2013-05-22 16:48:09 -04005700static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005701{
Al Viro9cdda8d2013-05-22 16:48:09 -04005702 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005703 struct btrfs_root *root = BTRFS_I(inode)->root;
5704 struct btrfs_item *item;
5705 struct btrfs_dir_item *di;
5706 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005707 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005708 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005709 struct list_head ins_list;
5710 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005711 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005712 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005713 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005714 unsigned char d_type;
5715 int over = 0;
5716 u32 di_cur;
5717 u32 di_total;
5718 u32 di_len;
5719 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005720 char tmp_name[32];
5721 char *name_ptr;
5722 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005723 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005724
5725 /* FIXME, use a real flag for deciding about the key type */
5726 if (root->fs_info->tree_root == root)
5727 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005728
Al Viro9cdda8d2013-05-22 16:48:09 -04005729 if (!dir_emit_dots(file, ctx))
5730 return 0;
5731
David Woodhouse49593bf2008-08-17 17:08:36 +01005732 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005733 if (!path)
5734 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005735
David Sterbae4058b52015-11-27 16:31:35 +01005736 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005737
Miao Xie16cdcec2011-04-22 18:12:22 +08005738 if (key_type == BTRFS_DIR_INDEX_KEY) {
5739 INIT_LIST_HEAD(&ins_list);
5740 INIT_LIST_HEAD(&del_list);
5741 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5742 }
5743
David Sterba962a2982014-06-04 18:41:45 +02005744 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005745 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005746 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005747
Chris Mason39279cc2007-06-12 06:35:45 -04005748 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5749 if (ret < 0)
5750 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005751
5752 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005753 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005754 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005755 if (slot >= btrfs_header_nritems(leaf)) {
5756 ret = btrfs_next_leaf(root, path);
5757 if (ret < 0)
5758 goto err;
5759 else if (ret > 0)
5760 break;
5761 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005762 }
Chris Mason3de45862008-11-17 21:02:50 -05005763
Ross Kirkdd3cc162013-09-16 15:58:09 +01005764 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005765 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5766
5767 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005768 break;
David Sterba962a2982014-06-04 18:41:45 +02005769 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005770 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005771 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005772 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005773 if (key_type == BTRFS_DIR_INDEX_KEY &&
5774 btrfs_should_delete_dir_index(&del_list,
5775 found_key.offset))
5776 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005777
Al Viro9cdda8d2013-05-22 16:48:09 -04005778 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005779 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005780
Chris Mason39279cc2007-06-12 06:35:45 -04005781 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5782 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005783 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005784
5785 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005786 struct btrfs_key location;
5787
Josef Bacik22a94d42011-03-16 16:47:17 -04005788 if (verify_dir_item(root, leaf, di))
5789 break;
5790
Chris Mason5f39d392007-10-15 16:14:19 -04005791 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005792 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005793 name_ptr = tmp_name;
5794 } else {
5795 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005796 if (!name_ptr) {
5797 ret = -ENOMEM;
5798 goto err;
5799 }
Chris Mason5f39d392007-10-15 16:14:19 -04005800 }
5801 read_extent_buffer(leaf, name_ptr,
5802 (unsigned long)(di + 1), name_len);
5803
5804 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5805 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005806
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005807
Chris Mason3de45862008-11-17 21:02:50 -05005808 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005809 * skip it.
5810 *
5811 * In contrast to old kernels, we insert the snapshot's
5812 * dir item and dir index after it has been created, so
5813 * we won't find a reference to our own snapshot. We
5814 * still keep the following code for backward
5815 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005816 */
5817 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5818 location.objectid == root->root_key.objectid) {
5819 over = 0;
5820 goto skip;
5821 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005822 over = !dir_emit(ctx, name_ptr, name_len,
5823 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005824
Chris Mason3de45862008-11-17 21:02:50 -05005825skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005826 if (name_ptr != tmp_name)
5827 kfree(name_ptr);
5828
Chris Mason39279cc2007-06-12 06:35:45 -04005829 if (over)
5830 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005831 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005832 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005833 di_cur += di_len;
5834 di = (struct btrfs_dir_item *)((char *)di + di_len);
5835 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005836next:
5837 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005838 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005839
Miao Xie16cdcec2011-04-22 18:12:22 +08005840 if (key_type == BTRFS_DIR_INDEX_KEY) {
5841 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005842 ctx->pos++;
5843 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005844 if (ret)
5845 goto nopos;
5846 }
5847
David Woodhouse49593bf2008-08-17 17:08:36 +01005848 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005849 ctx->pos++;
5850
5851 /*
5852 * Stop new entries from being returned after we return the last
5853 * entry.
5854 *
5855 * New directory entries are assigned a strictly increasing
5856 * offset. This means that new entries created during readdir
5857 * are *guaranteed* to be seen in the future by that readdir.
5858 * This has broken buggy programs which operate on names as
5859 * they're returned by readdir. Until we re-use freed offsets
5860 * we have this hack to stop new entries from being returned
5861 * under the assumption that they'll never reach this huge
5862 * offset.
5863 *
5864 * This is being careful not to overflow 32bit loff_t unless the
5865 * last entry requires it because doing so has broken 32bit apps
5866 * in the past.
5867 */
5868 if (key_type == BTRFS_DIR_INDEX_KEY) {
5869 if (ctx->pos >= INT_MAX)
5870 ctx->pos = LLONG_MAX;
5871 else
5872 ctx->pos = INT_MAX;
5873 }
Chris Mason39279cc2007-06-12 06:35:45 -04005874nopos:
5875 ret = 0;
5876err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005877 if (key_type == BTRFS_DIR_INDEX_KEY)
5878 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005879 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005880 return ret;
5881}
5882
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005883int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005884{
5885 struct btrfs_root *root = BTRFS_I(inode)->root;
5886 struct btrfs_trans_handle *trans;
5887 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005888 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005889
Josef Bacik72ac3c02012-05-23 14:13:11 -04005890 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005891 return 0;
5892
Liu Bo83eea1f2012-07-10 05:28:39 -06005893 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005894 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005895
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005896 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005897 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005898 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005899 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005900 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005901 if (IS_ERR(trans))
5902 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005903 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005904 }
5905 return ret;
5906}
5907
5908/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005909 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005910 * inode changes. But, it is most likely to find the inode in cache.
5911 * FIXME, needs more benchmarking...there are no reasons other than performance
5912 * to keep or drop this code.
5913 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005914static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005915{
5916 struct btrfs_root *root = BTRFS_I(inode)->root;
5917 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005918 int ret;
5919
Josef Bacik72ac3c02012-05-23 14:13:11 -04005920 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005921 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005922
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005923 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005924 if (IS_ERR(trans))
5925 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005926
5927 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005928 if (ret && ret == -ENOSPC) {
5929 /* whoops, lets try again with the full transaction */
5930 btrfs_end_transaction(trans, root);
5931 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005932 if (IS_ERR(trans))
5933 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005934
Chris Mason94b60442010-05-26 11:02:00 -04005935 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005936 }
Chris Mason39279cc2007-06-12 06:35:45 -04005937 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005938 if (BTRFS_I(inode)->delayed_node)
5939 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005940
5941 return ret;
5942}
5943
5944/*
5945 * This is a copy of file_update_time. We need this so we can return error on
5946 * ENOSPC for updating the inode in the case of file write and mmap writes.
5947 */
Josef Bacike41f9412012-03-26 09:46:47 -04005948static int btrfs_update_time(struct inode *inode, struct timespec *now,
5949 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005950{
Alexander Block2bc5565282012-06-15 09:49:33 +02005951 struct btrfs_root *root = BTRFS_I(inode)->root;
5952
5953 if (btrfs_root_readonly(root))
5954 return -EROFS;
5955
Josef Bacike41f9412012-03-26 09:46:47 -04005956 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005957 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005958 if (flags & S_CTIME)
5959 inode->i_ctime = *now;
5960 if (flags & S_MTIME)
5961 inode->i_mtime = *now;
5962 if (flags & S_ATIME)
5963 inode->i_atime = *now;
5964 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005965}
5966
Chris Masond352ac62008-09-29 15:18:18 -04005967/*
5968 * find the highest existing sequence number in a directory
5969 * and then set the in-memory index_cnt variable to reflect
5970 * free sequence numbers
5971 */
Josef Bacikaec74772008-07-24 12:12:38 -04005972static int btrfs_set_inode_index_count(struct inode *inode)
5973{
5974 struct btrfs_root *root = BTRFS_I(inode)->root;
5975 struct btrfs_key key, found_key;
5976 struct btrfs_path *path;
5977 struct extent_buffer *leaf;
5978 int ret;
5979
Li Zefan33345d012011-04-20 10:31:50 +08005980 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005981 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005982 key.offset = (u64)-1;
5983
5984 path = btrfs_alloc_path();
5985 if (!path)
5986 return -ENOMEM;
5987
5988 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5989 if (ret < 0)
5990 goto out;
5991 /* FIXME: we should be able to handle this */
5992 if (ret == 0)
5993 goto out;
5994 ret = 0;
5995
5996 /*
5997 * MAGIC NUMBER EXPLANATION:
5998 * since we search a directory based on f_pos we have to start at 2
5999 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6000 * else has to start at 2
6001 */
6002 if (path->slots[0] == 0) {
6003 BTRFS_I(inode)->index_cnt = 2;
6004 goto out;
6005 }
6006
6007 path->slots[0]--;
6008
6009 leaf = path->nodes[0];
6010 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6011
Li Zefan33345d012011-04-20 10:31:50 +08006012 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006013 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006014 BTRFS_I(inode)->index_cnt = 2;
6015 goto out;
6016 }
6017
6018 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6019out:
6020 btrfs_free_path(path);
6021 return ret;
6022}
6023
Chris Masond352ac62008-09-29 15:18:18 -04006024/*
6025 * helper to find a free sequence number in a given directory. This current
6026 * code is very simple, later versions will do smarter things in the btree
6027 */
Chris Mason3de45862008-11-17 21:02:50 -05006028int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006029{
6030 int ret = 0;
6031
6032 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006033 ret = btrfs_inode_delayed_dir_index_count(dir);
6034 if (ret) {
6035 ret = btrfs_set_inode_index_count(dir);
6036 if (ret)
6037 return ret;
6038 }
Josef Bacikaec74772008-07-24 12:12:38 -04006039 }
6040
Chris Mason00e4e6b2008-08-05 11:18:09 -04006041 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006042 BTRFS_I(dir)->index_cnt++;
6043
6044 return ret;
6045}
6046
Chris Masonb0d5d102014-09-08 13:08:51 -07006047static int btrfs_insert_inode_locked(struct inode *inode)
6048{
6049 struct btrfs_iget_args args;
6050 args.location = &BTRFS_I(inode)->location;
6051 args.root = BTRFS_I(inode)->root;
6052
6053 return insert_inode_locked4(inode,
6054 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6055 btrfs_find_actor, &args);
6056}
6057
Chris Mason39279cc2007-06-12 06:35:45 -04006058static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6059 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006060 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006061 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006062 u64 ref_objectid, u64 objectid,
6063 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006064{
6065 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006066 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006067 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006068 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006069 struct btrfs_inode_ref *ref;
6070 struct btrfs_key key[2];
6071 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006072 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006073 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006074 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006075
Chris Mason5f39d392007-10-15 16:14:19 -04006076 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006077 if (!path)
6078 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006079
Chris Mason39279cc2007-06-12 06:35:45 -04006080 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006081 if (!inode) {
6082 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006083 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006084 }
Chris Mason39279cc2007-06-12 06:35:45 -04006085
Li Zefan581bb052011-04-20 10:06:11 +08006086 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006087 * O_TMPFILE, set link count to 0, so that after this point,
6088 * we fill in an inode item with the correct link count.
6089 */
6090 if (!name)
6091 set_nlink(inode, 0);
6092
6093 /*
Li Zefan581bb052011-04-20 10:06:11 +08006094 * we have to initialize this early, so we can reclaim the inode
6095 * number if we fail afterwards in this function.
6096 */
6097 inode->i_ino = objectid;
6098
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006099 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006100 trace_btrfs_inode_request(dir);
6101
Chris Mason3de45862008-11-17 21:02:50 -05006102 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006103 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006104 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006105 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006106 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006107 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006108 } else if (dir) {
6109 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006110 }
6111 /*
6112 * index_cnt is ignored for everything but a dir,
6113 * btrfs_get_inode_index_count has an explanation for the magic
6114 * number
6115 */
6116 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006117 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006118 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006119 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006120 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006121
Josef Bacik5dc562c2012-08-17 13:14:17 -04006122 /*
6123 * We could have gotten an inode number from somebody who was fsynced
6124 * and then removed in this same transaction, so let's just set full
6125 * sync since it will be a full sync anyway and this will blow away the
6126 * old info in the log.
6127 */
6128 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6129
Chris Mason9c583092008-01-29 15:15:18 -05006130 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006131 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006132 key[0].offset = 0;
6133
Chris Mason9c583092008-01-29 15:15:18 -05006134 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006135
6136 if (name) {
6137 /*
6138 * Start new inodes with an inode_ref. This is slightly more
6139 * efficient for small numbers of hard links since they will
6140 * be packed into one item. Extended refs will kick in if we
6141 * add more hard links than can fit in the ref item.
6142 */
6143 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006144 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006145 key[1].offset = ref_objectid;
6146
6147 sizes[1] = name_len + sizeof(*ref);
6148 }
Chris Mason9c583092008-01-29 15:15:18 -05006149
Chris Masonb0d5d102014-09-08 13:08:51 -07006150 location = &BTRFS_I(inode)->location;
6151 location->objectid = objectid;
6152 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006153 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006154
6155 ret = btrfs_insert_inode_locked(inode);
6156 if (ret < 0)
6157 goto fail;
6158
Chris Masonb9473432009-03-13 11:00:37 -04006159 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006160 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006161 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006162 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006163
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006164 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006165 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306166
6167 inode->i_mtime = CURRENT_TIME;
6168 inode->i_atime = inode->i_mtime;
6169 inode->i_ctime = inode->i_mtime;
6170 BTRFS_I(inode)->i_otime = inode->i_mtime;
6171
Chris Mason5f39d392007-10-15 16:14:19 -04006172 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6173 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006174 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6175 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006176 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006177
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006178 if (name) {
6179 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6180 struct btrfs_inode_ref);
6181 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6182 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6183 ptr = (unsigned long)(ref + 1);
6184 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6185 }
Chris Mason9c583092008-01-29 15:15:18 -05006186
Chris Mason5f39d392007-10-15 16:14:19 -04006187 btrfs_mark_buffer_dirty(path->nodes[0]);
6188 btrfs_free_path(path);
6189
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006190 btrfs_inherit_iflags(inode, dir);
6191
Al Viro569254b2011-07-24 17:08:40 -04006192 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04006193 if (btrfs_test_opt(root, NODATASUM))
6194 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06006195 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006196 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6197 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006198 }
6199
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006200 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006201
6202 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006203 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006204
Alexander Block8ea05e32012-07-25 17:35:53 +02006205 btrfs_update_root_times(trans, root);
6206
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006207 ret = btrfs_inode_inherit_props(trans, inode, dir);
6208 if (ret)
6209 btrfs_err(root->fs_info,
6210 "error inheriting props for ino %llu (root %llu): %d",
6211 btrfs_ino(inode), root->root_key.objectid, ret);
6212
Chris Mason39279cc2007-06-12 06:35:45 -04006213 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006214
6215fail_unlock:
6216 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006217fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006218 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006219 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006220 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006221 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006222 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006223}
6224
6225static inline u8 btrfs_inode_type(struct inode *inode)
6226{
6227 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6228}
6229
Chris Masond352ac62008-09-29 15:18:18 -04006230/*
6231 * utility function to add 'inode' into 'parent_inode' with
6232 * a give name and a given sequence number.
6233 * if 'add_backref' is true, also insert a backref from the
6234 * inode to the parent directory.
6235 */
Chris Masone02119d2008-09-05 16:13:11 -04006236int btrfs_add_link(struct btrfs_trans_handle *trans,
6237 struct inode *parent_inode, struct inode *inode,
6238 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006239{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006240 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006241 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006242 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006243 u64 ino = btrfs_ino(inode);
6244 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006245
Li Zefan33345d012011-04-20 10:31:50 +08006246 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006247 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6248 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006249 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006250 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006251 key.offset = 0;
6252 }
Chris Mason39279cc2007-06-12 06:35:45 -04006253
Li Zefan33345d012011-04-20 10:31:50 +08006254 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006255 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6256 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006257 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006258 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006259 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6260 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006261 }
6262
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006263 /* Nothing to clean up yet */
6264 if (ret)
6265 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006266
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006267 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6268 parent_inode, &key,
6269 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006270 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006271 goto fail_dir_item;
6272 else if (ret) {
6273 btrfs_abort_transaction(trans, root, ret);
6274 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006275 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006276
6277 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6278 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006279 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006280 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
6281 ret = btrfs_update_inode(trans, root, parent_inode);
6282 if (ret)
6283 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006284 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006285
6286fail_dir_item:
6287 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6288 u64 local_index;
6289 int err;
6290 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6291 key.objectid, root->root_key.objectid,
6292 parent_ino, &local_index, name, name_len);
6293
6294 } else if (add_backref) {
6295 u64 local_index;
6296 int err;
6297
6298 err = btrfs_del_inode_ref(trans, root, name, name_len,
6299 ino, parent_ino, &local_index);
6300 }
6301 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006302}
6303
6304static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006305 struct inode *dir, struct dentry *dentry,
6306 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006307{
Josef Bacika1b075d2010-11-19 20:36:11 +00006308 int err = btrfs_add_link(trans, dir, inode,
6309 dentry->d_name.name, dentry->d_name.len,
6310 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006311 if (err > 0)
6312 err = -EEXIST;
6313 return err;
6314}
6315
Josef Bacik618e21d2007-07-11 10:18:17 -04006316static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006317 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006318{
6319 struct btrfs_trans_handle *trans;
6320 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006321 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006322 int err;
6323 int drop_inode = 0;
6324 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006325 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006326
Josef Bacik9ed74f22009-09-11 16:12:44 -04006327 /*
6328 * 2 for inode item and ref
6329 * 2 for dir items
6330 * 1 for xattr if selinux is on
6331 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006332 trans = btrfs_start_transaction(root, 5);
6333 if (IS_ERR(trans))
6334 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006335
Li Zefan581bb052011-04-20 10:06:11 +08006336 err = btrfs_find_free_ino(root, &objectid);
6337 if (err)
6338 goto out_unlock;
6339
Josef Bacikaec74772008-07-24 12:12:38 -04006340 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006341 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006342 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006343 if (IS_ERR(inode)) {
6344 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006345 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006346 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006347
Casey Schauflerad19db72011-12-15 10:09:07 -05006348 /*
6349 * If the active LSM wants to access the inode during
6350 * d_instantiate it needs these. Smack checks to see
6351 * if the filesystem supports xattrs by looking at the
6352 * ops vector.
6353 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006354 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006355 init_special_inode(inode, inode->i_mode, rdev);
6356
6357 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006358 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006359 goto out_unlock_inode;
6360
6361 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6362 if (err) {
6363 goto out_unlock_inode;
6364 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006365 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006366 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006367 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006368 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006369
Josef Bacik618e21d2007-07-11 10:18:17 -04006370out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006371 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006372 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006373 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006374 if (drop_inode) {
6375 inode_dec_link_count(inode);
6376 iput(inode);
6377 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006378 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006379
6380out_unlock_inode:
6381 drop_inode = 1;
6382 unlock_new_inode(inode);
6383 goto out_unlock;
6384
Josef Bacik618e21d2007-07-11 10:18:17 -04006385}
6386
Chris Mason39279cc2007-06-12 06:35:45 -04006387static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006388 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006389{
6390 struct btrfs_trans_handle *trans;
6391 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006392 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006393 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006394 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006395 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006396 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006397
Josef Bacik9ed74f22009-09-11 16:12:44 -04006398 /*
6399 * 2 for inode item and ref
6400 * 2 for dir items
6401 * 1 for xattr if selinux is on
6402 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006403 trans = btrfs_start_transaction(root, 5);
6404 if (IS_ERR(trans))
6405 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006406
Li Zefan581bb052011-04-20 10:06:11 +08006407 err = btrfs_find_free_ino(root, &objectid);
6408 if (err)
6409 goto out_unlock;
6410
Josef Bacikaec74772008-07-24 12:12:38 -04006411 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006412 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006413 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006414 if (IS_ERR(inode)) {
6415 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006416 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006417 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006418 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006419 /*
6420 * If the active LSM wants to access the inode during
6421 * d_instantiate it needs these. Smack checks to see
6422 * if the filesystem supports xattrs by looking at the
6423 * ops vector.
6424 */
6425 inode->i_fop = &btrfs_file_operations;
6426 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006427 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006428
6429 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6430 if (err)
6431 goto out_unlock_inode;
6432
6433 err = btrfs_update_inode(trans, root, inode);
6434 if (err)
6435 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006436
Josef Bacika1b075d2010-11-19 20:36:11 +00006437 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006438 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006439 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006440
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006441 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006442 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006443 d_instantiate(dentry, inode);
6444
Chris Mason39279cc2007-06-12 06:35:45 -04006445out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006446 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006447 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006448 inode_dec_link_count(inode);
6449 iput(inode);
6450 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006451 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006452 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006453 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006454
6455out_unlock_inode:
6456 unlock_new_inode(inode);
6457 goto out_unlock;
6458
Chris Mason39279cc2007-06-12 06:35:45 -04006459}
6460
6461static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6462 struct dentry *dentry)
6463{
Filipe Manana271dba42016-01-05 16:24:05 +00006464 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006465 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006466 struct inode *inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006467 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006468 int err;
6469 int drop_inode = 0;
6470
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006471 /* do not allow sys_link's with other subvols of the same device */
6472 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006473 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006474
Mark Fashehf1863732012-08-08 11:32:27 -07006475 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006476 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006477
Chris Mason3de45862008-11-17 21:02:50 -05006478 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006479 if (err)
6480 goto fail;
6481
Yan, Zhenga22285a2010-05-16 10:48:46 -04006482 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006483 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006484 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006485 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006486 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006487 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006488 if (IS_ERR(trans)) {
6489 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006490 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006491 goto fail;
6492 }
Chris Mason5f39d392007-10-15 16:14:19 -04006493
Miao Xie67de1172013-12-26 13:07:06 +08006494 /* There are several dir indexes for this inode, clear the cache. */
6495 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006496 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006497 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006498 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006499 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006500 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006501
Josef Bacika1b075d2010-11-19 20:36:11 +00006502 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006503
Yan, Zhenga5719522009-09-24 09:17:31 -04006504 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006505 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006506 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006507 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006508 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006509 if (err)
6510 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006511 if (inode->i_nlink == 1) {
6512 /*
6513 * If new hard link count is 1, it's a file created
6514 * with open(2) O_TMPFILE flag.
6515 */
6516 err = btrfs_orphan_del(trans, inode);
6517 if (err)
6518 goto fail;
6519 }
Al Viro08c422c2011-12-23 07:58:13 -05006520 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006521 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006522 }
Chris Mason39279cc2007-06-12 06:35:45 -04006523
Josef Bacikc581afc2014-02-06 16:06:06 -05006524 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006525fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006526 if (trans)
6527 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006528 if (drop_inode) {
6529 inode_dec_link_count(inode);
6530 iput(inode);
6531 }
Liu Bob53d3f52012-11-14 14:34:34 +00006532 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006533 return err;
6534}
6535
Al Viro18bb1db2011-07-26 01:41:39 -04006536static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006537{
Chris Masonb9d86662008-05-02 16:13:49 -04006538 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006539 struct btrfs_trans_handle *trans;
6540 struct btrfs_root *root = BTRFS_I(dir)->root;
6541 int err = 0;
6542 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006543 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006544 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006545
Josef Bacik9ed74f22009-09-11 16:12:44 -04006546 /*
6547 * 2 items for inode and ref
6548 * 2 items for dir items
6549 * 1 for xattr if selinux is on
6550 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006551 trans = btrfs_start_transaction(root, 5);
6552 if (IS_ERR(trans))
6553 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006554
Li Zefan581bb052011-04-20 10:06:11 +08006555 err = btrfs_find_free_ino(root, &objectid);
6556 if (err)
6557 goto out_fail;
6558
Josef Bacikaec74772008-07-24 12:12:38 -04006559 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006560 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006561 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006562 if (IS_ERR(inode)) {
6563 err = PTR_ERR(inode);
6564 goto out_fail;
6565 }
Chris Mason5f39d392007-10-15 16:14:19 -04006566
Chris Mason39279cc2007-06-12 06:35:45 -04006567 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006568 /* these must be set before we unlock the inode */
6569 inode->i_op = &btrfs_dir_inode_operations;
6570 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006571
Eric Paris2a7dba32011-02-01 11:05:39 -05006572 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006573 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006574 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006575
Chris Masondbe674a2008-07-17 12:54:05 -04006576 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006577 err = btrfs_update_inode(trans, root, inode);
6578 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006579 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006580
Josef Bacika1b075d2010-11-19 20:36:11 +00006581 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6582 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006583 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006584 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006585
Chris Mason39279cc2007-06-12 06:35:45 -04006586 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006587 /*
6588 * mkdir is special. We're unlocking after we call d_instantiate
6589 * to avoid a race with nfsd calling d_instantiate.
6590 */
6591 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006592 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006593
6594out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006595 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006596 if (drop_on_err) {
6597 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006598 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006599 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006600 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006601 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006602 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006603
6604out_fail_inode:
6605 unlock_new_inode(inode);
6606 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006607}
6608
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006609/* Find next extent map of a given extent map, caller needs to ensure locks */
6610static struct extent_map *next_extent_map(struct extent_map *em)
6611{
6612 struct rb_node *next;
6613
6614 next = rb_next(&em->rb_node);
6615 if (!next)
6616 return NULL;
6617 return container_of(next, struct extent_map, rb_node);
6618}
6619
6620static struct extent_map *prev_extent_map(struct extent_map *em)
6621{
6622 struct rb_node *prev;
6623
6624 prev = rb_prev(&em->rb_node);
6625 if (!prev)
6626 return NULL;
6627 return container_of(prev, struct extent_map, rb_node);
6628}
6629
Chris Masond352ac62008-09-29 15:18:18 -04006630/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006631 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006632 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006633 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006634 */
Chris Mason3b951512008-04-17 11:29:12 -04006635static int merge_extent_mapping(struct extent_map_tree *em_tree,
6636 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006637 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006638 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006639{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006640 struct extent_map *prev;
6641 struct extent_map *next;
6642 u64 start;
6643 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006644 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006645
Chris Masone6dcd2d2008-07-17 12:53:50 -04006646 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006647
6648 if (existing->start > map_start) {
6649 next = existing;
6650 prev = prev_extent_map(next);
6651 } else {
6652 prev = existing;
6653 next = next_extent_map(prev);
6654 }
6655
6656 start = prev ? extent_map_end(prev) : em->start;
6657 start = max_t(u64, start, em->start);
6658 end = next ? next->start : extent_map_end(em);
6659 end = min_t(u64, end, extent_map_end(em));
6660 start_diff = start - em->start;
6661 em->start = start;
6662 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006663 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6664 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006665 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006666 em->block_len -= start_diff;
6667 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006668 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006669}
6670
Chris Masonc8b97812008-10-29 14:49:59 -04006671static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006672 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006673 size_t pg_offset, u64 extent_offset,
6674 struct btrfs_file_extent_item *item)
6675{
6676 int ret;
6677 struct extent_buffer *leaf = path->nodes[0];
6678 char *tmp;
6679 size_t max_size;
6680 unsigned long inline_size;
6681 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006682 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006683
6684 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006685 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006686 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6687 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006688 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006689 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006690 if (!tmp)
6691 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006692 ptr = btrfs_file_extent_inline_start(item);
6693
6694 read_extent_buffer(leaf, tmp, ptr, inline_size);
6695
Chris Mason5b050f02008-11-11 09:34:41 -05006696 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006697 ret = btrfs_decompress(compress_type, tmp, page,
6698 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006699 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006700 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006701}
6702
Chris Masond352ac62008-09-29 15:18:18 -04006703/*
6704 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006705 * the ugly parts come from merging extents from the disk with the in-ram
6706 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006707 * where the in-ram extents might be locked pending data=ordered completion.
6708 *
6709 * This also copies inline extents directly into the page.
6710 */
Chris Masond3977122009-01-05 21:25:51 -05006711
Chris Masona52d9a82007-08-27 16:49:44 -04006712struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006713 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006714 int create)
6715{
6716 int ret;
6717 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006718 u64 extent_start = 0;
6719 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006720 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006721 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006722 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006723 struct btrfs_root *root = BTRFS_I(inode)->root;
6724 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006725 struct extent_buffer *leaf;
6726 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006727 struct extent_map *em = NULL;
6728 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006729 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006730 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006731 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006732
Chris Masona52d9a82007-08-27 16:49:44 -04006733again:
Chris Mason890871b2009-09-02 16:24:52 -04006734 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006735 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006736 if (em)
6737 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006738 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006739
Chris Masona52d9a82007-08-27 16:49:44 -04006740 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006741 if (em->start > start || em->start + em->len <= start)
6742 free_extent_map(em);
6743 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006744 free_extent_map(em);
6745 else
6746 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006747 }
David Sterba172ddd62011-04-21 00:48:27 +02006748 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006749 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006750 err = -ENOMEM;
6751 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006752 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006753 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006754 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006755 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006756 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006757 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006758
6759 if (!path) {
6760 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006761 if (!path) {
6762 err = -ENOMEM;
6763 goto out;
6764 }
6765 /*
6766 * Chances are we'll be called again, so go ahead and do
6767 * readahead
6768 */
David Sterbae4058b52015-11-27 16:31:35 +01006769 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006770 }
6771
Chris Mason179e29e2007-11-01 11:28:41 -04006772 ret = btrfs_lookup_file_extent(trans, root, path,
6773 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006774 if (ret < 0) {
6775 err = ret;
6776 goto out;
6777 }
6778
6779 if (ret != 0) {
6780 if (path->slots[0] == 0)
6781 goto not_found;
6782 path->slots[0]--;
6783 }
6784
Chris Mason5f39d392007-10-15 16:14:19 -04006785 leaf = path->nodes[0];
6786 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006787 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006788 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006789 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006790 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006791 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006792 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006793 /*
6794 * If we backup past the first extent we want to move forward
6795 * and see if there is an extent in front of us, otherwise we'll
6796 * say there is a hole for our whole search range which can
6797 * cause problems.
6798 */
6799 extent_end = start;
6800 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006801 }
6802
Chris Mason5f39d392007-10-15 16:14:19 -04006803 found_type = btrfs_file_extent_type(leaf, item);
6804 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006805 if (found_type == BTRFS_FILE_EXTENT_REG ||
6806 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006807 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006808 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006809 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6810 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006811 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006812 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006813 }
Josef Bacik25a50342013-10-14 12:08:38 -04006814next:
Yan Zheng9036c102008-10-30 14:19:41 -04006815 if (start >= extent_end) {
6816 path->slots[0]++;
6817 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6818 ret = btrfs_next_leaf(root, path);
6819 if (ret < 0) {
6820 err = ret;
6821 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006822 }
Yan Zheng9036c102008-10-30 14:19:41 -04006823 if (ret > 0)
6824 goto not_found;
6825 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006826 }
Yan Zheng9036c102008-10-30 14:19:41 -04006827 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6828 if (found_key.objectid != objectid ||
6829 found_key.type != BTRFS_EXTENT_DATA_KEY)
6830 goto not_found;
6831 if (start + len <= found_key.offset)
6832 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006833 if (start > found_key.offset)
6834 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006835 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006836 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006837 em->len = found_key.offset - start;
6838 goto not_found_em;
6839 }
6840
Filipe Manana7ffbb592014-06-09 03:48:05 +01006841 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6842
Yan Zhengd899e052008-10-30 14:25:28 -04006843 if (found_type == BTRFS_FILE_EXTENT_REG ||
6844 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006845 goto insert;
6846 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006847 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006848 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006849 size_t size;
6850 size_t extent_offset;
6851 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006852
Filipe Manana7ffbb592014-06-09 03:48:05 +01006853 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006854 goto out;
Yan689f9342007-10-29 11:41:07 -04006855
Chris Mason514ac8a2014-01-03 21:07:00 -08006856 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006857 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006858 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006859 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006860 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006861 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006862 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006863 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006864 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006865 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006866 if (btrfs_file_extent_compression(leaf, item) !=
6867 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006868 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006869 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006870 if (ret) {
6871 err = ret;
6872 goto out;
6873 }
Chris Masonc8b97812008-10-29 14:49:59 -04006874 } else {
6875 map = kmap(page);
6876 read_extent_buffer(leaf, map + pg_offset, ptr,
6877 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006878 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6879 memset(map + pg_offset + copy_size, 0,
6880 PAGE_CACHE_SIZE - pg_offset -
6881 copy_size);
6882 }
Chris Masonc8b97812008-10-29 14:49:59 -04006883 kunmap(page);
6884 }
Chris Mason179e29e2007-11-01 11:28:41 -04006885 flush_dcache_page(page);
6886 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006887 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006888 if (!trans) {
6889 kunmap(page);
6890 free_extent_map(em);
6891 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006892
David Sterbab3b4aa72011-04-21 01:20:15 +02006893 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006894 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006895
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006896 if (IS_ERR(trans))
6897 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006898 goto again;
6899 }
Chris Masonc8b97812008-10-29 14:49:59 -04006900 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006901 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006902 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006903 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006904 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006905 }
Chris Masond1310b22008-01-24 16:13:08 -05006906 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006907 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006908 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006909 }
6910not_found:
6911 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006912 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006913 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006914not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006915 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006916 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006917insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006918 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006919 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006920 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006921 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006922 err = -EIO;
6923 goto out;
6924 }
Chris Masond1310b22008-01-24 16:13:08 -05006925
6926 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006927 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006928 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006929 /* it is possible that someone inserted the extent into the tree
6930 * while we had the lock dropped. It is also possible that
6931 * an overlapping map exists in the tree
6932 */
Chris Masona52d9a82007-08-27 16:49:44 -04006933 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006934 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006935
6936 ret = 0;
6937
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006938 existing = search_extent_mapping(em_tree, start, len);
6939 /*
6940 * existing will always be non-NULL, since there must be
6941 * extent causing the -EEXIST.
6942 */
6943 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006944 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006945 /*
6946 * The existing extent map is the one nearest to
6947 * the [start, start + len) range which overlaps
6948 */
6949 err = merge_extent_mapping(em_tree, existing,
6950 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006951 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006952 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006953 free_extent_map(em);
6954 em = NULL;
6955 }
6956 } else {
6957 free_extent_map(em);
6958 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006959 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006960 }
Chris Masona52d9a82007-08-27 16:49:44 -04006961 }
Chris Mason890871b2009-09-02 16:24:52 -04006962 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006963out:
liubo1abe9b82011-03-24 11:18:59 +00006964
Steven Rostedt4cd85872013-11-14 22:57:29 -05006965 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006966
Tsutomu Itoh527afb42015-08-19 14:55:00 +09006967 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006968 if (trans) {
6969 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006970 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006971 err = ret;
6972 }
Chris Masona52d9a82007-08-27 16:49:44 -04006973 if (err) {
6974 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006975 return ERR_PTR(err);
6976 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006977 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006978 return em;
6979}
6980
Chris Masonec29ed52011-02-23 16:23:20 -05006981struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6982 size_t pg_offset, u64 start, u64 len,
6983 int create)
6984{
6985 struct extent_map *em;
6986 struct extent_map *hole_em = NULL;
6987 u64 range_start = start;
6988 u64 end;
6989 u64 found;
6990 u64 found_end;
6991 int err = 0;
6992
6993 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6994 if (IS_ERR(em))
6995 return em;
6996 if (em) {
6997 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006998 * if our em maps to
6999 * - a hole or
7000 * - a pre-alloc extent,
7001 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007002 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007003 if (em->block_start != EXTENT_MAP_HOLE &&
7004 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007005 return em;
7006 else
7007 hole_em = em;
7008 }
7009
7010 /* check to see if we've wrapped (len == -1 or similar) */
7011 end = start + len;
7012 if (end < start)
7013 end = (u64)-1;
7014 else
7015 end -= 1;
7016
7017 em = NULL;
7018
7019 /* ok, we didn't find anything, lets look for delalloc */
7020 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7021 end, len, EXTENT_DELALLOC, 1);
7022 found_end = range_start + found;
7023 if (found_end < range_start)
7024 found_end = (u64)-1;
7025
7026 /*
7027 * we didn't find anything useful, return
7028 * the original results from get_extent()
7029 */
7030 if (range_start > end || found_end <= start) {
7031 em = hole_em;
7032 hole_em = NULL;
7033 goto out;
7034 }
7035
7036 /* adjust the range_start to make sure it doesn't
7037 * go backwards from the start they passed in
7038 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307039 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007040 found = found_end - range_start;
7041
7042 if (found > 0) {
7043 u64 hole_start = start;
7044 u64 hole_len = len;
7045
David Sterba172ddd62011-04-21 00:48:27 +02007046 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007047 if (!em) {
7048 err = -ENOMEM;
7049 goto out;
7050 }
7051 /*
7052 * when btrfs_get_extent can't find anything it
7053 * returns one huge hole
7054 *
7055 * make sure what it found really fits our range, and
7056 * adjust to make sure it is based on the start from
7057 * the caller
7058 */
7059 if (hole_em) {
7060 u64 calc_end = extent_map_end(hole_em);
7061
7062 if (calc_end <= start || (hole_em->start > end)) {
7063 free_extent_map(hole_em);
7064 hole_em = NULL;
7065 } else {
7066 hole_start = max(hole_em->start, start);
7067 hole_len = calc_end - hole_start;
7068 }
7069 }
7070 em->bdev = NULL;
7071 if (hole_em && range_start > hole_start) {
7072 /* our hole starts before our delalloc, so we
7073 * have to return just the parts of the hole
7074 * that go until the delalloc starts
7075 */
7076 em->len = min(hole_len,
7077 range_start - hole_start);
7078 em->start = hole_start;
7079 em->orig_start = hole_start;
7080 /*
7081 * don't adjust block start at all,
7082 * it is fixed at EXTENT_MAP_HOLE
7083 */
7084 em->block_start = hole_em->block_start;
7085 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007086 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7087 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007088 } else {
7089 em->start = range_start;
7090 em->len = found;
7091 em->orig_start = range_start;
7092 em->block_start = EXTENT_MAP_DELALLOC;
7093 em->block_len = found;
7094 }
7095 } else if (hole_em) {
7096 return hole_em;
7097 }
7098out:
7099
7100 free_extent_map(hole_em);
7101 if (err) {
7102 free_extent_map(em);
7103 return ERR_PTR(err);
7104 }
7105 return em;
7106}
7107
Josef Bacik4b46fce2010-05-23 11:00:55 -04007108static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7109 u64 start, u64 len)
7110{
7111 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007112 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007113 struct btrfs_key ins;
7114 u64 alloc_hint;
7115 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007116
Josef Bacik4b46fce2010-05-23 11:00:55 -04007117 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04007118 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007119 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007120 if (ret)
7121 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007122
Filipe Mananade0ee0e2016-01-21 10:17:54 +00007123 /*
7124 * Create the ordered extent before the extent map. This is to avoid
7125 * races with the fast fsync path that would lead to it logging file
7126 * extent items that point to disk extents that were not yet written to.
7127 * The fast fsync path collects ordered extents into a local list and
7128 * then collects all the new extent maps, so we must create the ordered
7129 * extent first and make sure the fast fsync path collects any new
7130 * ordered extents after collecting new extent maps as well.
7131 * The fsync path simply can not rely on inode_dio_wait() because it
7132 * causes deadlock with AIO.
7133 */
Josef Bacik4b46fce2010-05-23 11:00:55 -04007134 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
7135 ins.offset, ins.offset, 0);
7136 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08007137 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007138 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007139 }
Josef Bacik00361582013-08-14 14:02:47 -04007140
Filipe Mananade0ee0e2016-01-21 10:17:54 +00007141 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
7142 ins.offset, ins.offset, ins.offset, 0);
7143 if (IS_ERR(em)) {
7144 struct btrfs_ordered_extent *oe;
7145
7146 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
7147 oe = btrfs_lookup_ordered_extent(inode, start);
7148 ASSERT(oe);
7149 if (WARN_ON(!oe))
7150 return em;
7151 set_bit(BTRFS_ORDERED_IOERR, &oe->flags);
7152 set_bit(BTRFS_ORDERED_IO_DONE, &oe->flags);
7153 btrfs_remove_ordered_extent(inode, oe);
7154 /* Once for our lookup and once for the ordered extents tree. */
7155 btrfs_put_ordered_extent(oe);
7156 btrfs_put_ordered_extent(oe);
7157 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007158 return em;
7159}
7160
Chris Mason46bfbb52010-05-26 11:04:10 -04007161/*
7162 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7163 * block must be cow'd
7164 */
Josef Bacik00361582013-08-14 14:02:47 -04007165noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007166 u64 *orig_start, u64 *orig_block_len,
7167 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007168{
Josef Bacik00361582013-08-14 14:02:47 -04007169 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007170 struct btrfs_path *path;
7171 int ret;
7172 struct extent_buffer *leaf;
7173 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007174 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007175 struct btrfs_file_extent_item *fi;
7176 struct btrfs_key key;
7177 u64 disk_bytenr;
7178 u64 backref_offset;
7179 u64 extent_end;
7180 u64 num_bytes;
7181 int slot;
7182 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007183 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007184
Chris Mason46bfbb52010-05-26 11:04:10 -04007185 path = btrfs_alloc_path();
7186 if (!path)
7187 return -ENOMEM;
7188
Josef Bacik00361582013-08-14 14:02:47 -04007189 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007190 offset, 0);
7191 if (ret < 0)
7192 goto out;
7193
7194 slot = path->slots[0];
7195 if (ret == 1) {
7196 if (slot == 0) {
7197 /* can't find the item, must cow */
7198 ret = 0;
7199 goto out;
7200 }
7201 slot--;
7202 }
7203 ret = 0;
7204 leaf = path->nodes[0];
7205 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007206 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007207 key.type != BTRFS_EXTENT_DATA_KEY) {
7208 /* not our file or wrong item type, must cow */
7209 goto out;
7210 }
7211
7212 if (key.offset > offset) {
7213 /* Wrong offset, must cow */
7214 goto out;
7215 }
7216
7217 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7218 found_type = btrfs_file_extent_type(leaf, fi);
7219 if (found_type != BTRFS_FILE_EXTENT_REG &&
7220 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7221 /* not a regular extent, must cow */
7222 goto out;
7223 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007224
7225 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7226 goto out;
7227
Miao Xiee77751a2013-12-27 21:11:50 +08007228 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7229 if (extent_end <= offset)
7230 goto out;
7231
Chris Mason46bfbb52010-05-26 11:04:10 -04007232 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007233 if (disk_bytenr == 0)
7234 goto out;
7235
7236 if (btrfs_file_extent_compression(leaf, fi) ||
7237 btrfs_file_extent_encryption(leaf, fi) ||
7238 btrfs_file_extent_other_encoding(leaf, fi))
7239 goto out;
7240
Chris Mason46bfbb52010-05-26 11:04:10 -04007241 backref_offset = btrfs_file_extent_offset(leaf, fi);
7242
Josef Bacik7ee9e442013-06-21 16:37:03 -04007243 if (orig_start) {
7244 *orig_start = key.offset - backref_offset;
7245 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7246 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7247 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007248
Chris Mason46bfbb52010-05-26 11:04:10 -04007249 if (btrfs_extent_readonly(root, disk_bytenr))
7250 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007251
7252 num_bytes = min(offset + *len, extent_end) - offset;
7253 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7254 u64 range_end;
7255
7256 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7257 ret = test_range_bit(io_tree, offset, range_end,
7258 EXTENT_DELALLOC, 0, NULL);
7259 if (ret) {
7260 ret = -EAGAIN;
7261 goto out;
7262 }
7263 }
7264
Josef Bacik1bda19e2013-10-18 12:10:36 -04007265 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007266
7267 /*
7268 * look for other files referencing this extent, if we
7269 * find any we must cow
7270 */
Josef Bacik00361582013-08-14 14:02:47 -04007271 trans = btrfs_join_transaction(root);
7272 if (IS_ERR(trans)) {
7273 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007274 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007275 }
7276
7277 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7278 key.offset - backref_offset, disk_bytenr);
7279 btrfs_end_transaction(trans, root);
7280 if (ret) {
7281 ret = 0;
7282 goto out;
7283 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007284
7285 /*
7286 * adjust disk_bytenr and num_bytes to cover just the bytes
7287 * in this extent we are about to write. If there
7288 * are any csums in that range we have to cow in order
7289 * to keep the csums correct
7290 */
7291 disk_bytenr += backref_offset;
7292 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007293 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7294 goto out;
7295 /*
7296 * all of the above have passed, it is safe to overwrite this extent
7297 * without cow
7298 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007299 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007300 ret = 1;
7301out:
7302 btrfs_free_path(path);
7303 return ret;
7304}
7305
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007306bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7307{
7308 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7309 int found = false;
7310 void **pagep = NULL;
7311 struct page *page = NULL;
7312 int start_idx;
7313 int end_idx;
7314
7315 start_idx = start >> PAGE_CACHE_SHIFT;
7316
7317 /*
7318 * end is the last byte in the last page. end == start is legal
7319 */
7320 end_idx = end >> PAGE_CACHE_SHIFT;
7321
7322 rcu_read_lock();
7323
7324 /* Most of the code in this while loop is lifted from
7325 * find_get_page. It's been modified to begin searching from a
7326 * page and return just the first page found in that range. If the
7327 * found idx is less than or equal to the end idx then we know that
7328 * a page exists. If no pages are found or if those pages are
7329 * outside of the range then we're fine (yay!) */
7330 while (page == NULL &&
7331 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7332 page = radix_tree_deref_slot(pagep);
7333 if (unlikely(!page))
7334 break;
7335
7336 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007337 if (radix_tree_deref_retry(page)) {
7338 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007339 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007340 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007341 /*
7342 * Otherwise, shmem/tmpfs must be storing a swap entry
7343 * here as an exceptional entry: so return it without
7344 * attempting to raise page count.
7345 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007346 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007347 break; /* TODO: Is this relevant for this use case? */
7348 }
7349
Filipe Manana91405152014-06-05 13:22:24 +01007350 if (!page_cache_get_speculative(page)) {
7351 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007352 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007353 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007354
7355 /*
7356 * Has the page moved?
7357 * This is part of the lockless pagecache protocol. See
7358 * include/linux/pagemap.h for details.
7359 */
7360 if (unlikely(page != *pagep)) {
7361 page_cache_release(page);
7362 page = NULL;
7363 }
7364 }
7365
7366 if (page) {
7367 if (page->index <= end_idx)
7368 found = true;
7369 page_cache_release(page);
7370 }
7371
7372 rcu_read_unlock();
7373 return found;
7374}
7375
Josef Bacikeb838e72012-07-31 16:28:48 -04007376static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7377 struct extent_state **cached_state, int writing)
7378{
7379 struct btrfs_ordered_extent *ordered;
7380 int ret = 0;
7381
7382 while (1) {
7383 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007384 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007385 /*
7386 * We're concerned with the entire range that we're going to be
7387 * doing DIO to, so we need to make sure theres no ordered
7388 * extents in this range.
7389 */
7390 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7391 lockend - lockstart + 1);
7392
7393 /*
7394 * We need to make sure there are no buffered pages in this
7395 * range either, we could have raced between the invalidate in
7396 * generic_file_direct_write and locking the extent. The
7397 * invalidate needs to happen so that reads after a write do not
7398 * get stale data.
7399 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007400 if (!ordered &&
7401 (!writing ||
7402 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007403 break;
7404
7405 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7406 cached_state, GFP_NOFS);
7407
7408 if (ordered) {
7409 btrfs_start_ordered_extent(inode, ordered, 1);
7410 btrfs_put_ordered_extent(ordered);
7411 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007412 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007413 * We could trigger writeback for this range (and wait
7414 * for it to complete) and then invalidate the pages for
7415 * this range (through invalidate_inode_pages2_range()),
7416 * but that can lead us to a deadlock with a concurrent
7417 * call to readpages() (a buffered read or a defrag call
7418 * triggered a readahead) on a page lock due to an
7419 * ordered dio extent we created before but did not have
7420 * yet a corresponding bio submitted (whence it can not
7421 * complete), which makes readpages() wait for that
7422 * ordered extent to complete while holding a lock on
7423 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007424 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007425 ret = -ENOTBLK;
7426 break;
Josef Bacikeb838e72012-07-31 16:28:48 -04007427 }
7428
7429 cond_resched();
7430 }
7431
7432 return ret;
7433}
7434
Josef Bacik69ffb542012-09-11 15:40:07 -04007435static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7436 u64 len, u64 orig_start,
7437 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007438 u64 orig_block_len, u64 ram_bytes,
7439 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007440{
7441 struct extent_map_tree *em_tree;
7442 struct extent_map *em;
7443 struct btrfs_root *root = BTRFS_I(inode)->root;
7444 int ret;
7445
7446 em_tree = &BTRFS_I(inode)->extent_tree;
7447 em = alloc_extent_map();
7448 if (!em)
7449 return ERR_PTR(-ENOMEM);
7450
7451 em->start = start;
7452 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007453 em->mod_start = start;
7454 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007455 em->len = len;
7456 em->block_len = block_len;
7457 em->block_start = block_start;
7458 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007459 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007460 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007461 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007462 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7463 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007464 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007465
7466 do {
7467 btrfs_drop_extent_cache(inode, em->start,
7468 em->start + em->len - 1, 0);
7469 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007470 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007471 write_unlock(&em_tree->lock);
7472 } while (ret == -EEXIST);
7473
7474 if (ret) {
7475 free_extent_map(em);
7476 return ERR_PTR(ret);
7477 }
7478
7479 return em;
7480}
7481
Filipe Manana9c9464c2015-11-04 09:52:04 +00007482static void adjust_dio_outstanding_extents(struct inode *inode,
7483 struct btrfs_dio_data *dio_data,
7484 const u64 len)
7485{
7486 unsigned num_extents;
7487
7488 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7489 BTRFS_MAX_EXTENT_SIZE);
7490 /*
7491 * If we have an outstanding_extents count still set then we're
7492 * within our reservation, otherwise we need to adjust our inode
7493 * counter appropriately.
7494 */
7495 if (dio_data->outstanding_extents) {
7496 dio_data->outstanding_extents -= num_extents;
7497 } else {
7498 spin_lock(&BTRFS_I(inode)->lock);
7499 BTRFS_I(inode)->outstanding_extents += num_extents;
7500 spin_unlock(&BTRFS_I(inode)->lock);
7501 }
7502}
7503
Josef Bacik4b46fce2010-05-23 11:00:55 -04007504static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7505 struct buffer_head *bh_result, int create)
7506{
7507 struct extent_map *em;
7508 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007509 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307510 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007511 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007512 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007513 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007514 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007515 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007516
Miao Xie172a5042013-02-21 02:48:22 -07007517 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007518 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007519 else
Josef Bacikc3298612012-08-03 16:49:19 -04007520 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007521
Josef Bacikc3298612012-08-03 16:49:19 -04007522 lockstart = start;
7523 lockend = start + len - 1;
7524
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007525 if (current->journal_info) {
7526 /*
7527 * Need to pull our outstanding extents and set journal_info to NULL so
7528 * that anything that needs to check if there's a transction doesn't get
7529 * confused.
7530 */
chandan50745b02015-08-28 21:10:13 +05307531 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007532 current->journal_info = NULL;
7533 }
7534
Josef Bacikeb838e72012-07-31 16:28:48 -04007535 /*
7536 * If this errors out it's because we couldn't invalidate pagecache for
7537 * this range and we need to fallback to buffered.
7538 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007539 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7540 create)) {
7541 ret = -ENOTBLK;
7542 goto err;
7543 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007544
Josef Bacik4b46fce2010-05-23 11:00:55 -04007545 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007546 if (IS_ERR(em)) {
7547 ret = PTR_ERR(em);
7548 goto unlock_err;
7549 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007550
7551 /*
7552 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7553 * io. INLINE is special, and we could probably kludge it in here, but
7554 * it's still buffered so for safety lets just fall back to the generic
7555 * buffered path.
7556 *
7557 * For COMPRESSED we _have_ to read the entire extent in so we can
7558 * decompress it, so there will be buffering required no matter what we
7559 * do, so go ahead and fallback to buffered.
7560 *
7561 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7562 * to buffered IO. Don't blame me, this is the price we pay for using
7563 * the generic code.
7564 */
7565 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7566 em->block_start == EXTENT_MAP_INLINE) {
7567 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007568 ret = -ENOTBLK;
7569 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007570 }
7571
7572 /* Just a good old fashioned hole, return */
7573 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7574 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7575 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007576 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007577 }
7578
7579 /*
7580 * We don't allocate a new extent in the following cases
7581 *
7582 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7583 * existing extent.
7584 * 2) The extent is marked as PREALLOC. We're good to go here and can
7585 * just use the extent.
7586 *
7587 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007588 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007589 len = min(len, em->len - (start - em->start));
7590 lockstart = start + len;
7591 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007592 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007593
7594 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7595 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7596 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007597 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007598 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007599
7600 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7601 type = BTRFS_ORDERED_PREALLOC;
7602 else
7603 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007604 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007605 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007606
Josef Bacik00361582013-08-14 14:02:47 -04007607 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007608 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007609 if (type == BTRFS_ORDERED_PREALLOC) {
7610 free_extent_map(em);
7611 em = create_pinned_em(inode, start, len,
7612 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007613 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007614 orig_block_len,
7615 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007616 if (IS_ERR(em)) {
7617 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007618 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007619 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007620 }
7621
Chris Mason46bfbb52010-05-26 11:04:10 -04007622 ret = btrfs_add_ordered_extent_dio(inode, start,
7623 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007624 if (ret) {
7625 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007626 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007627 }
7628 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007629 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007630 }
Josef Bacik00361582013-08-14 14:02:47 -04007631
Chris Mason46bfbb52010-05-26 11:04:10 -04007632 /*
7633 * this will cow the extent, reset the len in case we changed
7634 * it above
7635 */
7636 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007637 free_extent_map(em);
7638 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007639 if (IS_ERR(em)) {
7640 ret = PTR_ERR(em);
7641 goto unlock_err;
7642 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007643 len = min(len, em->len - (start - em->start));
7644unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007645 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7646 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007647 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007648 bh_result->b_bdev = em->bdev;
7649 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007650 if (create) {
7651 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7652 set_buffer_new(bh_result);
7653
7654 /*
7655 * Need to update the i_size under the extent lock so buffered
7656 * readers will get the updated i_size when we unlock.
7657 */
7658 if (start + len > i_size_read(inode))
7659 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007660
Filipe Manana9c9464c2015-11-04 09:52:04 +00007661 adjust_dio_outstanding_extents(inode, dio_data, len);
Qu Wenruo7cf5b972015-09-08 17:25:55 +08007662 btrfs_free_reserved_data_space(inode, start, len);
chandan50745b02015-08-28 21:10:13 +05307663 WARN_ON(dio_data->reserve < len);
7664 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007665 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307666 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007667 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007668
Josef Bacikeb838e72012-07-31 16:28:48 -04007669 /*
7670 * In the case of write we need to clear and unlock the entire range,
7671 * in the case of read we need to unlock only the end area that we
7672 * aren't using if there is any left over space.
7673 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007674 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007675 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7676 lockend, unlock_bits, 1, 0,
7677 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007678 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007679 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007680 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007681
Josef Bacik4b46fce2010-05-23 11:00:55 -04007682 free_extent_map(em);
7683
7684 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007685
7686unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007687 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7688 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007689err:
chandan50745b02015-08-28 21:10:13 +05307690 if (dio_data)
7691 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007692 /*
7693 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7694 * write less data then expected, so that we don't underflow our inode's
7695 * outstanding extents counter.
7696 */
7697 if (create && dio_data)
7698 adjust_dio_outstanding_extents(inode, dio_data, len);
7699
Josef Bacikeb838e72012-07-31 16:28:48 -04007700 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007701}
7702
Miao Xie8b110e32014-09-12 18:44:03 +08007703static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7704 int rw, int mirror_num)
7705{
7706 struct btrfs_root *root = BTRFS_I(inode)->root;
7707 int ret;
7708
7709 BUG_ON(rw & REQ_WRITE);
7710
7711 bio_get(bio);
7712
7713 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7714 BTRFS_WQ_ENDIO_DIO_REPAIR);
7715 if (ret)
7716 goto err;
7717
7718 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7719err:
7720 bio_put(bio);
7721 return ret;
7722}
7723
7724static int btrfs_check_dio_repairable(struct inode *inode,
7725 struct bio *failed_bio,
7726 struct io_failure_record *failrec,
7727 int failed_mirror)
7728{
7729 int num_copies;
7730
7731 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7732 failrec->logical, failrec->len);
7733 if (num_copies == 1) {
7734 /*
7735 * we only have a single copy of the data, so don't bother with
7736 * all the retry and error correction code that follows. no
7737 * matter what the error is, it is very likely to persist.
7738 */
7739 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7740 num_copies, failrec->this_mirror, failed_mirror);
7741 return 0;
7742 }
7743
7744 failrec->failed_mirror = failed_mirror;
7745 failrec->this_mirror++;
7746 if (failrec->this_mirror == failed_mirror)
7747 failrec->this_mirror++;
7748
7749 if (failrec->this_mirror > num_copies) {
7750 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7751 num_copies, failrec->this_mirror, failed_mirror);
7752 return 0;
7753 }
7754
7755 return 1;
7756}
7757
7758static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307759 struct page *page, unsigned int pgoff,
7760 u64 start, u64 end, int failed_mirror,
7761 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007762{
7763 struct io_failure_record *failrec;
7764 struct bio *bio;
7765 int isector;
7766 int read_mode;
7767 int ret;
7768
7769 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7770
7771 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7772 if (ret)
7773 return ret;
7774
7775 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7776 failed_mirror);
7777 if (!ret) {
7778 free_io_failure(inode, failrec);
7779 return -EIO;
7780 }
7781
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307782 if ((failed_bio->bi_vcnt > 1)
7783 || (failed_bio->bi_io_vec->bv_len
7784 > BTRFS_I(inode)->root->sectorsize))
Miao Xie8b110e32014-09-12 18:44:03 +08007785 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7786 else
7787 read_mode = READ_SYNC;
7788
7789 isector = start - btrfs_io_bio(failed_bio)->logical;
7790 isector >>= inode->i_sb->s_blocksize_bits;
7791 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307792 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007793 if (!bio) {
7794 free_io_failure(inode, failrec);
7795 return -EIO;
7796 }
7797
7798 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7799 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7800 read_mode, failrec->this_mirror, failrec->in_validation);
7801
7802 ret = submit_dio_repair_bio(inode, bio, read_mode,
7803 failrec->this_mirror);
7804 if (ret) {
7805 free_io_failure(inode, failrec);
7806 bio_put(bio);
7807 }
7808
7809 return ret;
7810}
7811
7812struct btrfs_retry_complete {
7813 struct completion done;
7814 struct inode *inode;
7815 u64 start;
7816 int uptodate;
7817};
7818
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007819static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007820{
7821 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307822 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007823 struct bio_vec *bvec;
7824 int i;
7825
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007826 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007827 goto end;
7828
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307829 ASSERT(bio->bi_vcnt == 1);
7830 inode = bio->bi_io_vec->bv_page->mapping->host;
7831 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
7832
Miao Xie8b110e32014-09-12 18:44:03 +08007833 done->uptodate = 1;
7834 bio_for_each_segment_all(bvec, bio, i)
7835 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7836end:
7837 complete(&done->done);
7838 bio_put(bio);
7839}
7840
7841static int __btrfs_correct_data_nocsum(struct inode *inode,
7842 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007843{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307844 struct btrfs_fs_info *fs_info;
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;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307848 unsigned int pgoff;
7849 u32 sectorsize;
7850 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007851 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007852 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007853
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307854 fs_info = BTRFS_I(inode)->root->fs_info;
7855 sectorsize = BTRFS_I(inode)->root->sectorsize;
7856
Miao Xiec1dc0892014-09-12 18:43:56 +08007857 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007858 done.inode = inode;
7859
7860 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307861 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7862 pgoff = bvec->bv_offset;
7863
7864next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007865 done.uptodate = 0;
7866 done.start = start;
7867 init_completion(&done.done);
7868
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307869 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7870 pgoff, start, start + sectorsize - 1,
7871 io_bio->mirror_num,
7872 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08007873 if (ret)
7874 return ret;
7875
7876 wait_for_completion(&done.done);
7877
7878 if (!done.uptodate) {
7879 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307880 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007881 }
7882
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307883 start += sectorsize;
7884
7885 if (nr_sectors--) {
7886 pgoff += sectorsize;
7887 goto next_block_or_try_again;
7888 }
Miao Xie8b110e32014-09-12 18:44:03 +08007889 }
7890
7891 return 0;
7892}
7893
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007894static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007895{
7896 struct btrfs_retry_complete *done = bio->bi_private;
7897 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307898 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007899 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307900 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08007901 int uptodate;
7902 int ret;
7903 int i;
7904
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007905 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007906 goto end;
7907
7908 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307909
7910 start = done->start;
7911
7912 ASSERT(bio->bi_vcnt == 1);
7913 inode = bio->bi_io_vec->bv_page->mapping->host;
7914 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
7915
Miao Xie8b110e32014-09-12 18:44:03 +08007916 bio_for_each_segment_all(bvec, bio, i) {
7917 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307918 bvec->bv_page, bvec->bv_offset,
7919 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007920 if (!ret)
7921 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307922 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08007923 else
7924 uptodate = 0;
7925 }
7926
7927 done->uptodate = uptodate;
7928end:
7929 complete(&done->done);
7930 bio_put(bio);
7931}
7932
7933static int __btrfs_subio_endio_read(struct inode *inode,
7934 struct btrfs_io_bio *io_bio, int err)
7935{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307936 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08007937 struct bio_vec *bvec;
7938 struct btrfs_retry_complete done;
7939 u64 start;
7940 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307941 u32 sectorsize;
7942 int nr_sectors;
7943 unsigned int pgoff;
7944 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08007945 int i;
7946 int ret;
7947
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307948 fs_info = BTRFS_I(inode)->root->fs_info;
7949 sectorsize = BTRFS_I(inode)->root->sectorsize;
7950
Miao Xie8b110e32014-09-12 18:44:03 +08007951 err = 0;
7952 start = io_bio->logical;
7953 done.inode = inode;
7954
Miao Xiec1dc0892014-09-12 18:43:56 +08007955 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307956 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7957
7958 pgoff = bvec->bv_offset;
7959next_block:
7960 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
7961 ret = __readpage_endio_check(inode, io_bio, csum_pos,
7962 bvec->bv_page, pgoff, start,
7963 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08007964 if (likely(!ret))
7965 goto next;
7966try_again:
7967 done.uptodate = 0;
7968 done.start = start;
7969 init_completion(&done.done);
7970
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307971 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7972 pgoff, start, start + sectorsize - 1,
7973 io_bio->mirror_num,
7974 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08007975 if (ret) {
7976 err = ret;
7977 goto next;
7978 }
7979
7980 wait_for_completion(&done.done);
7981
7982 if (!done.uptodate) {
7983 /* We might have another mirror, so try again */
7984 goto try_again;
7985 }
7986next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307987 offset += sectorsize;
7988 start += sectorsize;
7989
7990 ASSERT(nr_sectors);
7991
7992 if (--nr_sectors) {
7993 pgoff += sectorsize;
7994 goto next_block;
7995 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007996 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007997
7998 return err;
7999}
8000
Miao Xie8b110e32014-09-12 18:44:03 +08008001static int btrfs_subio_endio_read(struct inode *inode,
8002 struct btrfs_io_bio *io_bio, int err)
8003{
8004 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8005
8006 if (skip_csum) {
8007 if (unlikely(err))
8008 return __btrfs_correct_data_nocsum(inode, io_bio);
8009 else
8010 return 0;
8011 } else {
8012 return __btrfs_subio_endio_read(inode, io_bio, err);
8013 }
8014}
8015
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008016static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008017{
8018 struct btrfs_dio_private *dip = bio->bi_private;
8019 struct inode *inode = dip->inode;
8020 struct bio *dio_bio;
8021 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008022 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008023
Miao Xie8b110e32014-09-12 18:44:03 +08008024 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8025 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008026
Josef Bacik4b46fce2010-05-23 11:00:55 -04008027 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008028 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008029 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008030
Josef Bacik4b46fce2010-05-23 11:00:55 -04008031 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008032
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008033 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008034
8035 if (io_bio->end_io)
8036 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008037 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008038}
8039
Filipe Manana14543772015-11-24 16:23:54 +00008040static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8041 const u64 offset,
8042 const u64 bytes,
8043 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008044{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008045 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008046 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008047 u64 ordered_offset = offset;
8048 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008049 int ret;
8050
Chris Mason163cf092010-11-28 19:56:33 -05008051again:
8052 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8053 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008054 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008055 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008056 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008057 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008058
Liu Bo9e0af232014-08-15 23:36:53 +08008059 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8060 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08008061 btrfs_queue_work(root->fs_info->endio_write_workers,
8062 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008063out_test:
8064 /*
8065 * our bio might span multiple ordered extents. If we haven't
8066 * completed the accounting for the whole dio, go back and try again
8067 */
Filipe Manana14543772015-11-24 16:23:54 +00008068 if (ordered_offset < offset + bytes) {
8069 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008070 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008071 goto again;
8072 }
Filipe Manana14543772015-11-24 16:23:54 +00008073}
8074
8075static void btrfs_endio_direct_write(struct bio *bio)
8076{
8077 struct btrfs_dio_private *dip = bio->bi_private;
8078 struct bio *dio_bio = dip->dio_bio;
8079
8080 btrfs_endio_direct_write_update_ordered(dip->inode,
8081 dip->logical_offset,
8082 dip->bytes,
8083 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008084
Josef Bacik4b46fce2010-05-23 11:00:55 -04008085 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008086
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008087 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008088 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008089}
8090
Chris Masoneaf25d92010-05-25 09:48:28 -04008091static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
8092 struct bio *bio, int mirror_num,
8093 unsigned long bio_flags, u64 offset)
8094{
8095 int ret;
8096 struct btrfs_root *root = BTRFS_I(inode)->root;
8097 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008098 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008099 return 0;
8100}
8101
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008102static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008103{
8104 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008105 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008106
Miao Xie8b110e32014-09-12 18:44:03 +08008107 if (err)
8108 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
8109 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
8110 btrfs_ino(dip->inode), bio->bi_rw,
8111 (unsigned long long)bio->bi_iter.bi_sector,
8112 bio->bi_iter.bi_size, err);
8113
8114 if (dip->subio_endio)
8115 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008116
8117 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008118 dip->errors = 1;
8119
8120 /*
8121 * before atomic variable goto zero, we must make sure
8122 * dip->errors is perceived to be set.
8123 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008124 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008125 }
8126
8127 /* if there are more bios still pending for this dio, just exit */
8128 if (!atomic_dec_and_test(&dip->pending_bios))
8129 goto out;
8130
Chris Mason9be33952013-05-17 18:30:14 -04008131 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008132 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008133 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008134 dip->dio_bio->bi_error = 0;
8135 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008136 }
8137out:
8138 bio_put(bio);
8139}
8140
8141static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8142 u64 first_sector, gfp_t gfp_flags)
8143{
Chris Masonda2f0f72015-07-02 13:57:22 -07008144 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008145 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008146 if (bio)
8147 bio_associate_current(bio);
8148 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008149}
8150
Miao Xiec1dc0892014-09-12 18:43:56 +08008151static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
8152 struct inode *inode,
8153 struct btrfs_dio_private *dip,
8154 struct bio *bio,
8155 u64 file_offset)
8156{
8157 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8158 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8159 int ret;
8160
8161 /*
8162 * We load all the csum data we need when we submit
8163 * the first bio to reduce the csum tree search and
8164 * contention.
8165 */
8166 if (dip->logical_offset == file_offset) {
8167 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
8168 file_offset);
8169 if (ret)
8170 return ret;
8171 }
8172
8173 if (bio == dip->orig_bio)
8174 return 0;
8175
8176 file_offset -= dip->logical_offset;
8177 file_offset >>= inode->i_sb->s_blocksize_bits;
8178 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8179
8180 return 0;
8181}
8182
Miao Xiee65e1532010-11-22 03:04:43 +00008183static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8184 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008185 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008186{
Miao Xiefacc8a22013-07-25 19:22:34 +08008187 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00008188 int write = rw & REQ_WRITE;
8189 struct btrfs_root *root = BTRFS_I(inode)->root;
8190 int ret;
8191
Josef Bacikb812ce22012-11-16 13:56:32 -05008192 if (async_submit)
8193 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8194
Miao Xiee65e1532010-11-22 03:04:43 +00008195 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008196
8197 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02008198 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8199 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008200 if (ret)
8201 goto err;
8202 }
Miao Xiee65e1532010-11-22 03:04:43 +00008203
Josef Bacik1ae39932011-04-06 14:41:34 -04008204 if (skip_sum)
8205 goto map;
8206
8207 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008208 ret = btrfs_wq_submit_bio(root->fs_info,
8209 inode, rw, bio, 0, 0,
8210 file_offset,
8211 __btrfs_submit_bio_start_direct_io,
8212 __btrfs_submit_bio_done);
8213 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008214 } else if (write) {
8215 /*
8216 * If we aren't doing async submit, calculate the csum of the
8217 * bio now.
8218 */
8219 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8220 if (ret)
8221 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008222 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008223 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8224 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008225 if (ret)
8226 goto err;
8227 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008228map:
8229 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008230err:
8231 bio_put(bio);
8232 return ret;
8233}
8234
8235static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8236 int skip_sum)
8237{
8238 struct inode *inode = dip->inode;
8239 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008240 struct bio *bio;
8241 struct bio *orig_bio = dip->orig_bio;
8242 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008243 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008244 u64 file_offset = dip->logical_offset;
8245 u64 submit_len = 0;
8246 u64 map_length;
8247 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08008248 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04008249 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008250
Kent Overstreet4f024f32013-10-11 15:44:27 -07008251 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008252 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008253 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008254 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008255 return -EIO;
Miao Xiefacc8a22013-07-25 19:22:34 +08008256
Kent Overstreet4f024f32013-10-11 15:44:27 -07008257 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008258 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008259 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008260 goto submit;
8261 }
8262
David Woodhouse53b381b2013-01-29 18:40:14 -05008263 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008264 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008265 async_submit = 0;
8266 else
8267 async_submit = 1;
8268
Josef Bacik02f57c72011-04-06 14:25:44 -04008269 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8270 if (!bio)
8271 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008272
Josef Bacik02f57c72011-04-06 14:25:44 -04008273 bio->bi_private = dip;
8274 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008275 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008276 atomic_inc(&dip->pending_bios);
8277
Miao Xiee65e1532010-11-22 03:04:43 +00008278 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02008279 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00008280 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02008281 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00008282 /*
8283 * inc the count before we submit the bio so
8284 * we know the end IO handler won't happen before
8285 * we inc the count. Otherwise, the dip might get freed
8286 * before we're done setting it up
8287 */
8288 atomic_inc(&dip->pending_bios);
8289 ret = __btrfs_submit_dio_bio(bio, inode, rw,
8290 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008291 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008292 if (ret) {
8293 bio_put(bio);
8294 atomic_dec(&dip->pending_bios);
8295 goto out_err;
8296 }
8297
Miao Xiee65e1532010-11-22 03:04:43 +00008298 start_sector += submit_len >> 9;
8299 file_offset += submit_len;
8300
8301 submit_len = 0;
8302 nr_pages = 0;
8303
8304 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8305 start_sector, GFP_NOFS);
8306 if (!bio)
8307 goto out_err;
8308 bio->bi_private = dip;
8309 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008310 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008311
Kent Overstreet4f024f32013-10-11 15:44:27 -07008312 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008313 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008314 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008315 &map_length, NULL, 0);
8316 if (ret) {
8317 bio_put(bio);
8318 goto out_err;
8319 }
8320 } else {
8321 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308322 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00008323 bvec++;
8324 }
8325 }
8326
Josef Bacik02f57c72011-04-06 14:25:44 -04008327submit:
Miao Xiee65e1532010-11-22 03:04:43 +00008328 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008329 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008330 if (!ret)
8331 return 0;
8332
8333 bio_put(bio);
8334out_err:
8335 dip->errors = 1;
8336 /*
8337 * before atomic variable goto zero, we must
8338 * make sure dip->errors is perceived to be set.
8339 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008340 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008341 if (atomic_dec_and_test(&dip->pending_bios))
8342 bio_io_error(dip->orig_bio);
8343
8344 /* bio_end_io() will handle error, so we needn't return it */
8345 return 0;
8346}
8347
Chris Mason9be33952013-05-17 18:30:14 -04008348static void btrfs_submit_direct(int rw, struct bio *dio_bio,
8349 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008350{
Filipe Manana61de7182015-07-01 12:13:10 +01008351 struct btrfs_dio_private *dip = NULL;
8352 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008353 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008354 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02008355 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008356 int ret = 0;
8357
8358 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8359
Chris Mason9be33952013-05-17 18:30:14 -04008360 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008361 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008362 ret = -ENOMEM;
8363 goto free_ordered;
8364 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008365
Miao Xiec1dc0892014-09-12 18:43:56 +08008366 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008367 if (!dip) {
8368 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008369 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008370 }
8371
8372 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008373 dip->inode = inode;
8374 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008375 dip->bytes = dio_bio->bi_iter.bi_size;
8376 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008377 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008378 dip->orig_bio = io_bio;
8379 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008380 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008381 btrfs_bio = btrfs_io_bio(io_bio);
8382 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008383
Miao Xiec1dc0892014-09-12 18:43:56 +08008384 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008385 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008386 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008387 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008388 dip->subio_endio = btrfs_subio_endio_read;
8389 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008390
Filipe Mananaf28a4922015-12-08 19:23:20 +00008391 /*
8392 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8393 * even if we fail to submit a bio, because in such case we do the
8394 * corresponding error handling below and it must not be done a second
8395 * time by btrfs_direct_IO().
8396 */
8397 if (write) {
8398 struct btrfs_dio_data *dio_data = current->journal_info;
8399
8400 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8401 dip->bytes;
8402 dio_data->unsubmitted_oe_range_start =
8403 dio_data->unsubmitted_oe_range_end;
8404 }
8405
Miao Xiee65e1532010-11-22 03:04:43 +00008406 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8407 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008408 return;
Chris Mason9be33952013-05-17 18:30:14 -04008409
Miao Xie23ea8e52014-09-12 18:43:54 +08008410 if (btrfs_bio->end_io)
8411 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008412
Josef Bacik4b46fce2010-05-23 11:00:55 -04008413free_ordered:
8414 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008415 * If we arrived here it means either we failed to submit the dip
8416 * or we either failed to clone the dio_bio or failed to allocate the
8417 * dip. If we cloned the dio_bio and allocated the dip, we can just
8418 * call bio_endio against our io_bio so that we get proper resource
8419 * cleanup if we fail to submit the dip, otherwise, we must do the
8420 * same as btrfs_endio_direct_[write|read] because we can't call these
8421 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008422 */
Filipe Manana61de7182015-07-01 12:13:10 +01008423 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008424 io_bio->bi_error = -EIO;
8425 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008426 /*
8427 * The end io callbacks free our dip, do the final put on io_bio
8428 * and all the cleanup and final put for dio_bio (through
8429 * dio_end_io()).
8430 */
8431 dip = NULL;
8432 io_bio = NULL;
8433 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008434 if (write)
8435 btrfs_endio_direct_write_update_ordered(inode,
8436 file_offset,
8437 dio_bio->bi_iter.bi_size,
8438 0);
8439 else
Filipe Manana61de7182015-07-01 12:13:10 +01008440 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8441 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008442
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008443 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008444 /*
8445 * Releases and cleans up our dio_bio, no need to bio_put()
8446 * nor bio_endio()/bio_io_error() against dio_bio.
8447 */
8448 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008449 }
Filipe Manana61de7182015-07-01 12:13:10 +01008450 if (io_bio)
8451 bio_put(io_bio);
8452 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008453}
8454
Omar Sandoval6f673762015-03-16 04:33:52 -07008455static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008456 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008457{
8458 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008459 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008460 unsigned blocksize_mask = root->sectorsize - 1;
8461 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008462
8463 if (offset & blocksize_mask)
8464 goto out;
8465
Al Viro28060d52014-03-22 05:15:17 -04008466 if (iov_iter_alignment(iter) & blocksize_mask)
8467 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008468
Al Viro28060d52014-03-22 05:15:17 -04008469 /* If this is a write we don't need to check anymore */
Omar Sandoval6f673762015-03-16 04:33:52 -07008470 if (iov_iter_rw(iter) == WRITE)
Al Viro28060d52014-03-22 05:15:17 -04008471 return 0;
8472 /*
8473 * Check to make sure we don't have duplicate iov_base's in this
8474 * iovec, if so return EINVAL, otherwise we'll get csum errors
8475 * when reading back.
8476 */
8477 for (seg = 0; seg < iter->nr_segs; seg++) {
8478 for (i = seg + 1; i < iter->nr_segs; i++) {
8479 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008480 goto out;
8481 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008482 }
8483 retval = 0;
8484out:
8485 return retval;
8486}
Josef Bacikeb838e72012-07-31 16:28:48 -04008487
Omar Sandoval22c61862015-03-16 04:33:53 -07008488static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
8489 loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04008490{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008491 struct file *file = iocb->ki_filp;
8492 struct inode *inode = file->f_mapping->host;
chandan50745b02015-08-28 21:10:13 +05308493 struct btrfs_root *root = BTRFS_I(inode)->root;
8494 struct btrfs_dio_data dio_data = { 0 };
Miao Xie09348562013-02-07 10:12:07 +00008495 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008496 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008497 bool wakeup = true;
8498 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008499 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008500
Omar Sandoval6f673762015-03-16 04:33:52 -07008501 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008502 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008503
Jens Axboefe0f07d2015-04-15 17:05:48 -06008504 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008505 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008506
Josef Bacik0e267c42013-07-02 10:38:02 -04008507 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008508 * The generic stuff only does filemap_write_and_wait_range, which
8509 * isn't enough if we've written compressed pages to this area, so
8510 * we need to flush the dirty pages again to make absolutely sure
8511 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008512 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008513 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008514 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8515 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008516 filemap_fdatawrite_range(inode->i_mapping, offset,
8517 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008518
Omar Sandoval6f673762015-03-16 04:33:52 -07008519 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008520 /*
8521 * If the write DIO is beyond the EOF, we need update
8522 * the isize, but it is protected by i_mutex. So we can
8523 * not unlock the i_mutex at this case.
8524 */
8525 if (offset + count <= inode->i_size) {
8526 mutex_unlock(&inode->i_mutex);
8527 relock = true;
8528 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008529 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008530 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008531 goto out;
chandan50745b02015-08-28 21:10:13 +05308532 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008533 BTRFS_MAX_EXTENT_SIZE - 1,
8534 BTRFS_MAX_EXTENT_SIZE);
8535
8536 /*
8537 * We need to know how many extents we reserved so that we can
8538 * do the accounting properly if we go over the number we
8539 * originally calculated. Abuse current->journal_info for this.
8540 */
chandan50745b02015-08-28 21:10:13 +05308541 dio_data.reserve = round_up(count, root->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008542 dio_data.unsubmitted_oe_range_start = (u64)offset;
8543 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308544 current->journal_info = &dio_data;
David Sterbaee39b432014-09-30 01:33:33 +02008545 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8546 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008547 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008548 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8549 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008550 }
8551
Omar Sandoval17f8c842015-03-16 04:33:50 -07008552 ret = __blockdev_direct_IO(iocb, inode,
8553 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
8554 iter, offset, btrfs_get_blocks_direct, NULL,
8555 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008556 if (iov_iter_rw(iter) == WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008557 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008558 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308559 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008560 btrfs_delalloc_release_space(inode, offset,
8561 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008562 /*
8563 * On error we might have left some ordered extents
8564 * without submitting corresponding bios for them, so
8565 * cleanup them up to avoid other tasks getting them
8566 * and waiting for them to complete forever.
8567 */
8568 if (dio_data.unsubmitted_oe_range_start <
8569 dio_data.unsubmitted_oe_range_end)
8570 btrfs_endio_direct_write_update_ordered(inode,
8571 dio_data.unsubmitted_oe_range_start,
8572 dio_data.unsubmitted_oe_range_end -
8573 dio_data.unsubmitted_oe_range_start,
8574 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008575 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008576 btrfs_delalloc_release_space(inode, offset,
8577 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008578 }
Miao Xie38851cc2013-02-08 07:04:11 +00008579out:
Miao Xie2e60a512013-02-08 07:01:08 +00008580 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008581 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008582 if (relock)
8583 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008584
8585 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008586}
8587
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008588#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8589
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008590static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8591 __u64 start, __u64 len)
8592{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008593 int ret;
8594
8595 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8596 if (ret)
8597 return ret;
8598
Chris Masonec29ed52011-02-23 16:23:20 -05008599 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008600}
8601
Chris Mason9ebefb182007-06-15 13:50:00 -04008602int btrfs_readpage(struct file *file, struct page *page)
8603{
Chris Masond1310b22008-01-24 16:13:08 -05008604 struct extent_io_tree *tree;
8605 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008606 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008607}
Chris Mason1832a6d2007-12-21 16:27:21 -05008608
Chris Mason39279cc2007-06-12 06:35:45 -04008609static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8610{
Chris Masond1310b22008-01-24 16:13:08 -05008611 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008612 struct inode *inode = page->mapping->host;
8613 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008614
8615 if (current->flags & PF_MEMALLOC) {
8616 redirty_page_for_writepage(wbc, page);
8617 unlock_page(page);
8618 return 0;
8619 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008620
8621 /*
8622 * If we are under memory pressure we will call this directly from the
8623 * VM, we need to make sure we have the inode referenced for the ordered
8624 * extent. If not just return like we didn't do anything.
8625 */
8626 if (!igrab(inode)) {
8627 redirty_page_for_writepage(wbc, page);
8628 return AOP_WRITEPAGE_ACTIVATE;
8629 }
Chris Masond1310b22008-01-24 16:13:08 -05008630 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008631 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8632 btrfs_add_delayed_iput(inode);
8633 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008634}
Chris Mason39279cc2007-06-12 06:35:45 -04008635
Eric Sandeen48a3b632013-04-25 20:41:01 +00008636static int btrfs_writepages(struct address_space *mapping,
8637 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008638{
Chris Masond1310b22008-01-24 16:13:08 -05008639 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008640
Chris Masond1310b22008-01-24 16:13:08 -05008641 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008642 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8643}
8644
Chris Mason3ab2fb52007-11-08 10:59:22 -05008645static int
8646btrfs_readpages(struct file *file, struct address_space *mapping,
8647 struct list_head *pages, unsigned nr_pages)
8648{
Chris Masond1310b22008-01-24 16:13:08 -05008649 struct extent_io_tree *tree;
8650 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008651 return extent_readpages(tree, mapping, pages, nr_pages,
8652 btrfs_get_extent);
8653}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008654static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008655{
Chris Masond1310b22008-01-24 16:13:08 -05008656 struct extent_io_tree *tree;
8657 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008658 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008659
Chris Masond1310b22008-01-24 16:13:08 -05008660 tree = &BTRFS_I(page->mapping->host)->io_tree;
8661 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008662 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008663 if (ret == 1) {
8664 ClearPagePrivate(page);
8665 set_page_private(page, 0);
8666 page_cache_release(page);
8667 }
8668 return ret;
8669}
Chris Mason39279cc2007-06-12 06:35:45 -04008670
Chris Masone6dcd2d2008-07-17 12:53:50 -04008671static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8672{
Chris Mason98509cf2008-09-11 15:51:43 -04008673 if (PageWriteback(page) || PageDirty(page))
8674 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008675 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008676}
8677
Lukas Czernerd47992f2013-05-21 23:17:23 -04008678static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8679 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008680{
Josef Bacik5fd02042012-05-02 14:00:54 -04008681 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008682 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008683 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008684 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008685 u64 page_start = page_offset(page);
8686 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308687 u64 start;
8688 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008689 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008690
Chris Mason8b62b722009-09-02 16:53:46 -04008691 /*
8692 * we have the page locked, so new writeback can't start,
8693 * and the dirty bit won't be cleared while we are here.
8694 *
8695 * Wait for IO on this page so that we can safely clear
8696 * the PagePrivate2 bit and do ordered accounting
8697 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008698 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008699
Josef Bacik5fd02042012-05-02 14:00:54 -04008700 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008701 if (offset) {
8702 btrfs_releasepage(page, GFP_NOFS);
8703 return;
8704 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008705
8706 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008707 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308708again:
8709 start = page_start;
8710 ordered = btrfs_lookup_ordered_range(inode, start,
8711 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008712 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308713 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008714 /*
8715 * IO on this page will never be started, so we need
8716 * to account for any ordered extents now
8717 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008718 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308719 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008720 EXTENT_DIRTY | EXTENT_DELALLOC |
8721 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8722 EXTENT_DEFRAG, 1, 0, &cached_state,
8723 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008724 /*
8725 * whoever cleared the private bit is responsible
8726 * for the finish_ordered_io
8727 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008728 if (TestClearPagePrivate2(page)) {
8729 struct btrfs_ordered_inode_tree *tree;
8730 u64 new_len;
8731
8732 tree = &BTRFS_I(inode)->ordered_tree;
8733
8734 spin_lock_irq(&tree->lock);
8735 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308736 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008737 if (new_len < ordered->truncated_len)
8738 ordered->truncated_len = new_len;
8739 spin_unlock_irq(&tree->lock);
8740
8741 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308742 start,
8743 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008744 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008745 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008746 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008747 if (!inode_evicting) {
8748 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308749 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008750 &cached_state);
8751 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308752
8753 start = end + 1;
8754 if (start < page_end)
8755 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008756 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008757
Qu Wenruob9d0b382015-09-29 10:35:16 +08008758 /*
8759 * Qgroup reserved space handler
8760 * Page here will be either
8761 * 1) Already written to disk
8762 * In this case, its reserved space is released from data rsv map
8763 * and will be freed by delayed_ref handler finally.
8764 * So even we call qgroup_free_data(), it won't decrease reserved
8765 * space.
8766 * 2) Not written to disk
8767 * This means the reserved space should be freed here.
8768 */
8769 btrfs_qgroup_free_data(inode, page_start, PAGE_CACHE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008770 if (!inode_evicting) {
8771 clear_extent_bit(tree, page_start, page_end,
8772 EXTENT_LOCKED | EXTENT_DIRTY |
8773 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8774 EXTENT_DEFRAG, 1, 1,
8775 &cached_state, GFP_NOFS);
8776
8777 __btrfs_releasepage(page, GFP_NOFS);
8778 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008779
Chris Mason4a096752008-07-21 10:29:44 -04008780 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008781 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008782 ClearPagePrivate(page);
8783 set_page_private(page, 0);
8784 page_cache_release(page);
8785 }
Chris Mason39279cc2007-06-12 06:35:45 -04008786}
8787
Chris Mason9ebefb182007-06-15 13:50:00 -04008788/*
8789 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8790 * called from a page fault handler when a page is first dirtied. Hence we must
8791 * be careful to check for EOF conditions here. We set the page up correctly
8792 * for a written page which means we get ENOSPC checking when writing into
8793 * holes and correct delalloc and unwritten extent mapping on filesystems that
8794 * support these features.
8795 *
8796 * We are not allowed to take the i_mutex here so we have to play games to
8797 * protect against truncate races as the page could now be beyond EOF. Because
8798 * vmtruncate() writes the inode size before removing pages, once we have the
8799 * page lock we can determine safely if the page is beyond EOF. If it is not
8800 * beyond EOF, then the page is guaranteed safe against truncation until we
8801 * unlock the page.
8802 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008803int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008804{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008805 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008806 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008807 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008808 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8809 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008810 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008811 char *kaddr;
8812 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008813 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008814 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008815 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308816 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008817 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008818 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308819 u64 end;
8820
8821 reserved_space = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008822
Jan Karab2b5ef52012-06-12 16:20:45 +02008823 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008824 page_start = page_offset(page);
8825 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308826 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008827
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308828 /*
8829 * Reserving delalloc space after obtaining the page lock can lead to
8830 * deadlock. For example, if a dirty page is locked by this function
8831 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8832 * dirty page write out, then the btrfs_writepage() function could
8833 * end up waiting indefinitely to get a lock on the page currently
8834 * being processed by btrfs_page_mkwrite() function.
8835 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008836 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308837 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008838 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008839 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008840 reserved = 1;
8841 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008842 if (ret) {
8843 if (ret == -ENOMEM)
8844 ret = VM_FAULT_OOM;
8845 else /* -ENOSPC, -EIO, etc */
8846 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008847 if (reserved)
8848 goto out;
8849 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008850 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008851
Nick Piggin56a76f82009-03-31 15:23:23 -07008852 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008853again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008854 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008855 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008856
Chris Mason9ebefb182007-06-15 13:50:00 -04008857 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008858 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008859 /* page got truncated out from underneath us */
8860 goto out_unlock;
8861 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008862 wait_on_page_writeback(page);
8863
David Sterbaff13db42015-12-03 14:30:40 +01008864 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008865 set_page_extent_mapped(page);
8866
Chris Masoneb84ae02008-07-17 13:53:27 -04008867 /*
8868 * we can't set the delalloc bits if there are pending ordered
8869 * extents. Drop our locks and wait for them to finish
8870 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308871 ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008872 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008873 unlock_extent_cached(io_tree, page_start, page_end,
8874 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008875 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008876 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008877 btrfs_put_ordered_extent(ordered);
8878 goto again;
8879 }
8880
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308881 if (page->index == ((size - 1) >> PAGE_CACHE_SHIFT)) {
8882 reserved_space = round_up(size - page_start, root->sectorsize);
8883 if (reserved_space < PAGE_CACHE_SIZE) {
8884 end = page_start + reserved_space - 1;
8885 spin_lock(&BTRFS_I(inode)->lock);
8886 BTRFS_I(inode)->outstanding_extents++;
8887 spin_unlock(&BTRFS_I(inode)->lock);
8888 btrfs_delalloc_release_space(inode, page_start,
8889 PAGE_CACHE_SIZE - reserved_space);
8890 }
8891 }
8892
Josef Bacikfbf19082009-10-01 17:10:23 -04008893 /*
8894 * XXX - page_mkwrite gets called every time the page is dirtied, even
8895 * if it was already dirty, so for space accounting reasons we need to
8896 * clear any delalloc bits for the range we are fixing to save. There
8897 * is probably a better way to do this, but for now keep consistent with
8898 * prepare_pages in the normal write path.
8899 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308900 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008901 EXTENT_DIRTY | EXTENT_DELALLOC |
8902 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008903 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008904
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308905 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008906 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008907 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008908 unlock_extent_cached(io_tree, page_start, page_end,
8909 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008910 ret = VM_FAULT_SIGBUS;
8911 goto out_unlock;
8912 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008913 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008914
8915 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008916 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008917 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008918 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008919 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008920
Chris Masone6dcd2d2008-07-17 12:53:50 -04008921 if (zero_start != PAGE_CACHE_SIZE) {
8922 kaddr = kmap(page);
8923 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8924 flush_dcache_page(page);
8925 kunmap(page);
8926 }
Chris Mason247e7432008-07-17 12:53:51 -04008927 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008928 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008929 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008930
Chris Mason257c62e2009-10-13 13:21:08 -04008931 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8932 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008933 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008934
Josef Bacik2ac55d42010-02-03 19:33:23 +00008935 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008936
8937out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008938 if (!ret) {
8939 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008940 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008941 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008942 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008943out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308944 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008945out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008946 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008947 return ret;
8948}
8949
Josef Bacika41ad392011-01-31 15:30:16 -05008950static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008951{
8952 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008953 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008954 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008955 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008956 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008957 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008958 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008959
Josef Bacik0ef8b722013-10-25 16:13:35 -04008960 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8961 (u64)-1);
8962 if (ret)
8963 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008964
Josef Bacikfcb80c22011-05-03 10:40:22 -04008965 /*
8966 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8967 * 3 things going on here
8968 *
8969 * 1) We need to reserve space for our orphan item and the space to
8970 * delete our orphan item. Lord knows we don't want to have a dangling
8971 * orphan item because we didn't reserve space to remove it.
8972 *
8973 * 2) We need to reserve space to update our inode.
8974 *
8975 * 3) We need to have something to cache all the space that is going to
8976 * be free'd up by the truncate operation, but also have some slack
8977 * space reserved in case it uses space during the truncate (thank you
8978 * very much snapshotting).
8979 *
8980 * And we need these to all be seperate. The fact is we can use alot of
8981 * space doing the truncate, and we have no earthly idea how much space
8982 * we will use, so we need the truncate reservation to be seperate so it
8983 * doesn't end up using space reserved for updating the inode or
8984 * removing the orphan item. We also need to be able to stop the
8985 * transaction and start a new one, which means we need to be able to
8986 * update the inode several times, and we have no idea of knowing how
8987 * many times that will be, so we can't just reserve 1 item for the
8988 * entirety of the opration, so that has to be done seperately as well.
8989 * Then there is the orphan item, which does indeed need to be held on
8990 * to for the whole operation, and we need nobody to touch this reserved
8991 * space except the orphan code.
8992 *
8993 * So that leaves us with
8994 *
8995 * 1) root->orphan_block_rsv - for the orphan deletion.
8996 * 2) rsv - for the truncate reservation, which we will steal from the
8997 * transaction reservation.
8998 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8999 * updating the inode.
9000 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06009001 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009002 if (!rsv)
9003 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009004 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009005 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009006
Josef Bacik907cbce2011-08-08 13:46:15 -04009007 /*
Josef Bacik07127182011-08-19 10:29:59 -04009008 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009009 * 1 for updating the inode.
9010 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009011 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009012 if (IS_ERR(trans)) {
9013 err = PTR_ERR(trans);
9014 goto out;
9015 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009016
Josef Bacik907cbce2011-08-08 13:46:15 -04009017 /* Migrate the slack space for the truncate to our reserve */
9018 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
9019 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009020 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009021
Chris Mason5a3f23d2009-03-31 13:27:11 -04009022 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009023 * So if we truncate and then write and fsync we normally would just
9024 * write the extents that changed, which is a problem if we need to
9025 * first truncate that entire inode. So set this flag so we write out
9026 * all of the extents in the inode to the sync log so we're completely
9027 * safe.
9028 */
9029 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009030 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009031
Yan, Zheng80825102009-11-12 09:35:36 +00009032 while (1) {
9033 ret = btrfs_truncate_inode_items(trans, root, inode,
9034 inode->i_size,
9035 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009036 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009037 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009038 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009039 }
Chris Mason39279cc2007-06-12 06:35:45 -04009040
Josef Bacikfcb80c22011-05-03 10:40:22 -04009041 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009042 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009043 if (ret) {
9044 err = ret;
9045 break;
9046 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009047
Yan, Zheng80825102009-11-12 09:35:36 +00009048 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00009049 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009050
9051 trans = btrfs_start_transaction(root, 2);
9052 if (IS_ERR(trans)) {
9053 ret = err = PTR_ERR(trans);
9054 trans = NULL;
9055 break;
9056 }
9057
9058 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
9059 rsv, min_size);
9060 BUG_ON(ret); /* shouldn't happen */
9061 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009062 }
9063
9064 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009065 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009066 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009067 if (ret)
9068 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009069 }
9070
Chris Mason917c16b2011-11-08 14:49:59 -05009071 if (trans) {
9072 trans->block_rsv = &root->fs_info->trans_block_rsv;
9073 ret = btrfs_update_inode(trans, root, inode);
9074 if (ret && !err)
9075 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009076
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009077 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00009078 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05009079 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009080
9081out:
9082 btrfs_free_block_rsv(root, rsv);
9083
Josef Bacik3893e332011-01-31 16:03:11 -05009084 if (ret && !err)
9085 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009086
Josef Bacik3893e332011-01-31 16:03:11 -05009087 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009088}
9089
Sven Wegener3b963622008-06-09 21:57:42 -04009090/*
Chris Masond352ac62008-09-29 15:18:18 -04009091 * create a new subvolume directory/inode (helper for the ioctl).
9092 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009093int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009094 struct btrfs_root *new_root,
9095 struct btrfs_root *parent_root,
9096 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009097{
Chris Mason39279cc2007-06-12 06:35:45 -04009098 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009099 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009100 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009101
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009102 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9103 new_dirid, new_dirid,
9104 S_IFDIR | (~current_umask() & S_IRWXUGO),
9105 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009106 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009107 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009108 inode->i_op = &btrfs_dir_inode_operations;
9109 inode->i_fop = &btrfs_dir_file_operations;
9110
Miklos Szeredibfe86842011-10-28 14:13:29 +02009111 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009112 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009113 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009114
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009115 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9116 if (err)
9117 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009118 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009119 new_root->root_key.objectid, err);
9120
Yan, Zheng76dda932009-09-21 16:00:26 -04009121 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009122
Yan, Zheng76dda932009-09-21 16:00:26 -04009123 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009124 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009125}
9126
Chris Mason39279cc2007-06-12 06:35:45 -04009127struct inode *btrfs_alloc_inode(struct super_block *sb)
9128{
9129 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009130 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009131
9132 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9133 if (!ei)
9134 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009135
9136 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009137 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009138 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009139 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009140 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009141 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009142 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009143 ei->disk_i_size = 0;
9144 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009145 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009146 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009147 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009148 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009149 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009150 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009151
Josef Bacik9e0baf62011-07-15 15:16:44 +00009152 spin_lock_init(&ei->lock);
9153 ei->outstanding_extents = 0;
9154 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009155
Josef Bacik72ac3c02012-05-23 14:13:11 -04009156 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009157 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009158
Miao Xie16cdcec2011-04-22 18:12:22 +08009159 ei->delayed_node = NULL;
9160
chandan r9cc97d62012-07-04 12:48:07 +05309161 ei->i_otime.tv_sec = 0;
9162 ei->i_otime.tv_nsec = 0;
9163
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009164 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009165 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009166 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9167 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009168 ei->io_tree.track_uptodate = 1;
9169 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009170 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009171 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009172 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009173 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009174 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009175 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009176 RB_CLEAR_NODE(&ei->rb_node);
9177
9178 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009179}
9180
Josef Bacikaaedb552013-10-11 14:44:09 -04009181#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9182void btrfs_test_destroy_inode(struct inode *inode)
9183{
9184 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9185 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9186}
9187#endif
9188
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009189static void btrfs_i_callback(struct rcu_head *head)
9190{
9191 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009192 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9193}
9194
Chris Mason39279cc2007-06-12 06:35:45 -04009195void btrfs_destroy_inode(struct inode *inode)
9196{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009197 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009198 struct btrfs_root *root = BTRFS_I(inode)->root;
9199
Al Virob3d9b7a2012-06-09 13:51:19 -04009200 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009201 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009202 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9203 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009204 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9205 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009206 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009207
Chris Mason5a3f23d2009-03-31 13:27:11 -04009208 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009209 * This can happen where we create an inode, but somebody else also
9210 * created the same inode and we need to destroy the one we already
9211 * created.
9212 */
9213 if (!root)
9214 goto free;
9215
Josef Bacik8a35d952012-05-23 14:26:42 -04009216 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9217 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009218 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009219 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009220 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009221 }
Josef Bacik7b128762008-07-24 12:17:14 -04009222
Chris Masond3977122009-01-05 21:25:51 -05009223 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009224 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9225 if (!ordered)
9226 break;
9227 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009228 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009229 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009230 btrfs_remove_ordered_extent(inode, ordered);
9231 btrfs_put_ordered_extent(ordered);
9232 btrfs_put_ordered_extent(ordered);
9233 }
9234 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009235 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009236 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009237 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009238free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009239 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009240}
9241
Al Viro45321ac2010-06-07 13:43:19 -04009242int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009243{
9244 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009245
Naohiro Aota6379ef92013-06-06 09:56:34 +00009246 if (root == NULL)
9247 return 1;
9248
Liu Bofa6ac872013-02-20 14:10:23 +00009249 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009250 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009251 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009252 else
Al Viro45321ac2010-06-07 13:43:19 -04009253 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009254}
9255
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009256static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009257{
9258 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9259
9260 inode_init_once(&ei->vfs_inode);
9261}
9262
9263void btrfs_destroy_cachep(void)
9264{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009265 /*
9266 * Make sure all delayed rcu free inodes are flushed before we
9267 * destroy cache.
9268 */
9269 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04009270 if (btrfs_inode_cachep)
9271 kmem_cache_destroy(btrfs_inode_cachep);
9272 if (btrfs_trans_handle_cachep)
9273 kmem_cache_destroy(btrfs_trans_handle_cachep);
9274 if (btrfs_transaction_cachep)
9275 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009276 if (btrfs_path_cachep)
9277 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05009278 if (btrfs_free_space_cachep)
9279 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009280}
9281
9282int btrfs_init_cachep(void)
9283{
David Sterba837e1972012-09-07 03:00:48 -06009284 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009285 sizeof(struct btrfs_inode), 0,
9286 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009287 if (!btrfs_inode_cachep)
9288 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009289
David Sterba837e1972012-09-07 03:00:48 -06009290 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009291 sizeof(struct btrfs_trans_handle), 0,
9292 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009293 if (!btrfs_trans_handle_cachep)
9294 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009295
David Sterba837e1972012-09-07 03:00:48 -06009296 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009297 sizeof(struct btrfs_transaction), 0,
9298 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009299 if (!btrfs_transaction_cachep)
9300 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009301
David Sterba837e1972012-09-07 03:00:48 -06009302 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009303 sizeof(struct btrfs_path), 0,
9304 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009305 if (!btrfs_path_cachep)
9306 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009307
David Sterba837e1972012-09-07 03:00:48 -06009308 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009309 sizeof(struct btrfs_free_space), 0,
9310 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
9311 if (!btrfs_free_space_cachep)
9312 goto fail;
9313
Chris Mason39279cc2007-06-12 06:35:45 -04009314 return 0;
9315fail:
9316 btrfs_destroy_cachep();
9317 return -ENOMEM;
9318}
9319
9320static int btrfs_getattr(struct vfsmount *mnt,
9321 struct dentry *dentry, struct kstat *stat)
9322{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009323 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009324 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009325 u32 blocksize = inode->i_sb->s_blocksize;
9326
Chris Mason39279cc2007-06-12 06:35:45 -04009327 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009328 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05009329 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009330
9331 spin_lock(&BTRFS_I(inode)->lock);
9332 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9333 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009334 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009335 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009336 return 0;
9337}
9338
Chris Masond3977122009-01-05 21:25:51 -05009339static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9340 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009341{
9342 struct btrfs_trans_handle *trans;
9343 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009344 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009345 struct inode *new_inode = d_inode(new_dentry);
9346 struct inode *old_inode = d_inode(old_dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04009347 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009348 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009349 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009350 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009351 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009352
Li Zefan33345d012011-04-20 10:31:50 +08009353 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009354 return -EPERM;
9355
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009356 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009357 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009358 return -EXDEV;
9359
Li Zefan33345d012011-04-20 10:31:50 +08009360 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9361 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009362 return -ENOTEMPTY;
9363
Chris Mason39279cc2007-06-12 06:35:45 -04009364 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009365 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009366 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009367
9368
9369 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009370 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009371 new_dentry->d_name.name,
9372 new_dentry->d_name.len);
9373
9374 if (ret) {
9375 if (ret == -EEXIST) {
9376 /* we shouldn't get
9377 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309378 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009379 return ret;
9380 }
9381 } else {
9382 /* maybe -EOVERFLOW */
9383 return ret;
9384 }
9385 }
9386 ret = 0;
9387
Chris Mason5a3f23d2009-03-31 13:27:11 -04009388 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009389 * we're using rename to replace one file with another. Start IO on it
9390 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009391 */
Chris Mason8d875f92014-08-12 10:47:42 -07009392 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009393 filemap_flush(old_inode->i_mapping);
9394
Yan, Zheng76dda932009-09-21 16:00:26 -04009395 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009396 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009397 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009398 /*
9399 * We want to reserve the absolute worst case amount of items. So if
9400 * both inodes are subvols and we need to unlink them then that would
9401 * require 4 item modifications, but if they are both normal inodes it
9402 * would require 5 item modifications, so we'll assume their normal
9403 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9404 * should cover the worst case number of items we'll modify.
9405 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04009406 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009407 if (IS_ERR(trans)) {
9408 ret = PTR_ERR(trans);
9409 goto out_notrans;
9410 }
Chris Mason5f39d392007-10-15 16:14:19 -04009411
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009412 if (dest != root)
9413 btrfs_record_root_in_trans(trans, dest);
9414
Yan, Zhenga5719522009-09-24 09:17:31 -04009415 ret = btrfs_set_inode_index(new_dir, &index);
9416 if (ret)
9417 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009418
Miao Xie67de1172013-12-26 13:07:06 +08009419 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009420 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009421 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009422 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009423 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009424 ret = btrfs_insert_inode_ref(trans, dest,
9425 new_dentry->d_name.name,
9426 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009427 old_ino,
9428 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009429 if (ret)
9430 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009431 /*
9432 * this is an ugly little race, but the rename is required
9433 * to make sure that if we crash, the inode is either at the
9434 * old name or the new one. pinning the log transaction lets
9435 * us make sure we don't allow a log commit to come in after
9436 * we unlink the name but before we add the new name back in.
9437 */
9438 btrfs_pin_log_trans(root);
9439 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009440
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009441 inode_inc_iversion(old_dir);
9442 inode_inc_iversion(new_dir);
9443 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009444 old_dir->i_ctime = old_dir->i_mtime = ctime;
9445 new_dir->i_ctime = new_dir->i_mtime = ctime;
9446 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04009447
Chris Mason12fcfd22009-03-24 10:24:20 -04009448 if (old_dentry->d_parent != new_dentry->d_parent)
9449 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9450
Li Zefan33345d012011-04-20 10:31:50 +08009451 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009452 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9453 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9454 old_dentry->d_name.name,
9455 old_dentry->d_name.len);
9456 } else {
Al Viro92986792011-03-04 17:14:37 +00009457 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009458 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009459 old_dentry->d_name.name,
9460 old_dentry->d_name.len);
9461 if (!ret)
9462 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009463 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009464 if (ret) {
9465 btrfs_abort_transaction(trans, root, ret);
9466 goto out_fail;
9467 }
Chris Mason39279cc2007-06-12 06:35:45 -04009468
9469 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009470 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009471 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08009472 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009473 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9474 root_objectid = BTRFS_I(new_inode)->location.objectid;
9475 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9476 root_objectid,
9477 new_dentry->d_name.name,
9478 new_dentry->d_name.len);
9479 BUG_ON(new_inode->i_nlink == 0);
9480 } else {
9481 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009482 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009483 new_dentry->d_name.name,
9484 new_dentry->d_name.len);
9485 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009486 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009487 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009488 if (ret) {
9489 btrfs_abort_transaction(trans, root, ret);
9490 goto out_fail;
9491 }
Chris Mason39279cc2007-06-12 06:35:45 -04009492 }
Josef Bacikaec74772008-07-24 12:12:38 -04009493
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009494 ret = btrfs_add_link(trans, new_dir, old_inode,
9495 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009496 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009497 if (ret) {
9498 btrfs_abort_transaction(trans, root, ret);
9499 goto out_fail;
9500 }
Chris Mason39279cc2007-06-12 06:35:45 -04009501
Miao Xie67de1172013-12-26 13:07:06 +08009502 if (old_inode->i_nlink == 1)
9503 BTRFS_I(old_inode)->dir_index = index;
9504
Li Zefan33345d012011-04-20 10:31:50 +08009505 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04009506 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00009507 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009508 btrfs_end_log_trans(root);
9509 }
Chris Mason39279cc2007-06-12 06:35:45 -04009510out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009511 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009512out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009513 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009514 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009515
Chris Mason39279cc2007-06-12 06:35:45 -04009516 return ret;
9517}
9518
Miklos Szeredi80ace852014-07-23 15:15:32 +02009519static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9520 struct inode *new_dir, struct dentry *new_dentry,
9521 unsigned int flags)
9522{
9523 if (flags & ~RENAME_NOREPLACE)
9524 return -EINVAL;
9525
9526 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
9527}
9528
Miao Xie8ccf6f192012-10-25 09:28:04 +00009529static void btrfs_run_delalloc_work(struct btrfs_work *work)
9530{
9531 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009532 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009533
9534 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9535 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009536 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009537 filemap_flush(inode->i_mapping);
9538 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9539 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009540 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009541
9542 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009543 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009544 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009545 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009546 complete(&delalloc_work->completion);
9547}
9548
9549struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +01009550 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009551{
9552 struct btrfs_delalloc_work *work;
9553
David Sterba100d5702015-12-08 14:39:32 +01009554 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009555 if (!work)
9556 return NULL;
9557
9558 init_completion(&work->completion);
9559 INIT_LIST_HEAD(&work->list);
9560 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009561 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009562 WARN_ON_ONCE(!inode);
9563 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9564 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009565
9566 return work;
9567}
9568
9569void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9570{
9571 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +01009572 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009573}
9574
Chris Masond352ac62008-09-29 15:18:18 -04009575/*
9576 * some fairly slow code that needs optimization. This walks the list
9577 * of all the inodes with pending delalloc and forces them to disk.
9578 */
Miao Xie6c255e62014-03-06 13:55:01 +08009579static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9580 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009581{
Chris Masonea8c2812008-08-04 23:17:27 -04009582 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009583 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009584 struct btrfs_delalloc_work *work, *next;
9585 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009586 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009587 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009588
Miao Xie8ccf6f192012-10-25 09:28:04 +00009589 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009590 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009591
Miao Xie573bfb72014-03-06 13:55:03 +08009592 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009593 spin_lock(&root->delalloc_lock);
9594 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009595 while (!list_empty(&splice)) {
9596 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009597 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009598
Miao Xieeb73c1b2013-05-15 07:48:22 +00009599 list_move_tail(&binode->delalloc_inodes,
9600 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009601 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009602 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009603 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009604 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009605 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009606 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009607
David Sterba651d4942015-11-27 19:24:16 +01009608 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009609 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009610 if (delay_iput)
9611 btrfs_add_delayed_iput(inode);
9612 else
9613 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009614 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009615 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009616 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009617 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009618 btrfs_queue_work(root->fs_info->flush_workers,
9619 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009620 ret++;
9621 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009622 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009623 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009624 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009625 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009626 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee1d2008-09-29 11:19:10 -04009627
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009628out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009629 list_for_each_entry_safe(work, next, &works, list) {
9630 list_del_init(&work->list);
9631 btrfs_wait_and_free_delalloc_work(work);
9632 }
9633
Miao Xieeb73c1b2013-05-15 07:48:22 +00009634 if (!list_empty_careful(&splice)) {
9635 spin_lock(&root->delalloc_lock);
9636 list_splice_tail(&splice, &root->delalloc_inodes);
9637 spin_unlock(&root->delalloc_lock);
9638 }
Miao Xie573bfb72014-03-06 13:55:03 +08009639 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009640 return ret;
9641}
9642
9643int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9644{
9645 int ret;
9646
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009647 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009648 return -EROFS;
9649
Miao Xie6c255e62014-03-06 13:55:01 +08009650 ret = __start_delalloc_inodes(root, delay_iput, -1);
9651 if (ret > 0)
9652 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009653 /*
9654 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee1d2008-09-29 11:19:10 -04009655 * we have to make sure the IO is actually started and that
9656 * ordered extents get created before we return
9657 */
9658 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009659 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009660 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee1d2008-09-29 11:19:10 -04009661 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009662 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9663 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee1d2008-09-29 11:19:10 -04009664 }
9665 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009666 return ret;
9667}
9668
Miao Xie6c255e62014-03-06 13:55:01 +08009669int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9670 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009671{
9672 struct btrfs_root *root;
9673 struct list_head splice;
9674 int ret;
9675
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009676 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009677 return -EROFS;
9678
9679 INIT_LIST_HEAD(&splice);
9680
Miao Xie573bfb72014-03-06 13:55:03 +08009681 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009682 spin_lock(&fs_info->delalloc_root_lock);
9683 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009684 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009685 root = list_first_entry(&splice, struct btrfs_root,
9686 delalloc_root);
9687 root = btrfs_grab_fs_root(root);
9688 BUG_ON(!root);
9689 list_move_tail(&root->delalloc_root,
9690 &fs_info->delalloc_roots);
9691 spin_unlock(&fs_info->delalloc_root_lock);
9692
Miao Xie6c255e62014-03-06 13:55:01 +08009693 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009694 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009695 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009696 goto out;
9697
Miao Xie6c255e62014-03-06 13:55:01 +08009698 if (nr != -1) {
9699 nr -= ret;
9700 WARN_ON(nr < 0);
9701 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009702 spin_lock(&fs_info->delalloc_root_lock);
9703 }
9704 spin_unlock(&fs_info->delalloc_root_lock);
9705
Miao Xie6c255e62014-03-06 13:55:01 +08009706 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009707 atomic_inc(&fs_info->async_submit_draining);
9708 while (atomic_read(&fs_info->nr_async_submits) ||
9709 atomic_read(&fs_info->async_delalloc_pages)) {
9710 wait_event(fs_info->async_submit_wait,
9711 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9712 atomic_read(&fs_info->async_delalloc_pages) == 0));
9713 }
9714 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009715out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009716 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009717 spin_lock(&fs_info->delalloc_root_lock);
9718 list_splice_tail(&splice, &fs_info->delalloc_roots);
9719 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009720 }
Miao Xie573bfb72014-03-06 13:55:03 +08009721 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009722 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009723}
9724
Chris Mason39279cc2007-06-12 06:35:45 -04009725static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9726 const char *symname)
9727{
9728 struct btrfs_trans_handle *trans;
9729 struct btrfs_root *root = BTRFS_I(dir)->root;
9730 struct btrfs_path *path;
9731 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009732 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009733 int err;
9734 int drop_inode = 0;
9735 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309736 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009737 int name_len;
9738 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009739 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009740 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009741 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009742
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009743 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009744 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9745 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009746
Josef Bacik9ed74f22009-09-11 16:12:44 -04009747 /*
9748 * 2 items for inode item and ref
9749 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009750 * 1 item for updating parent inode item
9751 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009752 * 1 item for xattr if selinux is on
9753 */
Filipe Manana9269d122015-12-31 18:16:29 +00009754 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009755 if (IS_ERR(trans))
9756 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009757
Li Zefan581bb052011-04-20 10:06:11 +08009758 err = btrfs_find_free_ino(root, &objectid);
9759 if (err)
9760 goto out_unlock;
9761
Josef Bacikaec74772008-07-24 12:12:38 -04009762 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009763 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04009764 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009765 if (IS_ERR(inode)) {
9766 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009767 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009768 }
Chris Mason39279cc2007-06-12 06:35:45 -04009769
Casey Schauflerad19db72011-12-15 10:09:07 -05009770 /*
9771 * If the active LSM wants to access the inode during
9772 * d_instantiate it needs these. Smack checks to see
9773 * if the filesystem supports xattrs by looking at the
9774 * ops vector.
9775 */
9776 inode->i_fop = &btrfs_file_operations;
9777 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009778 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009779 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9780
9781 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9782 if (err)
9783 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009784
Chris Mason39279cc2007-06-12 06:35:45 -04009785 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009786 if (!path) {
9787 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009788 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009789 }
Li Zefan33345d012011-04-20 10:31:50 +08009790 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009791 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009792 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009793 datasize = btrfs_file_extent_calc_inline_size(name_len);
9794 err = btrfs_insert_empty_item(trans, root, path, &key,
9795 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009796 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009797 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009798 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009799 }
Chris Mason5f39d392007-10-15 16:14:19 -04009800 leaf = path->nodes[0];
9801 ei = btrfs_item_ptr(leaf, path->slots[0],
9802 struct btrfs_file_extent_item);
9803 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9804 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009805 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009806 btrfs_set_file_extent_encryption(leaf, ei, 0);
9807 btrfs_set_file_extent_compression(leaf, ei, 0);
9808 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9809 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9810
Chris Mason39279cc2007-06-12 06:35:45 -04009811 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009812 write_extent_buffer(leaf, symname, ptr, name_len);
9813 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009814 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009815
Chris Mason39279cc2007-06-12 06:35:45 -04009816 inode->i_op = &btrfs_symlink_inode_operations;
9817 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -04009818 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009819 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009820 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009821 /*
9822 * Last step, add directory indexes for our symlink inode. This is the
9823 * last step to avoid extra cleanup of these indexes if an error happens
9824 * elsewhere above.
9825 */
9826 if (!err)
9827 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07009828 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009829 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009830 goto out_unlock_inode;
9831 }
9832
9833 unlock_new_inode(inode);
9834 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009835
9836out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009837 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009838 if (drop_inode) {
9839 inode_dec_link_count(inode);
9840 iput(inode);
9841 }
Liu Bob53d3f52012-11-14 14:34:34 +00009842 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009843 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009844
9845out_unlock_inode:
9846 drop_inode = 1;
9847 unlock_new_inode(inode);
9848 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009849}
Chris Mason16432982008-04-10 10:23:21 -04009850
Josef Bacik0af3d002010-06-21 14:48:16 -04009851static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9852 u64 start, u64 num_bytes, u64 min_size,
9853 loff_t actual_len, u64 *alloc_hint,
9854 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009855{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009856 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9857 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009858 struct btrfs_root *root = BTRFS_I(inode)->root;
9859 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009860 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009861 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009862 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009863 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009864 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009865 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009866
Josef Bacik0af3d002010-06-21 14:48:16 -04009867 if (trans)
9868 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009869 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009870 if (own_trans) {
9871 trans = btrfs_start_transaction(root, 3);
9872 if (IS_ERR(trans)) {
9873 ret = PTR_ERR(trans);
9874 break;
9875 }
Yan Zhengd899e052008-10-30 14:25:28 -04009876 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009877
Byongho Leeee221842015-12-15 01:42:10 +09009878 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009879 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009880 /*
9881 * If we are severely fragmented we could end up with really
9882 * small allocations, so if the allocator is returning small
9883 * chunks lets make its job easier by only searching for those
9884 * sized chunks.
9885 */
9886 cur_bytes = min(cur_bytes, last_alloc);
Josef Bacik00361582013-08-14 14:02:47 -04009887 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009888 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009889 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009890 if (own_trans)
9891 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009892 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009893 }
9894
Josef Bacik0b670dc2015-09-23 17:11:16 -04009895 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04009896 ret = insert_reserved_file_extent(trans, inode,
9897 cur_offset, ins.objectid,
9898 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009899 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009900 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009901 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009902 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009903 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009904 btrfs_abort_transaction(trans, root, ret);
9905 if (own_trans)
9906 btrfs_end_transaction(trans, root);
9907 break;
9908 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009909
Chris Masona1ed8352009-09-11 12:27:37 -04009910 btrfs_drop_extent_cache(inode, cur_offset,
9911 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009912
Josef Bacik5dc562c2012-08-17 13:14:17 -04009913 em = alloc_extent_map();
9914 if (!em) {
9915 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9916 &BTRFS_I(inode)->runtime_flags);
9917 goto next;
9918 }
9919
9920 em->start = cur_offset;
9921 em->orig_start = cur_offset;
9922 em->len = ins.offset;
9923 em->block_start = ins.objectid;
9924 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009925 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009926 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009927 em->bdev = root->fs_info->fs_devices->latest_bdev;
9928 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9929 em->generation = trans->transid;
9930
9931 while (1) {
9932 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009933 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009934 write_unlock(&em_tree->lock);
9935 if (ret != -EEXIST)
9936 break;
9937 btrfs_drop_extent_cache(inode, cur_offset,
9938 cur_offset + ins.offset - 1,
9939 0);
9940 }
9941 free_extent_map(em);
9942next:
Yan Zhengd899e052008-10-30 14:25:28 -04009943 num_bytes -= ins.offset;
9944 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009945 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009946
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009947 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009948 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009949 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009950 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009951 (actual_len > inode->i_size) &&
9952 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009953 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009954 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009955 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009956 i_size = cur_offset;
9957 i_size_write(inode, i_size);
9958 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009959 }
9960
Yan Zhengd899e052008-10-30 14:25:28 -04009961 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009962
9963 if (ret) {
9964 btrfs_abort_transaction(trans, root, ret);
9965 if (own_trans)
9966 btrfs_end_transaction(trans, root);
9967 break;
9968 }
Yan Zhengd899e052008-10-30 14:25:28 -04009969
Josef Bacik0af3d002010-06-21 14:48:16 -04009970 if (own_trans)
9971 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009972 }
Yan Zhengd899e052008-10-30 14:25:28 -04009973 return ret;
9974}
9975
Josef Bacik0af3d002010-06-21 14:48:16 -04009976int btrfs_prealloc_file_range(struct inode *inode, int mode,
9977 u64 start, u64 num_bytes, u64 min_size,
9978 loff_t actual_len, u64 *alloc_hint)
9979{
9980 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9981 min_size, actual_len, alloc_hint,
9982 NULL);
9983}
9984
9985int btrfs_prealloc_file_range_trans(struct inode *inode,
9986 struct btrfs_trans_handle *trans, int mode,
9987 u64 start, u64 num_bytes, u64 min_size,
9988 loff_t actual_len, u64 *alloc_hint)
9989{
9990 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9991 min_size, actual_len, alloc_hint, trans);
9992}
9993
Chris Masone6dcd2d2008-07-17 12:53:50 -04009994static int btrfs_set_page_dirty(struct page *page)
9995{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009996 return __set_page_dirty_nobuffers(page);
9997}
9998
Al Viro10556cb2011-06-20 19:28:19 -04009999static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010000{
Li Zefanb83cc962010-12-20 16:04:08 +080010001 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010002 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010003
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010004 if (mask & MAY_WRITE &&
10005 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10006 if (btrfs_root_readonly(root))
10007 return -EROFS;
10008 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10009 return -EACCES;
10010 }
Al Viro2830ba72011-06-20 19:16:29 -040010011 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010012}
Chris Mason39279cc2007-06-12 06:35:45 -040010013
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010014static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10015{
10016 struct btrfs_trans_handle *trans;
10017 struct btrfs_root *root = BTRFS_I(dir)->root;
10018 struct inode *inode = NULL;
10019 u64 objectid;
10020 u64 index;
10021 int ret = 0;
10022
10023 /*
10024 * 5 units required for adding orphan entry
10025 */
10026 trans = btrfs_start_transaction(root, 5);
10027 if (IS_ERR(trans))
10028 return PTR_ERR(trans);
10029
10030 ret = btrfs_find_free_ino(root, &objectid);
10031 if (ret)
10032 goto out;
10033
10034 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
10035 btrfs_ino(dir), objectid, mode, &index);
10036 if (IS_ERR(inode)) {
10037 ret = PTR_ERR(inode);
10038 inode = NULL;
10039 goto out;
10040 }
10041
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010042 inode->i_fop = &btrfs_file_operations;
10043 inode->i_op = &btrfs_file_inode_operations;
10044
10045 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010046 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10047
Chris Masonb0d5d102014-09-08 13:08:51 -070010048 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10049 if (ret)
10050 goto out_inode;
10051
10052 ret = btrfs_update_inode(trans, root, inode);
10053 if (ret)
10054 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010055 ret = btrfs_orphan_add(trans, inode);
10056 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010057 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010058
Filipe Manana5762b5c2014-08-01 00:10:32 +010010059 /*
10060 * We set number of links to 0 in btrfs_new_inode(), and here we set
10061 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10062 * through:
10063 *
10064 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10065 */
10066 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010067 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010068 d_tmpfile(dentry, inode);
10069 mark_inode_dirty(inode);
10070
10071out:
10072 btrfs_end_transaction(trans, root);
10073 if (ret)
10074 iput(inode);
10075 btrfs_balance_delayed_items(root);
10076 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010077 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010078
10079out_inode:
10080 unlock_new_inode(inode);
10081 goto out;
10082
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010083}
10084
Filipe Mananab38ef712014-11-13 17:01:45 +000010085/* Inspired by filemap_check_errors() */
10086int btrfs_inode_check_errors(struct inode *inode)
10087{
10088 int ret = 0;
10089
10090 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
10091 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
10092 ret = -ENOSPC;
10093 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
10094 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
10095 ret = -EIO;
10096
10097 return ret;
10098}
10099
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010100static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010101 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010102 .lookup = btrfs_lookup,
10103 .create = btrfs_create,
10104 .unlink = btrfs_unlink,
10105 .link = btrfs_link,
10106 .mkdir = btrfs_mkdir,
10107 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +020010108 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010109 .symlink = btrfs_symlink,
10110 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010111 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010112 .setxattr = btrfs_setxattr,
10113 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010114 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010115 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -050010116 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010117 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010118 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010119 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010120 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010121};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010122static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010123 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010124 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010125 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010126 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010127 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010128};
Yan, Zheng76dda932009-09-21 16:00:26 -040010129
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010130static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010131 .llseek = generic_file_llseek,
10132 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -040010133 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010134 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010135#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010136 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010137#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010138 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010139 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010140};
10141
David Sterba20e55062015-11-19 11:42:28 +010010142static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010143 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010144 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010145 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010146 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010147 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010148 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010149 .set_bit_hook = btrfs_set_bit_hook,
10150 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010151 .merge_extent_hook = btrfs_merge_extent_hook,
10152 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010153};
10154
Chris Mason35054392009-01-21 13:11:13 -050010155/*
10156 * btrfs doesn't support the bmap operation because swapfiles
10157 * use bmap to make a mapping of extents in the file. They assume
10158 * these extents won't change over the life of the file and they
10159 * use the bmap result to do IO directly to the drive.
10160 *
10161 * the btrfs bmap call would return logical addresses that aren't
10162 * suitable for IO and they also will change frequently as COW
10163 * operations happen. So, swapfile + btrfs == corruption.
10164 *
10165 * For now we're avoiding this by dropping bmap.
10166 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010167static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010168 .readpage = btrfs_readpage,
10169 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010170 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010171 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010172 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010173 .invalidatepage = btrfs_invalidatepage,
10174 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010175 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010176 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010177};
10178
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010179static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010180 .readpage = btrfs_readpage,
10181 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010182 .invalidatepage = btrfs_invalidatepage,
10183 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010184};
10185
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010186static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010187 .getattr = btrfs_getattr,
10188 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010189 .setxattr = btrfs_setxattr,
10190 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010191 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010192 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -050010193 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010194 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010195 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010196 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010197 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010198};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010199static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010200 .getattr = btrfs_getattr,
10201 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010202 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010203 .setxattr = btrfs_setxattr,
10204 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -040010205 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010206 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010207 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010208 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010209 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010210};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010211static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010212 .readlink = generic_readlink,
10213 .follow_link = page_follow_link_light,
10214 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +000010215 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010216 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010217 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010218 .setxattr = btrfs_setxattr,
10219 .getxattr = btrfs_getxattr,
10220 .listxattr = btrfs_listxattr,
10221 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010222 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010223};
Yan, Zheng76dda932009-09-21 16:00:26 -040010224
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010225const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010226 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010227 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010228};