blob: dc2c1bedbafa0290a2318cf0d5b2cf36191947f7 [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
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070069static const struct inode_operations btrfs_dir_inode_operations;
70static const struct inode_operations btrfs_symlink_inode_operations;
71static const struct inode_operations btrfs_dir_ro_inode_operations;
72static const struct inode_operations btrfs_special_inode_operations;
73static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070074static const struct address_space_operations btrfs_aops;
75static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070076static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010077static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040078
79static struct kmem_cache *btrfs_inode_cachep;
80struct kmem_cache *btrfs_trans_handle_cachep;
81struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040082struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050083struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040084
85#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010086static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040087 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
88 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
89 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
90 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
91 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
92 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
93 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
94};
95
Eric Sandeen3972f262013-01-12 02:57:22 +000096static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050097static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040098static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050099static noinline int cow_file_range(struct inode *inode,
100 struct page *locked_page,
101 u64 start, u64 end, int *page_started,
102 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400103static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
104 u64 len, u64 orig_start,
105 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400106 u64 orig_block_len, u64 ram_bytes,
107 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400108
Eric Sandeen48a3b632013-04-25 20:41:01 +0000109static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400110
Josef Bacik6a3891c2015-03-16 17:38:52 -0400111#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
112void btrfs_test_inode_set_ops(struct inode *inode)
113{
114 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
115}
116#endif
117
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000118static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500119 struct inode *inode, struct inode *dir,
120 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500121{
122 int err;
123
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000124 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500125 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500126 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500127 return err;
128}
129
Chris Masond352ac62008-09-29 15:18:18 -0400130/*
Chris Masonc8b97812008-10-29 14:49:59 -0400131 * this does all the hard work for inserting an inline extent into
132 * the btree. The caller should have done a btrfs_drop_extents so that
133 * no overlapping inline items exist in the btree
134 */
Chris Mason40f76582014-05-21 13:35:51 -0700135static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000136 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400137 struct btrfs_root *root, struct inode *inode,
138 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000139 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400140 struct page **compressed_pages)
141{
Chris Masonc8b97812008-10-29 14:49:59 -0400142 struct extent_buffer *leaf;
143 struct page *page = NULL;
144 char *kaddr;
145 unsigned long ptr;
146 struct btrfs_file_extent_item *ei;
147 int err = 0;
148 int ret;
149 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400150 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400151
Li Zefanfe3f5662011-03-28 08:30:38 +0000152 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400153 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400154
Chris Masonc8b97812008-10-29 14:49:59 -0400155 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000156
157 if (!extent_inserted) {
158 struct btrfs_key key;
159 size_t datasize;
160
161 key.objectid = btrfs_ino(inode);
162 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200163 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000164
165 datasize = btrfs_file_extent_calc_inline_size(cur_size);
166 path->leave_spinning = 1;
167 ret = btrfs_insert_empty_item(trans, root, path, &key,
168 datasize);
169 if (ret) {
170 err = ret;
171 goto fail;
172 }
Chris Masonc8b97812008-10-29 14:49:59 -0400173 }
174 leaf = path->nodes[0];
175 ei = btrfs_item_ptr(leaf, path->slots[0],
176 struct btrfs_file_extent_item);
177 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
178 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
179 btrfs_set_file_extent_encryption(leaf, ei, 0);
180 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
181 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
182 ptr = btrfs_file_extent_inline_start(ei);
183
Li Zefan261507a02010-12-17 14:21:50 +0800184 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400185 struct page *cpage;
186 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500187 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400188 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500189 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400190 PAGE_CACHE_SIZE);
191
Cong Wang7ac687d2011-11-25 23:14:28 +0800192 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400193 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800194 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400195
196 i++;
197 ptr += cur_size;
198 compressed_size -= cur_size;
199 }
200 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800201 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400202 } else {
203 page = find_get_page(inode->i_mapping,
204 start >> PAGE_CACHE_SHIFT);
205 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800206 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400207 offset = start & (PAGE_CACHE_SIZE - 1);
208 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800209 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400210 page_cache_release(page);
211 }
212 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000213 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400214
Yan, Zhengc2167752009-11-12 09:34:21 +0000215 /*
216 * we're an inline extent, so nobody can
217 * extend the file past i_size without locking
218 * a page we already have locked.
219 *
220 * We must do any isize and inode updates
221 * before we unlock the pages. Otherwise we
222 * could end up racing with unlink.
223 */
Chris Masonc8b97812008-10-29 14:49:59 -0400224 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100225 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000226
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100227 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400228fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400229 return err;
230}
231
232
233/*
234 * conditionally insert an inline extent into the file. This
235 * does the checks required to make sure the data is small enough
236 * to fit as an inline extent.
237 */
Josef Bacik00361582013-08-14 14:02:47 -0400238static noinline int cow_file_range_inline(struct btrfs_root *root,
239 struct inode *inode, u64 start,
240 u64 end, size_t compressed_size,
241 int compress_type,
242 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400243{
Josef Bacik00361582013-08-14 14:02:47 -0400244 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400245 u64 isize = i_size_read(inode);
246 u64 actual_end = min(end + 1, isize);
247 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000248 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400249 u64 data_len = inline_len;
250 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000251 struct btrfs_path *path;
252 int extent_inserted = 0;
253 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400254
255 if (compressed_size)
256 data_len = compressed_size;
257
258 if (start > 0 ||
Wang Shilong354877b2014-07-17 11:44:11 +0800259 actual_end > PAGE_CACHE_SIZE ||
260 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400261 (!compressed_size &&
262 (actual_end & (root->sectorsize - 1)) == 0) ||
263 end + 1 < isize ||
264 data_len > root->fs_info->max_inline) {
265 return 1;
266 }
267
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000268 path = btrfs_alloc_path();
269 if (!path)
270 return -ENOMEM;
271
Josef Bacik00361582013-08-14 14:02:47 -0400272 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000273 if (IS_ERR(trans)) {
274 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400275 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000276 }
Josef Bacik00361582013-08-14 14:02:47 -0400277 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
278
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000279 if (compressed_size && compressed_pages)
280 extent_item_size = btrfs_file_extent_calc_inline_size(
281 compressed_size);
282 else
283 extent_item_size = btrfs_file_extent_calc_inline_size(
284 inline_len);
285
286 ret = __btrfs_drop_extents(trans, root, inode, path,
287 start, aligned_end, NULL,
288 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400289 if (ret) {
290 btrfs_abort_transaction(trans, root, ret);
291 goto out;
292 }
Chris Masonc8b97812008-10-29 14:49:59 -0400293
294 if (isize > actual_end)
295 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000296 ret = insert_inline_extent(trans, path, extent_inserted,
297 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400298 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000299 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400300 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100301 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400302 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400303 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400304 ret = 1;
305 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100306 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400307
Josef Bacikbdc20e62013-02-28 13:23:38 -0500308 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400309 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400310 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400311out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800312 /*
313 * Don't forget to free the reserved space, as for inlined extent
314 * it won't count as data extent, free them directly here.
315 * And at reserve time, it's always aligned to page size, so
316 * just free one page here.
317 */
318 btrfs_qgroup_free_data(inode, 0, PAGE_CACHE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000319 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400320 btrfs_end_transaction(trans, root);
321 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400322}
323
Chris Mason771ed682008-11-06 22:02:51 -0500324struct async_extent {
325 u64 start;
326 u64 ram_size;
327 u64 compressed_size;
328 struct page **pages;
329 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800330 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500331 struct list_head list;
332};
333
334struct async_cow {
335 struct inode *inode;
336 struct btrfs_root *root;
337 struct page *locked_page;
338 u64 start;
339 u64 end;
340 struct list_head extents;
341 struct btrfs_work work;
342};
343
344static noinline int add_async_extent(struct async_cow *cow,
345 u64 start, u64 ram_size,
346 u64 compressed_size,
347 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800348 unsigned long nr_pages,
349 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500350{
351 struct async_extent *async_extent;
352
353 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100354 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500355 async_extent->start = start;
356 async_extent->ram_size = ram_size;
357 async_extent->compressed_size = compressed_size;
358 async_extent->pages = pages;
359 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800360 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500361 list_add_tail(&async_extent->list, &cow->extents);
362 return 0;
363}
364
Wang Shilongf79707b2014-07-17 11:44:09 +0800365static inline int inode_need_compress(struct inode *inode)
366{
367 struct btrfs_root *root = BTRFS_I(inode)->root;
368
369 /* force compress */
370 if (btrfs_test_opt(root, FORCE_COMPRESS))
371 return 1;
372 /* bad compression ratios */
373 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
374 return 0;
375 if (btrfs_test_opt(root, COMPRESS) ||
376 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
377 BTRFS_I(inode)->force_compress)
378 return 1;
379 return 0;
380}
381
Chris Masonc8b97812008-10-29 14:49:59 -0400382/*
Chris Mason771ed682008-11-06 22:02:51 -0500383 * we create compressed extents in two phases. The first
384 * phase compresses a range of pages that have already been
385 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400386 *
Chris Mason771ed682008-11-06 22:02:51 -0500387 * This is done inside an ordered work queue, and the compression
388 * is spread across many cpus. The actual IO submission is step
389 * two, and the ordered work queue takes care of making sure that
390 * happens in the same order things were put onto the queue by
391 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400392 *
Chris Mason771ed682008-11-06 22:02:51 -0500393 * If this code finds it can't get good compression, it puts an
394 * entry onto the work queue to write the uncompressed bytes. This
395 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300396 * are written in the same order that the flusher thread sent them
397 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400398 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100399static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500400 struct page *locked_page,
401 u64 start, u64 end,
402 struct async_cow *async_cow,
403 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400404{
405 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400406 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400407 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400408 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500409 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400410 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400411 struct page **pages = NULL;
412 unsigned long nr_pages;
413 unsigned long nr_pages_ret = 0;
414 unsigned long total_compressed = 0;
415 unsigned long total_in = 0;
416 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500417 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400418 int i;
419 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800420 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400421 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400422
Liu Bo4cb13e52012-03-29 09:57:45 -0400423 /* if this is a small write inside eof, kick off a defrag */
424 if ((end - start + 1) < 16 * 1024 &&
425 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400426 btrfs_add_inode_defrag(NULL, inode);
427
Chris Mason42dc7ba2008-12-15 11:44:56 -0500428 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400429again:
430 will_compress = 0;
431 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
432 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
433
Chris Masonf03d9301f2009-02-04 09:31:06 -0500434 /*
435 * we don't want to send crud past the end of i_size through
436 * compression, that's just a waste of CPU time. So, if the
437 * end of the file is before the start of our current
438 * requested range of bytes, we bail out to the uncompressed
439 * cleanup code that can deal with all of this.
440 *
441 * It isn't really the fastest way to fix things, but this is a
442 * very uncommon corner.
443 */
444 if (actual_end <= start)
445 goto cleanup_and_bail_uncompressed;
446
Chris Masonc8b97812008-10-29 14:49:59 -0400447 total_compressed = actual_end - start;
448
Shilong Wang4bcbb332014-10-07 18:44:35 -0400449 /*
450 * skip compression for a small file range(<=blocksize) that
451 * isn't an inline extent, since it dosen't save disk space at all.
452 */
453 if (total_compressed <= blocksize &&
454 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
455 goto cleanup_and_bail_uncompressed;
456
Chris Masonc8b97812008-10-29 14:49:59 -0400457 /* we want to make sure that amount of ram required to uncompress
458 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500459 * of a compressed extent to 128k. This is a crucial number
460 * because it also controls how easily we can spread reads across
461 * cpus for decompression.
462 *
463 * We also want to make sure the amount of IO required to do
464 * a random read is reasonably small, so we limit the size of
465 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400466 */
467 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000468 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500469 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400470 total_in = 0;
471 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400472
Chris Mason771ed682008-11-06 22:02:51 -0500473 /*
474 * we do compression for mount -o compress and when the
475 * inode has not been flagged as nocompress. This flag can
476 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400477 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800478 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400479 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100480 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800481 if (!pages) {
482 /* just bail out to the uncompressed code */
483 goto cont;
484 }
Chris Mason179e29e2007-11-01 11:28:41 -0400485
Li Zefan261507a02010-12-17 14:21:50 +0800486 if (BTRFS_I(inode)->force_compress)
487 compress_type = BTRFS_I(inode)->force_compress;
488
Chris Mason4adaa612013-03-26 13:07:00 -0400489 /*
490 * we need to call clear_page_dirty_for_io on each
491 * page in the range. Otherwise applications with the file
492 * mmap'd can wander in and change the page contents while
493 * we are compressing them.
494 *
495 * If the compression fails for any reason, we set the pages
496 * dirty again later on.
497 */
498 extent_range_clear_dirty_for_io(inode, start, end);
499 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800500 ret = btrfs_compress_pages(compress_type,
501 inode->i_mapping, start,
502 total_compressed, pages,
503 nr_pages, &nr_pages_ret,
504 &total_in,
505 &total_compressed,
506 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400507
508 if (!ret) {
509 unsigned long offset = total_compressed &
510 (PAGE_CACHE_SIZE - 1);
511 struct page *page = pages[nr_pages_ret - 1];
512 char *kaddr;
513
514 /* zero the tail end of the last page, we might be
515 * sending it down to disk
516 */
517 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800518 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400519 memset(kaddr + offset, 0,
520 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800521 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400522 }
523 will_compress = 1;
524 }
525 }
Li Zefan560f7d72011-09-08 10:22:01 +0800526cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400527 if (start == 0) {
528 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500529 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400530 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500531 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400532 */
Josef Bacik00361582013-08-14 14:02:47 -0400533 ret = cow_file_range_inline(root, inode, start, end,
534 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400535 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500536 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400537 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000538 total_compressed,
539 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400540 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100541 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400542 unsigned long clear_flags = EXTENT_DELALLOC |
543 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100544 unsigned long page_error_op;
545
Josef Bacik151a41b2013-07-29 13:22:24 -0400546 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100547 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400548
Chris Mason771ed682008-11-06 22:02:51 -0500549 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100550 * inline extent creation worked or returned error,
551 * we don't need to create any more async work items.
552 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500553 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400554 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400555 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400556 PAGE_CLEAR_DIRTY |
557 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100558 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400559 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400560 goto free_pages_out;
561 }
562 }
563
564 if (will_compress) {
565 /*
566 * we aren't doing an inline extent round the compressed size
567 * up to a block size boundary so the allocator does sane
568 * things
569 */
Qu Wenruofda28322013-02-26 08:10:22 +0000570 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400571
572 /*
573 * one last check to make sure the compression is really a
574 * win, compare the page count read with the blocks on disk
575 */
Qu Wenruofda28322013-02-26 08:10:22 +0000576 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400577 if (total_compressed >= total_in) {
578 will_compress = 0;
579 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400580 num_bytes = total_in;
581 }
582 }
583 if (!will_compress && pages) {
584 /*
585 * the compression code ran but failed to make things smaller,
586 * free any pages it allocated and our page pointer array
587 */
588 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400589 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400590 page_cache_release(pages[i]);
591 }
592 kfree(pages);
593 pages = NULL;
594 total_compressed = 0;
595 nr_pages_ret = 0;
596
597 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500598 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
599 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500600 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500601 }
Chris Masonc8b97812008-10-29 14:49:59 -0400602 }
Chris Mason771ed682008-11-06 22:02:51 -0500603 if (will_compress) {
604 *num_added += 1;
605
606 /* the async work queues will take care of doing actual
607 * allocation on disk for these compressed pages,
608 * and will submit them to the elevator.
609 */
610 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800611 total_compressed, pages, nr_pages_ret,
612 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500613
Yan, Zheng24ae6362010-12-06 07:02:36 +0000614 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500615 start += num_bytes;
616 pages = NULL;
617 cond_resched();
618 goto again;
619 }
620 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500621cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500622 /*
623 * No compression, but we still need to write the pages in
624 * the file we've been given so far. redirty the locked
625 * page if it corresponds to our extent and set things up
626 * for the async work queue to run cow_file_range to do
627 * the normal delalloc dance
628 */
629 if (page_offset(locked_page) >= start &&
630 page_offset(locked_page) <= end) {
631 __set_page_dirty_nobuffers(locked_page);
632 /* unlocked later on in the async handlers */
633 }
Chris Mason4adaa612013-03-26 13:07:00 -0400634 if (redirty)
635 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800636 add_async_extent(async_cow, start, end - start + 1,
637 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500638 *num_added += 1;
639 }
640
Filipe Mananac44f6492014-10-09 21:15:44 +0100641 return;
Chris Mason771ed682008-11-06 22:02:51 -0500642
643free_pages_out:
644 for (i = 0; i < nr_pages_ret; i++) {
645 WARN_ON(pages[i]->mapping);
646 page_cache_release(pages[i]);
647 }
Chris Masond3977122009-01-05 21:25:51 -0500648 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500649}
Chris Mason771ed682008-11-06 22:02:51 -0500650
Filipe Manana40ae8372014-10-06 22:14:24 +0100651static void free_async_extent_pages(struct async_extent *async_extent)
652{
653 int i;
654
655 if (!async_extent->pages)
656 return;
657
658 for (i = 0; i < async_extent->nr_pages; i++) {
659 WARN_ON(async_extent->pages[i]->mapping);
660 page_cache_release(async_extent->pages[i]);
661 }
662 kfree(async_extent->pages);
663 async_extent->nr_pages = 0;
664 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500665}
666
667/*
668 * phase two of compressed writeback. This is the ordered portion
669 * of the code, which only gets called in the order the work was
670 * queued. We walk all the async extents created by compress_file_range
671 * and send them down to the disk.
672 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100673static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500674 struct async_cow *async_cow)
675{
676 struct async_extent *async_extent;
677 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500678 struct btrfs_key ins;
679 struct extent_map *em;
680 struct btrfs_root *root = BTRFS_I(inode)->root;
681 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
682 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500683 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500684
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500685again:
Chris Masond3977122009-01-05 21:25:51 -0500686 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500687 async_extent = list_entry(async_cow->extents.next,
688 struct async_extent, list);
689 list_del(&async_extent->list);
690
691 io_tree = &BTRFS_I(inode)->io_tree;
692
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500693retry:
Chris Mason771ed682008-11-06 22:02:51 -0500694 /* did the compression code fall back to uncompressed IO? */
695 if (!async_extent->pages) {
696 int page_started = 0;
697 unsigned long nr_written = 0;
698
699 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000700 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100701 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500702
703 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500704 ret = cow_file_range(inode, async_cow->locked_page,
705 async_extent->start,
706 async_extent->start +
707 async_extent->ram_size - 1,
708 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500709
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100710 /* JDM XXX */
711
Chris Mason771ed682008-11-06 22:02:51 -0500712 /*
713 * if page_started, cow_file_range inserted an
714 * inline extent and took care of all the unlocking
715 * and IO for us. Otherwise, we need to submit
716 * all those pages down to the drive.
717 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500718 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500719 extent_write_locked_range(io_tree,
720 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500721 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500722 async_extent->ram_size - 1,
723 btrfs_get_extent,
724 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500725 else if (ret)
726 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500727 kfree(async_extent);
728 cond_resched();
729 continue;
730 }
731
732 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100733 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500734
Josef Bacik00361582013-08-14 14:02:47 -0400735 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500736 async_extent->compressed_size,
737 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800738 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500739 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100740 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500741
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400742 if (ret == -ENOSPC) {
743 unlock_extent(io_tree, async_extent->start,
744 async_extent->start +
745 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800746
747 /*
748 * we need to redirty the pages if we decide to
749 * fallback to uncompressed IO, otherwise we
750 * will not submit these pages down to lower
751 * layers.
752 */
753 extent_range_redirty_for_io(inode,
754 async_extent->start,
755 async_extent->start +
756 async_extent->ram_size - 1);
757
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100758 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400759 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500760 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500761 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000762 /*
763 * here we're doing allocation and writeback of the
764 * compressed pages
765 */
766 btrfs_drop_extent_cache(inode, async_extent->start,
767 async_extent->start +
768 async_extent->ram_size - 1, 0);
769
David Sterba172ddd62011-04-21 00:48:27 +0200770 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000771 if (!em) {
772 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500773 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000774 }
Chris Mason771ed682008-11-06 22:02:51 -0500775 em->start = async_extent->start;
776 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500777 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400778 em->mod_start = em->start;
779 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500780
781 em->block_start = ins.objectid;
782 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500783 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400784 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500785 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800786 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500787 set_bit(EXTENT_FLAG_PINNED, &em->flags);
788 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400789 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500790
Chris Masond3977122009-01-05 21:25:51 -0500791 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400792 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400793 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400794 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500795 if (ret != -EEXIST) {
796 free_extent_map(em);
797 break;
798 }
799 btrfs_drop_extent_cache(inode, async_extent->start,
800 async_extent->start +
801 async_extent->ram_size - 1, 0);
802 }
803
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500804 if (ret)
805 goto out_free_reserve;
806
Li Zefan261507a02010-12-17 14:21:50 +0800807 ret = btrfs_add_ordered_extent_compress(inode,
808 async_extent->start,
809 ins.objectid,
810 async_extent->ram_size,
811 ins.offset,
812 BTRFS_ORDERED_COMPRESSED,
813 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100814 if (ret) {
815 btrfs_drop_extent_cache(inode, async_extent->start,
816 async_extent->start +
817 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500818 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100819 }
Chris Mason771ed682008-11-06 22:02:51 -0500820
Chris Mason771ed682008-11-06 22:02:51 -0500821 /*
822 * clear dirty, set writeback and unlock the pages.
823 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400824 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400825 async_extent->start +
826 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400827 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
828 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400829 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500830 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500831 async_extent->start,
832 async_extent->ram_size,
833 ins.objectid,
834 ins.offset, async_extent->pages,
835 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100836 if (ret) {
837 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
838 struct page *p = async_extent->pages[0];
839 const u64 start = async_extent->start;
840 const u64 end = start + async_extent->ram_size - 1;
841
842 p->mapping = inode->i_mapping;
843 tree->ops->writepage_end_io_hook(p, start, end,
844 NULL, 0);
845 p->mapping = NULL;
846 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
847 PAGE_END_WRITEBACK |
848 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100849 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100850 }
Chris Mason771ed682008-11-06 22:02:51 -0500851 alloc_hint = ins.objectid + ins.offset;
852 kfree(async_extent);
853 cond_resched();
854 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100855 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500856out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800857 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100858out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400859 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500860 async_extent->start +
861 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400862 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400863 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
864 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100865 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
866 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100867 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100868 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500869 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500870}
871
Josef Bacik4b46fce2010-05-23 11:00:55 -0400872static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
873 u64 num_bytes)
874{
875 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
876 struct extent_map *em;
877 u64 alloc_hint = 0;
878
879 read_lock(&em_tree->lock);
880 em = search_extent_mapping(em_tree, start, num_bytes);
881 if (em) {
882 /*
883 * if block start isn't an actual block number then find the
884 * first block in this inode and use that as a hint. If that
885 * block is also bogus then just don't worry about it.
886 */
887 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
888 free_extent_map(em);
889 em = search_extent_mapping(em_tree, 0, 0);
890 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
891 alloc_hint = em->block_start;
892 if (em)
893 free_extent_map(em);
894 } else {
895 alloc_hint = em->block_start;
896 free_extent_map(em);
897 }
898 }
899 read_unlock(&em_tree->lock);
900
901 return alloc_hint;
902}
903
Chris Mason771ed682008-11-06 22:02:51 -0500904/*
905 * when extent_io.c finds a delayed allocation range in the file,
906 * the call backs end up in this code. The basic idea is to
907 * allocate extents on disk for the range, and create ordered data structs
908 * in ram to track those extents.
909 *
910 * locked_page is the page that writepage had locked already. We use
911 * it to make sure we don't do extra locks or unlocks.
912 *
913 * *page_started is set to one if we unlock locked_page and do everything
914 * required to start IO on it. It may be clean and already done with
915 * IO when we return.
916 */
Josef Bacik00361582013-08-14 14:02:47 -0400917static noinline int cow_file_range(struct inode *inode,
918 struct page *locked_page,
919 u64 start, u64 end, int *page_started,
920 unsigned long *nr_written,
921 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500922{
Josef Bacik00361582013-08-14 14:02:47 -0400923 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500924 u64 alloc_hint = 0;
925 u64 num_bytes;
926 unsigned long ram_size;
927 u64 disk_num_bytes;
928 u64 cur_alloc_size;
929 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500930 struct btrfs_key ins;
931 struct extent_map *em;
932 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
933 int ret = 0;
934
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400935 if (btrfs_is_free_space_inode(inode)) {
936 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500937 ret = -EINVAL;
938 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400939 }
Chris Mason771ed682008-11-06 22:02:51 -0500940
Qu Wenruofda28322013-02-26 08:10:22 +0000941 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500942 num_bytes = max(blocksize, num_bytes);
943 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500944
Chris Mason4cb53002011-05-24 15:35:30 -0400945 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400946 if (num_bytes < 64 * 1024 &&
947 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400948 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400949
Chris Mason771ed682008-11-06 22:02:51 -0500950 if (start == 0) {
951 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400952 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
953 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500954 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400955 extent_clear_unlock_delalloc(inode, start, end, NULL,
956 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400957 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400958 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
959 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000960
Chris Mason771ed682008-11-06 22:02:51 -0500961 *nr_written = *nr_written +
962 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
963 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500964 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100965 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100966 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500967 }
968 }
Chris Masonc8b97812008-10-29 14:49:59 -0400969
970 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200971 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400972
Josef Bacik4b46fce2010-05-23 11:00:55 -0400973 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400974 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400975
Chris Masond3977122009-01-05 21:25:51 -0500976 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400977 unsigned long op;
978
Josef Bacik287a0ab2010-03-19 18:07:23 +0000979 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400980 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500981 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800982 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400983 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100984 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500985
David Sterba172ddd62011-04-21 00:48:27 +0200986 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000987 if (!em) {
988 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000989 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000990 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400991 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500992 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500993 ram_size = ins.offset;
994 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400995 em->mod_start = em->start;
996 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400997
Chris Masone6dcd2d2008-07-17 12:53:50 -0400998 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400999 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001000 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001001 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001002 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001003 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001004 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001005
Chris Masond3977122009-01-05 21:25:51 -05001006 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001007 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001008 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001009 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001010 if (ret != -EEXIST) {
1011 free_extent_map(em);
1012 break;
1013 }
1014 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001015 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001016 }
Liu Boace68ba2013-04-22 10:53:47 +00001017 if (ret)
1018 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001019
Chris Mason98d20f62008-04-14 09:46:10 -04001020 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001021 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001022 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001023 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001024 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001025
Yan Zheng17d217f2008-12-12 10:03:38 -05001026 if (root->root_key.objectid ==
1027 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1028 ret = btrfs_reloc_clone_csums(inode, start,
1029 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001030 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001031 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001032 }
1033
Chris Masond3977122009-01-05 21:25:51 -05001034 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001035 break;
Chris Masond3977122009-01-05 21:25:51 -05001036
Chris Masonc8b97812008-10-29 14:49:59 -04001037 /* we're not doing compressed IO, don't unlock the first
1038 * page (which the caller expects to stay locked), don't
1039 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001040 *
1041 * Do set the Private2 bit so we know this page was properly
1042 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001043 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001044 op = unlock ? PAGE_UNLOCK : 0;
1045 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001046
Josef Bacikc2790a22013-07-29 11:20:47 -04001047 extent_clear_unlock_delalloc(inode, start,
1048 start + ram_size - 1, locked_page,
1049 EXTENT_LOCKED | EXTENT_DELALLOC,
1050 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001051 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001052 num_bytes -= cur_alloc_size;
1053 alloc_hint = ins.objectid + ins.offset;
1054 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001055 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001056out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001057 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001058
Filipe Mananad9f85962014-08-25 10:43:00 +01001059out_drop_extent_cache:
1060 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001061out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001062 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001063out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001064 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001065 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1066 EXTENT_DELALLOC | EXTENT_DEFRAG,
1067 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1068 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001069 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001070}
Chris Masonc8b97812008-10-29 14:49:59 -04001071
Chris Mason771ed682008-11-06 22:02:51 -05001072/*
1073 * work queue call back to started compression on a file and pages
1074 */
1075static noinline void async_cow_start(struct btrfs_work *work)
1076{
1077 struct async_cow *async_cow;
1078 int num_added = 0;
1079 async_cow = container_of(work, struct async_cow, work);
1080
1081 compress_file_range(async_cow->inode, async_cow->locked_page,
1082 async_cow->start, async_cow->end, async_cow,
1083 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001084 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001085 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001086 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001087 }
Chris Mason771ed682008-11-06 22:02:51 -05001088}
1089
1090/*
1091 * work queue call back to submit previously compressed pages
1092 */
1093static noinline void async_cow_submit(struct btrfs_work *work)
1094{
1095 struct async_cow *async_cow;
1096 struct btrfs_root *root;
1097 unsigned long nr_pages;
1098
1099 async_cow = container_of(work, struct async_cow, work);
1100
1101 root = async_cow->root;
1102 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1103 PAGE_CACHE_SHIFT;
1104
David Sterbaee863952015-02-16 19:41:40 +01001105 /*
1106 * atomic_sub_return implies a barrier for waitqueue_active
1107 */
Josef Bacik66657b32012-08-01 15:36:24 -04001108 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001109 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001110 waitqueue_active(&root->fs_info->async_submit_wait))
1111 wake_up(&root->fs_info->async_submit_wait);
1112
Chris Masond3977122009-01-05 21:25:51 -05001113 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001114 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001115}
Chris Masonc8b97812008-10-29 14:49:59 -04001116
Chris Mason771ed682008-11-06 22:02:51 -05001117static noinline void async_cow_free(struct btrfs_work *work)
1118{
1119 struct async_cow *async_cow;
1120 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001121 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001122 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001123 kfree(async_cow);
1124}
1125
1126static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1127 u64 start, u64 end, int *page_started,
1128 unsigned long *nr_written)
1129{
1130 struct async_cow *async_cow;
1131 struct btrfs_root *root = BTRFS_I(inode)->root;
1132 unsigned long nr_pages;
1133 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001134 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001135
Chris Masona3429ab2009-10-08 12:30:20 -04001136 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1137 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001138 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001139 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001140 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001141 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001142 async_cow->root = root;
1143 async_cow->locked_page = locked_page;
1144 async_cow->start = start;
1145
Wang Shilongf79707b2014-07-17 11:44:09 +08001146 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1147 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001148 cur_end = end;
1149 else
1150 cur_end = min(end, start + 512 * 1024 - 1);
1151
1152 async_cow->end = cur_end;
1153 INIT_LIST_HEAD(&async_cow->extents);
1154
Liu Bo9e0af232014-08-15 23:36:53 +08001155 btrfs_init_work(&async_cow->work,
1156 btrfs_delalloc_helper,
1157 async_cow_start, async_cow_submit,
1158 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001159
Chris Mason771ed682008-11-06 22:02:51 -05001160 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1161 PAGE_CACHE_SHIFT;
1162 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1163
Qu Wenruoafe3d242014-02-28 10:46:07 +08001164 btrfs_queue_work(root->fs_info->delalloc_workers,
1165 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001166
1167 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1168 wait_event(root->fs_info->async_submit_wait,
1169 (atomic_read(&root->fs_info->async_delalloc_pages) <
1170 limit));
1171 }
1172
Chris Masond3977122009-01-05 21:25:51 -05001173 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001174 atomic_read(&root->fs_info->async_delalloc_pages)) {
1175 wait_event(root->fs_info->async_submit_wait,
1176 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1177 0));
1178 }
1179
1180 *nr_written += nr_pages;
1181 start = cur_end + 1;
1182 }
1183 *page_started = 1;
1184 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001185}
1186
Chris Masond3977122009-01-05 21:25:51 -05001187static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001188 u64 bytenr, u64 num_bytes)
1189{
1190 int ret;
1191 struct btrfs_ordered_sum *sums;
1192 LIST_HEAD(list);
1193
Yan Zheng07d400a2009-01-06 11:42:00 -05001194 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001195 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001196 if (ret == 0 && list_empty(&list))
1197 return 0;
1198
1199 while (!list_empty(&list)) {
1200 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1201 list_del(&sums->list);
1202 kfree(sums);
1203 }
1204 return 1;
1205}
1206
Chris Masond352ac62008-09-29 15:18:18 -04001207/*
1208 * when nowcow writeback call back. This checks for snapshots or COW copies
1209 * of the extents that exist in the file, and COWs the file as required.
1210 *
1211 * If no cow copies or snapshots exist, we write directly to the existing
1212 * blocks on disk
1213 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001214static noinline int run_delalloc_nocow(struct inode *inode,
1215 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001216 u64 start, u64 end, int *page_started, int force,
1217 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001218{
Chris Masonbe20aa92007-12-17 20:14:01 -05001219 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001220 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001221 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001222 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001223 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001224 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001225 u64 cow_start;
1226 u64 cur_offset;
1227 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001228 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001229 u64 disk_bytenr;
1230 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001231 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001232 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001233 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001234 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001235 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001236 int nocow;
1237 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001238 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001239 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001240
1241 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001242 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001243 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1244 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001245 EXTENT_DO_ACCOUNTING |
1246 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001247 PAGE_CLEAR_DIRTY |
1248 PAGE_SET_WRITEBACK |
1249 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001250 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001251 }
Li Zefan82d59022011-04-20 10:33:24 +08001252
Liu Bo83eea1f2012-07-10 05:28:39 -06001253 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001254
1255 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001256 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001257 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001258 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001259
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001260 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001261 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1262 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001263 EXTENT_DO_ACCOUNTING |
1264 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001265 PAGE_CLEAR_DIRTY |
1266 PAGE_SET_WRITEBACK |
1267 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001268 btrfs_free_path(path);
1269 return PTR_ERR(trans);
1270 }
1271
Josef Bacik74b21072011-04-13 12:02:53 -04001272 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001273
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 cow_start = (u64)-1;
1275 cur_offset = start;
1276 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001277 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001279 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001280 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1282 leaf = path->nodes[0];
1283 btrfs_item_key_to_cpu(leaf, &found_key,
1284 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001285 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 found_key.type == BTRFS_EXTENT_DATA_KEY)
1287 path->slots[0]--;
1288 }
1289 check_prev = 0;
1290next_slot:
1291 leaf = path->nodes[0];
1292 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1293 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001294 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001295 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 if (ret > 0)
1297 break;
1298 leaf = path->nodes[0];
1299 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001300
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 nocow = 0;
1302 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001303 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001304 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001305
Filipe Manana1d512cb2015-11-09 00:33:58 +00001306 if (found_key.objectid > ino)
1307 break;
1308 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1309 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1310 path->slots[0]++;
1311 goto next_slot;
1312 }
1313 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 found_key.offset > end)
1315 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001316
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 if (found_key.offset > cur_offset) {
1318 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001319 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001320 goto out_check;
1321 }
Chris Masonc31f8832008-01-08 15:46:31 -05001322
Yan Zheng80ff3852008-10-30 14:20:02 -04001323 fi = btrfs_item_ptr(leaf, path->slots[0],
1324 struct btrfs_file_extent_item);
1325 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001326
Josef Bacikcc95bef2013-04-04 14:31:27 -04001327 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001328 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1329 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001330 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001331 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001332 extent_end = found_key.offset +
1333 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001334 disk_num_bytes =
1335 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001336 if (extent_end <= start) {
1337 path->slots[0]++;
1338 goto next_slot;
1339 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001340 if (disk_bytenr == 0)
1341 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001342 if (btrfs_file_extent_compression(leaf, fi) ||
1343 btrfs_file_extent_encryption(leaf, fi) ||
1344 btrfs_file_extent_other_encoding(leaf, fi))
1345 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001346 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1347 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001348 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001349 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001350 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001351 found_key.offset -
1352 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001353 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001354 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001355 disk_bytenr += cur_offset - found_key.offset;
1356 num_bytes = min(end + 1, extent_end) - cur_offset;
1357 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001358 * if there are pending snapshots for this root,
1359 * we fall into common COW way.
1360 */
1361 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001362 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001363 if (!err)
1364 goto out_check;
1365 }
1366 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001367 * force cow if csum exists in the range.
1368 * this ensure that csum for a given extent are
1369 * either valid or do not exist.
1370 */
1371 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1372 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 nocow = 1;
1374 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1375 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001376 btrfs_file_extent_inline_len(leaf,
1377 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001378 extent_end = ALIGN(extent_end, root->sectorsize);
1379 } else {
1380 BUG_ON(1);
1381 }
1382out_check:
1383 if (extent_end <= start) {
1384 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001385 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001386 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001387 goto next_slot;
1388 }
1389 if (!nocow) {
1390 if (cow_start == (u64)-1)
1391 cow_start = cur_offset;
1392 cur_offset = extent_end;
1393 if (cur_offset > end)
1394 break;
1395 path->slots[0]++;
1396 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001397 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001398
David Sterbab3b4aa72011-04-21 01:20:15 +02001399 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001400 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001401 ret = cow_file_range(inode, locked_page,
1402 cow_start, found_key.offset - 1,
1403 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001404 if (ret) {
1405 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001406 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001407 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001408 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001409 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001410 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001411
Yan Zhengd899e052008-10-30 14:25:28 -04001412 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1413 struct extent_map *em;
1414 struct extent_map_tree *em_tree;
1415 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001416 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001417 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001418 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001419 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001420 em->len = num_bytes;
1421 em->block_len = num_bytes;
1422 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001423 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001424 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001425 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001426 em->mod_start = em->start;
1427 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001428 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001429 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001430 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001431 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001432 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001433 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001434 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001435 if (ret != -EEXIST) {
1436 free_extent_map(em);
1437 break;
1438 }
1439 btrfs_drop_extent_cache(inode, em->start,
1440 em->start + em->len - 1, 0);
1441 }
1442 type = BTRFS_ORDERED_PREALLOC;
1443 } else {
1444 type = BTRFS_ORDERED_NOCOW;
1445 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001446
1447 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001448 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001449 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001450
Yan, Zhengefa56462010-05-16 10:49:59 -04001451 if (root->root_key.objectid ==
1452 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1453 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1454 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001455 if (ret) {
1456 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001457 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001458 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001459 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001460 }
1461
Josef Bacikc2790a22013-07-29 11:20:47 -04001462 extent_clear_unlock_delalloc(inode, cur_offset,
1463 cur_offset + num_bytes - 1,
1464 locked_page, EXTENT_LOCKED |
1465 EXTENT_DELALLOC, PAGE_UNLOCK |
1466 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001467 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001468 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001469 cur_offset = extent_end;
1470 if (cur_offset > end)
1471 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001472 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001473 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001474
Josef Bacik17ca04a2012-05-31 15:58:55 -04001475 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001476 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001477 cur_offset = end;
1478 }
1479
Yan Zheng80ff3852008-10-30 14:20:02 -04001480 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001481 ret = cow_file_range(inode, locked_page, cow_start, end,
1482 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001483 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001484 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001485 }
1486
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001487error:
Miao Xiea698d0752012-09-20 01:51:59 -06001488 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001489 if (!ret)
1490 ret = err;
1491
Josef Bacik17ca04a2012-05-31 15:58:55 -04001492 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001493 extent_clear_unlock_delalloc(inode, cur_offset, end,
1494 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001495 EXTENT_DELALLOC | EXTENT_DEFRAG |
1496 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1497 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001498 PAGE_SET_WRITEBACK |
1499 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001500 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001501 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001502}
1503
Wang Shilong47059d92014-07-03 18:22:07 +08001504static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1505{
1506
1507 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1508 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1509 return 0;
1510
1511 /*
1512 * @defrag_bytes is a hint value, no spinlock held here,
1513 * if is not zero, it means the file is defragging.
1514 * Force cow if given extent needs to be defragged.
1515 */
1516 if (BTRFS_I(inode)->defrag_bytes &&
1517 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1518 EXTENT_DEFRAG, 0, NULL))
1519 return 1;
1520
1521 return 0;
1522}
1523
Chris Masond352ac62008-09-29 15:18:18 -04001524/*
1525 * extent_io.c call back to do delayed allocation processing
1526 */
Chris Masonc8b97812008-10-29 14:49:59 -04001527static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001528 u64 start, u64 end, int *page_started,
1529 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001530{
Chris Masonbe20aa92007-12-17 20:14:01 -05001531 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001532 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001533
Wang Shilong47059d92014-07-03 18:22:07 +08001534 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001535 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001536 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001537 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001538 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001539 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001540 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001541 ret = cow_file_range(inode, locked_page, start, end,
1542 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001543 } else {
1544 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1545 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001546 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001547 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001548 }
Chris Masonb888db22007-08-27 16:49:44 -04001549 return ret;
1550}
1551
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001552static void btrfs_split_extent_hook(struct inode *inode,
1553 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001554{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001555 u64 size;
1556
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001557 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001558 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001559 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001560
Josef Bacikdcab6a32015-02-11 15:08:59 -05001561 size = orig->end - orig->start + 1;
1562 if (size > BTRFS_MAX_EXTENT_SIZE) {
1563 u64 num_extents;
1564 u64 new_size;
1565
1566 /*
Josef Bacikba117212015-03-13 15:01:24 -04001567 * See the explanation in btrfs_merge_extent_hook, the same
1568 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001569 */
1570 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001571 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001572 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001573 new_size = split - orig->start;
1574 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1575 BTRFS_MAX_EXTENT_SIZE);
1576 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1577 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001578 return;
1579 }
1580
Josef Bacik9e0baf62011-07-15 15:16:44 +00001581 spin_lock(&BTRFS_I(inode)->lock);
1582 BTRFS_I(inode)->outstanding_extents++;
1583 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001584}
1585
1586/*
1587 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1588 * extents so we can keep track of new extents that are just merged onto old
1589 * extents, such as when we are doing sequential writes, so we can properly
1590 * account for the metadata space we'll need.
1591 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001592static void btrfs_merge_extent_hook(struct inode *inode,
1593 struct extent_state *new,
1594 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001595{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001596 u64 new_size, old_size;
1597 u64 num_extents;
1598
Josef Bacik9ed74f22009-09-11 16:12:44 -04001599 /* not delalloc, ignore it */
1600 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001601 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001602
Josef Bacik8461a3d2015-03-13 15:12:08 -04001603 if (new->start > other->start)
1604 new_size = new->end - other->start + 1;
1605 else
1606 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001607
1608 /* we're not bigger than the max, unreserve the space and go */
1609 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1610 spin_lock(&BTRFS_I(inode)->lock);
1611 BTRFS_I(inode)->outstanding_extents--;
1612 spin_unlock(&BTRFS_I(inode)->lock);
1613 return;
1614 }
1615
1616 /*
Josef Bacikba117212015-03-13 15:01:24 -04001617 * We have to add up either side to figure out how many extents were
1618 * accounted for before we merged into one big extent. If the number of
1619 * extents we accounted for is <= the amount we need for the new range
1620 * then we can return, otherwise drop. Think of it like this
1621 *
1622 * [ 4k][MAX_SIZE]
1623 *
1624 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1625 * need 2 outstanding extents, on one side we have 1 and the other side
1626 * we have 1 so they are == and we can return. But in this case
1627 *
1628 * [MAX_SIZE+4k][MAX_SIZE+4k]
1629 *
1630 * Each range on their own accounts for 2 extents, but merged together
1631 * they are only 3 extents worth of accounting, so we need to drop in
1632 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001633 */
Josef Bacikba117212015-03-13 15:01:24 -04001634 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001635 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1636 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001637 old_size = new->end - new->start + 1;
1638 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1639 BTRFS_MAX_EXTENT_SIZE);
1640
Josef Bacikdcab6a32015-02-11 15:08:59 -05001641 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001642 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001643 return;
1644
Josef Bacik9e0baf62011-07-15 15:16:44 +00001645 spin_lock(&BTRFS_I(inode)->lock);
1646 BTRFS_I(inode)->outstanding_extents--;
1647 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001648}
1649
Miao Xieeb73c1b2013-05-15 07:48:22 +00001650static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1651 struct inode *inode)
1652{
1653 spin_lock(&root->delalloc_lock);
1654 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1655 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1656 &root->delalloc_inodes);
1657 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1658 &BTRFS_I(inode)->runtime_flags);
1659 root->nr_delalloc_inodes++;
1660 if (root->nr_delalloc_inodes == 1) {
1661 spin_lock(&root->fs_info->delalloc_root_lock);
1662 BUG_ON(!list_empty(&root->delalloc_root));
1663 list_add_tail(&root->delalloc_root,
1664 &root->fs_info->delalloc_roots);
1665 spin_unlock(&root->fs_info->delalloc_root_lock);
1666 }
1667 }
1668 spin_unlock(&root->delalloc_lock);
1669}
1670
1671static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1672 struct inode *inode)
1673{
1674 spin_lock(&root->delalloc_lock);
1675 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1676 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1677 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1678 &BTRFS_I(inode)->runtime_flags);
1679 root->nr_delalloc_inodes--;
1680 if (!root->nr_delalloc_inodes) {
1681 spin_lock(&root->fs_info->delalloc_root_lock);
1682 BUG_ON(list_empty(&root->delalloc_root));
1683 list_del_init(&root->delalloc_root);
1684 spin_unlock(&root->fs_info->delalloc_root_lock);
1685 }
1686 }
1687 spin_unlock(&root->delalloc_lock);
1688}
1689
Chris Masond352ac62008-09-29 15:18:18 -04001690/*
1691 * extent_io.c set_bit_hook, used to track delayed allocation
1692 * bytes in this file, and to maintain the list of inodes that
1693 * have pending delalloc work to be done.
1694 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001695static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001696 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001697{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001698
Wang Shilong47059d92014-07-03 18:22:07 +08001699 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1700 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001701 /*
1702 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001703 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001704 * bit, which is only set or cleared with irqs on
1705 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001706 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001707 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001708 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001709 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001710
Josef Bacik9e0baf62011-07-15 15:16:44 +00001711 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001712 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001713 } else {
1714 spin_lock(&BTRFS_I(inode)->lock);
1715 BTRFS_I(inode)->outstanding_extents++;
1716 spin_unlock(&BTRFS_I(inode)->lock);
1717 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001718
Josef Bacik6a3891c2015-03-16 17:38:52 -04001719 /* For sanity tests */
1720 if (btrfs_test_is_dummy_root(root))
1721 return;
1722
Miao Xie963d6782013-01-29 10:10:51 +00001723 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1724 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001725 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001726 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001727 if (*bits & EXTENT_DEFRAG)
1728 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001729 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001730 &BTRFS_I(inode)->runtime_flags))
1731 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001732 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001733 }
Chris Mason291d6732008-01-29 15:55:23 -05001734}
1735
Chris Masond352ac62008-09-29 15:18:18 -04001736/*
1737 * extent_io.c clear_bit_hook, see set_bit_hook for why
1738 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001739static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001740 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001741 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001742{
Wang Shilong47059d92014-07-03 18:22:07 +08001743 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001744 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1745 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001746
1747 spin_lock(&BTRFS_I(inode)->lock);
1748 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1749 BTRFS_I(inode)->defrag_bytes -= len;
1750 spin_unlock(&BTRFS_I(inode)->lock);
1751
Chris Mason75eff682008-12-15 15:54:40 -05001752 /*
1753 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001754 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001755 * bit, which is only set or cleared with irqs on
1756 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001757 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001758 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001759 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001760
Josef Bacik9e0baf62011-07-15 15:16:44 +00001761 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001762 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001763 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1764 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001765 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001766 spin_unlock(&BTRFS_I(inode)->lock);
1767 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001768
Josef Bacikb6d08f02013-09-27 14:57:43 -04001769 /*
1770 * We don't reserve metadata space for space cache inodes so we
1771 * don't need to call dellalloc_release_metadata if there is an
1772 * error.
1773 */
1774 if (*bits & EXTENT_DO_ACCOUNTING &&
1775 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001776 btrfs_delalloc_release_metadata(inode, len);
1777
Josef Bacik6a3891c2015-03-16 17:38:52 -04001778 /* For sanity tests. */
1779 if (btrfs_test_is_dummy_root(root))
1780 return;
1781
Josef Bacik0cb59c92010-07-02 12:14:14 -04001782 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001783 && do_list && !(state->state & EXTENT_NORESERVE))
Qu Wenruo51773be2015-10-08 18:19:37 +08001784 btrfs_free_reserved_data_space_noquota(inode,
1785 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001786
Miao Xie963d6782013-01-29 10:10:51 +00001787 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1788 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001789 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001790 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001791 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001792 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001793 &BTRFS_I(inode)->runtime_flags))
1794 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001795 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001796 }
Chris Mason291d6732008-01-29 15:55:23 -05001797}
1798
Chris Masond352ac62008-09-29 15:18:18 -04001799/*
1800 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1801 * we don't create bios that span stripes or chunks
1802 */
David Woodhouse64a16702009-07-15 23:29:37 +01001803int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001804 size_t size, struct bio *bio,
1805 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001806{
1807 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001808 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001809 u64 length = 0;
1810 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001811 int ret;
1812
Chris Mason771ed682008-11-06 22:02:51 -05001813 if (bio_flags & EXTENT_BIO_COMPRESSED)
1814 return 0;
1815
Kent Overstreet4f024f32013-10-11 15:44:27 -07001816 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001817 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001818 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001819 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001820 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001821 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001822 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001823 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001824 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001825}
1826
Chris Masond352ac62008-09-29 15:18:18 -04001827/*
1828 * in order to insert checksums into the metadata in large chunks,
1829 * we wait until bio submission time. All the pages in the bio are
1830 * checksummed and sums are attached onto the ordered extent record.
1831 *
1832 * At IO completion time the cums attached on the ordered extent record
1833 * are inserted into the btree
1834 */
Chris Masond3977122009-01-05 21:25:51 -05001835static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1836 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001837 unsigned long bio_flags,
1838 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001839{
Chris Mason065631f2008-02-20 12:07:25 -05001840 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001841 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001842
Chris Masond20f7042008-12-08 16:58:54 -05001843 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001844 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001845 return 0;
1846}
Chris Masone0156402008-04-16 11:15:20 -04001847
Chris Mason4a69a412008-11-06 22:03:00 -05001848/*
1849 * in order to insert checksums into the metadata in large chunks,
1850 * we wait until bio submission time. All the pages in the bio are
1851 * checksummed and sums are attached onto the ordered extent record.
1852 *
1853 * At IO completion time the cums attached on the ordered extent record
1854 * are inserted into the btree
1855 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001856static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001857 int mirror_num, unsigned long bio_flags,
1858 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001859{
1860 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001861 int ret;
1862
1863 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001864 if (ret) {
1865 bio->bi_error = ret;
1866 bio_endio(bio);
1867 }
Stefan Behrens61891922012-11-05 18:51:52 +01001868 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001869}
1870
Chris Masond352ac62008-09-29 15:18:18 -04001871/*
Chris Masoncad321a2008-12-17 14:51:42 -05001872 * extent_io.c submission hook. This does the right thing for csum calculation
1873 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001874 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001875static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001876 int mirror_num, unsigned long bio_flags,
1877 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001878{
1879 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301880 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001881 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001882 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001883 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001884
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001885 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001886
Liu Bo83eea1f2012-07-10 05:28:39 -06001887 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301888 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001889
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001890 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001891 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1892 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001893 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001894
Chris Masond20f7042008-12-08 16:58:54 -05001895 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001896 ret = btrfs_submit_compressed_read(inode, bio,
1897 mirror_num,
1898 bio_flags);
1899 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001900 } else if (!skip_sum) {
1901 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1902 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001903 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001904 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001905 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001906 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001907 /* csum items have already been cloned */
1908 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1909 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001910 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001911 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001912 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001913 bio_flags, bio_offset,
1914 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001915 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001916 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001917 } else if (!skip_sum) {
1918 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1919 if (ret)
1920 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001921 }
1922
Chris Mason0b86a832008-03-24 15:01:56 -04001923mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001924 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1925
1926out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001927 if (ret < 0) {
1928 bio->bi_error = ret;
1929 bio_endio(bio);
1930 }
Stefan Behrens61891922012-11-05 18:51:52 +01001931 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001932}
Chris Mason6885f302008-02-20 16:11:05 -05001933
Chris Masond352ac62008-09-29 15:18:18 -04001934/*
1935 * given a list of ordered sums record them in the inode. This happens
1936 * at IO completion time based on sums calculated at bio submission time.
1937 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001938static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001939 struct inode *inode, u64 file_offset,
1940 struct list_head *list)
1941{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001942 struct btrfs_ordered_sum *sum;
1943
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001944 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001945 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001946 btrfs_csum_file_blocks(trans,
1947 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001948 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001949 }
1950 return 0;
1951}
1952
Josef Bacik2ac55d42010-02-03 19:33:23 +00001953int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1954 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001955{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001956 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001957 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001958 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001959}
1960
Chris Masond352ac62008-09-29 15:18:18 -04001961/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001962struct btrfs_writepage_fixup {
1963 struct page *page;
1964 struct btrfs_work work;
1965};
1966
Christoph Hellwigb2950862008-12-02 09:54:17 -05001967static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001968{
1969 struct btrfs_writepage_fixup *fixup;
1970 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001971 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001972 struct page *page;
1973 struct inode *inode;
1974 u64 page_start;
1975 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001976 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001977
1978 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1979 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001980again:
Chris Mason247e7432008-07-17 12:53:51 -04001981 lock_page(page);
1982 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1983 ClearPageChecked(page);
1984 goto out_page;
1985 }
1986
1987 inode = page->mapping->host;
1988 page_start = page_offset(page);
1989 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1990
Josef Bacik2ac55d42010-02-03 19:33:23 +00001991 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001992 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001993
1994 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001995 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001996 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001997
1998 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1999 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002000 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2001 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002002 unlock_page(page);
2003 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002004 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002005 goto again;
2006 }
Chris Mason247e7432008-07-17 12:53:51 -04002007
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002008 ret = btrfs_delalloc_reserve_space(inode, page_start,
2009 PAGE_CACHE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002010 if (ret) {
2011 mapping_set_error(page->mapping, ret);
2012 end_extent_writepage(page, ret, page_start, page_end);
2013 ClearPageChecked(page);
2014 goto out;
2015 }
2016
Josef Bacik2ac55d42010-02-03 19:33:23 +00002017 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002018 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002019 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002020out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002021 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2022 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002023out_page:
2024 unlock_page(page);
2025 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002026 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002027}
2028
2029/*
2030 * There are a few paths in the higher layers of the kernel that directly
2031 * set the page dirty bit without asking the filesystem if it is a
2032 * good idea. This causes problems because we want to make sure COW
2033 * properly happens and the data=ordered rules are followed.
2034 *
Chris Masonc8b97812008-10-29 14:49:59 -04002035 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002036 * hasn't been properly setup for IO. We kick off an async process
2037 * to fix it up. The async helper will wait for ordered extents, set
2038 * the delalloc bit and make it safe to write the page.
2039 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002040static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002041{
2042 struct inode *inode = page->mapping->host;
2043 struct btrfs_writepage_fixup *fixup;
2044 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002045
Chris Mason8b62b722009-09-02 16:53:46 -04002046 /* this page is properly in the ordered list */
2047 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002048 return 0;
2049
2050 if (PageChecked(page))
2051 return -EAGAIN;
2052
2053 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2054 if (!fixup)
2055 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002056
Chris Mason247e7432008-07-17 12:53:51 -04002057 SetPageChecked(page);
2058 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002059 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2060 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002061 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002062 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002063 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002064}
2065
Yan Zhengd899e052008-10-30 14:25:28 -04002066static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2067 struct inode *inode, u64 file_pos,
2068 u64 disk_bytenr, u64 disk_num_bytes,
2069 u64 num_bytes, u64 ram_bytes,
2070 u8 compression, u8 encryption,
2071 u16 other_encoding, int extent_type)
2072{
2073 struct btrfs_root *root = BTRFS_I(inode)->root;
2074 struct btrfs_file_extent_item *fi;
2075 struct btrfs_path *path;
2076 struct extent_buffer *leaf;
2077 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002078 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002079 int ret;
2080
2081 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002082 if (!path)
2083 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002084
Chris Masona1ed8352009-09-11 12:27:37 -04002085 /*
2086 * we may be replacing one extent in the tree with another.
2087 * The new extent is pinned in the extent map, and we don't want
2088 * to drop it from the cache until it is completely in the btree.
2089 *
2090 * So, tell btrfs_drop_extents to leave this extent in the cache.
2091 * the caller is expected to unpin it and allow it to be merged
2092 * with the others.
2093 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002094 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2095 file_pos + num_bytes, NULL, 0,
2096 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002097 if (ret)
2098 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002099
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002100 if (!extent_inserted) {
2101 ins.objectid = btrfs_ino(inode);
2102 ins.offset = file_pos;
2103 ins.type = BTRFS_EXTENT_DATA_KEY;
2104
2105 path->leave_spinning = 1;
2106 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2107 sizeof(*fi));
2108 if (ret)
2109 goto out;
2110 }
Yan Zhengd899e052008-10-30 14:25:28 -04002111 leaf = path->nodes[0];
2112 fi = btrfs_item_ptr(leaf, path->slots[0],
2113 struct btrfs_file_extent_item);
2114 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2115 btrfs_set_file_extent_type(leaf, fi, extent_type);
2116 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2117 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2118 btrfs_set_file_extent_offset(leaf, fi, 0);
2119 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2120 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2121 btrfs_set_file_extent_compression(leaf, fi, compression);
2122 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2123 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002124
Yan Zhengd899e052008-10-30 14:25:28 -04002125 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002126 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002127
2128 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002129
2130 ins.objectid = disk_bytenr;
2131 ins.offset = disk_num_bytes;
2132 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002133 ret = btrfs_alloc_reserved_file_extent(trans, root,
2134 root->root_key.objectid,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002135 btrfs_ino(inode), file_pos,
2136 ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002137 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002138 * Release the reserved range from inode dirty range map, as it is
2139 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002140 */
2141 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002142out:
Yan Zhengd899e052008-10-30 14:25:28 -04002143 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002144
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002145 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002146}
2147
Liu Bo38c227d2013-01-29 03:18:40 +00002148/* snapshot-aware defrag */
2149struct sa_defrag_extent_backref {
2150 struct rb_node node;
2151 struct old_sa_defrag_extent *old;
2152 u64 root_id;
2153 u64 inum;
2154 u64 file_pos;
2155 u64 extent_offset;
2156 u64 num_bytes;
2157 u64 generation;
2158};
2159
2160struct old_sa_defrag_extent {
2161 struct list_head list;
2162 struct new_sa_defrag_extent *new;
2163
2164 u64 extent_offset;
2165 u64 bytenr;
2166 u64 offset;
2167 u64 len;
2168 int count;
2169};
2170
2171struct new_sa_defrag_extent {
2172 struct rb_root root;
2173 struct list_head head;
2174 struct btrfs_path *path;
2175 struct inode *inode;
2176 u64 file_pos;
2177 u64 len;
2178 u64 bytenr;
2179 u64 disk_len;
2180 u8 compress_type;
2181};
2182
2183static int backref_comp(struct sa_defrag_extent_backref *b1,
2184 struct sa_defrag_extent_backref *b2)
2185{
2186 if (b1->root_id < b2->root_id)
2187 return -1;
2188 else if (b1->root_id > b2->root_id)
2189 return 1;
2190
2191 if (b1->inum < b2->inum)
2192 return -1;
2193 else if (b1->inum > b2->inum)
2194 return 1;
2195
2196 if (b1->file_pos < b2->file_pos)
2197 return -1;
2198 else if (b1->file_pos > b2->file_pos)
2199 return 1;
2200
2201 /*
2202 * [------------------------------] ===> (a range of space)
2203 * |<--->| |<---->| =============> (fs/file tree A)
2204 * |<---------------------------->| ===> (fs/file tree B)
2205 *
2206 * A range of space can refer to two file extents in one tree while
2207 * refer to only one file extent in another tree.
2208 *
2209 * So we may process a disk offset more than one time(two extents in A)
2210 * and locate at the same extent(one extent in B), then insert two same
2211 * backrefs(both refer to the extent in B).
2212 */
2213 return 0;
2214}
2215
2216static void backref_insert(struct rb_root *root,
2217 struct sa_defrag_extent_backref *backref)
2218{
2219 struct rb_node **p = &root->rb_node;
2220 struct rb_node *parent = NULL;
2221 struct sa_defrag_extent_backref *entry;
2222 int ret;
2223
2224 while (*p) {
2225 parent = *p;
2226 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2227
2228 ret = backref_comp(backref, entry);
2229 if (ret < 0)
2230 p = &(*p)->rb_left;
2231 else
2232 p = &(*p)->rb_right;
2233 }
2234
2235 rb_link_node(&backref->node, parent, p);
2236 rb_insert_color(&backref->node, root);
2237}
2238
2239/*
2240 * Note the backref might has changed, and in this case we just return 0.
2241 */
2242static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2243 void *ctx)
2244{
2245 struct btrfs_file_extent_item *extent;
2246 struct btrfs_fs_info *fs_info;
2247 struct old_sa_defrag_extent *old = ctx;
2248 struct new_sa_defrag_extent *new = old->new;
2249 struct btrfs_path *path = new->path;
2250 struct btrfs_key key;
2251 struct btrfs_root *root;
2252 struct sa_defrag_extent_backref *backref;
2253 struct extent_buffer *leaf;
2254 struct inode *inode = new->inode;
2255 int slot;
2256 int ret;
2257 u64 extent_offset;
2258 u64 num_bytes;
2259
2260 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2261 inum == btrfs_ino(inode))
2262 return 0;
2263
2264 key.objectid = root_id;
2265 key.type = BTRFS_ROOT_ITEM_KEY;
2266 key.offset = (u64)-1;
2267
2268 fs_info = BTRFS_I(inode)->root->fs_info;
2269 root = btrfs_read_fs_root_no_name(fs_info, &key);
2270 if (IS_ERR(root)) {
2271 if (PTR_ERR(root) == -ENOENT)
2272 return 0;
2273 WARN_ON(1);
2274 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2275 inum, offset, root_id);
2276 return PTR_ERR(root);
2277 }
2278
2279 key.objectid = inum;
2280 key.type = BTRFS_EXTENT_DATA_KEY;
2281 if (offset > (u64)-1 << 32)
2282 key.offset = 0;
2283 else
2284 key.offset = offset;
2285
2286 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302287 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002288 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002289 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002290
2291 while (1) {
2292 cond_resched();
2293
2294 leaf = path->nodes[0];
2295 slot = path->slots[0];
2296
2297 if (slot >= btrfs_header_nritems(leaf)) {
2298 ret = btrfs_next_leaf(root, path);
2299 if (ret < 0) {
2300 goto out;
2301 } else if (ret > 0) {
2302 ret = 0;
2303 goto out;
2304 }
2305 continue;
2306 }
2307
2308 path->slots[0]++;
2309
2310 btrfs_item_key_to_cpu(leaf, &key, slot);
2311
2312 if (key.objectid > inum)
2313 goto out;
2314
2315 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2316 continue;
2317
2318 extent = btrfs_item_ptr(leaf, slot,
2319 struct btrfs_file_extent_item);
2320
2321 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2322 continue;
2323
Liu Boe68afa42013-07-01 22:13:26 +08002324 /*
2325 * 'offset' refers to the exact key.offset,
2326 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2327 * (key.offset - extent_offset).
2328 */
2329 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002330 continue;
2331
Liu Boe68afa42013-07-01 22:13:26 +08002332 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002333 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002334
Liu Bo38c227d2013-01-29 03:18:40 +00002335 if (extent_offset >= old->extent_offset + old->offset +
2336 old->len || extent_offset + num_bytes <=
2337 old->extent_offset + old->offset)
2338 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002339 break;
2340 }
2341
2342 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2343 if (!backref) {
2344 ret = -ENOENT;
2345 goto out;
2346 }
2347
2348 backref->root_id = root_id;
2349 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002350 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002351 backref->num_bytes = num_bytes;
2352 backref->extent_offset = extent_offset;
2353 backref->generation = btrfs_file_extent_generation(leaf, extent);
2354 backref->old = old;
2355 backref_insert(&new->root, backref);
2356 old->count++;
2357out:
2358 btrfs_release_path(path);
2359 WARN_ON(ret);
2360 return ret;
2361}
2362
2363static noinline bool record_extent_backrefs(struct btrfs_path *path,
2364 struct new_sa_defrag_extent *new)
2365{
2366 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2367 struct old_sa_defrag_extent *old, *tmp;
2368 int ret;
2369
2370 new->path = path;
2371
2372 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002373 ret = iterate_inodes_from_logical(old->bytenr +
2374 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002375 path, record_one_backref,
2376 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002377 if (ret < 0 && ret != -ENOENT)
2378 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002379
2380 /* no backref to be processed for this extent */
2381 if (!old->count) {
2382 list_del(&old->list);
2383 kfree(old);
2384 }
2385 }
2386
2387 if (list_empty(&new->head))
2388 return false;
2389
2390 return true;
2391}
2392
2393static int relink_is_mergable(struct extent_buffer *leaf,
2394 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002395 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002396{
Liu Bo116e0022013-08-02 16:30:40 +08002397 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002398 return 0;
2399
2400 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2401 return 0;
2402
Liu Bo116e0022013-08-02 16:30:40 +08002403 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2404 return 0;
2405
2406 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002407 btrfs_file_extent_other_encoding(leaf, fi))
2408 return 0;
2409
2410 return 1;
2411}
2412
2413/*
2414 * Note the backref might has changed, and in this case we just return 0.
2415 */
2416static noinline int relink_extent_backref(struct btrfs_path *path,
2417 struct sa_defrag_extent_backref *prev,
2418 struct sa_defrag_extent_backref *backref)
2419{
2420 struct btrfs_file_extent_item *extent;
2421 struct btrfs_file_extent_item *item;
2422 struct btrfs_ordered_extent *ordered;
2423 struct btrfs_trans_handle *trans;
2424 struct btrfs_fs_info *fs_info;
2425 struct btrfs_root *root;
2426 struct btrfs_key key;
2427 struct extent_buffer *leaf;
2428 struct old_sa_defrag_extent *old = backref->old;
2429 struct new_sa_defrag_extent *new = old->new;
2430 struct inode *src_inode = new->inode;
2431 struct inode *inode;
2432 struct extent_state *cached = NULL;
2433 int ret = 0;
2434 u64 start;
2435 u64 len;
2436 u64 lock_start;
2437 u64 lock_end;
2438 bool merge = false;
2439 int index;
2440
2441 if (prev && prev->root_id == backref->root_id &&
2442 prev->inum == backref->inum &&
2443 prev->file_pos + prev->num_bytes == backref->file_pos)
2444 merge = true;
2445
2446 /* step 1: get root */
2447 key.objectid = backref->root_id;
2448 key.type = BTRFS_ROOT_ITEM_KEY;
2449 key.offset = (u64)-1;
2450
2451 fs_info = BTRFS_I(src_inode)->root->fs_info;
2452 index = srcu_read_lock(&fs_info->subvol_srcu);
2453
2454 root = btrfs_read_fs_root_no_name(fs_info, &key);
2455 if (IS_ERR(root)) {
2456 srcu_read_unlock(&fs_info->subvol_srcu, index);
2457 if (PTR_ERR(root) == -ENOENT)
2458 return 0;
2459 return PTR_ERR(root);
2460 }
Liu Bo38c227d2013-01-29 03:18:40 +00002461
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002462 if (btrfs_root_readonly(root)) {
2463 srcu_read_unlock(&fs_info->subvol_srcu, index);
2464 return 0;
2465 }
2466
Liu Bo38c227d2013-01-29 03:18:40 +00002467 /* step 2: get inode */
2468 key.objectid = backref->inum;
2469 key.type = BTRFS_INODE_ITEM_KEY;
2470 key.offset = 0;
2471
2472 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2473 if (IS_ERR(inode)) {
2474 srcu_read_unlock(&fs_info->subvol_srcu, index);
2475 return 0;
2476 }
2477
2478 srcu_read_unlock(&fs_info->subvol_srcu, index);
2479
2480 /* step 3: relink backref */
2481 lock_start = backref->file_pos;
2482 lock_end = backref->file_pos + backref->num_bytes - 1;
2483 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2484 0, &cached);
2485
2486 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2487 if (ordered) {
2488 btrfs_put_ordered_extent(ordered);
2489 goto out_unlock;
2490 }
2491
2492 trans = btrfs_join_transaction(root);
2493 if (IS_ERR(trans)) {
2494 ret = PTR_ERR(trans);
2495 goto out_unlock;
2496 }
2497
2498 key.objectid = backref->inum;
2499 key.type = BTRFS_EXTENT_DATA_KEY;
2500 key.offset = backref->file_pos;
2501
2502 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2503 if (ret < 0) {
2504 goto out_free_path;
2505 } else if (ret > 0) {
2506 ret = 0;
2507 goto out_free_path;
2508 }
2509
2510 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2511 struct btrfs_file_extent_item);
2512
2513 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2514 backref->generation)
2515 goto out_free_path;
2516
2517 btrfs_release_path(path);
2518
2519 start = backref->file_pos;
2520 if (backref->extent_offset < old->extent_offset + old->offset)
2521 start += old->extent_offset + old->offset -
2522 backref->extent_offset;
2523
2524 len = min(backref->extent_offset + backref->num_bytes,
2525 old->extent_offset + old->offset + old->len);
2526 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2527
2528 ret = btrfs_drop_extents(trans, root, inode, start,
2529 start + len, 1);
2530 if (ret)
2531 goto out_free_path;
2532again:
2533 key.objectid = btrfs_ino(inode);
2534 key.type = BTRFS_EXTENT_DATA_KEY;
2535 key.offset = start;
2536
Liu Boa09a0a72013-03-11 09:20:58 +00002537 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002538 if (merge) {
2539 struct btrfs_file_extent_item *fi;
2540 u64 extent_len;
2541 struct btrfs_key found_key;
2542
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002543 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002544 if (ret < 0)
2545 goto out_free_path;
2546
2547 path->slots[0]--;
2548 leaf = path->nodes[0];
2549 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2550
2551 fi = btrfs_item_ptr(leaf, path->slots[0],
2552 struct btrfs_file_extent_item);
2553 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2554
Liu Bo116e0022013-08-02 16:30:40 +08002555 if (extent_len + found_key.offset == start &&
2556 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002557 btrfs_set_file_extent_num_bytes(leaf, fi,
2558 extent_len + len);
2559 btrfs_mark_buffer_dirty(leaf);
2560 inode_add_bytes(inode, len);
2561
2562 ret = 1;
2563 goto out_free_path;
2564 } else {
2565 merge = false;
2566 btrfs_release_path(path);
2567 goto again;
2568 }
2569 }
2570
2571 ret = btrfs_insert_empty_item(trans, root, path, &key,
2572 sizeof(*extent));
2573 if (ret) {
2574 btrfs_abort_transaction(trans, root, ret);
2575 goto out_free_path;
2576 }
2577
2578 leaf = path->nodes[0];
2579 item = btrfs_item_ptr(leaf, path->slots[0],
2580 struct btrfs_file_extent_item);
2581 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2582 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2583 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2584 btrfs_set_file_extent_num_bytes(leaf, item, len);
2585 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2586 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2587 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2588 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2589 btrfs_set_file_extent_encryption(leaf, item, 0);
2590 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2591
2592 btrfs_mark_buffer_dirty(leaf);
2593 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002594 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002595
2596 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2597 new->disk_len, 0,
2598 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002599 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002600 if (ret) {
2601 btrfs_abort_transaction(trans, root, ret);
2602 goto out_free_path;
2603 }
2604
2605 ret = 1;
2606out_free_path:
2607 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002608 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002609 btrfs_end_transaction(trans, root);
2610out_unlock:
2611 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2612 &cached, GFP_NOFS);
2613 iput(inode);
2614 return ret;
2615}
2616
Liu Bo6f519562013-10-29 10:45:05 +08002617static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2618{
2619 struct old_sa_defrag_extent *old, *tmp;
2620
2621 if (!new)
2622 return;
2623
2624 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002625 kfree(old);
2626 }
2627 kfree(new);
2628}
2629
Liu Bo38c227d2013-01-29 03:18:40 +00002630static void relink_file_extents(struct new_sa_defrag_extent *new)
2631{
2632 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002633 struct sa_defrag_extent_backref *backref;
2634 struct sa_defrag_extent_backref *prev = NULL;
2635 struct inode *inode;
2636 struct btrfs_root *root;
2637 struct rb_node *node;
2638 int ret;
2639
2640 inode = new->inode;
2641 root = BTRFS_I(inode)->root;
2642
2643 path = btrfs_alloc_path();
2644 if (!path)
2645 return;
2646
2647 if (!record_extent_backrefs(path, new)) {
2648 btrfs_free_path(path);
2649 goto out;
2650 }
2651 btrfs_release_path(path);
2652
2653 while (1) {
2654 node = rb_first(&new->root);
2655 if (!node)
2656 break;
2657 rb_erase(node, &new->root);
2658
2659 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2660
2661 ret = relink_extent_backref(path, prev, backref);
2662 WARN_ON(ret < 0);
2663
2664 kfree(prev);
2665
2666 if (ret == 1)
2667 prev = backref;
2668 else
2669 prev = NULL;
2670 cond_resched();
2671 }
2672 kfree(prev);
2673
2674 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002675out:
Liu Bo6f519562013-10-29 10:45:05 +08002676 free_sa_defrag_extent(new);
2677
Liu Bo38c227d2013-01-29 03:18:40 +00002678 atomic_dec(&root->fs_info->defrag_running);
2679 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002680}
2681
2682static struct new_sa_defrag_extent *
2683record_old_file_extents(struct inode *inode,
2684 struct btrfs_ordered_extent *ordered)
2685{
2686 struct btrfs_root *root = BTRFS_I(inode)->root;
2687 struct btrfs_path *path;
2688 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002689 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002690 struct new_sa_defrag_extent *new;
2691 int ret;
2692
2693 new = kmalloc(sizeof(*new), GFP_NOFS);
2694 if (!new)
2695 return NULL;
2696
2697 new->inode = inode;
2698 new->file_pos = ordered->file_offset;
2699 new->len = ordered->len;
2700 new->bytenr = ordered->start;
2701 new->disk_len = ordered->disk_len;
2702 new->compress_type = ordered->compress_type;
2703 new->root = RB_ROOT;
2704 INIT_LIST_HEAD(&new->head);
2705
2706 path = btrfs_alloc_path();
2707 if (!path)
2708 goto out_kfree;
2709
2710 key.objectid = btrfs_ino(inode);
2711 key.type = BTRFS_EXTENT_DATA_KEY;
2712 key.offset = new->file_pos;
2713
2714 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2715 if (ret < 0)
2716 goto out_free_path;
2717 if (ret > 0 && path->slots[0] > 0)
2718 path->slots[0]--;
2719
2720 /* find out all the old extents for the file range */
2721 while (1) {
2722 struct btrfs_file_extent_item *extent;
2723 struct extent_buffer *l;
2724 int slot;
2725 u64 num_bytes;
2726 u64 offset;
2727 u64 end;
2728 u64 disk_bytenr;
2729 u64 extent_offset;
2730
2731 l = path->nodes[0];
2732 slot = path->slots[0];
2733
2734 if (slot >= btrfs_header_nritems(l)) {
2735 ret = btrfs_next_leaf(root, path);
2736 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002737 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002738 else if (ret > 0)
2739 break;
2740 continue;
2741 }
2742
2743 btrfs_item_key_to_cpu(l, &key, slot);
2744
2745 if (key.objectid != btrfs_ino(inode))
2746 break;
2747 if (key.type != BTRFS_EXTENT_DATA_KEY)
2748 break;
2749 if (key.offset >= new->file_pos + new->len)
2750 break;
2751
2752 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2753
2754 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2755 if (key.offset + num_bytes < new->file_pos)
2756 goto next;
2757
2758 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2759 if (!disk_bytenr)
2760 goto next;
2761
2762 extent_offset = btrfs_file_extent_offset(l, extent);
2763
2764 old = kmalloc(sizeof(*old), GFP_NOFS);
2765 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002766 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002767
2768 offset = max(new->file_pos, key.offset);
2769 end = min(new->file_pos + new->len, key.offset + num_bytes);
2770
2771 old->bytenr = disk_bytenr;
2772 old->extent_offset = extent_offset;
2773 old->offset = offset - key.offset;
2774 old->len = end - offset;
2775 old->new = new;
2776 old->count = 0;
2777 list_add_tail(&old->list, &new->head);
2778next:
2779 path->slots[0]++;
2780 cond_resched();
2781 }
2782
2783 btrfs_free_path(path);
2784 atomic_inc(&root->fs_info->defrag_running);
2785
2786 return new;
2787
Liu Bo38c227d2013-01-29 03:18:40 +00002788out_free_path:
2789 btrfs_free_path(path);
2790out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002791 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002792 return NULL;
2793}
2794
Miao Xiee570fd22014-06-19 10:42:50 +08002795static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2796 u64 start, u64 len)
2797{
2798 struct btrfs_block_group_cache *cache;
2799
2800 cache = btrfs_lookup_block_group(root->fs_info, start);
2801 ASSERT(cache);
2802
2803 spin_lock(&cache->lock);
2804 cache->delalloc_bytes -= len;
2805 spin_unlock(&cache->lock);
2806
2807 btrfs_put_block_group(cache);
2808}
2809
Chris Masond352ac62008-09-29 15:18:18 -04002810/* as ordered data IO finishes, this gets called so we can finish
2811 * an ordered extent if the range of bytes in the file it covers are
2812 * fully written.
2813 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002814static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002815{
Josef Bacik5fd02042012-05-02 14:00:54 -04002816 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002817 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002818 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002819 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002820 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002821 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002822 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002823 int ret = 0;
2824 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002825 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002826 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002827
Liu Bo83eea1f2012-07-10 05:28:39 -06002828 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002829
Josef Bacik5fd02042012-05-02 14:00:54 -04002830 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2831 ret = -EIO;
2832 goto out;
2833 }
2834
Miao Xief6124962014-09-12 18:44:04 +08002835 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2836 ordered_extent->file_offset +
2837 ordered_extent->len - 1);
2838
Josef Bacik77cef2e2013-08-29 13:57:21 -04002839 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2840 truncated = true;
2841 logical_len = ordered_extent->truncated_len;
2842 /* Truncated the entire extent, don't bother adding */
2843 if (!logical_len)
2844 goto out;
2845 }
2846
Yan, Zhengc2167752009-11-12 09:34:21 +00002847 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002848 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002849
2850 /*
2851 * For mwrite(mmap + memset to write) case, we still reserve
2852 * space for NOCOW range.
2853 * As NOCOW won't cause a new delayed ref, just free the space
2854 */
2855 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2856 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002857 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2858 if (nolock)
2859 trans = btrfs_join_transaction_nolock(root);
2860 else
2861 trans = btrfs_join_transaction(root);
2862 if (IS_ERR(trans)) {
2863 ret = PTR_ERR(trans);
2864 trans = NULL;
2865 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002866 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002867 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2868 ret = btrfs_update_inode_fallback(trans, root, inode);
2869 if (ret) /* -ENOMEM or corruption */
2870 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002871 goto out;
2872 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002873
Josef Bacik2ac55d42010-02-03 19:33:23 +00002874 lock_extent_bits(io_tree, ordered_extent->file_offset,
2875 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002876 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002877
Liu Bo38c227d2013-01-29 03:18:40 +00002878 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2879 ordered_extent->file_offset + ordered_extent->len - 1,
2880 EXTENT_DEFRAG, 1, cached_state);
2881 if (ret) {
2882 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002883 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002884 /* the inode is shared */
2885 new = record_old_file_extents(inode, ordered_extent);
2886
2887 clear_extent_bit(io_tree, ordered_extent->file_offset,
2888 ordered_extent->file_offset + ordered_extent->len - 1,
2889 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2890 }
2891
Josef Bacik0cb59c92010-07-02 12:14:14 -04002892 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002893 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002894 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002895 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002896 if (IS_ERR(trans)) {
2897 ret = PTR_ERR(trans);
2898 trans = NULL;
2899 goto out_unlock;
2900 }
Chris Masona79b7d42014-05-22 16:18:52 -07002901
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002902 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002903
Chris Masonc8b97812008-10-29 14:49:59 -04002904 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002905 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002906 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002907 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002908 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002909 ordered_extent->file_offset,
2910 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002911 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002912 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002913 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002914 ret = insert_reserved_file_extent(trans, inode,
2915 ordered_extent->file_offset,
2916 ordered_extent->start,
2917 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002918 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002919 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002920 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002921 if (!ret)
2922 btrfs_release_delalloc_bytes(root,
2923 ordered_extent->start,
2924 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002925 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002926 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2927 ordered_extent->file_offset, ordered_extent->len,
2928 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002929 if (ret < 0) {
2930 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002931 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002932 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002933
Chris Masone6dcd2d2008-07-17 12:53:50 -04002934 add_pending_csums(trans, inode, ordered_extent->file_offset,
2935 &ordered_extent->list);
2936
Josef Bacik6c760c02012-11-09 10:53:21 -05002937 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2938 ret = btrfs_update_inode_fallback(trans, root, inode);
2939 if (ret) { /* -ENOMEM or corruption */
2940 btrfs_abort_transaction(trans, root, ret);
2941 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002942 }
2943 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002944out_unlock:
2945 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2946 ordered_extent->file_offset +
2947 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002948out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002949 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002950 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002951 if (trans)
2952 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002953
Josef Bacik77cef2e2013-08-29 13:57:21 -04002954 if (ret || truncated) {
2955 u64 start, end;
2956
2957 if (truncated)
2958 start = ordered_extent->file_offset + logical_len;
2959 else
2960 start = ordered_extent->file_offset;
2961 end = ordered_extent->file_offset + ordered_extent->len - 1;
2962 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2963
2964 /* Drop the cache for the part of the extent we didn't write. */
2965 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002966
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002967 /*
2968 * If the ordered extent had an IOERR or something else went
2969 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002970 * back to the allocator. We only free the extent in the
2971 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002972 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002973 if ((ret || !logical_len) &&
2974 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002975 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2976 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002977 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002978 }
2979
2980
Josef Bacik5fd02042012-05-02 14:00:54 -04002981 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002982 * This needs to be done to make sure anybody waiting knows we are done
2983 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002984 */
2985 btrfs_remove_ordered_extent(inode, ordered_extent);
2986
Liu Bo38c227d2013-01-29 03:18:40 +00002987 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002988 if (new) {
2989 if (ret) {
2990 free_sa_defrag_extent(new);
2991 atomic_dec(&root->fs_info->defrag_running);
2992 } else {
2993 relink_file_extents(new);
2994 }
2995 }
Liu Bo38c227d2013-01-29 03:18:40 +00002996
Chris Masone6dcd2d2008-07-17 12:53:50 -04002997 /* once for us */
2998 btrfs_put_ordered_extent(ordered_extent);
2999 /* once for the tree */
3000 btrfs_put_ordered_extent(ordered_extent);
3001
Josef Bacik5fd02042012-05-02 14:00:54 -04003002 return ret;
3003}
3004
3005static void finish_ordered_fn(struct btrfs_work *work)
3006{
3007 struct btrfs_ordered_extent *ordered_extent;
3008 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3009 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003010}
3011
Christoph Hellwigb2950862008-12-02 09:54:17 -05003012static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003013 struct extent_state *state, int uptodate)
3014{
Josef Bacik5fd02042012-05-02 14:00:54 -04003015 struct inode *inode = page->mapping->host;
3016 struct btrfs_root *root = BTRFS_I(inode)->root;
3017 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003018 struct btrfs_workqueue *wq;
3019 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003020
liubo1abe9b82011-03-24 11:18:59 +00003021 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3022
Chris Mason8b62b722009-09-02 16:53:46 -04003023 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003024 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3025 end - start + 1, uptodate))
3026 return 0;
3027
Liu Bo9e0af232014-08-15 23:36:53 +08003028 if (btrfs_is_free_space_inode(inode)) {
3029 wq = root->fs_info->endio_freespace_worker;
3030 func = btrfs_freespace_write_helper;
3031 } else {
3032 wq = root->fs_info->endio_write_workers;
3033 func = btrfs_endio_write_helper;
3034 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003035
Liu Bo9e0af232014-08-15 23:36:53 +08003036 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3037 NULL);
3038 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003039
3040 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003041}
3042
Miao Xiedc380ae2014-09-12 18:43:55 +08003043static int __readpage_endio_check(struct inode *inode,
3044 struct btrfs_io_bio *io_bio,
3045 int icsum, struct page *page,
3046 int pgoff, u64 start, size_t len)
3047{
3048 char *kaddr;
3049 u32 csum_expected;
3050 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003051
3052 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3053
3054 kaddr = kmap_atomic(page);
3055 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3056 btrfs_csum_final(csum, (char *)&csum);
3057 if (csum != csum_expected)
3058 goto zeroit;
3059
3060 kunmap_atomic(kaddr);
3061 return 0;
3062zeroit:
David Sterba94647322015-10-08 11:01:36 +02003063 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3064 "csum failed ino %llu off %llu csum %u expected csum %u",
Miao Xiedc380ae2014-09-12 18:43:55 +08003065 btrfs_ino(inode), start, csum, csum_expected);
3066 memset(kaddr + pgoff, 1, len);
3067 flush_dcache_page(page);
3068 kunmap_atomic(kaddr);
3069 if (csum_expected == 0)
3070 return 0;
3071 return -EIO;
3072}
3073
Chris Masond352ac62008-09-29 15:18:18 -04003074/*
Chris Masond352ac62008-09-29 15:18:18 -04003075 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003076 * if there's a match, we allow the bio to finish. If not, the code in
3077 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003078 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003079static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3080 u64 phy_offset, struct page *page,
3081 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003082{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003083 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003084 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003085 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003086 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003087
Chris Masond20f7042008-12-08 16:58:54 -05003088 if (PageChecked(page)) {
3089 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003090 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003091 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003092
3093 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003094 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003095
Yan Zheng17d217f2008-12-12 10:03:38 -05003096 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003097 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05003098 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
3099 GFP_NOFS);
3100 return 0;
3101 }
3102
Miao Xiefacc8a222013-07-25 19:22:34 +08003103 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003104 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3105 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003106}
Chris Masonb888db22007-08-27 16:49:44 -04003107
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003108void btrfs_add_delayed_iput(struct inode *inode)
3109{
3110 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
David Sterba8089fe62015-11-19 14:15:51 +01003111 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003112
3113 if (atomic_add_unless(&inode->i_count, -1, 1))
3114 return;
3115
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003116 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003117 if (binode->delayed_iput_count == 0) {
3118 ASSERT(list_empty(&binode->delayed_iput));
3119 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3120 } else {
3121 binode->delayed_iput_count++;
3122 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003123 spin_unlock(&fs_info->delayed_iput_lock);
3124}
3125
3126void btrfs_run_delayed_iputs(struct btrfs_root *root)
3127{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003128 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003129
Zhao Leid7c15172015-02-26 10:49:20 +08003130 down_read(&fs_info->delayed_iput_sem);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003131 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003132 while (!list_empty(&fs_info->delayed_iputs)) {
3133 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003134
David Sterba8089fe62015-11-19 14:15:51 +01003135 inode = list_first_entry(&fs_info->delayed_iputs,
3136 struct btrfs_inode, delayed_iput);
3137 if (inode->delayed_iput_count) {
3138 inode->delayed_iput_count--;
3139 list_move_tail(&inode->delayed_iput,
3140 &fs_info->delayed_iputs);
3141 } else {
3142 list_del_init(&inode->delayed_iput);
3143 }
3144 spin_unlock(&fs_info->delayed_iput_lock);
3145 iput(&inode->vfs_inode);
3146 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003147 }
David Sterba8089fe62015-11-19 14:15:51 +01003148 spin_unlock(&fs_info->delayed_iput_lock);
Zhao Leid7c15172015-02-26 10:49:20 +08003149 up_read(&root->fs_info->delayed_iput_sem);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003150}
3151
Yan, Zhengd68fc572010-05-16 10:49:58 -04003152/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003153 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003154 * files in the subvolume, it removes orphan item and frees block_rsv
3155 * structure.
3156 */
3157void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3158 struct btrfs_root *root)
3159{
Josef Bacik90290e12011-12-02 15:44:12 -05003160 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003161 int ret;
3162
Josef Bacik8a35d952012-05-23 14:26:42 -04003163 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003164 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3165 return;
3166
Josef Bacik90290e12011-12-02 15:44:12 -05003167 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003168 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003169 spin_unlock(&root->orphan_lock);
3170 return;
3171 }
3172
3173 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3174 spin_unlock(&root->orphan_lock);
3175 return;
3176 }
3177
3178 block_rsv = root->orphan_block_rsv;
3179 root->orphan_block_rsv = NULL;
3180 spin_unlock(&root->orphan_lock);
3181
Miao Xie27cdeb72014-04-02 19:51:05 +08003182 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003183 btrfs_root_refs(&root->root_item) > 0) {
3184 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3185 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003186 if (ret)
3187 btrfs_abort_transaction(trans, root, ret);
3188 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003189 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3190 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003191 }
3192
Josef Bacik90290e12011-12-02 15:44:12 -05003193 if (block_rsv) {
3194 WARN_ON(block_rsv->size > 0);
3195 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003196 }
3197}
3198
3199/*
Josef Bacik7b128762008-07-24 12:17:14 -04003200 * This creates an orphan entry for the given inode in case something goes
3201 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003202 *
3203 * NOTE: caller of this function should reserve 5 units of metadata for
3204 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003205 */
3206int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3207{
3208 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003209 struct btrfs_block_rsv *block_rsv = NULL;
3210 int reserve = 0;
3211 int insert = 0;
3212 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003213
Yan, Zhengd68fc572010-05-16 10:49:58 -04003214 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003215 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003216 if (!block_rsv)
3217 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003218 }
3219
Yan, Zhengd68fc572010-05-16 10:49:58 -04003220 spin_lock(&root->orphan_lock);
3221 if (!root->orphan_block_rsv) {
3222 root->orphan_block_rsv = block_rsv;
3223 } else if (block_rsv) {
3224 btrfs_free_block_rsv(root, block_rsv);
3225 block_rsv = NULL;
3226 }
Josef Bacik7b128762008-07-24 12:17:14 -04003227
Josef Bacik8a35d952012-05-23 14:26:42 -04003228 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3229 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003230#if 0
3231 /*
3232 * For proper ENOSPC handling, we should do orphan
3233 * cleanup when mounting. But this introduces backward
3234 * compatibility issue.
3235 */
3236 if (!xchg(&root->orphan_item_inserted, 1))
3237 insert = 2;
3238 else
3239 insert = 1;
3240#endif
3241 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003242 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003243 }
Josef Bacik7b128762008-07-24 12:17:14 -04003244
Josef Bacik72ac3c02012-05-23 14:13:11 -04003245 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3246 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003247 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003248 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003249
Yan, Zhengd68fc572010-05-16 10:49:58 -04003250 /* grab metadata reservation from transaction handle */
3251 if (reserve) {
3252 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003253 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003254 }
3255
3256 /* insert an orphan item to track this unlinked/truncated file */
3257 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003258 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003259 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003260 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003261 if (reserve) {
3262 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3263 &BTRFS_I(inode)->runtime_flags);
3264 btrfs_orphan_release_metadata(inode);
3265 }
3266 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003267 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3268 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003269 btrfs_abort_transaction(trans, root, ret);
3270 return ret;
3271 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003272 }
3273 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003274 }
3275
3276 /* insert an orphan item to track subvolume contains orphan files */
3277 if (insert >= 2) {
3278 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3279 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003280 if (ret && ret != -EEXIST) {
3281 btrfs_abort_transaction(trans, root, ret);
3282 return ret;
3283 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003284 }
3285 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003286}
3287
3288/*
3289 * We have done the truncate/delete so we can go ahead and remove the orphan
3290 * item for this particular inode.
3291 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003292static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3293 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003294{
3295 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003296 int delete_item = 0;
3297 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003298 int ret = 0;
3299
Yan, Zhengd68fc572010-05-16 10:49:58 -04003300 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003301 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3302 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003303 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003304
Josef Bacik72ac3c02012-05-23 14:13:11 -04003305 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3306 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003307 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003308 spin_unlock(&root->orphan_lock);
3309
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003310 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003311 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003312 if (trans)
3313 ret = btrfs_del_orphan_item(trans, root,
3314 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003315 }
Josef Bacik7b128762008-07-24 12:17:14 -04003316
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003317 if (release_rsv)
3318 btrfs_orphan_release_metadata(inode);
3319
Josef Bacik4ef31a42013-08-13 14:10:08 -04003320 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003321}
3322
3323/*
3324 * this cleans up any orphans that may be left on the list from the last use
3325 * of this root.
3326 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003327int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003328{
3329 struct btrfs_path *path;
3330 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003331 struct btrfs_key key, found_key;
3332 struct btrfs_trans_handle *trans;
3333 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003334 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003335 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3336
Yan, Zhengd68fc572010-05-16 10:49:58 -04003337 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003338 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003339
3340 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003341 if (!path) {
3342 ret = -ENOMEM;
3343 goto out;
3344 }
David Sterbae4058b52015-11-27 16:31:35 +01003345 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003346
3347 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003348 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003349 key.offset = (u64)-1;
3350
Josef Bacik7b128762008-07-24 12:17:14 -04003351 while (1) {
3352 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003353 if (ret < 0)
3354 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003355
3356 /*
3357 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003358 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003359 * find the key and see if we have stuff that matches
3360 */
3361 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003362 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003363 if (path->slots[0] == 0)
3364 break;
3365 path->slots[0]--;
3366 }
3367
3368 /* pull out the item */
3369 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003370 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3371
3372 /* make sure the item matches what we want */
3373 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3374 break;
David Sterba962a2982014-06-04 18:41:45 +02003375 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003376 break;
3377
3378 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003379 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003380
3381 /*
3382 * this is where we are basically btrfs_lookup, without the
3383 * crossing root thing. we store the inode number in the
3384 * offset of the orphan item.
3385 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003386
3387 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003388 btrfs_err(root->fs_info,
3389 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003390 ret = -EINVAL;
3391 goto out;
3392 }
3393
3394 last_objectid = found_key.offset;
3395
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003396 found_key.objectid = found_key.offset;
3397 found_key.type = BTRFS_INODE_ITEM_KEY;
3398 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003399 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303400 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003401 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003402 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003403
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003404 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3405 struct btrfs_root *dead_root;
3406 struct btrfs_fs_info *fs_info = root->fs_info;
3407 int is_dead_root = 0;
3408
3409 /*
3410 * this is an orphan in the tree root. Currently these
3411 * could come from 2 sources:
3412 * a) a snapshot deletion in progress
3413 * b) a free space cache inode
3414 * We need to distinguish those two, as the snapshot
3415 * orphan must not get deleted.
3416 * find_dead_roots already ran before us, so if this
3417 * is a snapshot deletion, we should find the root
3418 * in the dead_roots list
3419 */
3420 spin_lock(&fs_info->trans_lock);
3421 list_for_each_entry(dead_root, &fs_info->dead_roots,
3422 root_list) {
3423 if (dead_root->root_key.objectid ==
3424 found_key.objectid) {
3425 is_dead_root = 1;
3426 break;
3427 }
3428 }
3429 spin_unlock(&fs_info->trans_lock);
3430 if (is_dead_root) {
3431 /* prevent this orphan from being found again */
3432 key.offset = found_key.objectid - 1;
3433 continue;
3434 }
3435 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003436 /*
3437 * Inode is already gone but the orphan item is still there,
3438 * kill the orphan item.
3439 */
3440 if (ret == -ESTALE) {
3441 trans = btrfs_start_transaction(root, 1);
3442 if (IS_ERR(trans)) {
3443 ret = PTR_ERR(trans);
3444 goto out;
3445 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003446 btrfs_debug(root->fs_info, "auto deleting %Lu",
3447 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003448 ret = btrfs_del_orphan_item(trans, root,
3449 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003450 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003451 if (ret)
3452 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003453 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003454 }
Josef Bacik7b128762008-07-24 12:17:14 -04003455
Josef Bacik7b128762008-07-24 12:17:14 -04003456 /*
3457 * add this inode to the orphan list so btrfs_orphan_del does
3458 * the proper thing when we hit it
3459 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003460 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3461 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003462 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003463
Josef Bacik7b128762008-07-24 12:17:14 -04003464 /* if we have links, this was a truncate, lets do that */
3465 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303466 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003467 iput(inode);
3468 continue;
3469 }
Josef Bacik7b128762008-07-24 12:17:14 -04003470 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003471
3472 /* 1 for the orphan item deletion. */
3473 trans = btrfs_start_transaction(root, 1);
3474 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003475 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003476 ret = PTR_ERR(trans);
3477 goto out;
3478 }
3479 ret = btrfs_orphan_add(trans, inode);
3480 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003481 if (ret) {
3482 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003483 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003484 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003485
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003486 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003487 if (ret)
3488 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003489 } else {
3490 nr_unlink++;
3491 }
3492
3493 /* this will do delete_inode and everything for us */
3494 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003495 if (ret)
3496 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003497 }
Miao Xie3254c872011-11-10 20:45:05 -05003498 /* release the path since we're done with it */
3499 btrfs_release_path(path);
3500
Yan, Zhengd68fc572010-05-16 10:49:58 -04003501 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3502
3503 if (root->orphan_block_rsv)
3504 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3505 (u64)-1);
3506
Miao Xie27cdeb72014-04-02 19:51:05 +08003507 if (root->orphan_block_rsv ||
3508 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003509 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003510 if (!IS_ERR(trans))
3511 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003512 }
Josef Bacik7b128762008-07-24 12:17:14 -04003513
3514 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003515 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003516 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003517 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003518
3519out:
3520 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003521 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003522 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003523 btrfs_free_path(path);
3524 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003525}
3526
Chris Masond352ac62008-09-29 15:18:18 -04003527/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003528 * very simple check to peek ahead in the leaf looking for xattrs. If we
3529 * don't find any xattrs, we know there can't be any acls.
3530 *
3531 * slot is the slot the inode is in, objectid is the objectid of the inode
3532 */
3533static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003534 int slot, u64 objectid,
3535 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003536{
3537 u32 nritems = btrfs_header_nritems(leaf);
3538 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003539 static u64 xattr_access = 0;
3540 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003541 int scanned = 0;
3542
Josef Bacikf23b5a52013-06-19 10:16:26 -04003543 if (!xattr_access) {
3544 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3545 strlen(POSIX_ACL_XATTR_ACCESS));
3546 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3547 strlen(POSIX_ACL_XATTR_DEFAULT));
3548 }
3549
Chris Mason46a53cc2009-04-27 11:47:50 -04003550 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003551 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003552 while (slot < nritems) {
3553 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3554
3555 /* we found a different objectid, there must not be acls */
3556 if (found_key.objectid != objectid)
3557 return 0;
3558
3559 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003560 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003561 if (*first_xattr_slot == -1)
3562 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003563 if (found_key.offset == xattr_access ||
3564 found_key.offset == xattr_default)
3565 return 1;
3566 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003567
3568 /*
3569 * we found a key greater than an xattr key, there can't
3570 * be any acls later on
3571 */
3572 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3573 return 0;
3574
3575 slot++;
3576 scanned++;
3577
3578 /*
3579 * it goes inode, inode backrefs, xattrs, extents,
3580 * so if there are a ton of hard links to an inode there can
3581 * be a lot of backrefs. Don't waste time searching too hard,
3582 * this is just an optimization
3583 */
3584 if (scanned >= 8)
3585 break;
3586 }
3587 /* we hit the end of the leaf before we found an xattr or
3588 * something larger than an xattr. We have to assume the inode
3589 * has acls
3590 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003591 if (*first_xattr_slot == -1)
3592 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003593 return 1;
3594}
3595
3596/*
Chris Masond352ac62008-09-29 15:18:18 -04003597 * read an inode from the btree into the in-memory inode
3598 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003599static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003600{
3601 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003602 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003603 struct btrfs_inode_item *inode_item;
3604 struct btrfs_root *root = BTRFS_I(inode)->root;
3605 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003606 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003607 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003608 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003609 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003610 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003611 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003612
3613 ret = btrfs_fill_inode(inode, &rdev);
3614 if (!ret)
3615 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003616
3617 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003618 if (!path)
3619 goto make_bad;
3620
Chris Mason39279cc2007-06-12 06:35:45 -04003621 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003622
Chris Mason39279cc2007-06-12 06:35:45 -04003623 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003624 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003625 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003626
Chris Mason5f39d392007-10-15 16:14:19 -04003627 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003628
3629 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003630 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003631
Chris Mason5f39d392007-10-15 16:14:19 -04003632 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3633 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003634 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003635 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003636 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3637 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003638 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003639
David Sterbaa937b972014-12-12 17:39:12 +01003640 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3641 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003642
David Sterbaa937b972014-12-12 17:39:12 +01003643 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3644 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003645
David Sterbaa937b972014-12-12 17:39:12 +01003646 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3647 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003648
chandan r9cc97d62012-07-04 12:48:07 +05303649 BTRFS_I(inode)->i_otime.tv_sec =
3650 btrfs_timespec_sec(leaf, &inode_item->otime);
3651 BTRFS_I(inode)->i_otime.tv_nsec =
3652 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003653
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003654 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003655 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003656 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3657
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003658 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003659 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003660 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003661 rdev = btrfs_inode_rdev(leaf, inode_item);
3662
Josef Bacikaec74772008-07-24 12:12:38 -04003663 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003664 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003665
3666cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003667 /*
3668 * If we were modified in the current generation and evicted from memory
3669 * and then re-read we need to do a full sync since we don't have any
3670 * idea about which extents were modified before we were evicted from
3671 * cache.
3672 *
3673 * This is required for both inode re-read from disk and delayed inode
3674 * in delayed_nodes_tree.
3675 */
3676 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3677 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3678 &BTRFS_I(inode)->runtime_flags);
3679
Filipe Mananabde6c242015-07-24 00:00:19 +01003680 /*
3681 * We don't persist the id of the transaction where an unlink operation
3682 * against the inode was last made. So here we assume the inode might
3683 * have been evicted, and therefore the exact value of last_unlink_trans
3684 * lost, and set it to last_trans to avoid metadata inconsistencies
3685 * between the inode and its parent if the inode is fsync'ed and the log
3686 * replayed. For example, in the scenario:
3687 *
3688 * touch mydir/foo
3689 * ln mydir/foo mydir/bar
3690 * sync
3691 * unlink mydir/bar
3692 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3693 * xfs_io -c fsync mydir/foo
3694 * <power failure>
3695 * mount fs, triggers fsync log replay
3696 *
3697 * We must make sure that when we fsync our inode foo we also log its
3698 * parent inode, otherwise after log replay the parent still has the
3699 * dentry with the "bar" name but our inode foo has a link count of 1
3700 * and doesn't have an inode ref with the name "bar" anymore.
3701 *
3702 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3703 * but it guarantees correctness at the expense of ocassional full
3704 * transaction commits on fsync if our inode is a directory, or if our
3705 * inode is not a directory, logging its parent unnecessarily.
3706 */
3707 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3708
Miao Xie67de1172013-12-26 13:07:06 +08003709 path->slots[0]++;
3710 if (inode->i_nlink != 1 ||
3711 path->slots[0] >= btrfs_header_nritems(leaf))
3712 goto cache_acl;
3713
3714 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3715 if (location.objectid != btrfs_ino(inode))
3716 goto cache_acl;
3717
3718 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3719 if (location.type == BTRFS_INODE_REF_KEY) {
3720 struct btrfs_inode_ref *ref;
3721
3722 ref = (struct btrfs_inode_ref *)ptr;
3723 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3724 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3725 struct btrfs_inode_extref *extref;
3726
3727 extref = (struct btrfs_inode_extref *)ptr;
3728 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3729 extref);
3730 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003731cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003732 /*
3733 * try to precache a NULL acl entry for files that don't have
3734 * any xattrs or acls
3735 */
Li Zefan33345d012011-04-20 10:31:50 +08003736 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003737 btrfs_ino(inode), &first_xattr_slot);
3738 if (first_xattr_slot != -1) {
3739 path->slots[0] = first_xattr_slot;
3740 ret = btrfs_load_inode_props(inode, path);
3741 if (ret)
3742 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003743 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003744 btrfs_ino(inode),
3745 root->root_key.objectid, ret);
3746 }
3747 btrfs_free_path(path);
3748
Al Viro72c04902009-06-24 16:58:48 -04003749 if (!maybe_acls)
3750 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003751
Chris Mason39279cc2007-06-12 06:35:45 -04003752 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003753 case S_IFREG:
3754 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003755 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003756 inode->i_fop = &btrfs_file_operations;
3757 inode->i_op = &btrfs_file_inode_operations;
3758 break;
3759 case S_IFDIR:
3760 inode->i_fop = &btrfs_dir_file_operations;
3761 if (root == root->fs_info->tree_root)
3762 inode->i_op = &btrfs_dir_ro_inode_operations;
3763 else
3764 inode->i_op = &btrfs_dir_inode_operations;
3765 break;
3766 case S_IFLNK:
3767 inode->i_op = &btrfs_symlink_inode_operations;
3768 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3769 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003770 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003771 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003772 init_special_inode(inode, inode->i_mode, rdev);
3773 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003774 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003775
3776 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003777 return;
3778
3779make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003780 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003781 make_bad_inode(inode);
3782}
3783
Chris Masond352ac62008-09-29 15:18:18 -04003784/*
3785 * given a leaf and an inode, copy the inode fields into the leaf
3786 */
Chris Masone02119d2008-09-05 16:13:11 -04003787static void fill_inode_item(struct btrfs_trans_handle *trans,
3788 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003789 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003790 struct inode *inode)
3791{
Liu Bo51fab692012-12-27 09:01:21 +00003792 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003793
Liu Bo51fab692012-12-27 09:01:21 +00003794 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003795
Liu Bo51fab692012-12-27 09:01:21 +00003796 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3797 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3798 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3799 &token);
3800 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3801 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003802
David Sterbaa937b972014-12-12 17:39:12 +01003803 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003804 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003805 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003806 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003807
David Sterbaa937b972014-12-12 17:39:12 +01003808 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003809 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003810 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003811 inode->i_mtime.tv_nsec, &token);
3812
David Sterbaa937b972014-12-12 17:39:12 +01003813 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003814 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003815 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003816 inode->i_ctime.tv_nsec, &token);
3817
chandan r9cc97d62012-07-04 12:48:07 +05303818 btrfs_set_token_timespec_sec(leaf, &item->otime,
3819 BTRFS_I(inode)->i_otime.tv_sec, &token);
3820 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3821 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3822
Liu Bo51fab692012-12-27 09:01:21 +00003823 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3824 &token);
3825 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3826 &token);
3827 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3828 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3829 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3830 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3831 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003832}
3833
Chris Masond352ac62008-09-29 15:18:18 -04003834/*
3835 * copy everything in the in-memory inode into the btree.
3836 */
Chris Mason21151332011-11-10 20:39:08 -05003837static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003838 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003839{
3840 struct btrfs_inode_item *inode_item;
3841 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003842 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003843 int ret;
3844
3845 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003846 if (!path)
3847 return -ENOMEM;
3848
Chris Masonb9473432009-03-13 11:00:37 -04003849 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003850 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3851 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003852 if (ret) {
3853 if (ret > 0)
3854 ret = -ENOENT;
3855 goto failed;
3856 }
3857
Chris Mason5f39d392007-10-15 16:14:19 -04003858 leaf = path->nodes[0];
3859 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003860 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003861
Chris Masone02119d2008-09-05 16:13:11 -04003862 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003863 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003864 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003865 ret = 0;
3866failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003867 btrfs_free_path(path);
3868 return ret;
3869}
3870
Chris Masond352ac62008-09-29 15:18:18 -04003871/*
Chris Mason21151332011-11-10 20:39:08 -05003872 * copy everything in the in-memory inode into the btree.
3873 */
3874noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3875 struct btrfs_root *root, struct inode *inode)
3876{
3877 int ret;
3878
3879 /*
3880 * If the inode is a free space inode, we can deadlock during commit
3881 * if we put it into the delayed code.
3882 *
3883 * The data relocation inode should also be directly updated
3884 * without delay
3885 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003886 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003887 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3888 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003889 btrfs_update_root_times(trans, root);
3890
Chris Mason21151332011-11-10 20:39:08 -05003891 ret = btrfs_delayed_update_inode(trans, root, inode);
3892 if (!ret)
3893 btrfs_set_inode_last_trans(trans, inode);
3894 return ret;
3895 }
3896
3897 return btrfs_update_inode_item(trans, root, inode);
3898}
3899
Josef Bacikbe6aef62012-10-22 15:43:12 -04003900noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3901 struct btrfs_root *root,
3902 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003903{
3904 int ret;
3905
3906 ret = btrfs_update_inode(trans, root, inode);
3907 if (ret == -ENOSPC)
3908 return btrfs_update_inode_item(trans, root, inode);
3909 return ret;
3910}
3911
3912/*
Chris Masond352ac62008-09-29 15:18:18 -04003913 * unlink helper that gets used here in inode.c and in the tree logging
3914 * recovery code. It remove a link in a directory with a given name, and
3915 * also drops the back refs in the inode to the directory
3916 */
Al Viro92986792011-03-04 17:14:37 +00003917static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3918 struct btrfs_root *root,
3919 struct inode *dir, struct inode *inode,
3920 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003921{
3922 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003923 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003924 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003925 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003926 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003927 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003928 u64 ino = btrfs_ino(inode);
3929 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003930
3931 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003932 if (!path) {
3933 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003934 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003935 }
3936
Chris Masonb9473432009-03-13 11:00:37 -04003937 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003938 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003939 name, name_len, -1);
3940 if (IS_ERR(di)) {
3941 ret = PTR_ERR(di);
3942 goto err;
3943 }
3944 if (!di) {
3945 ret = -ENOENT;
3946 goto err;
3947 }
Chris Mason5f39d392007-10-15 16:14:19 -04003948 leaf = path->nodes[0];
3949 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003950 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003951 if (ret)
3952 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003953 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003954
Miao Xie67de1172013-12-26 13:07:06 +08003955 /*
3956 * If we don't have dir index, we have to get it by looking up
3957 * the inode ref, since we get the inode ref, remove it directly,
3958 * it is unnecessary to do delayed deletion.
3959 *
3960 * But if we have dir index, needn't search inode ref to get it.
3961 * Since the inode ref is close to the inode item, it is better
3962 * that we delay to delete it, and just do this deletion when
3963 * we update the inode item.
3964 */
3965 if (BTRFS_I(inode)->dir_index) {
3966 ret = btrfs_delayed_delete_inode_ref(inode);
3967 if (!ret) {
3968 index = BTRFS_I(inode)->dir_index;
3969 goto skip_backref;
3970 }
3971 }
3972
Li Zefan33345d012011-04-20 10:31:50 +08003973 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3974 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003975 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003976 btrfs_info(root->fs_info,
3977 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003978 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003979 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003980 goto err;
3981 }
Miao Xie67de1172013-12-26 13:07:06 +08003982skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003983 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003984 if (ret) {
3985 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003986 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003987 }
Chris Mason39279cc2007-06-12 06:35:45 -04003988
Chris Masone02119d2008-09-05 16:13:11 -04003989 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003990 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003991 if (ret != 0 && ret != -ENOENT) {
3992 btrfs_abort_transaction(trans, root, ret);
3993 goto err;
3994 }
Chris Masone02119d2008-09-05 16:13:11 -04003995
3996 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3997 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003998 if (ret == -ENOENT)
3999 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004000 else if (ret)
4001 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004002err:
4003 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004004 if (ret)
4005 goto out;
4006
4007 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004008 inode_inc_iversion(inode);
4009 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04004010 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06004011 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004012out:
Chris Mason39279cc2007-06-12 06:35:45 -04004013 return ret;
4014}
4015
Al Viro92986792011-03-04 17:14:37 +00004016int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4017 struct btrfs_root *root,
4018 struct inode *dir, struct inode *inode,
4019 const char *name, int name_len)
4020{
4021 int ret;
4022 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4023 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004024 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004025 ret = btrfs_update_inode(trans, root, inode);
4026 }
4027 return ret;
4028}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004029
4030/*
4031 * helper to start transaction for unlink and rmdir.
4032 *
Josef Bacikd52be812013-05-29 14:54:47 -04004033 * unlink and rmdir are special in btrfs, they do not always free space, so
4034 * if we cannot make our reservations the normal way try and see if there is
4035 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4036 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004037 */
Josef Bacikd52be812013-05-29 14:54:47 -04004038static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004039{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004040 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004041
Josef Bacike70bea52011-10-11 14:18:24 -04004042 /*
4043 * 1 for the possible orphan item
4044 * 1 for the dir item
4045 * 1 for the dir index
4046 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004047 * 1 for the inode
4048 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004049 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004050}
4051
Chris Mason39279cc2007-06-12 06:35:45 -04004052static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4053{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004054 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004055 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004056 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004057 int ret;
4058
Josef Bacikd52be812013-05-29 14:54:47 -04004059 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004060 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004061 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004062
David Howells2b0143b2015-03-17 22:25:59 +00004063 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004064
David Howells2b0143b2015-03-17 22:25:59 +00004065 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004066 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004067 if (ret)
4068 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004069
Yan, Zhenga22285a2010-05-16 10:48:46 -04004070 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004071 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004072 if (ret)
4073 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004074 }
Josef Bacik7b128762008-07-24 12:17:14 -04004075
Tsutomu Itohb5324022011-07-19 07:27:20 +00004076out:
Josef Bacikd52be812013-05-29 14:54:47 -04004077 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004078 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004079 return ret;
4080}
4081
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004082int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4083 struct btrfs_root *root,
4084 struct inode *dir, u64 objectid,
4085 const char *name, int name_len)
4086{
4087 struct btrfs_path *path;
4088 struct extent_buffer *leaf;
4089 struct btrfs_dir_item *di;
4090 struct btrfs_key key;
4091 u64 index;
4092 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004093 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004094
4095 path = btrfs_alloc_path();
4096 if (!path)
4097 return -ENOMEM;
4098
Li Zefan33345d012011-04-20 10:31:50 +08004099 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004100 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004101 if (IS_ERR_OR_NULL(di)) {
4102 if (!di)
4103 ret = -ENOENT;
4104 else
4105 ret = PTR_ERR(di);
4106 goto out;
4107 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004108
4109 leaf = path->nodes[0];
4110 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4111 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4112 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004113 if (ret) {
4114 btrfs_abort_transaction(trans, root, ret);
4115 goto out;
4116 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004117 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004118
4119 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4120 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004121 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004122 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004123 if (ret != -ENOENT) {
4124 btrfs_abort_transaction(trans, root, ret);
4125 goto out;
4126 }
Li Zefan33345d012011-04-20 10:31:50 +08004127 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004128 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004129 if (IS_ERR_OR_NULL(di)) {
4130 if (!di)
4131 ret = -ENOENT;
4132 else
4133 ret = PTR_ERR(di);
4134 btrfs_abort_transaction(trans, root, ret);
4135 goto out;
4136 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004137
4138 leaf = path->nodes[0];
4139 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004140 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004141 index = key.offset;
4142 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004143 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004144
Miao Xie16cdcec2011-04-22 18:12:22 +08004145 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004146 if (ret) {
4147 btrfs_abort_transaction(trans, root, ret);
4148 goto out;
4149 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004150
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004151 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004152 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004153 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004154 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004155 if (ret)
4156 btrfs_abort_transaction(trans, root, ret);
4157out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004158 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004159 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004160}
4161
Chris Mason39279cc2007-06-12 06:35:45 -04004162static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4163{
David Howells2b0143b2015-03-17 22:25:59 +00004164 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004165 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004166 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004167 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004168
David Sterbab3ae2442012-09-13 16:04:34 -06004169 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004170 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004171 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4172 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004173
Josef Bacikd52be812013-05-29 14:54:47 -04004174 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004175 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004176 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004177
Li Zefan33345d012011-04-20 10:31:50 +08004178 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004179 err = btrfs_unlink_subvol(trans, root, dir,
4180 BTRFS_I(inode)->location.objectid,
4181 dentry->d_name.name,
4182 dentry->d_name.len);
4183 goto out;
4184 }
4185
Josef Bacik7b128762008-07-24 12:17:14 -04004186 err = btrfs_orphan_add(trans, inode);
4187 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004188 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004189
Chris Mason39279cc2007-06-12 06:35:45 -04004190 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004191 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004192 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004193 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004194 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004195out:
Josef Bacikd52be812013-05-29 14:54:47 -04004196 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004197 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004198
Chris Mason39279cc2007-06-12 06:35:45 -04004199 return err;
4200}
4201
Chris Mason28f75a02015-02-04 06:59:29 -08004202static int truncate_space_check(struct btrfs_trans_handle *trans,
4203 struct btrfs_root *root,
4204 u64 bytes_deleted)
4205{
4206 int ret;
4207
4208 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
4209 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4210 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4211 if (!ret)
4212 trans->bytes_reserved += bytes_deleted;
4213 return ret;
4214
4215}
4216
Filipe Manana0305cd52015-10-16 12:34:25 +01004217static int truncate_inline_extent(struct inode *inode,
4218 struct btrfs_path *path,
4219 struct btrfs_key *found_key,
4220 const u64 item_end,
4221 const u64 new_size)
4222{
4223 struct extent_buffer *leaf = path->nodes[0];
4224 int slot = path->slots[0];
4225 struct btrfs_file_extent_item *fi;
4226 u32 size = (u32)(new_size - found_key->offset);
4227 struct btrfs_root *root = BTRFS_I(inode)->root;
4228
4229 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4230
4231 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4232 loff_t offset = new_size;
4233 loff_t page_end = ALIGN(offset, PAGE_CACHE_SIZE);
4234
4235 /*
4236 * Zero out the remaining of the last page of our inline extent,
4237 * instead of directly truncating our inline extent here - that
4238 * would be much more complex (decompressing all the data, then
4239 * compressing the truncated data, which might be bigger than
4240 * the size of the inline extent, resize the extent, etc).
4241 * We release the path because to get the page we might need to
4242 * read the extent item from disk (data not in the page cache).
4243 */
4244 btrfs_release_path(path);
4245 return btrfs_truncate_page(inode, offset, page_end - offset, 0);
4246 }
4247
4248 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4249 size = btrfs_file_extent_calc_inline_size(size);
4250 btrfs_truncate_item(root, path, size, 1);
4251
4252 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4253 inode_sub_bytes(inode, item_end + 1 - new_size);
4254
4255 return 0;
4256}
4257
Chris Mason323ac952008-10-01 19:05:46 -04004258/*
Chris Mason39279cc2007-06-12 06:35:45 -04004259 * this can truncate away extent items, csum items and directory items.
4260 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004261 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004262 *
4263 * csum items that cross the new i_size are truncated to the new size
4264 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004265 *
4266 * min_type is the minimum key type to truncate down to. If set to 0, this
4267 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004268 */
Yan, Zheng80825102009-11-12 09:35:36 +00004269int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4270 struct btrfs_root *root,
4271 struct inode *inode,
4272 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004273{
Chris Mason39279cc2007-06-12 06:35:45 -04004274 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004275 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004276 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004277 struct btrfs_key key;
4278 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004279 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004280 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004281 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004282 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004283 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004284 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004285 int found_extent;
4286 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004287 int pending_del_nr = 0;
4288 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004289 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004290 int ret;
4291 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004292 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004293 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004294 bool be_nice = 0;
4295 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004296 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004297
4298 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004299
Chris Mason28ed1342014-12-17 09:41:04 -08004300 /*
4301 * for non-free space inodes and ref cows, we want to back off from
4302 * time to time
4303 */
4304 if (!btrfs_is_free_space_inode(inode) &&
4305 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4306 be_nice = 1;
4307
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004308 path = btrfs_alloc_path();
4309 if (!path)
4310 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004311 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004312
Josef Bacik5dc562c2012-08-17 13:14:17 -04004313 /*
4314 * We want to drop from the next block forward in case this new size is
4315 * not block aligned since we will be keeping the last block of the
4316 * extent just the way it is.
4317 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004318 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4319 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004320 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4321 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004322
Miao Xie16cdcec2011-04-22 18:12:22 +08004323 /*
4324 * This function is also used to drop the items in the log tree before
4325 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4326 * it is used to drop the loged items. So we shouldn't kill the delayed
4327 * items.
4328 */
4329 if (min_type == 0 && root == BTRFS_I(inode)->root)
4330 btrfs_kill_delayed_inode_items(inode);
4331
Li Zefan33345d012011-04-20 10:31:50 +08004332 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004333 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004334 key.type = (u8)-1;
4335
Chris Mason85e21ba2008-01-29 15:11:36 -05004336search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004337 /*
4338 * with a 16K leaf size and 128MB extents, you can actually queue
4339 * up a huge file in a single leaf. Most of the time that
4340 * bytes_deleted is > 0, it will be huge by the time we get here
4341 */
4342 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
4343 if (btrfs_should_end_transaction(trans, root)) {
4344 err = -EAGAIN;
4345 goto error;
4346 }
4347 }
4348
4349
Chris Masonb9473432009-03-13 11:00:37 -04004350 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004351 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004352 if (ret < 0) {
4353 err = ret;
4354 goto out;
4355 }
Chris Masond3977122009-01-05 21:25:51 -05004356
Chris Mason85e21ba2008-01-29 15:11:36 -05004357 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004358 /* there are no items in the tree for us to truncate, we're
4359 * done
4360 */
Yan, Zheng80825102009-11-12 09:35:36 +00004361 if (path->slots[0] == 0)
4362 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004363 path->slots[0]--;
4364 }
4365
Chris Masond3977122009-01-05 21:25:51 -05004366 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004367 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004368 leaf = path->nodes[0];
4369 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004370 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004371
Li Zefan33345d012011-04-20 10:31:50 +08004372 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004373 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004374
Chris Mason85e21ba2008-01-29 15:11:36 -05004375 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004376 break;
4377
Chris Mason5f39d392007-10-15 16:14:19 -04004378 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004379 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004380 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004381 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004382 extent_type = btrfs_file_extent_type(leaf, fi);
4383 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004384 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004385 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004386 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004387 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004388 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004389 }
Yan008630c2007-11-07 13:31:09 -05004390 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004391 }
Yan, Zheng80825102009-11-12 09:35:36 +00004392 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004393 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004394 } else {
4395 if (item_end < new_size)
4396 break;
4397 if (found_key.offset >= new_size)
4398 del_item = 1;
4399 else
4400 del_item = 0;
4401 }
Chris Mason39279cc2007-06-12 06:35:45 -04004402 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004403 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004404 if (found_type != BTRFS_EXTENT_DATA_KEY)
4405 goto delete;
4406
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004407 if (del_item)
4408 last_size = found_key.offset;
4409 else
4410 last_size = new_size;
4411
Chris Mason179e29e2007-11-01 11:28:41 -04004412 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004413 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004414 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004415 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004416 u64 orig_num_bytes =
4417 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004418 extent_num_bytes = ALIGN(new_size -
4419 found_key.offset,
4420 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004421 btrfs_set_file_extent_num_bytes(leaf, fi,
4422 extent_num_bytes);
4423 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004424 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004425 if (test_bit(BTRFS_ROOT_REF_COWS,
4426 &root->state) &&
4427 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004428 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004429 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004430 } else {
Chris Masondb945352007-10-15 16:15:53 -04004431 extent_num_bytes =
4432 btrfs_file_extent_disk_num_bytes(leaf,
4433 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004434 extent_offset = found_key.offset -
4435 btrfs_file_extent_offset(leaf, fi);
4436
Chris Mason39279cc2007-06-12 06:35:45 -04004437 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004438 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004439 if (extent_start != 0) {
4440 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004441 if (test_bit(BTRFS_ROOT_REF_COWS,
4442 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004443 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004444 }
4445 }
Chris Mason90692182008-02-08 13:49:28 -05004446 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004447 /*
4448 * we can't truncate inline items that have had
4449 * special encodings
4450 */
4451 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004452 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4453 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004454
4455 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004456 * Need to release path in order to truncate a
4457 * compressed extent. So delete any accumulated
4458 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004459 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004460 if (btrfs_file_extent_compression(leaf, fi) !=
4461 BTRFS_COMPRESS_NONE && pending_del_nr) {
4462 err = btrfs_del_items(trans, root, path,
4463 pending_del_slot,
4464 pending_del_nr);
4465 if (err) {
4466 btrfs_abort_transaction(trans,
4467 root,
4468 err);
4469 goto error;
4470 }
4471 pending_del_nr = 0;
4472 }
4473
4474 err = truncate_inline_extent(inode, path,
4475 &found_key,
4476 item_end,
4477 new_size);
4478 if (err) {
4479 btrfs_abort_transaction(trans,
4480 root, err);
4481 goto error;
4482 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004483 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4484 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004485 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004486 }
Chris Mason39279cc2007-06-12 06:35:45 -04004487 }
Chris Mason179e29e2007-11-01 11:28:41 -04004488delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004489 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004490 if (!pending_del_nr) {
4491 /* no pending yet, add ourselves */
4492 pending_del_slot = path->slots[0];
4493 pending_del_nr = 1;
4494 } else if (pending_del_nr &&
4495 path->slots[0] + 1 == pending_del_slot) {
4496 /* hop on the pending chunk */
4497 pending_del_nr++;
4498 pending_del_slot = path->slots[0];
4499 } else {
Chris Masond3977122009-01-05 21:25:51 -05004500 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004501 }
Chris Mason39279cc2007-06-12 06:35:45 -04004502 } else {
4503 break;
4504 }
Chris Mason28f75a02015-02-04 06:59:29 -08004505 should_throttle = 0;
4506
Miao Xie27cdeb72014-04-02 19:51:05 +08004507 if (found_extent &&
4508 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4509 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004510 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004511 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004512 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004513 extent_num_bytes, 0,
4514 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004515 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004516 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004517 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004518 btrfs_async_run_delayed_refs(root,
4519 trans->delayed_ref_updates * 2, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004520 if (be_nice) {
4521 if (truncate_space_check(trans, root,
4522 extent_num_bytes)) {
4523 should_end = 1;
4524 }
4525 if (btrfs_should_throttle_delayed_refs(trans,
4526 root)) {
4527 should_throttle = 1;
4528 }
4529 }
Chris Mason39279cc2007-06-12 06:35:45 -04004530 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004531
Yan, Zheng80825102009-11-12 09:35:36 +00004532 if (found_type == BTRFS_INODE_ITEM_KEY)
4533 break;
4534
4535 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004536 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004537 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004538 if (pending_del_nr) {
4539 ret = btrfs_del_items(trans, root, path,
4540 pending_del_slot,
4541 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004542 if (ret) {
4543 btrfs_abort_transaction(trans,
4544 root, ret);
4545 goto error;
4546 }
Yan, Zheng80825102009-11-12 09:35:36 +00004547 pending_del_nr = 0;
4548 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004549 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004550 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004551 unsigned long updates = trans->delayed_ref_updates;
4552 if (updates) {
4553 trans->delayed_ref_updates = 0;
4554 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4555 if (ret && !err)
4556 err = ret;
4557 }
4558 }
Chris Mason28f75a02015-02-04 06:59:29 -08004559 /*
4560 * if we failed to refill our space rsv, bail out
4561 * and let the transaction restart
4562 */
4563 if (should_end) {
4564 err = -EAGAIN;
4565 goto error;
4566 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004567 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004568 } else {
4569 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004570 }
Chris Mason39279cc2007-06-12 06:35:45 -04004571 }
Yan, Zheng80825102009-11-12 09:35:36 +00004572out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004573 if (pending_del_nr) {
4574 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4575 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004576 if (ret)
4577 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004578 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004579error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004580 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004581 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004582
Chris Mason39279cc2007-06-12 06:35:45 -04004583 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004584
Chris Mason28f75a02015-02-04 06:59:29 -08004585 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
Chris Mason28ed1342014-12-17 09:41:04 -08004586 unsigned long updates = trans->delayed_ref_updates;
4587 if (updates) {
4588 trans->delayed_ref_updates = 0;
4589 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4590 if (ret && !err)
4591 err = ret;
4592 }
4593 }
Yan, Zheng80825102009-11-12 09:35:36 +00004594 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004595}
4596
Chris Masona52d9a82007-08-27 16:49:44 -04004597/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004598 * btrfs_truncate_page - read, zero a chunk and write a page
4599 * @inode - inode that we're zeroing
4600 * @from - the offset to start zeroing
4601 * @len - the length to zero, 0 to zero the entire range respective to the
4602 * offset
4603 * @front - zero up to the offset instead of from the offset on
4604 *
4605 * This will find the page for the "from" offset and cow the page and zero the
4606 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004607 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004608int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4609 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004610{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004611 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004612 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004613 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4614 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004615 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004616 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004617 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004618 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4619 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4620 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004621 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004622 int ret = 0;
4623 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004624 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004625
Josef Bacik2aaa6652012-08-29 14:27:18 -04004626 if ((offset & (blocksize - 1)) == 0 &&
4627 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004628 goto out;
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004629 ret = btrfs_delalloc_reserve_space(inode,
Qu Wenruodf480632015-09-08 17:25:54 +08004630 round_down(from, PAGE_CACHE_SIZE), PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004631 if (ret)
4632 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004633
Chris Mason211c17f2008-05-15 09:13:45 -04004634again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004635 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004636 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004637 btrfs_delalloc_release_space(inode,
Qu Wenruodf480632015-09-08 17:25:54 +08004638 round_down(from, PAGE_CACHE_SIZE),
4639 PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004640 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004641 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004642 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004643
4644 page_start = page_offset(page);
4645 page_end = page_start + PAGE_CACHE_SIZE - 1;
4646
Chris Masona52d9a82007-08-27 16:49:44 -04004647 if (!PageUptodate(page)) {
4648 ret = btrfs_readpage(NULL, page);
4649 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004650 if (page->mapping != mapping) {
4651 unlock_page(page);
4652 page_cache_release(page);
4653 goto again;
4654 }
Chris Masona52d9a82007-08-27 16:49:44 -04004655 if (!PageUptodate(page)) {
4656 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004657 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004658 }
4659 }
Chris Mason211c17f2008-05-15 09:13:45 -04004660 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004661
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004662 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004663 set_page_extent_mapped(page);
4664
4665 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4666 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004667 unlock_extent_cached(io_tree, page_start, page_end,
4668 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004669 unlock_page(page);
4670 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004671 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004672 btrfs_put_ordered_extent(ordered);
4673 goto again;
4674 }
4675
Josef Bacik2ac55d42010-02-03 19:33:23 +00004676 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004677 EXTENT_DIRTY | EXTENT_DELALLOC |
4678 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004679 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004680
Josef Bacik2ac55d42010-02-03 19:33:23 +00004681 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4682 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004683 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004684 unlock_extent_cached(io_tree, page_start, page_end,
4685 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004686 goto out_unlock;
4687 }
4688
Chris Masone6dcd2d2008-07-17 12:53:50 -04004689 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004690 if (!len)
4691 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004692 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004693 if (front)
4694 memset(kaddr, 0, offset);
4695 else
4696 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004697 flush_dcache_page(page);
4698 kunmap(page);
4699 }
Chris Mason247e7432008-07-17 12:53:51 -04004700 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004701 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004702 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4703 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004704
Chris Mason89642222008-07-24 09:41:53 -04004705out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004706 if (ret)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004707 btrfs_delalloc_release_space(inode, page_start,
4708 PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004709 unlock_page(page);
4710 page_cache_release(page);
4711out:
4712 return ret;
4713}
4714
Josef Bacik16e75492013-10-22 12:18:51 -04004715static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4716 u64 offset, u64 len)
4717{
4718 struct btrfs_trans_handle *trans;
4719 int ret;
4720
4721 /*
4722 * Still need to make sure the inode looks like it's been updated so
4723 * that any holes get logged if we fsync.
4724 */
4725 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4726 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4727 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4728 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4729 return 0;
4730 }
4731
4732 /*
4733 * 1 - for the one we're dropping
4734 * 1 - for the one we're adding
4735 * 1 - for updating the inode.
4736 */
4737 trans = btrfs_start_transaction(root, 3);
4738 if (IS_ERR(trans))
4739 return PTR_ERR(trans);
4740
4741 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4742 if (ret) {
4743 btrfs_abort_transaction(trans, root, ret);
4744 btrfs_end_transaction(trans, root);
4745 return ret;
4746 }
4747
4748 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4749 0, 0, len, 0, len, 0, 0, 0);
4750 if (ret)
4751 btrfs_abort_transaction(trans, root, ret);
4752 else
4753 btrfs_update_inode(trans, root, inode);
4754 btrfs_end_transaction(trans, root);
4755 return ret;
4756}
4757
Josef Bacik695a0d02011-03-04 15:46:53 -05004758/*
4759 * This function puts in dummy file extents for the area we're creating a hole
4760 * for. So if we are truncating this file to a larger size we need to insert
4761 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4762 * the range between oldsize and size
4763 */
Josef Bacika41ad392011-01-31 15:30:16 -05004764int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004765{
Yan Zheng9036c102008-10-30 14:19:41 -04004766 struct btrfs_root *root = BTRFS_I(inode)->root;
4767 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004768 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004769 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004770 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004771 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4772 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004773 u64 last_byte;
4774 u64 cur_offset;
4775 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004776 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004777
Josef Bacika71754f2013-06-17 17:14:39 -04004778 /*
4779 * If our size started in the middle of a page we need to zero out the
4780 * rest of the page before we expand the i_size, otherwise we could
4781 * expose stale data.
4782 */
4783 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4784 if (err)
4785 return err;
4786
Yan Zheng9036c102008-10-30 14:19:41 -04004787 if (size <= hole_start)
4788 return 0;
4789
Yan Zheng9036c102008-10-30 14:19:41 -04004790 while (1) {
4791 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004792
Josef Bacik2ac55d42010-02-03 19:33:23 +00004793 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004794 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004795 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4796 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004797 if (!ordered)
4798 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004799 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4800 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004801 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004802 btrfs_put_ordered_extent(ordered);
4803 }
4804
Yan Zheng9036c102008-10-30 14:19:41 -04004805 cur_offset = hole_start;
4806 while (1) {
4807 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4808 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004809 if (IS_ERR(em)) {
4810 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004811 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004812 break;
4813 }
Yan Zheng9036c102008-10-30 14:19:41 -04004814 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004815 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004816 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004817 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004818 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004819
Josef Bacik16e75492013-10-22 12:18:51 -04004820 err = maybe_insert_hole(root, inode, cur_offset,
4821 hole_size);
4822 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004823 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004824 btrfs_drop_extent_cache(inode, cur_offset,
4825 cur_offset + hole_size - 1, 0);
4826 hole_em = alloc_extent_map();
4827 if (!hole_em) {
4828 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4829 &BTRFS_I(inode)->runtime_flags);
4830 goto next;
4831 }
4832 hole_em->start = cur_offset;
4833 hole_em->len = hole_size;
4834 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004835
Josef Bacik5dc562c2012-08-17 13:14:17 -04004836 hole_em->block_start = EXTENT_MAP_HOLE;
4837 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004838 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004839 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004840 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4841 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004842 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004843
4844 while (1) {
4845 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004846 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004847 write_unlock(&em_tree->lock);
4848 if (err != -EEXIST)
4849 break;
4850 btrfs_drop_extent_cache(inode, cur_offset,
4851 cur_offset +
4852 hole_size - 1, 0);
4853 }
4854 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004855 }
Josef Bacik16e75492013-10-22 12:18:51 -04004856next:
Yan Zheng9036c102008-10-30 14:19:41 -04004857 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004858 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004859 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004860 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004861 break;
4862 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004863 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004864 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4865 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004866 return err;
4867}
4868
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004869static int wait_snapshoting_atomic_t(atomic_t *a)
4870{
4871 schedule();
4872 return 0;
4873}
4874
4875static void wait_for_snapshot_creation(struct btrfs_root *root)
4876{
4877 while (true) {
4878 int ret;
4879
4880 ret = btrfs_start_write_no_snapshoting(root);
4881 if (ret)
4882 break;
4883 wait_on_atomic_t(&root->will_be_snapshoted,
4884 wait_snapshoting_atomic_t,
4885 TASK_UNINTERRUPTIBLE);
4886 }
4887}
4888
Eric Sandeen3972f262013-01-12 02:57:22 +00004889static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004890{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004891 struct btrfs_root *root = BTRFS_I(inode)->root;
4892 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004893 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004894 loff_t newsize = attr->ia_size;
4895 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004896 int ret;
4897
Eric Sandeen3972f262013-01-12 02:57:22 +00004898 /*
4899 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4900 * special case where we need to update the times despite not having
4901 * these flags set. For all other operations the VFS set these flags
4902 * explicitly if it wants a timestamp update.
4903 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004904 if (newsize != oldsize) {
4905 inode_inc_iversion(inode);
4906 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4907 inode->i_ctime = inode->i_mtime =
4908 current_fs_time(inode->i_sb);
4909 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004910
Josef Bacika41ad392011-01-31 15:30:16 -05004911 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004912 truncate_pagecache(inode, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004913 /*
4914 * Don't do an expanding truncate while snapshoting is ongoing.
4915 * This is to ensure the snapshot captures a fully consistent
4916 * state of this file - if the snapshot captures this expanding
4917 * truncation, it must capture all writes that happened before
4918 * this truncation.
4919 */
4920 wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004921 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004922 if (ret) {
4923 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004924 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004925 }
Yan, Zheng80825102009-11-12 09:35:36 +00004926
Miao Xief4a2f4c2011-12-14 20:12:01 -05004927 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004928 if (IS_ERR(trans)) {
4929 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004930 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004931 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004932
4933 i_size_write(inode, newsize);
4934 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4935 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004936 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004937 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004938 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004939
Josef Bacika41ad392011-01-31 15:30:16 -05004940 /*
4941 * We're truncating a file that used to have good data down to
4942 * zero. Make sure it gets into the ordered flush list so that
4943 * any new writes get down to disk quickly.
4944 */
4945 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004946 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4947 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004948
Josef Bacikf3fe8202013-01-07 17:03:21 -05004949 /*
4950 * 1 for the orphan item we're going to add
4951 * 1 for the orphan item deletion.
4952 */
4953 trans = btrfs_start_transaction(root, 2);
4954 if (IS_ERR(trans))
4955 return PTR_ERR(trans);
4956
4957 /*
4958 * We need to do this in case we fail at _any_ point during the
4959 * actual truncate. Once we do the truncate_setsize we could
4960 * invalidate pages which forces any outstanding ordered io to
4961 * be instantly completed which will give us extents that need
4962 * to be truncated. If we fail to get an orphan inode down we
4963 * could have left over extents that were never meant to live,
4964 * so we need to garuntee from this point on that everything
4965 * will be consistent.
4966 */
4967 ret = btrfs_orphan_add(trans, inode);
4968 btrfs_end_transaction(trans, root);
4969 if (ret)
4970 return ret;
4971
Josef Bacika41ad392011-01-31 15:30:16 -05004972 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4973 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004974
4975 /* Disable nonlocked read DIO to avoid the end less truncate */
4976 btrfs_inode_block_unlocked_dio(inode);
4977 inode_dio_wait(inode);
4978 btrfs_inode_resume_unlocked_dio(inode);
4979
Josef Bacika41ad392011-01-31 15:30:16 -05004980 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004981 if (ret && inode->i_nlink) {
4982 int err;
4983
4984 /*
4985 * failed to truncate, disk_i_size is only adjusted down
4986 * as we remove extents, so it should represent the true
4987 * size of the inode, so reset the in memory size and
4988 * delete our orphan entry.
4989 */
4990 trans = btrfs_join_transaction(root);
4991 if (IS_ERR(trans)) {
4992 btrfs_orphan_del(NULL, inode);
4993 return ret;
4994 }
4995 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4996 err = btrfs_orphan_del(trans, inode);
4997 if (err)
4998 btrfs_abort_transaction(trans, root, err);
4999 btrfs_end_transaction(trans, root);
5000 }
Yan, Zheng80825102009-11-12 09:35:36 +00005001 }
5002
Josef Bacika41ad392011-01-31 15:30:16 -05005003 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005004}
5005
Chris Mason39279cc2007-06-12 06:35:45 -04005006static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5007{
David Howells2b0143b2015-03-17 22:25:59 +00005008 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005009 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005010 int err;
5011
Li Zefanb83cc962010-12-20 16:04:08 +08005012 if (btrfs_root_readonly(root))
5013 return -EROFS;
5014
Chris Mason39279cc2007-06-12 06:35:45 -04005015 err = inode_change_ok(inode, attr);
5016 if (err)
5017 return err;
5018
Chris Mason5a3f23d2009-03-31 13:27:11 -04005019 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005020 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005021 if (err)
5022 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005023 }
Yan Zheng9036c102008-10-30 14:19:41 -04005024
Christoph Hellwig10257742010-06-04 11:30:02 +02005025 if (attr->ia_valid) {
5026 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005027 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005028 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005029
Josef Bacik22c44fe2011-11-30 10:45:38 -05005030 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005031 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005032 }
5033
Chris Mason39279cc2007-06-12 06:35:45 -04005034 return err;
5035}
Chris Mason61295eb2008-01-14 16:24:38 -05005036
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005037/*
5038 * While truncating the inode pages during eviction, we get the VFS calling
5039 * btrfs_invalidatepage() against each page of the inode. This is slow because
5040 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5041 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5042 * extent_state structures over and over, wasting lots of time.
5043 *
5044 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5045 * those expensive operations on a per page basis and do only the ordered io
5046 * finishing, while we release here the extent_map and extent_state structures,
5047 * without the excessive merging and splitting.
5048 */
5049static void evict_inode_truncate_pages(struct inode *inode)
5050{
5051 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5052 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5053 struct rb_node *node;
5054
5055 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005056 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005057
5058 write_lock(&map_tree->lock);
5059 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5060 struct extent_map *em;
5061
5062 node = rb_first(&map_tree->map);
5063 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005064 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5065 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005066 remove_extent_mapping(map_tree, em);
5067 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005068 if (need_resched()) {
5069 write_unlock(&map_tree->lock);
5070 cond_resched();
5071 write_lock(&map_tree->lock);
5072 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005073 }
5074 write_unlock(&map_tree->lock);
5075
Filipe Manana6ca07092015-05-26 00:55:42 +01005076 /*
5077 * Keep looping until we have no more ranges in the io tree.
5078 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005079 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5080 * still in progress (unlocked the pages in the bio but did not yet
5081 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005082 * ranges can still be locked and eviction started because before
5083 * submitting those bios, which are executed by a separate task (work
5084 * queue kthread), inode references (inode->i_count) were not taken
5085 * (which would be dropped in the end io callback of each bio).
5086 * Therefore here we effectively end up waiting for those bios and
5087 * anyone else holding locked ranges without having bumped the inode's
5088 * reference count - if we don't do it, when they access the inode's
5089 * io_tree to unlock a range it may be too late, leading to an
5090 * use-after-free issue.
5091 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005092 spin_lock(&io_tree->lock);
5093 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5094 struct extent_state *state;
5095 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005096 u64 start;
5097 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005098
5099 node = rb_first(&io_tree->state);
5100 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005101 start = state->start;
5102 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005103 spin_unlock(&io_tree->lock);
5104
Filipe Manana6ca07092015-05-26 00:55:42 +01005105 lock_extent_bits(io_tree, start, end, 0, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005106
5107 /*
5108 * If still has DELALLOC flag, the extent didn't reach disk,
5109 * and its reserved space won't be freed by delayed_ref.
5110 * So we need to free its reserved space here.
5111 * (Refer to comment in btrfs_invalidatepage, case 2)
5112 *
5113 * Note, end is the bytenr of last byte, so we need + 1 here.
5114 */
5115 if (state->state & EXTENT_DELALLOC)
5116 btrfs_qgroup_free_data(inode, start, end - start + 1);
5117
Filipe Manana6ca07092015-05-26 00:55:42 +01005118 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005119 EXTENT_LOCKED | EXTENT_DIRTY |
5120 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5121 EXTENT_DEFRAG, 1, 1,
5122 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005123
Filipe Manana7064dd52014-08-08 02:47:05 +01005124 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005125 spin_lock(&io_tree->lock);
5126 }
5127 spin_unlock(&io_tree->lock);
5128}
5129
Al Virobd555972010-06-07 11:35:40 -04005130void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005131{
5132 struct btrfs_trans_handle *trans;
5133 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005134 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005135 int steal_from_global = 0;
Josef Bacik07127182011-08-19 10:29:59 -04005136 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04005137 int ret;
5138
liubo1abe9b82011-03-24 11:18:59 +00005139 trace_btrfs_inode_evict(inode);
5140
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005141 evict_inode_truncate_pages(inode);
5142
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005143 if (inode->i_nlink &&
5144 ((btrfs_root_refs(&root->root_item) != 0 &&
5145 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5146 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005147 goto no_delete;
5148
Chris Mason39279cc2007-06-12 06:35:45 -04005149 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005150 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005151 goto no_delete;
5152 }
Al Virobd555972010-06-07 11:35:40 -04005153 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005154 if (!special_file(inode->i_mode))
5155 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005156
Miao Xief6124962014-09-12 18:44:04 +08005157 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5158
Yan, Zhengc71bf092009-11-12 09:34:40 +00005159 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06005160 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005161 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005162 goto no_delete;
5163 }
5164
Yan, Zheng76dda932009-09-21 16:00:26 -04005165 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005166 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5167 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005168 goto no_delete;
5169 }
5170
Miao Xie0e8c36a2012-12-19 06:59:51 +00005171 ret = btrfs_commit_inode_delayed_inode(inode);
5172 if (ret) {
5173 btrfs_orphan_del(NULL, inode);
5174 goto no_delete;
5175 }
5176
Miao Xie66d8f3d2012-09-06 04:02:28 -06005177 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005178 if (!rsv) {
5179 btrfs_orphan_del(NULL, inode);
5180 goto no_delete;
5181 }
Josef Bacik4a338542011-08-29 11:01:31 -04005182 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005183 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005184 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005185
Chris Masondbe674a2008-07-17 12:54:05 -04005186 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005187
Josef Bacik4289a662011-08-05 13:22:24 -04005188 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005189 * This is a bit simpler than btrfs_truncate since we've already
5190 * reserved our space for our orphan item in the unlink, so we just
5191 * need to reserve some slack space in case we add bytes and update
5192 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005193 */
Yan, Zheng80825102009-11-12 09:35:36 +00005194 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005195 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5196 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005197
Josef Bacik726c35f2011-09-26 15:46:06 -04005198 /*
5199 * Try and steal from the global reserve since we will
5200 * likely not use this space anyway, we want to try as
5201 * hard as possible to get this to work.
5202 */
5203 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005204 steal_from_global++;
5205 else
5206 steal_from_global = 0;
5207 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005208
Josef Bacik3bce8762015-02-24 12:35:51 -08005209 /*
5210 * steal_from_global == 0: we reserved stuff, hooray!
5211 * steal_from_global == 1: we didn't reserve stuff, boo!
5212 * steal_from_global == 2: we've committed, still not a lot of
5213 * room but maybe we'll have room in the global reserve this
5214 * time.
5215 * steal_from_global == 3: abandon all hope!
5216 */
5217 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005218 btrfs_warn(root->fs_info,
5219 "Could not get space for a delete, will truncate on mount %d",
5220 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005221 btrfs_orphan_del(NULL, inode);
5222 btrfs_free_block_rsv(root, rsv);
5223 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005224 }
5225
Miao Xie0e8c36a2012-12-19 06:59:51 +00005226 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005227 if (IS_ERR(trans)) {
5228 btrfs_orphan_del(NULL, inode);
5229 btrfs_free_block_rsv(root, rsv);
5230 goto no_delete;
5231 }
5232
Josef Bacik3bce8762015-02-24 12:35:51 -08005233 /*
5234 * We can't just steal from the global reserve, we need tomake
5235 * sure there is room to do it, if not we need to commit and try
5236 * again.
5237 */
5238 if (steal_from_global) {
5239 if (!btrfs_check_space_for_delayed_refs(trans, root))
5240 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5241 min_size);
5242 else
5243 ret = -ENOSPC;
5244 }
5245
5246 /*
5247 * Couldn't steal from the global reserve, we have too much
5248 * pending stuff built up, commit the transaction and try it
5249 * again.
5250 */
5251 if (ret) {
5252 ret = btrfs_commit_transaction(trans, root);
5253 if (ret) {
5254 btrfs_orphan_del(NULL, inode);
5255 btrfs_free_block_rsv(root, rsv);
5256 goto no_delete;
5257 }
5258 continue;
5259 } else {
5260 steal_from_global = 0;
5261 }
5262
Josef Bacik4289a662011-08-05 13:22:24 -04005263 trans->block_rsv = rsv;
5264
Yan, Zhengd68fc572010-05-16 10:49:58 -04005265 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005266 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005267 break;
5268
Miao Xie8407aa42012-09-07 01:43:32 -06005269 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005270 btrfs_end_transaction(trans, root);
5271 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005272 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005273 }
5274
Josef Bacik4289a662011-08-05 13:22:24 -04005275 btrfs_free_block_rsv(root, rsv);
5276
Josef Bacik4ef31a42013-08-13 14:10:08 -04005277 /*
5278 * Errors here aren't a big deal, it just means we leave orphan items
5279 * in the tree. They will be cleaned up on the next mount.
5280 */
Yan, Zheng80825102009-11-12 09:35:36 +00005281 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005282 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005283 btrfs_orphan_del(trans, inode);
5284 } else {
5285 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005286 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005287
Josef Bacik4289a662011-08-05 13:22:24 -04005288 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005289 if (!(root == root->fs_info->tree_root ||
5290 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005291 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005292
Chris Mason54aa1f42007-06-22 14:16:25 -04005293 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005294 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005295no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005296 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005297 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005298 return;
Chris Mason39279cc2007-06-12 06:35:45 -04005299}
5300
5301/*
5302 * this returns the key found in the dir entry in the location pointer.
5303 * If no dir entries were found, location->objectid is 0.
5304 */
5305static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5306 struct btrfs_key *location)
5307{
5308 const char *name = dentry->d_name.name;
5309 int namelen = dentry->d_name.len;
5310 struct btrfs_dir_item *di;
5311 struct btrfs_path *path;
5312 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005313 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005314
5315 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005316 if (!path)
5317 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005318
Li Zefan33345d012011-04-20 10:31:50 +08005319 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005320 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005321 if (IS_ERR(di))
5322 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005323
David Sterbac7040052011-04-19 18:00:01 +02005324 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005325 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005326
Chris Mason5f39d392007-10-15 16:14:19 -04005327 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005328out:
Chris Mason39279cc2007-06-12 06:35:45 -04005329 btrfs_free_path(path);
5330 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005331out_err:
5332 location->objectid = 0;
5333 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005334}
5335
5336/*
5337 * when we hit a tree root in a directory, the btrfs part of the inode
5338 * needs to be changed to reflect the root directory of the tree root. This
5339 * is kind of like crossing a mount point.
5340 */
5341static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005342 struct inode *dir,
5343 struct dentry *dentry,
5344 struct btrfs_key *location,
5345 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005346{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005347 struct btrfs_path *path;
5348 struct btrfs_root *new_root;
5349 struct btrfs_root_ref *ref;
5350 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005351 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005352 int ret;
5353 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005354
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005355 path = btrfs_alloc_path();
5356 if (!path) {
5357 err = -ENOMEM;
5358 goto out;
5359 }
Chris Mason39279cc2007-06-12 06:35:45 -04005360
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005361 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005362 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5363 key.type = BTRFS_ROOT_REF_KEY;
5364 key.offset = location->objectid;
5365
5366 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5367 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005368 if (ret) {
5369 if (ret < 0)
5370 err = ret;
5371 goto out;
5372 }
Chris Mason39279cc2007-06-12 06:35:45 -04005373
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005374 leaf = path->nodes[0];
5375 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005376 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005377 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5378 goto out;
5379
5380 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5381 (unsigned long)(ref + 1),
5382 dentry->d_name.len);
5383 if (ret)
5384 goto out;
5385
David Sterbab3b4aa72011-04-21 01:20:15 +02005386 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005387
5388 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5389 if (IS_ERR(new_root)) {
5390 err = PTR_ERR(new_root);
5391 goto out;
5392 }
5393
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005394 *sub_root = new_root;
5395 location->objectid = btrfs_root_dirid(&new_root->root_item);
5396 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005397 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005398 err = 0;
5399out:
5400 btrfs_free_path(path);
5401 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005402}
5403
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005404static void inode_tree_add(struct inode *inode)
5405{
5406 struct btrfs_root *root = BTRFS_I(inode)->root;
5407 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005408 struct rb_node **p;
5409 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005410 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005411 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005412
Al Viro1d3382cb2010-10-23 15:19:20 -04005413 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005414 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005415 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005416 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005417 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005418 while (*p) {
5419 parent = *p;
5420 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5421
Li Zefan33345d012011-04-20 10:31:50 +08005422 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005423 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005424 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005425 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005426 else {
5427 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005428 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005429 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005430 RB_CLEAR_NODE(parent);
5431 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005432 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005433 }
5434 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005435 rb_link_node(new, parent, p);
5436 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005437 spin_unlock(&root->inode_lock);
5438}
5439
5440static void inode_tree_del(struct inode *inode)
5441{
5442 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005443 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005444
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005445 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005446 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005447 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005448 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005449 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005450 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005451 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005452
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005453 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005454 synchronize_srcu(&root->fs_info->subvol_srcu);
5455 spin_lock(&root->inode_lock);
5456 empty = RB_EMPTY_ROOT(&root->inode_tree);
5457 spin_unlock(&root->inode_lock);
5458 if (empty)
5459 btrfs_add_dead_root(root);
5460 }
5461}
5462
Jeff Mahoney143bede2012-03-01 14:56:26 +01005463void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005464{
5465 struct rb_node *node;
5466 struct rb_node *prev;
5467 struct btrfs_inode *entry;
5468 struct inode *inode;
5469 u64 objectid = 0;
5470
Liu Bo7813b3d2014-02-10 17:37:25 +08005471 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5472 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005473
5474 spin_lock(&root->inode_lock);
5475again:
5476 node = root->inode_tree.rb_node;
5477 prev = NULL;
5478 while (node) {
5479 prev = node;
5480 entry = rb_entry(node, struct btrfs_inode, rb_node);
5481
Li Zefan33345d012011-04-20 10:31:50 +08005482 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005483 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005484 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005485 node = node->rb_right;
5486 else
5487 break;
5488 }
5489 if (!node) {
5490 while (prev) {
5491 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005492 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005493 node = prev;
5494 break;
5495 }
5496 prev = rb_next(prev);
5497 }
5498 }
5499 while (node) {
5500 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005501 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005502 inode = igrab(&entry->vfs_inode);
5503 if (inode) {
5504 spin_unlock(&root->inode_lock);
5505 if (atomic_read(&inode->i_count) > 1)
5506 d_prune_aliases(inode);
5507 /*
Al Viro45321ac2010-06-07 13:43:19 -04005508 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005509 * the inode cache when its usage count
5510 * hits zero.
5511 */
5512 iput(inode);
5513 cond_resched();
5514 spin_lock(&root->inode_lock);
5515 goto again;
5516 }
5517
5518 if (cond_resched_lock(&root->inode_lock))
5519 goto again;
5520
5521 node = rb_next(node);
5522 }
5523 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005524}
5525
Chris Masone02119d2008-09-05 16:13:11 -04005526static int btrfs_init_locked_inode(struct inode *inode, void *p)
5527{
5528 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005529 inode->i_ino = args->location->objectid;
5530 memcpy(&BTRFS_I(inode)->location, args->location,
5531 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005532 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005533 return 0;
5534}
5535
5536static int btrfs_find_actor(struct inode *inode, void *opaque)
5537{
5538 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005539 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005540 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005541}
5542
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005543static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005544 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005545 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005546{
5547 struct inode *inode;
5548 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005549 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005550
Chris Mason90d3e592014-01-09 17:28:00 -08005551 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005552 args.root = root;
5553
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005554 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005555 btrfs_init_locked_inode,
5556 (void *)&args);
5557 return inode;
5558}
5559
Balaji Rao1a54ef82008-07-21 02:01:04 +05305560/* Get an inode object given its location and corresponding root.
5561 * Returns in *is_new if the inode was read from disk
5562 */
5563struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005564 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305565{
5566 struct inode *inode;
5567
Chris Mason90d3e592014-01-09 17:28:00 -08005568 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305569 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005570 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305571
5572 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305573 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005574 if (!is_bad_inode(inode)) {
5575 inode_tree_add(inode);
5576 unlock_new_inode(inode);
5577 if (new)
5578 *new = 1;
5579 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005580 unlock_new_inode(inode);
5581 iput(inode);
5582 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005583 }
5584 }
5585
Balaji Rao1a54ef82008-07-21 02:01:04 +05305586 return inode;
5587}
5588
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005589static struct inode *new_simple_dir(struct super_block *s,
5590 struct btrfs_key *key,
5591 struct btrfs_root *root)
5592{
5593 struct inode *inode = new_inode(s);
5594
5595 if (!inode)
5596 return ERR_PTR(-ENOMEM);
5597
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005598 BTRFS_I(inode)->root = root;
5599 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005600 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005601
5602 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005603 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005604 inode->i_fop = &simple_dir_operations;
5605 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
chandan r9cc97d62012-07-04 12:48:07 +05305606 inode->i_mtime = CURRENT_TIME;
5607 inode->i_atime = inode->i_mtime;
5608 inode->i_ctime = inode->i_mtime;
5609 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005610
5611 return inode;
5612}
5613
Chris Mason3de45862008-11-17 21:02:50 -05005614struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005615{
Chris Masond3977122009-01-05 21:25:51 -05005616 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005617 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005618 struct btrfs_root *sub_root = root;
5619 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005620 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005621 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005622
5623 if (dentry->d_name.len > BTRFS_NAME_LEN)
5624 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005625
Jeff Layton39e3c952012-11-28 11:30:53 -05005626 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005627 if (ret < 0)
5628 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005629
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005630 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005631 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005632
5633 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005634 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005635 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005636 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005637
5638 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5639
Yan, Zheng76dda932009-09-21 16:00:26 -04005640 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005641 ret = fixup_tree_root_location(root, dir, dentry,
5642 &location, &sub_root);
5643 if (ret < 0) {
5644 if (ret != -ENOENT)
5645 inode = ERR_PTR(ret);
5646 else
5647 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5648 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005649 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005650 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005651 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5652
Julia Lawall34d19ba2011-01-24 19:55:19 +00005653 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005654 down_read(&root->fs_info->cleanup_work_sem);
5655 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005656 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005657 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005658 if (ret) {
5659 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005660 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005661 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005662 }
5663
Chris Mason3de45862008-11-17 21:02:50 -05005664 return inode;
5665}
5666
Nick Pigginfe15ce42011-01-07 17:49:23 +11005667static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005668{
5669 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005670 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005671
Li Zefan848cce02012-02-21 17:04:28 +08005672 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005673 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005674
Li Zefan848cce02012-02-21 17:04:28 +08005675 if (inode) {
5676 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005677 if (btrfs_root_refs(&root->root_item) == 0)
5678 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005679
5680 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5681 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005682 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005683 return 0;
5684}
5685
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005686static void btrfs_dentry_release(struct dentry *dentry)
5687{
Daeseok Youn944a4512014-04-14 15:37:02 +09005688 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005689}
5690
Chris Mason3de45862008-11-17 21:02:50 -05005691static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005692 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005693{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005694 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005695
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005696 inode = btrfs_lookup_dentry(dir, dentry);
5697 if (IS_ERR(inode)) {
5698 if (PTR_ERR(inode) == -ENOENT)
5699 inode = NULL;
5700 else
5701 return ERR_CAST(inode);
5702 }
5703
Al Viro41d28bc2014-10-12 22:24:21 -04005704 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005705}
5706
Miao Xie16cdcec2011-04-22 18:12:22 +08005707unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005708 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5709};
5710
Al Viro9cdda8d2013-05-22 16:48:09 -04005711static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005712{
Al Viro9cdda8d2013-05-22 16:48:09 -04005713 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005714 struct btrfs_root *root = BTRFS_I(inode)->root;
5715 struct btrfs_item *item;
5716 struct btrfs_dir_item *di;
5717 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005718 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005719 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005720 struct list_head ins_list;
5721 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005722 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005723 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005724 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005725 unsigned char d_type;
5726 int over = 0;
5727 u32 di_cur;
5728 u32 di_total;
5729 u32 di_len;
5730 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005731 char tmp_name[32];
5732 char *name_ptr;
5733 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005734 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005735
5736 /* FIXME, use a real flag for deciding about the key type */
5737 if (root->fs_info->tree_root == root)
5738 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005739
Al Viro9cdda8d2013-05-22 16:48:09 -04005740 if (!dir_emit_dots(file, ctx))
5741 return 0;
5742
David Woodhouse49593bf2008-08-17 17:08:36 +01005743 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005744 if (!path)
5745 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005746
David Sterbae4058b52015-11-27 16:31:35 +01005747 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005748
Miao Xie16cdcec2011-04-22 18:12:22 +08005749 if (key_type == BTRFS_DIR_INDEX_KEY) {
5750 INIT_LIST_HEAD(&ins_list);
5751 INIT_LIST_HEAD(&del_list);
5752 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5753 }
5754
David Sterba962a2982014-06-04 18:41:45 +02005755 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005756 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005757 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005758
Chris Mason39279cc2007-06-12 06:35:45 -04005759 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5760 if (ret < 0)
5761 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005762
5763 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005764 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005765 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005766 if (slot >= btrfs_header_nritems(leaf)) {
5767 ret = btrfs_next_leaf(root, path);
5768 if (ret < 0)
5769 goto err;
5770 else if (ret > 0)
5771 break;
5772 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005773 }
Chris Mason3de45862008-11-17 21:02:50 -05005774
Ross Kirkdd3cc162013-09-16 15:58:09 +01005775 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005776 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5777
5778 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005779 break;
David Sterba962a2982014-06-04 18:41:45 +02005780 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005781 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005782 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005783 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005784 if (key_type == BTRFS_DIR_INDEX_KEY &&
5785 btrfs_should_delete_dir_index(&del_list,
5786 found_key.offset))
5787 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005788
Al Viro9cdda8d2013-05-22 16:48:09 -04005789 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005790 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005791
Chris Mason39279cc2007-06-12 06:35:45 -04005792 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5793 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005794 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005795
5796 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005797 struct btrfs_key location;
5798
Josef Bacik22a94d42011-03-16 16:47:17 -04005799 if (verify_dir_item(root, leaf, di))
5800 break;
5801
Chris Mason5f39d392007-10-15 16:14:19 -04005802 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005803 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005804 name_ptr = tmp_name;
5805 } else {
5806 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005807 if (!name_ptr) {
5808 ret = -ENOMEM;
5809 goto err;
5810 }
Chris Mason5f39d392007-10-15 16:14:19 -04005811 }
5812 read_extent_buffer(leaf, name_ptr,
5813 (unsigned long)(di + 1), name_len);
5814
5815 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5816 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005817
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005818
Chris Mason3de45862008-11-17 21:02:50 -05005819 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005820 * skip it.
5821 *
5822 * In contrast to old kernels, we insert the snapshot's
5823 * dir item and dir index after it has been created, so
5824 * we won't find a reference to our own snapshot. We
5825 * still keep the following code for backward
5826 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005827 */
5828 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5829 location.objectid == root->root_key.objectid) {
5830 over = 0;
5831 goto skip;
5832 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005833 over = !dir_emit(ctx, name_ptr, name_len,
5834 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005835
Chris Mason3de45862008-11-17 21:02:50 -05005836skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005837 if (name_ptr != tmp_name)
5838 kfree(name_ptr);
5839
Chris Mason39279cc2007-06-12 06:35:45 -04005840 if (over)
5841 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005842 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005843 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005844 di_cur += di_len;
5845 di = (struct btrfs_dir_item *)((char *)di + di_len);
5846 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005847next:
5848 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005849 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005850
Miao Xie16cdcec2011-04-22 18:12:22 +08005851 if (key_type == BTRFS_DIR_INDEX_KEY) {
5852 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005853 ctx->pos++;
5854 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005855 if (ret)
5856 goto nopos;
5857 }
5858
David Woodhouse49593bf2008-08-17 17:08:36 +01005859 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005860 ctx->pos++;
5861
5862 /*
5863 * Stop new entries from being returned after we return the last
5864 * entry.
5865 *
5866 * New directory entries are assigned a strictly increasing
5867 * offset. This means that new entries created during readdir
5868 * are *guaranteed* to be seen in the future by that readdir.
5869 * This has broken buggy programs which operate on names as
5870 * they're returned by readdir. Until we re-use freed offsets
5871 * we have this hack to stop new entries from being returned
5872 * under the assumption that they'll never reach this huge
5873 * offset.
5874 *
5875 * This is being careful not to overflow 32bit loff_t unless the
5876 * last entry requires it because doing so has broken 32bit apps
5877 * in the past.
5878 */
5879 if (key_type == BTRFS_DIR_INDEX_KEY) {
5880 if (ctx->pos >= INT_MAX)
5881 ctx->pos = LLONG_MAX;
5882 else
5883 ctx->pos = INT_MAX;
5884 }
Chris Mason39279cc2007-06-12 06:35:45 -04005885nopos:
5886 ret = 0;
5887err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005888 if (key_type == BTRFS_DIR_INDEX_KEY)
5889 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005890 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005891 return ret;
5892}
5893
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005894int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005895{
5896 struct btrfs_root *root = BTRFS_I(inode)->root;
5897 struct btrfs_trans_handle *trans;
5898 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005899 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005900
Josef Bacik72ac3c02012-05-23 14:13:11 -04005901 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005902 return 0;
5903
Liu Bo83eea1f2012-07-10 05:28:39 -06005904 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005905 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005906
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005907 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005908 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005909 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005910 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005911 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005912 if (IS_ERR(trans))
5913 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005914 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005915 }
5916 return ret;
5917}
5918
5919/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005920 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005921 * inode changes. But, it is most likely to find the inode in cache.
5922 * FIXME, needs more benchmarking...there are no reasons other than performance
5923 * to keep or drop this code.
5924 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005925static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005926{
5927 struct btrfs_root *root = BTRFS_I(inode)->root;
5928 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005929 int ret;
5930
Josef Bacik72ac3c02012-05-23 14:13:11 -04005931 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005932 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005933
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005934 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005935 if (IS_ERR(trans))
5936 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005937
5938 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005939 if (ret && ret == -ENOSPC) {
5940 /* whoops, lets try again with the full transaction */
5941 btrfs_end_transaction(trans, root);
5942 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005943 if (IS_ERR(trans))
5944 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005945
Chris Mason94b60442010-05-26 11:02:00 -04005946 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005947 }
Chris Mason39279cc2007-06-12 06:35:45 -04005948 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005949 if (BTRFS_I(inode)->delayed_node)
5950 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005951
5952 return ret;
5953}
5954
5955/*
5956 * This is a copy of file_update_time. We need this so we can return error on
5957 * ENOSPC for updating the inode in the case of file write and mmap writes.
5958 */
Josef Bacike41f9412012-03-26 09:46:47 -04005959static int btrfs_update_time(struct inode *inode, struct timespec *now,
5960 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005961{
Alexander Block2bc5565282012-06-15 09:49:33 +02005962 struct btrfs_root *root = BTRFS_I(inode)->root;
5963
5964 if (btrfs_root_readonly(root))
5965 return -EROFS;
5966
Josef Bacike41f9412012-03-26 09:46:47 -04005967 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005968 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005969 if (flags & S_CTIME)
5970 inode->i_ctime = *now;
5971 if (flags & S_MTIME)
5972 inode->i_mtime = *now;
5973 if (flags & S_ATIME)
5974 inode->i_atime = *now;
5975 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005976}
5977
Chris Masond352ac62008-09-29 15:18:18 -04005978/*
5979 * find the highest existing sequence number in a directory
5980 * and then set the in-memory index_cnt variable to reflect
5981 * free sequence numbers
5982 */
Josef Bacikaec74772008-07-24 12:12:38 -04005983static int btrfs_set_inode_index_count(struct inode *inode)
5984{
5985 struct btrfs_root *root = BTRFS_I(inode)->root;
5986 struct btrfs_key key, found_key;
5987 struct btrfs_path *path;
5988 struct extent_buffer *leaf;
5989 int ret;
5990
Li Zefan33345d012011-04-20 10:31:50 +08005991 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005992 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005993 key.offset = (u64)-1;
5994
5995 path = btrfs_alloc_path();
5996 if (!path)
5997 return -ENOMEM;
5998
5999 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6000 if (ret < 0)
6001 goto out;
6002 /* FIXME: we should be able to handle this */
6003 if (ret == 0)
6004 goto out;
6005 ret = 0;
6006
6007 /*
6008 * MAGIC NUMBER EXPLANATION:
6009 * since we search a directory based on f_pos we have to start at 2
6010 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6011 * else has to start at 2
6012 */
6013 if (path->slots[0] == 0) {
6014 BTRFS_I(inode)->index_cnt = 2;
6015 goto out;
6016 }
6017
6018 path->slots[0]--;
6019
6020 leaf = path->nodes[0];
6021 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6022
Li Zefan33345d012011-04-20 10:31:50 +08006023 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006024 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006025 BTRFS_I(inode)->index_cnt = 2;
6026 goto out;
6027 }
6028
6029 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6030out:
6031 btrfs_free_path(path);
6032 return ret;
6033}
6034
Chris Masond352ac62008-09-29 15:18:18 -04006035/*
6036 * helper to find a free sequence number in a given directory. This current
6037 * code is very simple, later versions will do smarter things in the btree
6038 */
Chris Mason3de45862008-11-17 21:02:50 -05006039int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006040{
6041 int ret = 0;
6042
6043 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006044 ret = btrfs_inode_delayed_dir_index_count(dir);
6045 if (ret) {
6046 ret = btrfs_set_inode_index_count(dir);
6047 if (ret)
6048 return ret;
6049 }
Josef Bacikaec74772008-07-24 12:12:38 -04006050 }
6051
Chris Mason00e4e6b2008-08-05 11:18:09 -04006052 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006053 BTRFS_I(dir)->index_cnt++;
6054
6055 return ret;
6056}
6057
Chris Masonb0d5d102014-09-08 13:08:51 -07006058static int btrfs_insert_inode_locked(struct inode *inode)
6059{
6060 struct btrfs_iget_args args;
6061 args.location = &BTRFS_I(inode)->location;
6062 args.root = BTRFS_I(inode)->root;
6063
6064 return insert_inode_locked4(inode,
6065 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6066 btrfs_find_actor, &args);
6067}
6068
Chris Mason39279cc2007-06-12 06:35:45 -04006069static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6070 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006071 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006072 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006073 u64 ref_objectid, u64 objectid,
6074 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006075{
6076 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006077 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006078 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006079 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006080 struct btrfs_inode_ref *ref;
6081 struct btrfs_key key[2];
6082 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006083 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006084 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006085 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006086
Chris Mason5f39d392007-10-15 16:14:19 -04006087 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006088 if (!path)
6089 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006090
Chris Mason39279cc2007-06-12 06:35:45 -04006091 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006092 if (!inode) {
6093 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006094 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006095 }
Chris Mason39279cc2007-06-12 06:35:45 -04006096
Li Zefan581bb052011-04-20 10:06:11 +08006097 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006098 * O_TMPFILE, set link count to 0, so that after this point,
6099 * we fill in an inode item with the correct link count.
6100 */
6101 if (!name)
6102 set_nlink(inode, 0);
6103
6104 /*
Li Zefan581bb052011-04-20 10:06:11 +08006105 * we have to initialize this early, so we can reclaim the inode
6106 * number if we fail afterwards in this function.
6107 */
6108 inode->i_ino = objectid;
6109
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006110 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006111 trace_btrfs_inode_request(dir);
6112
Chris Mason3de45862008-11-17 21:02:50 -05006113 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006114 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006115 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006116 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006117 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006118 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006119 } else if (dir) {
6120 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006121 }
6122 /*
6123 * index_cnt is ignored for everything but a dir,
6124 * btrfs_get_inode_index_count has an explanation for the magic
6125 * number
6126 */
6127 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006128 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006129 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006130 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006131 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006132
Josef Bacik5dc562c2012-08-17 13:14:17 -04006133 /*
6134 * We could have gotten an inode number from somebody who was fsynced
6135 * and then removed in this same transaction, so let's just set full
6136 * sync since it will be a full sync anyway and this will blow away the
6137 * old info in the log.
6138 */
6139 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6140
Chris Mason9c583092008-01-29 15:15:18 -05006141 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006142 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006143 key[0].offset = 0;
6144
Chris Mason9c583092008-01-29 15:15:18 -05006145 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006146
6147 if (name) {
6148 /*
6149 * Start new inodes with an inode_ref. This is slightly more
6150 * efficient for small numbers of hard links since they will
6151 * be packed into one item. Extended refs will kick in if we
6152 * add more hard links than can fit in the ref item.
6153 */
6154 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006155 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006156 key[1].offset = ref_objectid;
6157
6158 sizes[1] = name_len + sizeof(*ref);
6159 }
Chris Mason9c583092008-01-29 15:15:18 -05006160
Chris Masonb0d5d102014-09-08 13:08:51 -07006161 location = &BTRFS_I(inode)->location;
6162 location->objectid = objectid;
6163 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006164 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006165
6166 ret = btrfs_insert_inode_locked(inode);
6167 if (ret < 0)
6168 goto fail;
6169
Chris Masonb9473432009-03-13 11:00:37 -04006170 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006171 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006172 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006173 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006174
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006175 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006176 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306177
6178 inode->i_mtime = CURRENT_TIME;
6179 inode->i_atime = inode->i_mtime;
6180 inode->i_ctime = inode->i_mtime;
6181 BTRFS_I(inode)->i_otime = inode->i_mtime;
6182
Chris Mason5f39d392007-10-15 16:14:19 -04006183 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6184 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006185 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6186 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006187 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006188
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006189 if (name) {
6190 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6191 struct btrfs_inode_ref);
6192 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6193 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6194 ptr = (unsigned long)(ref + 1);
6195 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6196 }
Chris Mason9c583092008-01-29 15:15:18 -05006197
Chris Mason5f39d392007-10-15 16:14:19 -04006198 btrfs_mark_buffer_dirty(path->nodes[0]);
6199 btrfs_free_path(path);
6200
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006201 btrfs_inherit_iflags(inode, dir);
6202
Al Viro569254b2011-07-24 17:08:40 -04006203 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04006204 if (btrfs_test_opt(root, NODATASUM))
6205 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06006206 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006207 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6208 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006209 }
6210
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006211 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006212
6213 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006214 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006215
Alexander Block8ea05e32012-07-25 17:35:53 +02006216 btrfs_update_root_times(trans, root);
6217
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006218 ret = btrfs_inode_inherit_props(trans, inode, dir);
6219 if (ret)
6220 btrfs_err(root->fs_info,
6221 "error inheriting props for ino %llu (root %llu): %d",
6222 btrfs_ino(inode), root->root_key.objectid, ret);
6223
Chris Mason39279cc2007-06-12 06:35:45 -04006224 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006225
6226fail_unlock:
6227 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006228fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006229 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006230 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006231 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006232 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006233 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006234}
6235
6236static inline u8 btrfs_inode_type(struct inode *inode)
6237{
6238 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6239}
6240
Chris Masond352ac62008-09-29 15:18:18 -04006241/*
6242 * utility function to add 'inode' into 'parent_inode' with
6243 * a give name and a given sequence number.
6244 * if 'add_backref' is true, also insert a backref from the
6245 * inode to the parent directory.
6246 */
Chris Masone02119d2008-09-05 16:13:11 -04006247int btrfs_add_link(struct btrfs_trans_handle *trans,
6248 struct inode *parent_inode, struct inode *inode,
6249 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006250{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006251 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006252 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006253 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006254 u64 ino = btrfs_ino(inode);
6255 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006256
Li Zefan33345d012011-04-20 10:31:50 +08006257 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006258 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6259 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006260 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006261 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006262 key.offset = 0;
6263 }
Chris Mason39279cc2007-06-12 06:35:45 -04006264
Li Zefan33345d012011-04-20 10:31:50 +08006265 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006266 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6267 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006268 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006269 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006270 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6271 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006272 }
6273
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006274 /* Nothing to clean up yet */
6275 if (ret)
6276 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006277
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006278 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6279 parent_inode, &key,
6280 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006281 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006282 goto fail_dir_item;
6283 else if (ret) {
6284 btrfs_abort_transaction(trans, root, ret);
6285 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006286 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006287
6288 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6289 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006290 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006291 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
6292 ret = btrfs_update_inode(trans, root, parent_inode);
6293 if (ret)
6294 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006295 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006296
6297fail_dir_item:
6298 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6299 u64 local_index;
6300 int err;
6301 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6302 key.objectid, root->root_key.objectid,
6303 parent_ino, &local_index, name, name_len);
6304
6305 } else if (add_backref) {
6306 u64 local_index;
6307 int err;
6308
6309 err = btrfs_del_inode_ref(trans, root, name, name_len,
6310 ino, parent_ino, &local_index);
6311 }
6312 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006313}
6314
6315static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006316 struct inode *dir, struct dentry *dentry,
6317 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006318{
Josef Bacika1b075d2010-11-19 20:36:11 +00006319 int err = btrfs_add_link(trans, dir, inode,
6320 dentry->d_name.name, dentry->d_name.len,
6321 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006322 if (err > 0)
6323 err = -EEXIST;
6324 return err;
6325}
6326
Josef Bacik618e21d2007-07-11 10:18:17 -04006327static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006328 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006329{
6330 struct btrfs_trans_handle *trans;
6331 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006332 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006333 int err;
6334 int drop_inode = 0;
6335 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006336 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006337
Josef Bacik9ed74f22009-09-11 16:12:44 -04006338 /*
6339 * 2 for inode item and ref
6340 * 2 for dir items
6341 * 1 for xattr if selinux is on
6342 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006343 trans = btrfs_start_transaction(root, 5);
6344 if (IS_ERR(trans))
6345 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006346
Li Zefan581bb052011-04-20 10:06:11 +08006347 err = btrfs_find_free_ino(root, &objectid);
6348 if (err)
6349 goto out_unlock;
6350
Josef Bacikaec74772008-07-24 12:12:38 -04006351 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006352 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006353 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006354 if (IS_ERR(inode)) {
6355 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006356 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006357 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006358
Casey Schauflerad19db72011-12-15 10:09:07 -05006359 /*
6360 * If the active LSM wants to access the inode during
6361 * d_instantiate it needs these. Smack checks to see
6362 * if the filesystem supports xattrs by looking at the
6363 * ops vector.
6364 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006365 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006366 init_special_inode(inode, inode->i_mode, rdev);
6367
6368 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006369 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006370 goto out_unlock_inode;
6371
6372 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6373 if (err) {
6374 goto out_unlock_inode;
6375 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006376 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006377 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006378 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006379 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006380
Josef Bacik618e21d2007-07-11 10:18:17 -04006381out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006382 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006383 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006384 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006385 if (drop_inode) {
6386 inode_dec_link_count(inode);
6387 iput(inode);
6388 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006389 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006390
6391out_unlock_inode:
6392 drop_inode = 1;
6393 unlock_new_inode(inode);
6394 goto out_unlock;
6395
Josef Bacik618e21d2007-07-11 10:18:17 -04006396}
6397
Chris Mason39279cc2007-06-12 06:35:45 -04006398static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006399 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006400{
6401 struct btrfs_trans_handle *trans;
6402 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006403 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006404 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006405 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006406 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006407 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006408
Josef Bacik9ed74f22009-09-11 16:12:44 -04006409 /*
6410 * 2 for inode item and ref
6411 * 2 for dir items
6412 * 1 for xattr if selinux is on
6413 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006414 trans = btrfs_start_transaction(root, 5);
6415 if (IS_ERR(trans))
6416 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006417
Li Zefan581bb052011-04-20 10:06:11 +08006418 err = btrfs_find_free_ino(root, &objectid);
6419 if (err)
6420 goto out_unlock;
6421
Josef Bacikaec74772008-07-24 12:12:38 -04006422 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006423 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006424 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006425 if (IS_ERR(inode)) {
6426 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006427 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006428 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006429 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006430 /*
6431 * If the active LSM wants to access the inode during
6432 * d_instantiate it needs these. Smack checks to see
6433 * if the filesystem supports xattrs by looking at the
6434 * ops vector.
6435 */
6436 inode->i_fop = &btrfs_file_operations;
6437 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006438 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006439
6440 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6441 if (err)
6442 goto out_unlock_inode;
6443
6444 err = btrfs_update_inode(trans, root, inode);
6445 if (err)
6446 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006447
Josef Bacika1b075d2010-11-19 20:36:11 +00006448 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006449 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006450 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006451
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006452 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006453 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006454 d_instantiate(dentry, inode);
6455
Chris Mason39279cc2007-06-12 06:35:45 -04006456out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006457 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006458 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006459 inode_dec_link_count(inode);
6460 iput(inode);
6461 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006462 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006463 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006464 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006465
6466out_unlock_inode:
6467 unlock_new_inode(inode);
6468 goto out_unlock;
6469
Chris Mason39279cc2007-06-12 06:35:45 -04006470}
6471
6472static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6473 struct dentry *dentry)
6474{
6475 struct btrfs_trans_handle *trans;
6476 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006477 struct inode *inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006478 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006479 int err;
6480 int drop_inode = 0;
6481
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006482 /* do not allow sys_link's with other subvols of the same device */
6483 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006484 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006485
Mark Fashehf1863732012-08-08 11:32:27 -07006486 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006487 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006488
Chris Mason3de45862008-11-17 21:02:50 -05006489 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006490 if (err)
6491 goto fail;
6492
Yan, Zhenga22285a2010-05-16 10:48:46 -04006493 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006494 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006495 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006496 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006497 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006498 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006499 if (IS_ERR(trans)) {
6500 err = PTR_ERR(trans);
6501 goto fail;
6502 }
Chris Mason5f39d392007-10-15 16:14:19 -04006503
Miao Xie67de1172013-12-26 13:07:06 +08006504 /* There are several dir indexes for this inode, clear the cache. */
6505 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006506 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006507 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006508 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006509 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006510 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006511
Josef Bacika1b075d2010-11-19 20:36:11 +00006512 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006513
Yan, Zhenga5719522009-09-24 09:17:31 -04006514 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006515 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006516 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006517 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006518 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006519 if (err)
6520 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006521 if (inode->i_nlink == 1) {
6522 /*
6523 * If new hard link count is 1, it's a file created
6524 * with open(2) O_TMPFILE flag.
6525 */
6526 err = btrfs_orphan_del(trans, inode);
6527 if (err)
6528 goto fail;
6529 }
Al Viro08c422c2011-12-23 07:58:13 -05006530 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006531 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006532 }
Chris Mason39279cc2007-06-12 06:35:45 -04006533
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006534 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006535 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006536fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006537 if (drop_inode) {
6538 inode_dec_link_count(inode);
6539 iput(inode);
6540 }
Liu Bob53d3f52012-11-14 14:34:34 +00006541 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006542 return err;
6543}
6544
Al Viro18bb1db2011-07-26 01:41:39 -04006545static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006546{
Chris Masonb9d86662008-05-02 16:13:49 -04006547 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006548 struct btrfs_trans_handle *trans;
6549 struct btrfs_root *root = BTRFS_I(dir)->root;
6550 int err = 0;
6551 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006552 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006553 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006554
Josef Bacik9ed74f22009-09-11 16:12:44 -04006555 /*
6556 * 2 items for inode and ref
6557 * 2 items for dir items
6558 * 1 for xattr if selinux is on
6559 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006560 trans = btrfs_start_transaction(root, 5);
6561 if (IS_ERR(trans))
6562 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006563
Li Zefan581bb052011-04-20 10:06:11 +08006564 err = btrfs_find_free_ino(root, &objectid);
6565 if (err)
6566 goto out_fail;
6567
Josef Bacikaec74772008-07-24 12:12:38 -04006568 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006569 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006570 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006571 if (IS_ERR(inode)) {
6572 err = PTR_ERR(inode);
6573 goto out_fail;
6574 }
Chris Mason5f39d392007-10-15 16:14:19 -04006575
Chris Mason39279cc2007-06-12 06:35:45 -04006576 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006577 /* these must be set before we unlock the inode */
6578 inode->i_op = &btrfs_dir_inode_operations;
6579 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006580
Eric Paris2a7dba32011-02-01 11:05:39 -05006581 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006582 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006583 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006584
Chris Masondbe674a2008-07-17 12:54:05 -04006585 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006586 err = btrfs_update_inode(trans, root, inode);
6587 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006588 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006589
Josef Bacika1b075d2010-11-19 20:36:11 +00006590 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6591 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006592 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006593 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006594
Chris Mason39279cc2007-06-12 06:35:45 -04006595 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006596 /*
6597 * mkdir is special. We're unlocking after we call d_instantiate
6598 * to avoid a race with nfsd calling d_instantiate.
6599 */
6600 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006601 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006602
6603out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006604 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006605 if (drop_on_err) {
6606 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006607 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006608 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006609 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006610 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006611 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006612
6613out_fail_inode:
6614 unlock_new_inode(inode);
6615 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006616}
6617
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006618/* Find next extent map of a given extent map, caller needs to ensure locks */
6619static struct extent_map *next_extent_map(struct extent_map *em)
6620{
6621 struct rb_node *next;
6622
6623 next = rb_next(&em->rb_node);
6624 if (!next)
6625 return NULL;
6626 return container_of(next, struct extent_map, rb_node);
6627}
6628
6629static struct extent_map *prev_extent_map(struct extent_map *em)
6630{
6631 struct rb_node *prev;
6632
6633 prev = rb_prev(&em->rb_node);
6634 if (!prev)
6635 return NULL;
6636 return container_of(prev, struct extent_map, rb_node);
6637}
6638
Chris Masond352ac62008-09-29 15:18:18 -04006639/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006640 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006641 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006642 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006643 */
Chris Mason3b951512008-04-17 11:29:12 -04006644static int merge_extent_mapping(struct extent_map_tree *em_tree,
6645 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006646 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006647 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006648{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006649 struct extent_map *prev;
6650 struct extent_map *next;
6651 u64 start;
6652 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006653 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006654
Chris Masone6dcd2d2008-07-17 12:53:50 -04006655 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006656
6657 if (existing->start > map_start) {
6658 next = existing;
6659 prev = prev_extent_map(next);
6660 } else {
6661 prev = existing;
6662 next = next_extent_map(prev);
6663 }
6664
6665 start = prev ? extent_map_end(prev) : em->start;
6666 start = max_t(u64, start, em->start);
6667 end = next ? next->start : extent_map_end(em);
6668 end = min_t(u64, end, extent_map_end(em));
6669 start_diff = start - em->start;
6670 em->start = start;
6671 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006672 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6673 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006674 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006675 em->block_len -= start_diff;
6676 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006677 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006678}
6679
Chris Masonc8b97812008-10-29 14:49:59 -04006680static noinline int uncompress_inline(struct btrfs_path *path,
6681 struct inode *inode, struct page *page,
6682 size_t pg_offset, u64 extent_offset,
6683 struct btrfs_file_extent_item *item)
6684{
6685 int ret;
6686 struct extent_buffer *leaf = path->nodes[0];
6687 char *tmp;
6688 size_t max_size;
6689 unsigned long inline_size;
6690 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006691 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006692
6693 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006694 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006695 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6696 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006697 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006698 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006699 if (!tmp)
6700 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006701 ptr = btrfs_file_extent_inline_start(item);
6702
6703 read_extent_buffer(leaf, tmp, ptr, inline_size);
6704
Chris Mason5b050f02008-11-11 09:34:41 -05006705 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006706 ret = btrfs_decompress(compress_type, tmp, page,
6707 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006708 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006709 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006710}
6711
Chris Masond352ac62008-09-29 15:18:18 -04006712/*
6713 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006714 * the ugly parts come from merging extents from the disk with the in-ram
6715 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006716 * where the in-ram extents might be locked pending data=ordered completion.
6717 *
6718 * This also copies inline extents directly into the page.
6719 */
Chris Masond3977122009-01-05 21:25:51 -05006720
Chris Masona52d9a82007-08-27 16:49:44 -04006721struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006722 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006723 int create)
6724{
6725 int ret;
6726 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006727 u64 extent_start = 0;
6728 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006729 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006730 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006731 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006732 struct btrfs_root *root = BTRFS_I(inode)->root;
6733 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006734 struct extent_buffer *leaf;
6735 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006736 struct extent_map *em = NULL;
6737 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006738 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006739 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006740 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006741
Chris Masona52d9a82007-08-27 16:49:44 -04006742again:
Chris Mason890871b2009-09-02 16:24:52 -04006743 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006744 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006745 if (em)
6746 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006747 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006748
Chris Masona52d9a82007-08-27 16:49:44 -04006749 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006750 if (em->start > start || em->start + em->len <= start)
6751 free_extent_map(em);
6752 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006753 free_extent_map(em);
6754 else
6755 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006756 }
David Sterba172ddd62011-04-21 00:48:27 +02006757 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006758 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006759 err = -ENOMEM;
6760 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006761 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006762 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006763 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006764 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006765 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006766 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006767
6768 if (!path) {
6769 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006770 if (!path) {
6771 err = -ENOMEM;
6772 goto out;
6773 }
6774 /*
6775 * Chances are we'll be called again, so go ahead and do
6776 * readahead
6777 */
David Sterbae4058b52015-11-27 16:31:35 +01006778 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006779 }
6780
Chris Mason179e29e2007-11-01 11:28:41 -04006781 ret = btrfs_lookup_file_extent(trans, root, path,
6782 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006783 if (ret < 0) {
6784 err = ret;
6785 goto out;
6786 }
6787
6788 if (ret != 0) {
6789 if (path->slots[0] == 0)
6790 goto not_found;
6791 path->slots[0]--;
6792 }
6793
Chris Mason5f39d392007-10-15 16:14:19 -04006794 leaf = path->nodes[0];
6795 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006796 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006797 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006798 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006799 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006800 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006801 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006802 /*
6803 * If we backup past the first extent we want to move forward
6804 * and see if there is an extent in front of us, otherwise we'll
6805 * say there is a hole for our whole search range which can
6806 * cause problems.
6807 */
6808 extent_end = start;
6809 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006810 }
6811
Chris Mason5f39d392007-10-15 16:14:19 -04006812 found_type = btrfs_file_extent_type(leaf, item);
6813 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006814 if (found_type == BTRFS_FILE_EXTENT_REG ||
6815 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006816 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006817 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006818 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6819 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006820 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006821 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006822 }
Josef Bacik25a50342013-10-14 12:08:38 -04006823next:
Yan Zheng9036c102008-10-30 14:19:41 -04006824 if (start >= extent_end) {
6825 path->slots[0]++;
6826 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6827 ret = btrfs_next_leaf(root, path);
6828 if (ret < 0) {
6829 err = ret;
6830 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006831 }
Yan Zheng9036c102008-10-30 14:19:41 -04006832 if (ret > 0)
6833 goto not_found;
6834 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006835 }
Yan Zheng9036c102008-10-30 14:19:41 -04006836 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6837 if (found_key.objectid != objectid ||
6838 found_key.type != BTRFS_EXTENT_DATA_KEY)
6839 goto not_found;
6840 if (start + len <= found_key.offset)
6841 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006842 if (start > found_key.offset)
6843 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006844 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006845 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006846 em->len = found_key.offset - start;
6847 goto not_found_em;
6848 }
6849
Filipe Manana7ffbb592014-06-09 03:48:05 +01006850 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6851
Yan Zhengd899e052008-10-30 14:25:28 -04006852 if (found_type == BTRFS_FILE_EXTENT_REG ||
6853 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006854 goto insert;
6855 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006856 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006857 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006858 size_t size;
6859 size_t extent_offset;
6860 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006861
Filipe Manana7ffbb592014-06-09 03:48:05 +01006862 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006863 goto out;
Yan689f9342007-10-29 11:41:07 -04006864
Chris Mason514ac8a2014-01-03 21:07:00 -08006865 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006866 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006867 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006868 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006869 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006870 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006871 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006872 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006873 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006874 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006875 if (btrfs_file_extent_compression(leaf, item) !=
6876 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006877 ret = uncompress_inline(path, inode, page,
6878 pg_offset,
6879 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006880 if (ret) {
6881 err = ret;
6882 goto out;
6883 }
Chris Masonc8b97812008-10-29 14:49:59 -04006884 } else {
6885 map = kmap(page);
6886 read_extent_buffer(leaf, map + pg_offset, ptr,
6887 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006888 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6889 memset(map + pg_offset + copy_size, 0,
6890 PAGE_CACHE_SIZE - pg_offset -
6891 copy_size);
6892 }
Chris Masonc8b97812008-10-29 14:49:59 -04006893 kunmap(page);
6894 }
Chris Mason179e29e2007-11-01 11:28:41 -04006895 flush_dcache_page(page);
6896 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006897 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006898 if (!trans) {
6899 kunmap(page);
6900 free_extent_map(em);
6901 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006902
David Sterbab3b4aa72011-04-21 01:20:15 +02006903 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006904 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006905
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006906 if (IS_ERR(trans))
6907 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006908 goto again;
6909 }
Chris Masonc8b97812008-10-29 14:49:59 -04006910 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006911 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006912 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006913 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006914 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006915 }
Chris Masond1310b22008-01-24 16:13:08 -05006916 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006917 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006918 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006919 }
6920not_found:
6921 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006922 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006923 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006924not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006925 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006926 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006927insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006928 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006929 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006930 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006931 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006932 err = -EIO;
6933 goto out;
6934 }
Chris Masond1310b22008-01-24 16:13:08 -05006935
6936 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006937 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006938 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006939 /* it is possible that someone inserted the extent into the tree
6940 * while we had the lock dropped. It is also possible that
6941 * an overlapping map exists in the tree
6942 */
Chris Masona52d9a82007-08-27 16:49:44 -04006943 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006944 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006945
6946 ret = 0;
6947
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006948 existing = search_extent_mapping(em_tree, start, len);
6949 /*
6950 * existing will always be non-NULL, since there must be
6951 * extent causing the -EEXIST.
6952 */
6953 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006954 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006955 /*
6956 * The existing extent map is the one nearest to
6957 * the [start, start + len) range which overlaps
6958 */
6959 err = merge_extent_mapping(em_tree, existing,
6960 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006961 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006962 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006963 free_extent_map(em);
6964 em = NULL;
6965 }
6966 } else {
6967 free_extent_map(em);
6968 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006969 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006970 }
Chris Masona52d9a82007-08-27 16:49:44 -04006971 }
Chris Mason890871b2009-09-02 16:24:52 -04006972 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006973out:
liubo1abe9b82011-03-24 11:18:59 +00006974
Steven Rostedt4cd85872013-11-14 22:57:29 -05006975 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006976
Tsutomu Itoh527afb42015-08-19 14:55:00 +09006977 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006978 if (trans) {
6979 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006980 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006981 err = ret;
6982 }
Chris Masona52d9a82007-08-27 16:49:44 -04006983 if (err) {
6984 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006985 return ERR_PTR(err);
6986 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006987 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006988 return em;
6989}
6990
Chris Masonec29ed52011-02-23 16:23:20 -05006991struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6992 size_t pg_offset, u64 start, u64 len,
6993 int create)
6994{
6995 struct extent_map *em;
6996 struct extent_map *hole_em = NULL;
6997 u64 range_start = start;
6998 u64 end;
6999 u64 found;
7000 u64 found_end;
7001 int err = 0;
7002
7003 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7004 if (IS_ERR(em))
7005 return em;
7006 if (em) {
7007 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007008 * if our em maps to
7009 * - a hole or
7010 * - a pre-alloc extent,
7011 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007012 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007013 if (em->block_start != EXTENT_MAP_HOLE &&
7014 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007015 return em;
7016 else
7017 hole_em = em;
7018 }
7019
7020 /* check to see if we've wrapped (len == -1 or similar) */
7021 end = start + len;
7022 if (end < start)
7023 end = (u64)-1;
7024 else
7025 end -= 1;
7026
7027 em = NULL;
7028
7029 /* ok, we didn't find anything, lets look for delalloc */
7030 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7031 end, len, EXTENT_DELALLOC, 1);
7032 found_end = range_start + found;
7033 if (found_end < range_start)
7034 found_end = (u64)-1;
7035
7036 /*
7037 * we didn't find anything useful, return
7038 * the original results from get_extent()
7039 */
7040 if (range_start > end || found_end <= start) {
7041 em = hole_em;
7042 hole_em = NULL;
7043 goto out;
7044 }
7045
7046 /* adjust the range_start to make sure it doesn't
7047 * go backwards from the start they passed in
7048 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307049 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007050 found = found_end - range_start;
7051
7052 if (found > 0) {
7053 u64 hole_start = start;
7054 u64 hole_len = len;
7055
David Sterba172ddd62011-04-21 00:48:27 +02007056 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007057 if (!em) {
7058 err = -ENOMEM;
7059 goto out;
7060 }
7061 /*
7062 * when btrfs_get_extent can't find anything it
7063 * returns one huge hole
7064 *
7065 * make sure what it found really fits our range, and
7066 * adjust to make sure it is based on the start from
7067 * the caller
7068 */
7069 if (hole_em) {
7070 u64 calc_end = extent_map_end(hole_em);
7071
7072 if (calc_end <= start || (hole_em->start > end)) {
7073 free_extent_map(hole_em);
7074 hole_em = NULL;
7075 } else {
7076 hole_start = max(hole_em->start, start);
7077 hole_len = calc_end - hole_start;
7078 }
7079 }
7080 em->bdev = NULL;
7081 if (hole_em && range_start > hole_start) {
7082 /* our hole starts before our delalloc, so we
7083 * have to return just the parts of the hole
7084 * that go until the delalloc starts
7085 */
7086 em->len = min(hole_len,
7087 range_start - hole_start);
7088 em->start = hole_start;
7089 em->orig_start = hole_start;
7090 /*
7091 * don't adjust block start at all,
7092 * it is fixed at EXTENT_MAP_HOLE
7093 */
7094 em->block_start = hole_em->block_start;
7095 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007096 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7097 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007098 } else {
7099 em->start = range_start;
7100 em->len = found;
7101 em->orig_start = range_start;
7102 em->block_start = EXTENT_MAP_DELALLOC;
7103 em->block_len = found;
7104 }
7105 } else if (hole_em) {
7106 return hole_em;
7107 }
7108out:
7109
7110 free_extent_map(hole_em);
7111 if (err) {
7112 free_extent_map(em);
7113 return ERR_PTR(err);
7114 }
7115 return em;
7116}
7117
Josef Bacik4b46fce2010-05-23 11:00:55 -04007118static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7119 u64 start, u64 len)
7120{
7121 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007122 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007123 struct btrfs_key ins;
7124 u64 alloc_hint;
7125 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007126
Josef Bacik4b46fce2010-05-23 11:00:55 -04007127 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04007128 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007129 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007130 if (ret)
7131 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007132
Josef Bacik70c8a912012-10-11 16:54:30 -04007133 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007134 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04007135 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08007136 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007137 return em;
7138 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007139
7140 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
7141 ins.offset, ins.offset, 0);
7142 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08007143 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007144 free_extent_map(em);
7145 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007146 }
Josef Bacik00361582013-08-14 14:02:47 -04007147
Josef Bacik4b46fce2010-05-23 11:00:55 -04007148 return em;
7149}
7150
Chris Mason46bfbb52010-05-26 11:04:10 -04007151/*
7152 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7153 * block must be cow'd
7154 */
Josef Bacik00361582013-08-14 14:02:47 -04007155noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007156 u64 *orig_start, u64 *orig_block_len,
7157 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007158{
Josef Bacik00361582013-08-14 14:02:47 -04007159 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007160 struct btrfs_path *path;
7161 int ret;
7162 struct extent_buffer *leaf;
7163 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007164 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007165 struct btrfs_file_extent_item *fi;
7166 struct btrfs_key key;
7167 u64 disk_bytenr;
7168 u64 backref_offset;
7169 u64 extent_end;
7170 u64 num_bytes;
7171 int slot;
7172 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007173 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007174
Chris Mason46bfbb52010-05-26 11:04:10 -04007175 path = btrfs_alloc_path();
7176 if (!path)
7177 return -ENOMEM;
7178
Josef Bacik00361582013-08-14 14:02:47 -04007179 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007180 offset, 0);
7181 if (ret < 0)
7182 goto out;
7183
7184 slot = path->slots[0];
7185 if (ret == 1) {
7186 if (slot == 0) {
7187 /* can't find the item, must cow */
7188 ret = 0;
7189 goto out;
7190 }
7191 slot--;
7192 }
7193 ret = 0;
7194 leaf = path->nodes[0];
7195 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007196 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007197 key.type != BTRFS_EXTENT_DATA_KEY) {
7198 /* not our file or wrong item type, must cow */
7199 goto out;
7200 }
7201
7202 if (key.offset > offset) {
7203 /* Wrong offset, must cow */
7204 goto out;
7205 }
7206
7207 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7208 found_type = btrfs_file_extent_type(leaf, fi);
7209 if (found_type != BTRFS_FILE_EXTENT_REG &&
7210 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7211 /* not a regular extent, must cow */
7212 goto out;
7213 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007214
7215 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7216 goto out;
7217
Miao Xiee77751a2013-12-27 21:11:50 +08007218 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7219 if (extent_end <= offset)
7220 goto out;
7221
Chris Mason46bfbb52010-05-26 11:04:10 -04007222 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007223 if (disk_bytenr == 0)
7224 goto out;
7225
7226 if (btrfs_file_extent_compression(leaf, fi) ||
7227 btrfs_file_extent_encryption(leaf, fi) ||
7228 btrfs_file_extent_other_encoding(leaf, fi))
7229 goto out;
7230
Chris Mason46bfbb52010-05-26 11:04:10 -04007231 backref_offset = btrfs_file_extent_offset(leaf, fi);
7232
Josef Bacik7ee9e442013-06-21 16:37:03 -04007233 if (orig_start) {
7234 *orig_start = key.offset - backref_offset;
7235 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7236 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7237 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007238
Chris Mason46bfbb52010-05-26 11:04:10 -04007239 if (btrfs_extent_readonly(root, disk_bytenr))
7240 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007241
7242 num_bytes = min(offset + *len, extent_end) - offset;
7243 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7244 u64 range_end;
7245
7246 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7247 ret = test_range_bit(io_tree, offset, range_end,
7248 EXTENT_DELALLOC, 0, NULL);
7249 if (ret) {
7250 ret = -EAGAIN;
7251 goto out;
7252 }
7253 }
7254
Josef Bacik1bda19e2013-10-18 12:10:36 -04007255 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007256
7257 /*
7258 * look for other files referencing this extent, if we
7259 * find any we must cow
7260 */
Josef Bacik00361582013-08-14 14:02:47 -04007261 trans = btrfs_join_transaction(root);
7262 if (IS_ERR(trans)) {
7263 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007264 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007265 }
7266
7267 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7268 key.offset - backref_offset, disk_bytenr);
7269 btrfs_end_transaction(trans, root);
7270 if (ret) {
7271 ret = 0;
7272 goto out;
7273 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007274
7275 /*
7276 * adjust disk_bytenr and num_bytes to cover just the bytes
7277 * in this extent we are about to write. If there
7278 * are any csums in that range we have to cow in order
7279 * to keep the csums correct
7280 */
7281 disk_bytenr += backref_offset;
7282 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007283 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7284 goto out;
7285 /*
7286 * all of the above have passed, it is safe to overwrite this extent
7287 * without cow
7288 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007289 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007290 ret = 1;
7291out:
7292 btrfs_free_path(path);
7293 return ret;
7294}
7295
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007296bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7297{
7298 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7299 int found = false;
7300 void **pagep = NULL;
7301 struct page *page = NULL;
7302 int start_idx;
7303 int end_idx;
7304
7305 start_idx = start >> PAGE_CACHE_SHIFT;
7306
7307 /*
7308 * end is the last byte in the last page. end == start is legal
7309 */
7310 end_idx = end >> PAGE_CACHE_SHIFT;
7311
7312 rcu_read_lock();
7313
7314 /* Most of the code in this while loop is lifted from
7315 * find_get_page. It's been modified to begin searching from a
7316 * page and return just the first page found in that range. If the
7317 * found idx is less than or equal to the end idx then we know that
7318 * a page exists. If no pages are found or if those pages are
7319 * outside of the range then we're fine (yay!) */
7320 while (page == NULL &&
7321 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7322 page = radix_tree_deref_slot(pagep);
7323 if (unlikely(!page))
7324 break;
7325
7326 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007327 if (radix_tree_deref_retry(page)) {
7328 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007329 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007330 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007331 /*
7332 * Otherwise, shmem/tmpfs must be storing a swap entry
7333 * here as an exceptional entry: so return it without
7334 * attempting to raise page count.
7335 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007336 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007337 break; /* TODO: Is this relevant for this use case? */
7338 }
7339
Filipe Manana91405152014-06-05 13:22:24 +01007340 if (!page_cache_get_speculative(page)) {
7341 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007342 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007343 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007344
7345 /*
7346 * Has the page moved?
7347 * This is part of the lockless pagecache protocol. See
7348 * include/linux/pagemap.h for details.
7349 */
7350 if (unlikely(page != *pagep)) {
7351 page_cache_release(page);
7352 page = NULL;
7353 }
7354 }
7355
7356 if (page) {
7357 if (page->index <= end_idx)
7358 found = true;
7359 page_cache_release(page);
7360 }
7361
7362 rcu_read_unlock();
7363 return found;
7364}
7365
Josef Bacikeb838e72012-07-31 16:28:48 -04007366static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7367 struct extent_state **cached_state, int writing)
7368{
7369 struct btrfs_ordered_extent *ordered;
7370 int ret = 0;
7371
7372 while (1) {
7373 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7374 0, cached_state);
7375 /*
7376 * We're concerned with the entire range that we're going to be
7377 * doing DIO to, so we need to make sure theres no ordered
7378 * extents in this range.
7379 */
7380 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7381 lockend - lockstart + 1);
7382
7383 /*
7384 * We need to make sure there are no buffered pages in this
7385 * range either, we could have raced between the invalidate in
7386 * generic_file_direct_write and locking the extent. The
7387 * invalidate needs to happen so that reads after a write do not
7388 * get stale data.
7389 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007390 if (!ordered &&
7391 (!writing ||
7392 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007393 break;
7394
7395 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7396 cached_state, GFP_NOFS);
7397
7398 if (ordered) {
7399 btrfs_start_ordered_extent(inode, ordered, 1);
7400 btrfs_put_ordered_extent(ordered);
7401 } else {
7402 /* Screw you mmap */
Filipe Manana728404d2014-10-10 09:43:11 +01007403 ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
Filipe Manana075bdbd2014-10-09 21:18:55 +01007404 if (ret)
7405 break;
7406 ret = filemap_fdatawait_range(inode->i_mapping,
7407 lockstart,
7408 lockend);
Josef Bacikeb838e72012-07-31 16:28:48 -04007409 if (ret)
7410 break;
7411
7412 /*
7413 * If we found a page that couldn't be invalidated just
7414 * fall back to buffered.
7415 */
7416 ret = invalidate_inode_pages2_range(inode->i_mapping,
7417 lockstart >> PAGE_CACHE_SHIFT,
7418 lockend >> PAGE_CACHE_SHIFT);
7419 if (ret)
7420 break;
7421 }
7422
7423 cond_resched();
7424 }
7425
7426 return ret;
7427}
7428
Josef Bacik69ffb542012-09-11 15:40:07 -04007429static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7430 u64 len, u64 orig_start,
7431 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007432 u64 orig_block_len, u64 ram_bytes,
7433 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007434{
7435 struct extent_map_tree *em_tree;
7436 struct extent_map *em;
7437 struct btrfs_root *root = BTRFS_I(inode)->root;
7438 int ret;
7439
7440 em_tree = &BTRFS_I(inode)->extent_tree;
7441 em = alloc_extent_map();
7442 if (!em)
7443 return ERR_PTR(-ENOMEM);
7444
7445 em->start = start;
7446 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007447 em->mod_start = start;
7448 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007449 em->len = len;
7450 em->block_len = block_len;
7451 em->block_start = block_start;
7452 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007453 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007454 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007455 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007456 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7457 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007458 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007459
7460 do {
7461 btrfs_drop_extent_cache(inode, em->start,
7462 em->start + em->len - 1, 0);
7463 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007464 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007465 write_unlock(&em_tree->lock);
7466 } while (ret == -EEXIST);
7467
7468 if (ret) {
7469 free_extent_map(em);
7470 return ERR_PTR(ret);
7471 }
7472
7473 return em;
7474}
7475
chandan50745b02015-08-28 21:10:13 +05307476struct btrfs_dio_data {
7477 u64 outstanding_extents;
7478 u64 reserve;
7479};
Josef Bacik69ffb542012-09-11 15:40:07 -04007480
Filipe Manana9c9464c2015-11-04 09:52:04 +00007481static void adjust_dio_outstanding_extents(struct inode *inode,
7482 struct btrfs_dio_data *dio_data,
7483 const u64 len)
7484{
7485 unsigned num_extents;
7486
7487 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7488 BTRFS_MAX_EXTENT_SIZE);
7489 /*
7490 * If we have an outstanding_extents count still set then we're
7491 * within our reservation, otherwise we need to adjust our inode
7492 * counter appropriately.
7493 */
7494 if (dio_data->outstanding_extents) {
7495 dio_data->outstanding_extents -= num_extents;
7496 } else {
7497 spin_lock(&BTRFS_I(inode)->lock);
7498 BTRFS_I(inode)->outstanding_extents += num_extents;
7499 spin_unlock(&BTRFS_I(inode)->lock);
7500 }
7501}
7502
Josef Bacik4b46fce2010-05-23 11:00:55 -04007503static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7504 struct buffer_head *bh_result, int create)
7505{
7506 struct extent_map *em;
7507 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007508 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307509 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007510 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007511 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007512 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007513 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007514 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007515
Miao Xie172a5042013-02-21 02:48:22 -07007516 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007517 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007518 else
Josef Bacikc3298612012-08-03 16:49:19 -04007519 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007520
Josef Bacikc3298612012-08-03 16:49:19 -04007521 lockstart = start;
7522 lockend = start + len - 1;
7523
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007524 if (current->journal_info) {
7525 /*
7526 * Need to pull our outstanding extents and set journal_info to NULL so
7527 * that anything that needs to check if there's a transction doesn't get
7528 * confused.
7529 */
chandan50745b02015-08-28 21:10:13 +05307530 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007531 current->journal_info = NULL;
7532 }
7533
Josef Bacikeb838e72012-07-31 16:28:48 -04007534 /*
7535 * If this errors out it's because we couldn't invalidate pagecache for
7536 * this range and we need to fallback to buffered.
7537 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007538 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7539 create)) {
7540 ret = -ENOTBLK;
7541 goto err;
7542 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007543
Josef Bacik4b46fce2010-05-23 11:00:55 -04007544 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007545 if (IS_ERR(em)) {
7546 ret = PTR_ERR(em);
7547 goto unlock_err;
7548 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007549
7550 /*
7551 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7552 * io. INLINE is special, and we could probably kludge it in here, but
7553 * it's still buffered so for safety lets just fall back to the generic
7554 * buffered path.
7555 *
7556 * For COMPRESSED we _have_ to read the entire extent in so we can
7557 * decompress it, so there will be buffering required no matter what we
7558 * do, so go ahead and fallback to buffered.
7559 *
7560 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7561 * to buffered IO. Don't blame me, this is the price we pay for using
7562 * the generic code.
7563 */
7564 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7565 em->block_start == EXTENT_MAP_INLINE) {
7566 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007567 ret = -ENOTBLK;
7568 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007569 }
7570
7571 /* Just a good old fashioned hole, return */
7572 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7573 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7574 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007575 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007576 }
7577
7578 /*
7579 * We don't allocate a new extent in the following cases
7580 *
7581 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7582 * existing extent.
7583 * 2) The extent is marked as PREALLOC. We're good to go here and can
7584 * just use the extent.
7585 *
7586 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007587 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007588 len = min(len, em->len - (start - em->start));
7589 lockstart = start + len;
7590 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007591 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007592
7593 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7594 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7595 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007596 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007597 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007598
7599 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7600 type = BTRFS_ORDERED_PREALLOC;
7601 else
7602 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007603 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007604 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007605
Josef Bacik00361582013-08-14 14:02:47 -04007606 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007607 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007608 if (type == BTRFS_ORDERED_PREALLOC) {
7609 free_extent_map(em);
7610 em = create_pinned_em(inode, start, len,
7611 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007612 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007613 orig_block_len,
7614 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007615 if (IS_ERR(em)) {
7616 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007617 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007618 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007619 }
7620
Chris Mason46bfbb52010-05-26 11:04:10 -04007621 ret = btrfs_add_ordered_extent_dio(inode, start,
7622 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007623 if (ret) {
7624 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007625 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007626 }
7627 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007628 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007629 }
Josef Bacik00361582013-08-14 14:02:47 -04007630
Chris Mason46bfbb52010-05-26 11:04:10 -04007631 /*
7632 * this will cow the extent, reset the len in case we changed
7633 * it above
7634 */
7635 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007636 free_extent_map(em);
7637 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007638 if (IS_ERR(em)) {
7639 ret = PTR_ERR(em);
7640 goto unlock_err;
7641 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007642 len = min(len, em->len - (start - em->start));
7643unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007644 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7645 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007646 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007647 bh_result->b_bdev = em->bdev;
7648 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007649 if (create) {
7650 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7651 set_buffer_new(bh_result);
7652
7653 /*
7654 * Need to update the i_size under the extent lock so buffered
7655 * readers will get the updated i_size when we unlock.
7656 */
7657 if (start + len > i_size_read(inode))
7658 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007659
Filipe Manana9c9464c2015-11-04 09:52:04 +00007660 adjust_dio_outstanding_extents(inode, dio_data, len);
Qu Wenruo7cf5b972015-09-08 17:25:55 +08007661 btrfs_free_reserved_data_space(inode, start, len);
chandan50745b02015-08-28 21:10:13 +05307662 WARN_ON(dio_data->reserve < len);
7663 dio_data->reserve -= len;
7664 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007665 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007666
Josef Bacikeb838e72012-07-31 16:28:48 -04007667 /*
7668 * In the case of write we need to clear and unlock the entire range,
7669 * in the case of read we need to unlock only the end area that we
7670 * aren't using if there is any left over space.
7671 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007672 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007673 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7674 lockend, unlock_bits, 1, 0,
7675 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007676 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007677 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007678 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007679
Josef Bacik4b46fce2010-05-23 11:00:55 -04007680 free_extent_map(em);
7681
7682 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007683
7684unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007685 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7686 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007687err:
chandan50745b02015-08-28 21:10:13 +05307688 if (dio_data)
7689 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007690 /*
7691 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7692 * write less data then expected, so that we don't underflow our inode's
7693 * outstanding extents counter.
7694 */
7695 if (create && dio_data)
7696 adjust_dio_outstanding_extents(inode, dio_data, len);
7697
Josef Bacikeb838e72012-07-31 16:28:48 -04007698 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007699}
7700
Miao Xie8b110e32014-09-12 18:44:03 +08007701static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7702 int rw, int mirror_num)
7703{
7704 struct btrfs_root *root = BTRFS_I(inode)->root;
7705 int ret;
7706
7707 BUG_ON(rw & REQ_WRITE);
7708
7709 bio_get(bio);
7710
7711 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7712 BTRFS_WQ_ENDIO_DIO_REPAIR);
7713 if (ret)
7714 goto err;
7715
7716 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7717err:
7718 bio_put(bio);
7719 return ret;
7720}
7721
7722static int btrfs_check_dio_repairable(struct inode *inode,
7723 struct bio *failed_bio,
7724 struct io_failure_record *failrec,
7725 int failed_mirror)
7726{
7727 int num_copies;
7728
7729 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7730 failrec->logical, failrec->len);
7731 if (num_copies == 1) {
7732 /*
7733 * we only have a single copy of the data, so don't bother with
7734 * all the retry and error correction code that follows. no
7735 * matter what the error is, it is very likely to persist.
7736 */
7737 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7738 num_copies, failrec->this_mirror, failed_mirror);
7739 return 0;
7740 }
7741
7742 failrec->failed_mirror = failed_mirror;
7743 failrec->this_mirror++;
7744 if (failrec->this_mirror == failed_mirror)
7745 failrec->this_mirror++;
7746
7747 if (failrec->this_mirror > num_copies) {
7748 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7749 num_copies, failrec->this_mirror, failed_mirror);
7750 return 0;
7751 }
7752
7753 return 1;
7754}
7755
7756static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7757 struct page *page, u64 start, u64 end,
7758 int failed_mirror, bio_end_io_t *repair_endio,
7759 void *repair_arg)
7760{
7761 struct io_failure_record *failrec;
7762 struct bio *bio;
7763 int isector;
7764 int read_mode;
7765 int ret;
7766
7767 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7768
7769 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7770 if (ret)
7771 return ret;
7772
7773 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7774 failed_mirror);
7775 if (!ret) {
7776 free_io_failure(inode, failrec);
7777 return -EIO;
7778 }
7779
7780 if (failed_bio->bi_vcnt > 1)
7781 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7782 else
7783 read_mode = READ_SYNC;
7784
7785 isector = start - btrfs_io_bio(failed_bio)->logical;
7786 isector >>= inode->i_sb->s_blocksize_bits;
7787 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7788 0, isector, repair_endio, repair_arg);
7789 if (!bio) {
7790 free_io_failure(inode, failrec);
7791 return -EIO;
7792 }
7793
7794 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7795 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7796 read_mode, failrec->this_mirror, failrec->in_validation);
7797
7798 ret = submit_dio_repair_bio(inode, bio, read_mode,
7799 failrec->this_mirror);
7800 if (ret) {
7801 free_io_failure(inode, failrec);
7802 bio_put(bio);
7803 }
7804
7805 return ret;
7806}
7807
7808struct btrfs_retry_complete {
7809 struct completion done;
7810 struct inode *inode;
7811 u64 start;
7812 int uptodate;
7813};
7814
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007815static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007816{
7817 struct btrfs_retry_complete *done = bio->bi_private;
7818 struct bio_vec *bvec;
7819 int i;
7820
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007821 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007822 goto end;
7823
7824 done->uptodate = 1;
7825 bio_for_each_segment_all(bvec, bio, i)
7826 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7827end:
7828 complete(&done->done);
7829 bio_put(bio);
7830}
7831
7832static int __btrfs_correct_data_nocsum(struct inode *inode,
7833 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007834{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007835 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007836 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007837 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007838 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007839 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007840
Miao Xiec1dc0892014-09-12 18:43:56 +08007841 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007842 done.inode = inode;
7843
7844 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7845try_again:
7846 done.uptodate = 0;
7847 done.start = start;
7848 init_completion(&done.done);
7849
7850 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7851 start + bvec->bv_len - 1,
7852 io_bio->mirror_num,
7853 btrfs_retry_endio_nocsum, &done);
7854 if (ret)
7855 return ret;
7856
7857 wait_for_completion(&done.done);
7858
7859 if (!done.uptodate) {
7860 /* We might have another mirror, so try again */
7861 goto try_again;
7862 }
7863
7864 start += bvec->bv_len;
7865 }
7866
7867 return 0;
7868}
7869
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007870static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007871{
7872 struct btrfs_retry_complete *done = bio->bi_private;
7873 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7874 struct bio_vec *bvec;
7875 int uptodate;
7876 int ret;
7877 int i;
7878
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007879 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007880 goto end;
7881
7882 uptodate = 1;
7883 bio_for_each_segment_all(bvec, bio, i) {
7884 ret = __readpage_endio_check(done->inode, io_bio, i,
7885 bvec->bv_page, 0,
7886 done->start, bvec->bv_len);
7887 if (!ret)
7888 clean_io_failure(done->inode, done->start,
7889 bvec->bv_page, 0);
7890 else
7891 uptodate = 0;
7892 }
7893
7894 done->uptodate = uptodate;
7895end:
7896 complete(&done->done);
7897 bio_put(bio);
7898}
7899
7900static int __btrfs_subio_endio_read(struct inode *inode,
7901 struct btrfs_io_bio *io_bio, int err)
7902{
7903 struct bio_vec *bvec;
7904 struct btrfs_retry_complete done;
7905 u64 start;
7906 u64 offset = 0;
7907 int i;
7908 int ret;
7909
7910 err = 0;
7911 start = io_bio->logical;
7912 done.inode = inode;
7913
Miao Xiec1dc0892014-09-12 18:43:56 +08007914 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007915 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7916 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007917 if (likely(!ret))
7918 goto next;
7919try_again:
7920 done.uptodate = 0;
7921 done.start = start;
7922 init_completion(&done.done);
7923
7924 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7925 start + bvec->bv_len - 1,
7926 io_bio->mirror_num,
7927 btrfs_retry_endio, &done);
7928 if (ret) {
7929 err = ret;
7930 goto next;
7931 }
7932
7933 wait_for_completion(&done.done);
7934
7935 if (!done.uptodate) {
7936 /* We might have another mirror, so try again */
7937 goto try_again;
7938 }
7939next:
7940 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007941 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007942 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007943
7944 return err;
7945}
7946
Miao Xie8b110e32014-09-12 18:44:03 +08007947static int btrfs_subio_endio_read(struct inode *inode,
7948 struct btrfs_io_bio *io_bio, int err)
7949{
7950 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7951
7952 if (skip_csum) {
7953 if (unlikely(err))
7954 return __btrfs_correct_data_nocsum(inode, io_bio);
7955 else
7956 return 0;
7957 } else {
7958 return __btrfs_subio_endio_read(inode, io_bio, err);
7959 }
7960}
7961
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007962static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08007963{
7964 struct btrfs_dio_private *dip = bio->bi_private;
7965 struct inode *inode = dip->inode;
7966 struct bio *dio_bio;
7967 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007968 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08007969
Miao Xie8b110e32014-09-12 18:44:03 +08007970 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7971 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007972
Josef Bacik4b46fce2010-05-23 11:00:55 -04007973 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007974 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007975 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007976
Josef Bacik4b46fce2010-05-23 11:00:55 -04007977 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007978
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007979 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08007980
7981 if (io_bio->end_io)
7982 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007983 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007984}
7985
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007986static void btrfs_endio_direct_write(struct bio *bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007987{
7988 struct btrfs_dio_private *dip = bio->bi_private;
7989 struct inode *inode = dip->inode;
7990 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007991 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007992 u64 ordered_offset = dip->logical_offset;
7993 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007994 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007995 int ret;
7996
Chris Mason163cf092010-11-28 19:56:33 -05007997again:
7998 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7999 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008000 ordered_bytes,
8001 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008002 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008003 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008004
Liu Bo9e0af232014-08-15 23:36:53 +08008005 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8006 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08008007 btrfs_queue_work(root->fs_info->endio_write_workers,
8008 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008009out_test:
8010 /*
8011 * our bio might span multiple ordered extents. If we haven't
8012 * completed the accounting for the whole dio, go back and try again
8013 */
8014 if (ordered_offset < dip->logical_offset + dip->bytes) {
8015 ordered_bytes = dip->logical_offset + dip->bytes -
8016 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008017 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008018 goto again;
8019 }
Chris Mason9be33952013-05-17 18:30:14 -04008020 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008021
Josef Bacik4b46fce2010-05-23 11:00:55 -04008022 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008023
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008024 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008025 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008026}
8027
Chris Masoneaf25d92010-05-25 09:48:28 -04008028static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
8029 struct bio *bio, int mirror_num,
8030 unsigned long bio_flags, u64 offset)
8031{
8032 int ret;
8033 struct btrfs_root *root = BTRFS_I(inode)->root;
8034 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008035 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008036 return 0;
8037}
8038
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008039static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008040{
8041 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008042 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008043
Miao Xie8b110e32014-09-12 18:44:03 +08008044 if (err)
8045 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
8046 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
8047 btrfs_ino(dip->inode), bio->bi_rw,
8048 (unsigned long long)bio->bi_iter.bi_sector,
8049 bio->bi_iter.bi_size, err);
8050
8051 if (dip->subio_endio)
8052 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008053
8054 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008055 dip->errors = 1;
8056
8057 /*
8058 * before atomic variable goto zero, we must make sure
8059 * dip->errors is perceived to be set.
8060 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008061 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008062 }
8063
8064 /* if there are more bios still pending for this dio, just exit */
8065 if (!atomic_dec_and_test(&dip->pending_bios))
8066 goto out;
8067
Chris Mason9be33952013-05-17 18:30:14 -04008068 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008069 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008070 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008071 dip->dio_bio->bi_error = 0;
8072 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008073 }
8074out:
8075 bio_put(bio);
8076}
8077
8078static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8079 u64 first_sector, gfp_t gfp_flags)
8080{
Chris Masonda2f0f72015-07-02 13:57:22 -07008081 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008082 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008083 if (bio)
8084 bio_associate_current(bio);
8085 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008086}
8087
Miao Xiec1dc0892014-09-12 18:43:56 +08008088static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
8089 struct inode *inode,
8090 struct btrfs_dio_private *dip,
8091 struct bio *bio,
8092 u64 file_offset)
8093{
8094 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8095 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8096 int ret;
8097
8098 /*
8099 * We load all the csum data we need when we submit
8100 * the first bio to reduce the csum tree search and
8101 * contention.
8102 */
8103 if (dip->logical_offset == file_offset) {
8104 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
8105 file_offset);
8106 if (ret)
8107 return ret;
8108 }
8109
8110 if (bio == dip->orig_bio)
8111 return 0;
8112
8113 file_offset -= dip->logical_offset;
8114 file_offset >>= inode->i_sb->s_blocksize_bits;
8115 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8116
8117 return 0;
8118}
8119
Miao Xiee65e1532010-11-22 03:04:43 +00008120static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8121 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008122 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008123{
Miao Xiefacc8a222013-07-25 19:22:34 +08008124 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00008125 int write = rw & REQ_WRITE;
8126 struct btrfs_root *root = BTRFS_I(inode)->root;
8127 int ret;
8128
Josef Bacikb812ce22012-11-16 13:56:32 -05008129 if (async_submit)
8130 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8131
Miao Xiee65e1532010-11-22 03:04:43 +00008132 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008133
8134 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02008135 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8136 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008137 if (ret)
8138 goto err;
8139 }
Miao Xiee65e1532010-11-22 03:04:43 +00008140
Josef Bacik1ae39932011-04-06 14:41:34 -04008141 if (skip_sum)
8142 goto map;
8143
8144 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008145 ret = btrfs_wq_submit_bio(root->fs_info,
8146 inode, rw, bio, 0, 0,
8147 file_offset,
8148 __btrfs_submit_bio_start_direct_io,
8149 __btrfs_submit_bio_done);
8150 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008151 } else if (write) {
8152 /*
8153 * If we aren't doing async submit, calculate the csum of the
8154 * bio now.
8155 */
8156 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8157 if (ret)
8158 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008159 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008160 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8161 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008162 if (ret)
8163 goto err;
8164 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008165map:
8166 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008167err:
8168 bio_put(bio);
8169 return ret;
8170}
8171
8172static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8173 int skip_sum)
8174{
8175 struct inode *inode = dip->inode;
8176 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008177 struct bio *bio;
8178 struct bio *orig_bio = dip->orig_bio;
8179 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008180 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008181 u64 file_offset = dip->logical_offset;
8182 u64 submit_len = 0;
8183 u64 map_length;
8184 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08008185 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04008186 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008187
Kent Overstreet4f024f32013-10-11 15:44:27 -07008188 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008189 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008190 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008191 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008192 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008193
Kent Overstreet4f024f32013-10-11 15:44:27 -07008194 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008195 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008196 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008197 goto submit;
8198 }
8199
David Woodhouse53b381b2013-01-29 18:40:14 -05008200 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008201 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008202 async_submit = 0;
8203 else
8204 async_submit = 1;
8205
Josef Bacik02f57c72011-04-06 14:25:44 -04008206 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8207 if (!bio)
8208 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008209
Josef Bacik02f57c72011-04-06 14:25:44 -04008210 bio->bi_private = dip;
8211 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008212 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008213 atomic_inc(&dip->pending_bios);
8214
Miao Xiee65e1532010-11-22 03:04:43 +00008215 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02008216 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00008217 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02008218 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00008219 /*
8220 * inc the count before we submit the bio so
8221 * we know the end IO handler won't happen before
8222 * we inc the count. Otherwise, the dip might get freed
8223 * before we're done setting it up
8224 */
8225 atomic_inc(&dip->pending_bios);
8226 ret = __btrfs_submit_dio_bio(bio, inode, rw,
8227 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008228 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008229 if (ret) {
8230 bio_put(bio);
8231 atomic_dec(&dip->pending_bios);
8232 goto out_err;
8233 }
8234
Miao Xiee65e1532010-11-22 03:04:43 +00008235 start_sector += submit_len >> 9;
8236 file_offset += submit_len;
8237
8238 submit_len = 0;
8239 nr_pages = 0;
8240
8241 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8242 start_sector, GFP_NOFS);
8243 if (!bio)
8244 goto out_err;
8245 bio->bi_private = dip;
8246 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008247 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008248
Kent Overstreet4f024f32013-10-11 15:44:27 -07008249 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008250 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008251 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008252 &map_length, NULL, 0);
8253 if (ret) {
8254 bio_put(bio);
8255 goto out_err;
8256 }
8257 } else {
8258 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308259 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00008260 bvec++;
8261 }
8262 }
8263
Josef Bacik02f57c72011-04-06 14:25:44 -04008264submit:
Miao Xiee65e1532010-11-22 03:04:43 +00008265 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008266 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008267 if (!ret)
8268 return 0;
8269
8270 bio_put(bio);
8271out_err:
8272 dip->errors = 1;
8273 /*
8274 * before atomic variable goto zero, we must
8275 * make sure dip->errors is perceived to be set.
8276 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008277 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008278 if (atomic_dec_and_test(&dip->pending_bios))
8279 bio_io_error(dip->orig_bio);
8280
8281 /* bio_end_io() will handle error, so we needn't return it */
8282 return 0;
8283}
8284
Chris Mason9be33952013-05-17 18:30:14 -04008285static void btrfs_submit_direct(int rw, struct bio *dio_bio,
8286 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008287{
Filipe Manana61de7182015-07-01 12:13:10 +01008288 struct btrfs_dio_private *dip = NULL;
8289 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008290 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008291 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02008292 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008293 int ret = 0;
8294
8295 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8296
Chris Mason9be33952013-05-17 18:30:14 -04008297 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008298 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008299 ret = -ENOMEM;
8300 goto free_ordered;
8301 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008302
Miao Xiec1dc0892014-09-12 18:43:56 +08008303 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008304 if (!dip) {
8305 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008306 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008307 }
8308
8309 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008310 dip->inode = inode;
8311 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008312 dip->bytes = dio_bio->bi_iter.bi_size;
8313 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008314 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008315 dip->orig_bio = io_bio;
8316 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008317 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008318 btrfs_bio = btrfs_io_bio(io_bio);
8319 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008320
Miao Xiec1dc0892014-09-12 18:43:56 +08008321 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008322 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008323 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008324 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008325 dip->subio_endio = btrfs_subio_endio_read;
8326 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008327
Miao Xiee65e1532010-11-22 03:04:43 +00008328 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8329 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008330 return;
Chris Mason9be33952013-05-17 18:30:14 -04008331
Miao Xie23ea8e52014-09-12 18:43:54 +08008332 if (btrfs_bio->end_io)
8333 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008334
Josef Bacik4b46fce2010-05-23 11:00:55 -04008335free_ordered:
8336 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008337 * If we arrived here it means either we failed to submit the dip
8338 * or we either failed to clone the dio_bio or failed to allocate the
8339 * dip. If we cloned the dio_bio and allocated the dip, we can just
8340 * call bio_endio against our io_bio so that we get proper resource
8341 * cleanup if we fail to submit the dip, otherwise, we must do the
8342 * same as btrfs_endio_direct_[write|read] because we can't call these
8343 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008344 */
Filipe Manana61de7182015-07-01 12:13:10 +01008345 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008346 io_bio->bi_error = -EIO;
8347 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008348 /*
8349 * The end io callbacks free our dip, do the final put on io_bio
8350 * and all the cleanup and final put for dio_bio (through
8351 * dio_end_io()).
8352 */
8353 dip = NULL;
8354 io_bio = NULL;
8355 } else {
8356 if (write) {
8357 struct btrfs_ordered_extent *ordered;
8358
8359 ordered = btrfs_lookup_ordered_extent(inode,
8360 file_offset);
8361 set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
8362 /*
8363 * Decrements our ref on the ordered extent and removes
8364 * the ordered extent from the inode's ordered tree,
8365 * doing all the proper resource cleanup such as for the
8366 * reserved space and waking up any waiters for this
8367 * ordered extent (through btrfs_remove_ordered_extent).
8368 */
8369 btrfs_finish_ordered_io(ordered);
8370 } else {
8371 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8372 file_offset + dio_bio->bi_iter.bi_size - 1);
8373 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008374 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008375 /*
8376 * Releases and cleans up our dio_bio, no need to bio_put()
8377 * nor bio_endio()/bio_io_error() against dio_bio.
8378 */
8379 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008380 }
Filipe Manana61de7182015-07-01 12:13:10 +01008381 if (io_bio)
8382 bio_put(io_bio);
8383 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008384}
8385
Omar Sandoval6f673762015-03-16 04:33:52 -07008386static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008387 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008388{
8389 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008390 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008391 unsigned blocksize_mask = root->sectorsize - 1;
8392 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008393
8394 if (offset & blocksize_mask)
8395 goto out;
8396
Al Viro28060d52014-03-22 05:15:17 -04008397 if (iov_iter_alignment(iter) & blocksize_mask)
8398 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008399
Al Viro28060d52014-03-22 05:15:17 -04008400 /* If this is a write we don't need to check anymore */
Omar Sandoval6f673762015-03-16 04:33:52 -07008401 if (iov_iter_rw(iter) == WRITE)
Al Viro28060d52014-03-22 05:15:17 -04008402 return 0;
8403 /*
8404 * Check to make sure we don't have duplicate iov_base's in this
8405 * iovec, if so return EINVAL, otherwise we'll get csum errors
8406 * when reading back.
8407 */
8408 for (seg = 0; seg < iter->nr_segs; seg++) {
8409 for (i = seg + 1; i < iter->nr_segs; i++) {
8410 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008411 goto out;
8412 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008413 }
8414 retval = 0;
8415out:
8416 return retval;
8417}
Josef Bacikeb838e72012-07-31 16:28:48 -04008418
Omar Sandoval22c61862015-03-16 04:33:53 -07008419static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
8420 loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04008421{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008422 struct file *file = iocb->ki_filp;
8423 struct inode *inode = file->f_mapping->host;
chandan50745b02015-08-28 21:10:13 +05308424 struct btrfs_root *root = BTRFS_I(inode)->root;
8425 struct btrfs_dio_data dio_data = { 0 };
Miao Xie09348562013-02-07 10:12:07 +00008426 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008427 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008428 bool wakeup = true;
8429 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008430 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008431
Omar Sandoval6f673762015-03-16 04:33:52 -07008432 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008433 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008434
Jens Axboefe0f07d2015-04-15 17:05:48 -06008435 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008436 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008437
Josef Bacik0e267c42013-07-02 10:38:02 -04008438 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008439 * The generic stuff only does filemap_write_and_wait_range, which
8440 * isn't enough if we've written compressed pages to this area, so
8441 * we need to flush the dirty pages again to make absolutely sure
8442 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008443 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008444 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008445 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8446 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008447 filemap_fdatawrite_range(inode->i_mapping, offset,
8448 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008449
Omar Sandoval6f673762015-03-16 04:33:52 -07008450 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008451 /*
8452 * If the write DIO is beyond the EOF, we need update
8453 * the isize, but it is protected by i_mutex. So we can
8454 * not unlock the i_mutex at this case.
8455 */
8456 if (offset + count <= inode->i_size) {
8457 mutex_unlock(&inode->i_mutex);
8458 relock = true;
8459 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008460 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008461 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008462 goto out;
chandan50745b02015-08-28 21:10:13 +05308463 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008464 BTRFS_MAX_EXTENT_SIZE - 1,
8465 BTRFS_MAX_EXTENT_SIZE);
8466
8467 /*
8468 * We need to know how many extents we reserved so that we can
8469 * do the accounting properly if we go over the number we
8470 * originally calculated. Abuse current->journal_info for this.
8471 */
chandan50745b02015-08-28 21:10:13 +05308472 dio_data.reserve = round_up(count, root->sectorsize);
8473 current->journal_info = &dio_data;
David Sterbaee39b432014-09-30 01:33:33 +02008474 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8475 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008476 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008477 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8478 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008479 }
8480
Omar Sandoval17f8c842015-03-16 04:33:50 -07008481 ret = __blockdev_direct_IO(iocb, inode,
8482 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
8483 iter, offset, btrfs_get_blocks_direct, NULL,
8484 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008485 if (iov_iter_rw(iter) == WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008486 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008487 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308488 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008489 btrfs_delalloc_release_space(inode, offset,
8490 dio_data.reserve);
Liu Boddba1bf2015-06-17 16:59:58 +08008491 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008492 btrfs_delalloc_release_space(inode, offset,
8493 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008494 }
Miao Xie38851cc2013-02-08 07:04:11 +00008495out:
Miao Xie2e60a512013-02-08 07:01:08 +00008496 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008497 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008498 if (relock)
8499 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008500
8501 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008502}
8503
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008504#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8505
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008506static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8507 __u64 start, __u64 len)
8508{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008509 int ret;
8510
8511 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8512 if (ret)
8513 return ret;
8514
Chris Masonec29ed52011-02-23 16:23:20 -05008515 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008516}
8517
Chris Mason9ebefb182007-06-15 13:50:00 -04008518int btrfs_readpage(struct file *file, struct page *page)
8519{
Chris Masond1310b22008-01-24 16:13:08 -05008520 struct extent_io_tree *tree;
8521 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008522 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008523}
Chris Mason1832a6d2007-12-21 16:27:21 -05008524
Chris Mason39279cc2007-06-12 06:35:45 -04008525static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8526{
Chris Masond1310b22008-01-24 16:13:08 -05008527 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008528 struct inode *inode = page->mapping->host;
8529 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008530
8531 if (current->flags & PF_MEMALLOC) {
8532 redirty_page_for_writepage(wbc, page);
8533 unlock_page(page);
8534 return 0;
8535 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008536
8537 /*
8538 * If we are under memory pressure we will call this directly from the
8539 * VM, we need to make sure we have the inode referenced for the ordered
8540 * extent. If not just return like we didn't do anything.
8541 */
8542 if (!igrab(inode)) {
8543 redirty_page_for_writepage(wbc, page);
8544 return AOP_WRITEPAGE_ACTIVATE;
8545 }
Chris Masond1310b22008-01-24 16:13:08 -05008546 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008547 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8548 btrfs_add_delayed_iput(inode);
8549 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008550}
Chris Mason39279cc2007-06-12 06:35:45 -04008551
Eric Sandeen48a3b632013-04-25 20:41:01 +00008552static int btrfs_writepages(struct address_space *mapping,
8553 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008554{
Chris Masond1310b22008-01-24 16:13:08 -05008555 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008556
Chris Masond1310b22008-01-24 16:13:08 -05008557 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008558 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8559}
8560
Chris Mason3ab2fb52007-11-08 10:59:22 -05008561static int
8562btrfs_readpages(struct file *file, struct address_space *mapping,
8563 struct list_head *pages, unsigned nr_pages)
8564{
Chris Masond1310b22008-01-24 16:13:08 -05008565 struct extent_io_tree *tree;
8566 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008567 return extent_readpages(tree, mapping, pages, nr_pages,
8568 btrfs_get_extent);
8569}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008570static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008571{
Chris Masond1310b22008-01-24 16:13:08 -05008572 struct extent_io_tree *tree;
8573 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008574 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008575
Chris Masond1310b22008-01-24 16:13:08 -05008576 tree = &BTRFS_I(page->mapping->host)->io_tree;
8577 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008578 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008579 if (ret == 1) {
8580 ClearPagePrivate(page);
8581 set_page_private(page, 0);
8582 page_cache_release(page);
8583 }
8584 return ret;
8585}
Chris Mason39279cc2007-06-12 06:35:45 -04008586
Chris Masone6dcd2d2008-07-17 12:53:50 -04008587static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8588{
Chris Mason98509cf2008-09-11 15:51:43 -04008589 if (PageWriteback(page) || PageDirty(page))
8590 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008591 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008592}
8593
Lukas Czernerd47992f2013-05-21 23:17:23 -04008594static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8595 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008596{
Josef Bacik5fd02042012-05-02 14:00:54 -04008597 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008598 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008599 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008600 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008601 u64 page_start = page_offset(page);
8602 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008603 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008604
Chris Mason8b62b722009-09-02 16:53:46 -04008605 /*
8606 * we have the page locked, so new writeback can't start,
8607 * and the dirty bit won't be cleared while we are here.
8608 *
8609 * Wait for IO on this page so that we can safely clear
8610 * the PagePrivate2 bit and do ordered accounting
8611 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008612 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008613
Josef Bacik5fd02042012-05-02 14:00:54 -04008614 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008615 if (offset) {
8616 btrfs_releasepage(page, GFP_NOFS);
8617 return;
8618 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008619
8620 if (!inode_evicting)
8621 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8622 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008623 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008624 /*
8625 * IO on this page will never be started, so we need
8626 * to account for any ordered extents now
8627 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008628 if (!inode_evicting)
8629 clear_extent_bit(tree, page_start, page_end,
8630 EXTENT_DIRTY | EXTENT_DELALLOC |
8631 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8632 EXTENT_DEFRAG, 1, 0, &cached_state,
8633 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008634 /*
8635 * whoever cleared the private bit is responsible
8636 * for the finish_ordered_io
8637 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008638 if (TestClearPagePrivate2(page)) {
8639 struct btrfs_ordered_inode_tree *tree;
8640 u64 new_len;
8641
8642 tree = &BTRFS_I(inode)->ordered_tree;
8643
8644 spin_lock_irq(&tree->lock);
8645 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8646 new_len = page_start - ordered->file_offset;
8647 if (new_len < ordered->truncated_len)
8648 ordered->truncated_len = new_len;
8649 spin_unlock_irq(&tree->lock);
8650
8651 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8652 page_start,
8653 PAGE_CACHE_SIZE, 1))
8654 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008655 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008656 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008657 if (!inode_evicting) {
8658 cached_state = NULL;
8659 lock_extent_bits(tree, page_start, page_end, 0,
8660 &cached_state);
8661 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008662 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008663
Qu Wenruob9d0b382015-09-29 10:35:16 +08008664 /*
8665 * Qgroup reserved space handler
8666 * Page here will be either
8667 * 1) Already written to disk
8668 * In this case, its reserved space is released from data rsv map
8669 * and will be freed by delayed_ref handler finally.
8670 * So even we call qgroup_free_data(), it won't decrease reserved
8671 * space.
8672 * 2) Not written to disk
8673 * This means the reserved space should be freed here.
8674 */
8675 btrfs_qgroup_free_data(inode, page_start, PAGE_CACHE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008676 if (!inode_evicting) {
8677 clear_extent_bit(tree, page_start, page_end,
8678 EXTENT_LOCKED | EXTENT_DIRTY |
8679 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8680 EXTENT_DEFRAG, 1, 1,
8681 &cached_state, GFP_NOFS);
8682
8683 __btrfs_releasepage(page, GFP_NOFS);
8684 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008685
Chris Mason4a096752008-07-21 10:29:44 -04008686 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008687 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008688 ClearPagePrivate(page);
8689 set_page_private(page, 0);
8690 page_cache_release(page);
8691 }
Chris Mason39279cc2007-06-12 06:35:45 -04008692}
8693
Chris Mason9ebefb182007-06-15 13:50:00 -04008694/*
8695 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8696 * called from a page fault handler when a page is first dirtied. Hence we must
8697 * be careful to check for EOF conditions here. We set the page up correctly
8698 * for a written page which means we get ENOSPC checking when writing into
8699 * holes and correct delalloc and unwritten extent mapping on filesystems that
8700 * support these features.
8701 *
8702 * We are not allowed to take the i_mutex here so we have to play games to
8703 * protect against truncate races as the page could now be beyond EOF. Because
8704 * vmtruncate() writes the inode size before removing pages, once we have the
8705 * page lock we can determine safely if the page is beyond EOF. If it is not
8706 * beyond EOF, then the page is guaranteed safe against truncation until we
8707 * unlock the page.
8708 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008709int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008710{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008711 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008712 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008713 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008714 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8715 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008716 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008717 char *kaddr;
8718 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008719 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008720 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008721 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008722 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008723 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008724
Jan Karab2b5ef52012-06-12 16:20:45 +02008725 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008726 page_start = page_offset(page);
8727 page_end = page_start + PAGE_CACHE_SIZE - 1;
8728
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008729 ret = btrfs_delalloc_reserve_space(inode, page_start,
8730 PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008731 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008732 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008733 reserved = 1;
8734 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008735 if (ret) {
8736 if (ret == -ENOMEM)
8737 ret = VM_FAULT_OOM;
8738 else /* -ENOSPC, -EIO, etc */
8739 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008740 if (reserved)
8741 goto out;
8742 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008743 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008744
Nick Piggin56a76f82009-03-31 15:23:23 -07008745 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008746again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008747 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008748 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008749
Chris Mason9ebefb182007-06-15 13:50:00 -04008750 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008751 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008752 /* page got truncated out from underneath us */
8753 goto out_unlock;
8754 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008755 wait_on_page_writeback(page);
8756
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008757 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008758 set_page_extent_mapped(page);
8759
Chris Masoneb84ae02008-07-17 13:53:27 -04008760 /*
8761 * we can't set the delalloc bits if there are pending ordered
8762 * extents. Drop our locks and wait for them to finish
8763 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008764 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8765 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008766 unlock_extent_cached(io_tree, page_start, page_end,
8767 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008768 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008769 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008770 btrfs_put_ordered_extent(ordered);
8771 goto again;
8772 }
8773
Josef Bacikfbf19082009-10-01 17:10:23 -04008774 /*
8775 * XXX - page_mkwrite gets called every time the page is dirtied, even
8776 * if it was already dirty, so for space accounting reasons we need to
8777 * clear any delalloc bits for the range we are fixing to save. There
8778 * is probably a better way to do this, but for now keep consistent with
8779 * prepare_pages in the normal write path.
8780 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008781 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008782 EXTENT_DIRTY | EXTENT_DELALLOC |
8783 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008784 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008785
Josef Bacik2ac55d42010-02-03 19:33:23 +00008786 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8787 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008788 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008789 unlock_extent_cached(io_tree, page_start, page_end,
8790 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008791 ret = VM_FAULT_SIGBUS;
8792 goto out_unlock;
8793 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008794 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008795
8796 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008797 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008798 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008799 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008800 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008801
Chris Masone6dcd2d2008-07-17 12:53:50 -04008802 if (zero_start != PAGE_CACHE_SIZE) {
8803 kaddr = kmap(page);
8804 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8805 flush_dcache_page(page);
8806 kunmap(page);
8807 }
Chris Mason247e7432008-07-17 12:53:51 -04008808 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008809 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008810 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008811
Chris Mason257c62e2009-10-13 13:21:08 -04008812 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8813 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008814 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008815
Josef Bacik2ac55d42010-02-03 19:33:23 +00008816 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008817
8818out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008819 if (!ret) {
8820 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008821 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008822 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008823 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008824out:
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008825 btrfs_delalloc_release_space(inode, page_start, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008826out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008827 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008828 return ret;
8829}
8830
Josef Bacika41ad392011-01-31 15:30:16 -05008831static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008832{
8833 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008834 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008835 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008836 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008837 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008838 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008839 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008840
Josef Bacik0ef8b722013-10-25 16:13:35 -04008841 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8842 (u64)-1);
8843 if (ret)
8844 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008845
Josef Bacikfcb80c22011-05-03 10:40:22 -04008846 /*
8847 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8848 * 3 things going on here
8849 *
8850 * 1) We need to reserve space for our orphan item and the space to
8851 * delete our orphan item. Lord knows we don't want to have a dangling
8852 * orphan item because we didn't reserve space to remove it.
8853 *
8854 * 2) We need to reserve space to update our inode.
8855 *
8856 * 3) We need to have something to cache all the space that is going to
8857 * be free'd up by the truncate operation, but also have some slack
8858 * space reserved in case it uses space during the truncate (thank you
8859 * very much snapshotting).
8860 *
8861 * And we need these to all be seperate. The fact is we can use alot of
8862 * space doing the truncate, and we have no earthly idea how much space
8863 * we will use, so we need the truncate reservation to be seperate so it
8864 * doesn't end up using space reserved for updating the inode or
8865 * removing the orphan item. We also need to be able to stop the
8866 * transaction and start a new one, which means we need to be able to
8867 * update the inode several times, and we have no idea of knowing how
8868 * many times that will be, so we can't just reserve 1 item for the
8869 * entirety of the opration, so that has to be done seperately as well.
8870 * Then there is the orphan item, which does indeed need to be held on
8871 * to for the whole operation, and we need nobody to touch this reserved
8872 * space except the orphan code.
8873 *
8874 * So that leaves us with
8875 *
8876 * 1) root->orphan_block_rsv - for the orphan deletion.
8877 * 2) rsv - for the truncate reservation, which we will steal from the
8878 * transaction reservation.
8879 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8880 * updating the inode.
8881 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008882 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008883 if (!rsv)
8884 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008885 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008886 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008887
Josef Bacik907cbce2011-08-08 13:46:15 -04008888 /*
Josef Bacik07127182011-08-19 10:29:59 -04008889 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008890 * 1 for updating the inode.
8891 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008892 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008893 if (IS_ERR(trans)) {
8894 err = PTR_ERR(trans);
8895 goto out;
8896 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008897
Josef Bacik907cbce2011-08-08 13:46:15 -04008898 /* Migrate the slack space for the truncate to our reserve */
8899 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8900 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008901 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008902
Chris Mason5a3f23d2009-03-31 13:27:11 -04008903 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008904 * So if we truncate and then write and fsync we normally would just
8905 * write the extents that changed, which is a problem if we need to
8906 * first truncate that entire inode. So set this flag so we write out
8907 * all of the extents in the inode to the sync log so we're completely
8908 * safe.
8909 */
8910 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008911 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008912
Yan, Zheng80825102009-11-12 09:35:36 +00008913 while (1) {
8914 ret = btrfs_truncate_inode_items(trans, root, inode,
8915 inode->i_size,
8916 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08008917 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05008918 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008919 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008920 }
Chris Mason39279cc2007-06-12 06:35:45 -04008921
Josef Bacikfcb80c22011-05-03 10:40:22 -04008922 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008923 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008924 if (ret) {
8925 err = ret;
8926 break;
8927 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008928
Yan, Zheng80825102009-11-12 09:35:36 +00008929 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008930 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008931
8932 trans = btrfs_start_transaction(root, 2);
8933 if (IS_ERR(trans)) {
8934 ret = err = PTR_ERR(trans);
8935 trans = NULL;
8936 break;
8937 }
8938
8939 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8940 rsv, min_size);
8941 BUG_ON(ret); /* shouldn't happen */
8942 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008943 }
8944
8945 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008946 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008947 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008948 if (ret)
8949 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008950 }
8951
Chris Mason917c16b2011-11-08 14:49:59 -05008952 if (trans) {
8953 trans->block_rsv = &root->fs_info->trans_block_rsv;
8954 ret = btrfs_update_inode(trans, root, inode);
8955 if (ret && !err)
8956 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008957
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008958 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008959 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008960 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008961
8962out:
8963 btrfs_free_block_rsv(root, rsv);
8964
Josef Bacik3893e332011-01-31 16:03:11 -05008965 if (ret && !err)
8966 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008967
Josef Bacik3893e332011-01-31 16:03:11 -05008968 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008969}
8970
Sven Wegener3b963622008-06-09 21:57:42 -04008971/*
Chris Masond352ac62008-09-29 15:18:18 -04008972 * create a new subvolume directory/inode (helper for the ioctl).
8973 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008974int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008975 struct btrfs_root *new_root,
8976 struct btrfs_root *parent_root,
8977 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008978{
Chris Mason39279cc2007-06-12 06:35:45 -04008979 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008980 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008981 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008982
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008983 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8984 new_dirid, new_dirid,
8985 S_IFDIR | (~current_umask() & S_IRWXUGO),
8986 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008987 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008988 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008989 inode->i_op = &btrfs_dir_inode_operations;
8990 inode->i_fop = &btrfs_dir_file_operations;
8991
Miklos Szeredibfe86842011-10-28 14:13:29 +02008992 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008993 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008994 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008995
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008996 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8997 if (err)
8998 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008999 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009000 new_root->root_key.objectid, err);
9001
Yan, Zheng76dda932009-09-21 16:00:26 -04009002 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009003
Yan, Zheng76dda932009-09-21 16:00:26 -04009004 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009005 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009006}
9007
Chris Mason39279cc2007-06-12 06:35:45 -04009008struct inode *btrfs_alloc_inode(struct super_block *sb)
9009{
9010 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009011 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009012
9013 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9014 if (!ei)
9015 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009016
9017 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009018 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009019 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009020 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009021 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009022 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009023 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009024 ei->disk_i_size = 0;
9025 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009026 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009027 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009028 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009029 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009030 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009031 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009032
Josef Bacik9e0baf62011-07-15 15:16:44 +00009033 spin_lock_init(&ei->lock);
9034 ei->outstanding_extents = 0;
9035 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009036
Josef Bacik72ac3c02012-05-23 14:13:11 -04009037 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009038 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009039
Miao Xie16cdcec2011-04-22 18:12:22 +08009040 ei->delayed_node = NULL;
9041
chandan r9cc97d62012-07-04 12:48:07 +05309042 ei->i_otime.tv_sec = 0;
9043 ei->i_otime.tv_nsec = 0;
9044
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009045 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009046 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009047 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9048 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009049 ei->io_tree.track_uptodate = 1;
9050 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009051 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009052 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009053 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009054 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009055 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009056 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009057 RB_CLEAR_NODE(&ei->rb_node);
9058
9059 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009060}
9061
Josef Bacikaaedb552013-10-11 14:44:09 -04009062#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9063void btrfs_test_destroy_inode(struct inode *inode)
9064{
9065 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9066 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9067}
9068#endif
9069
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009070static void btrfs_i_callback(struct rcu_head *head)
9071{
9072 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009073 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9074}
9075
Chris Mason39279cc2007-06-12 06:35:45 -04009076void btrfs_destroy_inode(struct inode *inode)
9077{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009078 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009079 struct btrfs_root *root = BTRFS_I(inode)->root;
9080
Al Virob3d9b7a2012-06-09 13:51:19 -04009081 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009082 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009083 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9084 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009085 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9086 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009087 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009088
Chris Mason5a3f23d2009-03-31 13:27:11 -04009089 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009090 * This can happen where we create an inode, but somebody else also
9091 * created the same inode and we need to destroy the one we already
9092 * created.
9093 */
9094 if (!root)
9095 goto free;
9096
Josef Bacik8a35d952012-05-23 14:26:42 -04009097 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9098 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009099 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009100 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009101 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009102 }
Josef Bacik7b128762008-07-24 12:17:14 -04009103
Chris Masond3977122009-01-05 21:25:51 -05009104 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009105 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9106 if (!ordered)
9107 break;
9108 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009109 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009110 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009111 btrfs_remove_ordered_extent(inode, ordered);
9112 btrfs_put_ordered_extent(ordered);
9113 btrfs_put_ordered_extent(ordered);
9114 }
9115 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009116 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009117 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009118 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009119free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009120 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009121}
9122
Al Viro45321ac2010-06-07 13:43:19 -04009123int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009124{
9125 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009126
Naohiro Aota6379ef92013-06-06 09:56:34 +00009127 if (root == NULL)
9128 return 1;
9129
Liu Bofa6ac872013-02-20 14:10:23 +00009130 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009131 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009132 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009133 else
Al Viro45321ac2010-06-07 13:43:19 -04009134 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009135}
9136
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009137static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009138{
9139 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9140
9141 inode_init_once(&ei->vfs_inode);
9142}
9143
9144void btrfs_destroy_cachep(void)
9145{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009146 /*
9147 * Make sure all delayed rcu free inodes are flushed before we
9148 * destroy cache.
9149 */
9150 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04009151 if (btrfs_inode_cachep)
9152 kmem_cache_destroy(btrfs_inode_cachep);
9153 if (btrfs_trans_handle_cachep)
9154 kmem_cache_destroy(btrfs_trans_handle_cachep);
9155 if (btrfs_transaction_cachep)
9156 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009157 if (btrfs_path_cachep)
9158 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05009159 if (btrfs_free_space_cachep)
9160 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009161}
9162
9163int btrfs_init_cachep(void)
9164{
David Sterba837e1972012-09-07 03:00:48 -06009165 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009166 sizeof(struct btrfs_inode), 0,
9167 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009168 if (!btrfs_inode_cachep)
9169 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009170
David Sterba837e1972012-09-07 03:00:48 -06009171 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009172 sizeof(struct btrfs_trans_handle), 0,
9173 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009174 if (!btrfs_trans_handle_cachep)
9175 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009176
David Sterba837e1972012-09-07 03:00:48 -06009177 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009178 sizeof(struct btrfs_transaction), 0,
9179 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009180 if (!btrfs_transaction_cachep)
9181 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009182
David Sterba837e1972012-09-07 03:00:48 -06009183 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009184 sizeof(struct btrfs_path), 0,
9185 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009186 if (!btrfs_path_cachep)
9187 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009188
David Sterba837e1972012-09-07 03:00:48 -06009189 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009190 sizeof(struct btrfs_free_space), 0,
9191 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
9192 if (!btrfs_free_space_cachep)
9193 goto fail;
9194
Chris Mason39279cc2007-06-12 06:35:45 -04009195 return 0;
9196fail:
9197 btrfs_destroy_cachep();
9198 return -ENOMEM;
9199}
9200
9201static int btrfs_getattr(struct vfsmount *mnt,
9202 struct dentry *dentry, struct kstat *stat)
9203{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009204 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009205 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009206 u32 blocksize = inode->i_sb->s_blocksize;
9207
Chris Mason39279cc2007-06-12 06:35:45 -04009208 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009209 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05009210 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009211
9212 spin_lock(&BTRFS_I(inode)->lock);
9213 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9214 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009215 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009216 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009217 return 0;
9218}
9219
Chris Masond3977122009-01-05 21:25:51 -05009220static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9221 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009222{
9223 struct btrfs_trans_handle *trans;
9224 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009225 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009226 struct inode *new_inode = d_inode(new_dentry);
9227 struct inode *old_inode = d_inode(old_dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04009228 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009229 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009230 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009231 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009232 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009233
Li Zefan33345d012011-04-20 10:31:50 +08009234 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009235 return -EPERM;
9236
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009237 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009238 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009239 return -EXDEV;
9240
Li Zefan33345d012011-04-20 10:31:50 +08009241 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9242 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009243 return -ENOTEMPTY;
9244
Chris Mason39279cc2007-06-12 06:35:45 -04009245 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009246 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009247 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009248
9249
9250 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009251 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009252 new_dentry->d_name.name,
9253 new_dentry->d_name.len);
9254
9255 if (ret) {
9256 if (ret == -EEXIST) {
9257 /* we shouldn't get
9258 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309259 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009260 return ret;
9261 }
9262 } else {
9263 /* maybe -EOVERFLOW */
9264 return ret;
9265 }
9266 }
9267 ret = 0;
9268
Chris Mason5a3f23d2009-03-31 13:27:11 -04009269 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009270 * we're using rename to replace one file with another. Start IO on it
9271 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009272 */
Chris Mason8d875f92014-08-12 10:47:42 -07009273 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009274 filemap_flush(old_inode->i_mapping);
9275
Yan, Zheng76dda932009-09-21 16:00:26 -04009276 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009277 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009278 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009279 /*
9280 * We want to reserve the absolute worst case amount of items. So if
9281 * both inodes are subvols and we need to unlink them then that would
9282 * require 4 item modifications, but if they are both normal inodes it
9283 * would require 5 item modifications, so we'll assume their normal
9284 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9285 * should cover the worst case number of items we'll modify.
9286 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04009287 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009288 if (IS_ERR(trans)) {
9289 ret = PTR_ERR(trans);
9290 goto out_notrans;
9291 }
Chris Mason5f39d392007-10-15 16:14:19 -04009292
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009293 if (dest != root)
9294 btrfs_record_root_in_trans(trans, dest);
9295
Yan, Zhenga5719522009-09-24 09:17:31 -04009296 ret = btrfs_set_inode_index(new_dir, &index);
9297 if (ret)
9298 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009299
Miao Xie67de1172013-12-26 13:07:06 +08009300 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009301 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009302 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009303 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009304 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009305 ret = btrfs_insert_inode_ref(trans, dest,
9306 new_dentry->d_name.name,
9307 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009308 old_ino,
9309 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009310 if (ret)
9311 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009312 /*
9313 * this is an ugly little race, but the rename is required
9314 * to make sure that if we crash, the inode is either at the
9315 * old name or the new one. pinning the log transaction lets
9316 * us make sure we don't allow a log commit to come in after
9317 * we unlink the name but before we add the new name back in.
9318 */
9319 btrfs_pin_log_trans(root);
9320 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009321
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009322 inode_inc_iversion(old_dir);
9323 inode_inc_iversion(new_dir);
9324 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009325 old_dir->i_ctime = old_dir->i_mtime = ctime;
9326 new_dir->i_ctime = new_dir->i_mtime = ctime;
9327 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04009328
Chris Mason12fcfd22009-03-24 10:24:20 -04009329 if (old_dentry->d_parent != new_dentry->d_parent)
9330 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9331
Li Zefan33345d012011-04-20 10:31:50 +08009332 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009333 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9334 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9335 old_dentry->d_name.name,
9336 old_dentry->d_name.len);
9337 } else {
Al Viro92986792011-03-04 17:14:37 +00009338 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009339 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009340 old_dentry->d_name.name,
9341 old_dentry->d_name.len);
9342 if (!ret)
9343 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009344 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009345 if (ret) {
9346 btrfs_abort_transaction(trans, root, ret);
9347 goto out_fail;
9348 }
Chris Mason39279cc2007-06-12 06:35:45 -04009349
9350 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009351 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009352 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08009353 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009354 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9355 root_objectid = BTRFS_I(new_inode)->location.objectid;
9356 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9357 root_objectid,
9358 new_dentry->d_name.name,
9359 new_dentry->d_name.len);
9360 BUG_ON(new_inode->i_nlink == 0);
9361 } else {
9362 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009363 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009364 new_dentry->d_name.name,
9365 new_dentry->d_name.len);
9366 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009367 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009368 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009369 if (ret) {
9370 btrfs_abort_transaction(trans, root, ret);
9371 goto out_fail;
9372 }
Chris Mason39279cc2007-06-12 06:35:45 -04009373 }
Josef Bacikaec74772008-07-24 12:12:38 -04009374
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009375 ret = btrfs_add_link(trans, new_dir, old_inode,
9376 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009377 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009378 if (ret) {
9379 btrfs_abort_transaction(trans, root, ret);
9380 goto out_fail;
9381 }
Chris Mason39279cc2007-06-12 06:35:45 -04009382
Miao Xie67de1172013-12-26 13:07:06 +08009383 if (old_inode->i_nlink == 1)
9384 BTRFS_I(old_inode)->dir_index = index;
9385
Li Zefan33345d012011-04-20 10:31:50 +08009386 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04009387 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00009388 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009389 btrfs_end_log_trans(root);
9390 }
Chris Mason39279cc2007-06-12 06:35:45 -04009391out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009392 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009393out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009394 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009395 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009396
Chris Mason39279cc2007-06-12 06:35:45 -04009397 return ret;
9398}
9399
Miklos Szeredi80ace852014-07-23 15:15:32 +02009400static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9401 struct inode *new_dir, struct dentry *new_dentry,
9402 unsigned int flags)
9403{
9404 if (flags & ~RENAME_NOREPLACE)
9405 return -EINVAL;
9406
9407 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
9408}
9409
Miao Xie8ccf6f192012-10-25 09:28:04 +00009410static void btrfs_run_delalloc_work(struct btrfs_work *work)
9411{
9412 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009413 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009414
9415 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9416 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009417 inode = delalloc_work->inode;
9418 if (delalloc_work->wait) {
9419 btrfs_wait_ordered_range(inode, 0, (u64)-1);
9420 } else {
9421 filemap_flush(inode->i_mapping);
9422 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9423 &BTRFS_I(inode)->runtime_flags))
9424 filemap_flush(inode->i_mapping);
9425 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00009426
9427 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009428 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009429 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009430 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009431 complete(&delalloc_work->completion);
9432}
9433
9434struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
9435 int wait, int delay_iput)
9436{
9437 struct btrfs_delalloc_work *work;
9438
David Sterba100d5702015-12-08 14:39:32 +01009439 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009440 if (!work)
9441 return NULL;
9442
9443 init_completion(&work->completion);
9444 INIT_LIST_HEAD(&work->list);
9445 work->inode = inode;
9446 work->wait = wait;
9447 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009448 WARN_ON_ONCE(!inode);
9449 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9450 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009451
9452 return work;
9453}
9454
9455void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9456{
9457 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +01009458 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009459}
9460
Chris Masond352ac62008-09-29 15:18:18 -04009461/*
9462 * some fairly slow code that needs optimization. This walks the list
9463 * of all the inodes with pending delalloc and forces them to disk.
9464 */
Miao Xie6c255e62014-03-06 13:55:01 +08009465static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9466 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009467{
Chris Masonea8c2812008-08-04 23:17:27 -04009468 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009469 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009470 struct btrfs_delalloc_work *work, *next;
9471 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009472 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009473 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009474
Miao Xie8ccf6f192012-10-25 09:28:04 +00009475 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009476 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009477
Miao Xie573bfb72014-03-06 13:55:03 +08009478 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009479 spin_lock(&root->delalloc_lock);
9480 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009481 while (!list_empty(&splice)) {
9482 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009483 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009484
Miao Xieeb73c1b2013-05-15 07:48:22 +00009485 list_move_tail(&binode->delalloc_inodes,
9486 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009487 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009488 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009489 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009490 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009491 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009492 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009493
9494 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009495 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009496 if (delay_iput)
9497 btrfs_add_delayed_iput(inode);
9498 else
9499 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009500 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009501 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009502 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009503 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009504 btrfs_queue_work(root->fs_info->flush_workers,
9505 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009506 ret++;
9507 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009508 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009509 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009510 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009511 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009512 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009513
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009514out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009515 list_for_each_entry_safe(work, next, &works, list) {
9516 list_del_init(&work->list);
9517 btrfs_wait_and_free_delalloc_work(work);
9518 }
9519
Miao Xieeb73c1b2013-05-15 07:48:22 +00009520 if (!list_empty_careful(&splice)) {
9521 spin_lock(&root->delalloc_lock);
9522 list_splice_tail(&splice, &root->delalloc_inodes);
9523 spin_unlock(&root->delalloc_lock);
9524 }
Miao Xie573bfb72014-03-06 13:55:03 +08009525 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009526 return ret;
9527}
9528
9529int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9530{
9531 int ret;
9532
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009533 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009534 return -EROFS;
9535
Miao Xie6c255e62014-03-06 13:55:01 +08009536 ret = __start_delalloc_inodes(root, delay_iput, -1);
9537 if (ret > 0)
9538 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009539 /*
9540 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009541 * we have to make sure the IO is actually started and that
9542 * ordered extents get created before we return
9543 */
9544 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009545 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009546 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009547 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009548 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9549 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009550 }
9551 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009552 return ret;
9553}
9554
Miao Xie6c255e62014-03-06 13:55:01 +08009555int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9556 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009557{
9558 struct btrfs_root *root;
9559 struct list_head splice;
9560 int ret;
9561
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009562 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009563 return -EROFS;
9564
9565 INIT_LIST_HEAD(&splice);
9566
Miao Xie573bfb72014-03-06 13:55:03 +08009567 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009568 spin_lock(&fs_info->delalloc_root_lock);
9569 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009570 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009571 root = list_first_entry(&splice, struct btrfs_root,
9572 delalloc_root);
9573 root = btrfs_grab_fs_root(root);
9574 BUG_ON(!root);
9575 list_move_tail(&root->delalloc_root,
9576 &fs_info->delalloc_roots);
9577 spin_unlock(&fs_info->delalloc_root_lock);
9578
Miao Xie6c255e62014-03-06 13:55:01 +08009579 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009580 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009581 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009582 goto out;
9583
Miao Xie6c255e62014-03-06 13:55:01 +08009584 if (nr != -1) {
9585 nr -= ret;
9586 WARN_ON(nr < 0);
9587 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009588 spin_lock(&fs_info->delalloc_root_lock);
9589 }
9590 spin_unlock(&fs_info->delalloc_root_lock);
9591
Miao Xie6c255e62014-03-06 13:55:01 +08009592 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009593 atomic_inc(&fs_info->async_submit_draining);
9594 while (atomic_read(&fs_info->nr_async_submits) ||
9595 atomic_read(&fs_info->async_delalloc_pages)) {
9596 wait_event(fs_info->async_submit_wait,
9597 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9598 atomic_read(&fs_info->async_delalloc_pages) == 0));
9599 }
9600 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009601out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009602 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009603 spin_lock(&fs_info->delalloc_root_lock);
9604 list_splice_tail(&splice, &fs_info->delalloc_roots);
9605 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009606 }
Miao Xie573bfb72014-03-06 13:55:03 +08009607 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009608 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009609}
9610
Chris Mason39279cc2007-06-12 06:35:45 -04009611static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9612 const char *symname)
9613{
9614 struct btrfs_trans_handle *trans;
9615 struct btrfs_root *root = BTRFS_I(dir)->root;
9616 struct btrfs_path *path;
9617 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009618 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009619 int err;
9620 int drop_inode = 0;
9621 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309622 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009623 int name_len;
9624 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009625 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009626 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009627 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009628
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009629 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009630 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9631 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009632
Josef Bacik9ed74f22009-09-11 16:12:44 -04009633 /*
9634 * 2 items for inode item and ref
9635 * 2 items for dir items
9636 * 1 item for xattr if selinux is on
9637 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009638 trans = btrfs_start_transaction(root, 5);
9639 if (IS_ERR(trans))
9640 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009641
Li Zefan581bb052011-04-20 10:06:11 +08009642 err = btrfs_find_free_ino(root, &objectid);
9643 if (err)
9644 goto out_unlock;
9645
Josef Bacikaec74772008-07-24 12:12:38 -04009646 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009647 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04009648 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009649 if (IS_ERR(inode)) {
9650 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009651 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009652 }
Chris Mason39279cc2007-06-12 06:35:45 -04009653
Casey Schauflerad19db72011-12-15 10:09:07 -05009654 /*
9655 * If the active LSM wants to access the inode during
9656 * d_instantiate it needs these. Smack checks to see
9657 * if the filesystem supports xattrs by looking at the
9658 * ops vector.
9659 */
9660 inode->i_fop = &btrfs_file_operations;
9661 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009662 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009663 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9664
9665 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9666 if (err)
9667 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009668
Josef Bacika1b075d2010-11-19 20:36:11 +00009669 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009670 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009671 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009672
9673 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009674 if (!path) {
9675 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009676 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009677 }
Li Zefan33345d012011-04-20 10:31:50 +08009678 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009679 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009680 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009681 datasize = btrfs_file_extent_calc_inline_size(name_len);
9682 err = btrfs_insert_empty_item(trans, root, path, &key,
9683 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009684 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009685 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009686 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009687 }
Chris Mason5f39d392007-10-15 16:14:19 -04009688 leaf = path->nodes[0];
9689 ei = btrfs_item_ptr(leaf, path->slots[0],
9690 struct btrfs_file_extent_item);
9691 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9692 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009693 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009694 btrfs_set_file_extent_encryption(leaf, ei, 0);
9695 btrfs_set_file_extent_compression(leaf, ei, 0);
9696 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9697 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9698
Chris Mason39279cc2007-06-12 06:35:45 -04009699 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009700 write_extent_buffer(leaf, symname, ptr, name_len);
9701 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009702 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009703
Chris Mason39279cc2007-06-12 06:35:45 -04009704 inode->i_op = &btrfs_symlink_inode_operations;
9705 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -04009706 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009707 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009708 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009709 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009710 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009711 goto out_unlock_inode;
9712 }
9713
9714 unlock_new_inode(inode);
9715 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009716
9717out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009718 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009719 if (drop_inode) {
9720 inode_dec_link_count(inode);
9721 iput(inode);
9722 }
Liu Bob53d3f52012-11-14 14:34:34 +00009723 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009724 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009725
9726out_unlock_inode:
9727 drop_inode = 1;
9728 unlock_new_inode(inode);
9729 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009730}
Chris Mason16432982008-04-10 10:23:21 -04009731
Josef Bacik0af3d002010-06-21 14:48:16 -04009732static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9733 u64 start, u64 num_bytes, u64 min_size,
9734 loff_t actual_len, u64 *alloc_hint,
9735 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009736{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009737 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9738 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009739 struct btrfs_root *root = BTRFS_I(inode)->root;
9740 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009741 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009742 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009743 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009744 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009745 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009746 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009747
Josef Bacik0af3d002010-06-21 14:48:16 -04009748 if (trans)
9749 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009750 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009751 if (own_trans) {
9752 trans = btrfs_start_transaction(root, 3);
9753 if (IS_ERR(trans)) {
9754 ret = PTR_ERR(trans);
9755 break;
9756 }
Yan Zhengd899e052008-10-30 14:25:28 -04009757 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009758
Chris Mason154ea282013-03-05 11:11:26 -05009759 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9760 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009761 /*
9762 * If we are severely fragmented we could end up with really
9763 * small allocations, so if the allocator is returning small
9764 * chunks lets make its job easier by only searching for those
9765 * sized chunks.
9766 */
9767 cur_bytes = min(cur_bytes, last_alloc);
Josef Bacik00361582013-08-14 14:02:47 -04009768 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009769 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009770 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009771 if (own_trans)
9772 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009773 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009774 }
9775
Josef Bacik0b670dc2015-09-23 17:11:16 -04009776 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04009777 ret = insert_reserved_file_extent(trans, inode,
9778 cur_offset, ins.objectid,
9779 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009780 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009781 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009782 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009783 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009784 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009785 btrfs_abort_transaction(trans, root, ret);
9786 if (own_trans)
9787 btrfs_end_transaction(trans, root);
9788 break;
9789 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009790
Chris Masona1ed8352009-09-11 12:27:37 -04009791 btrfs_drop_extent_cache(inode, cur_offset,
9792 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009793
Josef Bacik5dc562c2012-08-17 13:14:17 -04009794 em = alloc_extent_map();
9795 if (!em) {
9796 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9797 &BTRFS_I(inode)->runtime_flags);
9798 goto next;
9799 }
9800
9801 em->start = cur_offset;
9802 em->orig_start = cur_offset;
9803 em->len = ins.offset;
9804 em->block_start = ins.objectid;
9805 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009806 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009807 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009808 em->bdev = root->fs_info->fs_devices->latest_bdev;
9809 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9810 em->generation = trans->transid;
9811
9812 while (1) {
9813 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009814 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009815 write_unlock(&em_tree->lock);
9816 if (ret != -EEXIST)
9817 break;
9818 btrfs_drop_extent_cache(inode, cur_offset,
9819 cur_offset + ins.offset - 1,
9820 0);
9821 }
9822 free_extent_map(em);
9823next:
Yan Zhengd899e052008-10-30 14:25:28 -04009824 num_bytes -= ins.offset;
9825 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009826 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009827
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009828 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009829 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009830 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009831 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009832 (actual_len > inode->i_size) &&
9833 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009834 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009835 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009836 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009837 i_size = cur_offset;
9838 i_size_write(inode, i_size);
9839 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009840 }
9841
Yan Zhengd899e052008-10-30 14:25:28 -04009842 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009843
9844 if (ret) {
9845 btrfs_abort_transaction(trans, root, ret);
9846 if (own_trans)
9847 btrfs_end_transaction(trans, root);
9848 break;
9849 }
Yan Zhengd899e052008-10-30 14:25:28 -04009850
Josef Bacik0af3d002010-06-21 14:48:16 -04009851 if (own_trans)
9852 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009853 }
Yan Zhengd899e052008-10-30 14:25:28 -04009854 return ret;
9855}
9856
Josef Bacik0af3d002010-06-21 14:48:16 -04009857int btrfs_prealloc_file_range(struct inode *inode, int mode,
9858 u64 start, u64 num_bytes, u64 min_size,
9859 loff_t actual_len, u64 *alloc_hint)
9860{
9861 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9862 min_size, actual_len, alloc_hint,
9863 NULL);
9864}
9865
9866int btrfs_prealloc_file_range_trans(struct inode *inode,
9867 struct btrfs_trans_handle *trans, int mode,
9868 u64 start, u64 num_bytes, u64 min_size,
9869 loff_t actual_len, u64 *alloc_hint)
9870{
9871 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9872 min_size, actual_len, alloc_hint, trans);
9873}
9874
Chris Masone6dcd2d2008-07-17 12:53:50 -04009875static int btrfs_set_page_dirty(struct page *page)
9876{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009877 return __set_page_dirty_nobuffers(page);
9878}
9879
Al Viro10556cb22011-06-20 19:28:19 -04009880static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009881{
Li Zefanb83cc962010-12-20 16:04:08 +08009882 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009883 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009884
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009885 if (mask & MAY_WRITE &&
9886 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9887 if (btrfs_root_readonly(root))
9888 return -EROFS;
9889 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9890 return -EACCES;
9891 }
Al Viro2830ba72011-06-20 19:16:29 -04009892 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009893}
Chris Mason39279cc2007-06-12 06:35:45 -04009894
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009895static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9896{
9897 struct btrfs_trans_handle *trans;
9898 struct btrfs_root *root = BTRFS_I(dir)->root;
9899 struct inode *inode = NULL;
9900 u64 objectid;
9901 u64 index;
9902 int ret = 0;
9903
9904 /*
9905 * 5 units required for adding orphan entry
9906 */
9907 trans = btrfs_start_transaction(root, 5);
9908 if (IS_ERR(trans))
9909 return PTR_ERR(trans);
9910
9911 ret = btrfs_find_free_ino(root, &objectid);
9912 if (ret)
9913 goto out;
9914
9915 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9916 btrfs_ino(dir), objectid, mode, &index);
9917 if (IS_ERR(inode)) {
9918 ret = PTR_ERR(inode);
9919 inode = NULL;
9920 goto out;
9921 }
9922
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009923 inode->i_fop = &btrfs_file_operations;
9924 inode->i_op = &btrfs_file_inode_operations;
9925
9926 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009927 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9928
Chris Masonb0d5d102014-09-08 13:08:51 -07009929 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9930 if (ret)
9931 goto out_inode;
9932
9933 ret = btrfs_update_inode(trans, root, inode);
9934 if (ret)
9935 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009936 ret = btrfs_orphan_add(trans, inode);
9937 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009938 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009939
Filipe Manana5762b5c2014-08-01 00:10:32 +01009940 /*
9941 * We set number of links to 0 in btrfs_new_inode(), and here we set
9942 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9943 * through:
9944 *
9945 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9946 */
9947 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009948 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009949 d_tmpfile(dentry, inode);
9950 mark_inode_dirty(inode);
9951
9952out:
9953 btrfs_end_transaction(trans, root);
9954 if (ret)
9955 iput(inode);
9956 btrfs_balance_delayed_items(root);
9957 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009958 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009959
9960out_inode:
9961 unlock_new_inode(inode);
9962 goto out;
9963
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009964}
9965
Filipe Mananab38ef712014-11-13 17:01:45 +00009966/* Inspired by filemap_check_errors() */
9967int btrfs_inode_check_errors(struct inode *inode)
9968{
9969 int ret = 0;
9970
9971 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
9972 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
9973 ret = -ENOSPC;
9974 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
9975 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
9976 ret = -EIO;
9977
9978 return ret;
9979}
9980
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009981static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009982 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009983 .lookup = btrfs_lookup,
9984 .create = btrfs_create,
9985 .unlink = btrfs_unlink,
9986 .link = btrfs_link,
9987 .mkdir = btrfs_mkdir,
9988 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009989 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009990 .symlink = btrfs_symlink,
9991 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009992 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009993 .setxattr = btrfs_setxattr,
9994 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009995 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009996 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009997 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009998 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009999 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010000 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010001 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010002};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010003static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010004 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010005 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010006 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010007 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010008 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010009};
Yan, Zheng76dda932009-09-21 16:00:26 -040010010
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010011static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010012 .llseek = generic_file_llseek,
10013 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -040010014 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010015 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010016#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010017 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010018#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010019 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010020 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010021};
10022
David Sterba20e55062015-11-19 11:42:28 +010010023static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010024 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010025 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010026 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010027 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010028 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010029 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010030 .set_bit_hook = btrfs_set_bit_hook,
10031 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010032 .merge_extent_hook = btrfs_merge_extent_hook,
10033 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010034};
10035
Chris Mason35054392009-01-21 13:11:13 -050010036/*
10037 * btrfs doesn't support the bmap operation because swapfiles
10038 * use bmap to make a mapping of extents in the file. They assume
10039 * these extents won't change over the life of the file and they
10040 * use the bmap result to do IO directly to the drive.
10041 *
10042 * the btrfs bmap call would return logical addresses that aren't
10043 * suitable for IO and they also will change frequently as COW
10044 * operations happen. So, swapfile + btrfs == corruption.
10045 *
10046 * For now we're avoiding this by dropping bmap.
10047 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010048static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010049 .readpage = btrfs_readpage,
10050 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010051 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010052 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010053 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010054 .invalidatepage = btrfs_invalidatepage,
10055 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010056 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010057 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010058};
10059
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010060static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010061 .readpage = btrfs_readpage,
10062 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010063 .invalidatepage = btrfs_invalidatepage,
10064 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010065};
10066
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010067static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010068 .getattr = btrfs_getattr,
10069 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010070 .setxattr = btrfs_setxattr,
10071 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010072 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010073 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -050010074 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010075 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010076 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010077 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010078 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010079};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010080static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010081 .getattr = btrfs_getattr,
10082 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010083 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010084 .setxattr = btrfs_setxattr,
10085 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -040010086 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010087 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010088 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010089 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010090 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010091};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010092static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010093 .readlink = generic_readlink,
10094 .follow_link = page_follow_link_light,
10095 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +000010096 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010097 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010098 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010099 .setxattr = btrfs_setxattr,
10100 .getxattr = btrfs_getxattr,
10101 .listxattr = btrfs_listxattr,
10102 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010103 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010104};
Yan, Zheng76dda932009-09-21 16:00:26 -040010105
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010106const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010107 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010108 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010109};