blob: a85c23dfcddbcfd992069811f24b116d74e4dc21 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070035#include <linux/aio.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050037#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040038#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040039#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020041#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050042#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000043#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050044#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040045#include <linux/posix_acl_xattr.h>
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"
Chris Mason39279cc2007-06-12 06:35:45 -040062
63struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080064 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040065 struct btrfs_root *root;
66};
67
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_dir_inode_operations;
69static const struct inode_operations btrfs_symlink_inode_operations;
70static const struct inode_operations btrfs_dir_ro_inode_operations;
71static const struct inode_operations btrfs_special_inode_operations;
72static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070073static const struct address_space_operations btrfs_aops;
74static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070075static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050076static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040077
78static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000079static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080struct 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
86static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
87 [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
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000111static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500112 struct inode *inode, struct inode *dir,
113 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500114{
115 int err;
116
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000117 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500118 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500119 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500120 return err;
121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/*
Chris Masonc8b97812008-10-29 14:49:59 -0400124 * this does all the hard work for inserting an inline extent into
125 * the btree. The caller should have done a btrfs_drop_extents so that
126 * no overlapping inline items exist in the btree
127 */
Chris Mason40f76582014-05-21 13:35:51 -0700128static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000129 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400130 struct btrfs_root *root, struct inode *inode,
131 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000132 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400133 struct page **compressed_pages)
134{
Chris Masonc8b97812008-10-29 14:49:59 -0400135 struct extent_buffer *leaf;
136 struct page *page = NULL;
137 char *kaddr;
138 unsigned long ptr;
139 struct btrfs_file_extent_item *ei;
140 int err = 0;
141 int ret;
142 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400143 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400144
Li Zefanfe3f5662011-03-28 08:30:38 +0000145 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400146 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400147
Chris Masonc8b97812008-10-29 14:49:59 -0400148 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000149
150 if (!extent_inserted) {
151 struct btrfs_key key;
152 size_t datasize;
153
154 key.objectid = btrfs_ino(inode);
155 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200156 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000157
158 datasize = btrfs_file_extent_calc_inline_size(cur_size);
159 path->leave_spinning = 1;
160 ret = btrfs_insert_empty_item(trans, root, path, &key,
161 datasize);
162 if (ret) {
163 err = ret;
164 goto fail;
165 }
Chris Masonc8b97812008-10-29 14:49:59 -0400166 }
167 leaf = path->nodes[0];
168 ei = btrfs_item_ptr(leaf, path->slots[0],
169 struct btrfs_file_extent_item);
170 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
171 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
172 btrfs_set_file_extent_encryption(leaf, ei, 0);
173 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
174 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
175 ptr = btrfs_file_extent_inline_start(ei);
176
Li Zefan261507a02010-12-17 14:21:50 +0800177 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400178 struct page *cpage;
179 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500180 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400181 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500182 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400183 PAGE_CACHE_SIZE);
184
Cong Wang7ac687d2011-11-25 23:14:28 +0800185 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400186 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800187 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400188
189 i++;
190 ptr += cur_size;
191 compressed_size -= cur_size;
192 }
193 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800194 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400195 } else {
196 page = find_get_page(inode->i_mapping,
197 start >> PAGE_CACHE_SHIFT);
198 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800199 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400200 offset = start & (PAGE_CACHE_SIZE - 1);
201 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800202 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400203 page_cache_release(page);
204 }
205 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400207
Yan, Zhengc2167752009-11-12 09:34:21 +0000208 /*
209 * we're an inline extent, so nobody can
210 * extend the file past i_size without locking
211 * a page we already have locked.
212 *
213 * We must do any isize and inode updates
214 * before we unlock the pages. Otherwise we
215 * could end up racing with unlink.
216 */
Chris Masonc8b97812008-10-29 14:49:59 -0400217 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100218 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000219
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100220 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400221fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400222 return err;
223}
224
225
226/*
227 * conditionally insert an inline extent into the file. This
228 * does the checks required to make sure the data is small enough
229 * to fit as an inline extent.
230 */
Josef Bacik00361582013-08-14 14:02:47 -0400231static noinline int cow_file_range_inline(struct btrfs_root *root,
232 struct inode *inode, u64 start,
233 u64 end, size_t compressed_size,
234 int compress_type,
235 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400236{
Josef Bacik00361582013-08-14 14:02:47 -0400237 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400238 u64 isize = i_size_read(inode);
239 u64 actual_end = min(end + 1, isize);
240 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000241 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400242 u64 data_len = inline_len;
243 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000244 struct btrfs_path *path;
245 int extent_inserted = 0;
246 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400247
248 if (compressed_size)
249 data_len = compressed_size;
250
251 if (start > 0 ||
Wang Shilong354877b2014-07-17 11:44:11 +0800252 actual_end > PAGE_CACHE_SIZE ||
253 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400254 (!compressed_size &&
255 (actual_end & (root->sectorsize - 1)) == 0) ||
256 end + 1 < isize ||
257 data_len > root->fs_info->max_inline) {
258 return 1;
259 }
260
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000261 path = btrfs_alloc_path();
262 if (!path)
263 return -ENOMEM;
264
Josef Bacik00361582013-08-14 14:02:47 -0400265 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000266 if (IS_ERR(trans)) {
267 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400268 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000269 }
Josef Bacik00361582013-08-14 14:02:47 -0400270 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
271
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000272 if (compressed_size && compressed_pages)
273 extent_item_size = btrfs_file_extent_calc_inline_size(
274 compressed_size);
275 else
276 extent_item_size = btrfs_file_extent_calc_inline_size(
277 inline_len);
278
279 ret = __btrfs_drop_extents(trans, root, inode, path,
280 start, aligned_end, NULL,
281 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400282 if (ret) {
283 btrfs_abort_transaction(trans, root, ret);
284 goto out;
285 }
Chris Masonc8b97812008-10-29 14:49:59 -0400286
287 if (isize > actual_end)
288 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000289 ret = insert_inline_extent(trans, path, extent_inserted,
290 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400291 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000292 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400293 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100294 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400295 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400296 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400297 ret = 1;
298 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100299 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400300
Josef Bacikbdc20e62013-02-28 13:23:38 -0500301 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400302 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400303 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400304out:
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400306 btrfs_end_transaction(trans, root);
307 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400308}
309
Chris Mason771ed682008-11-06 22:02:51 -0500310struct async_extent {
311 u64 start;
312 u64 ram_size;
313 u64 compressed_size;
314 struct page **pages;
315 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800316 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500317 struct list_head list;
318};
319
320struct async_cow {
321 struct inode *inode;
322 struct btrfs_root *root;
323 struct page *locked_page;
324 u64 start;
325 u64 end;
326 struct list_head extents;
327 struct btrfs_work work;
328};
329
330static noinline int add_async_extent(struct async_cow *cow,
331 u64 start, u64 ram_size,
332 u64 compressed_size,
333 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800334 unsigned long nr_pages,
335 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500336{
337 struct async_extent *async_extent;
338
339 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100340 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500341 async_extent->start = start;
342 async_extent->ram_size = ram_size;
343 async_extent->compressed_size = compressed_size;
344 async_extent->pages = pages;
345 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800346 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500347 list_add_tail(&async_extent->list, &cow->extents);
348 return 0;
349}
350
Wang Shilongf79707b2014-07-17 11:44:09 +0800351static inline int inode_need_compress(struct inode *inode)
352{
353 struct btrfs_root *root = BTRFS_I(inode)->root;
354
355 /* force compress */
356 if (btrfs_test_opt(root, FORCE_COMPRESS))
357 return 1;
358 /* bad compression ratios */
359 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
360 return 0;
361 if (btrfs_test_opt(root, COMPRESS) ||
362 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
363 BTRFS_I(inode)->force_compress)
364 return 1;
365 return 0;
366}
367
Chris Masonc8b97812008-10-29 14:49:59 -0400368/*
Chris Mason771ed682008-11-06 22:02:51 -0500369 * we create compressed extents in two phases. The first
370 * phase compresses a range of pages that have already been
371 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400372 *
Chris Mason771ed682008-11-06 22:02:51 -0500373 * This is done inside an ordered work queue, and the compression
374 * is spread across many cpus. The actual IO submission is step
375 * two, and the ordered work queue takes care of making sure that
376 * happens in the same order things were put onto the queue by
377 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400378 *
Chris Mason771ed682008-11-06 22:02:51 -0500379 * If this code finds it can't get good compression, it puts an
380 * entry onto the work queue to write the uncompressed bytes. This
381 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300382 * are written in the same order that the flusher thread sent them
383 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400384 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100385static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500386 struct page *locked_page,
387 u64 start, u64 end,
388 struct async_cow *async_cow,
389 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400390{
391 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400392 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400393 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400394 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500395 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400396 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400397 struct page **pages = NULL;
398 unsigned long nr_pages;
399 unsigned long nr_pages_ret = 0;
400 unsigned long total_compressed = 0;
401 unsigned long total_in = 0;
402 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500403 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400404 int i;
405 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800406 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400407 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400408
Liu Bo4cb13e52012-03-29 09:57:45 -0400409 /* if this is a small write inside eof, kick off a defrag */
410 if ((end - start + 1) < 16 * 1024 &&
411 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400412 btrfs_add_inode_defrag(NULL, inode);
413
Chris Mason42dc7ba2008-12-15 11:44:56 -0500414 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400415again:
416 will_compress = 0;
417 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
418 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
419
Chris Masonf03d9302009-02-04 09:31:06 -0500420 /*
421 * we don't want to send crud past the end of i_size through
422 * compression, that's just a waste of CPU time. So, if the
423 * end of the file is before the start of our current
424 * requested range of bytes, we bail out to the uncompressed
425 * cleanup code that can deal with all of this.
426 *
427 * It isn't really the fastest way to fix things, but this is a
428 * very uncommon corner.
429 */
430 if (actual_end <= start)
431 goto cleanup_and_bail_uncompressed;
432
Chris Masonc8b97812008-10-29 14:49:59 -0400433 total_compressed = actual_end - start;
434
Shilong Wang4bcbb332014-10-07 18:44:35 -0400435 /*
436 * skip compression for a small file range(<=blocksize) that
437 * isn't an inline extent, since it dosen't save disk space at all.
438 */
439 if (total_compressed <= blocksize &&
440 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
441 goto cleanup_and_bail_uncompressed;
442
Chris Masonc8b97812008-10-29 14:49:59 -0400443 /* we want to make sure that amount of ram required to uncompress
444 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500445 * of a compressed extent to 128k. This is a crucial number
446 * because it also controls how easily we can spread reads across
447 * cpus for decompression.
448 *
449 * We also want to make sure the amount of IO required to do
450 * a random read is reasonably small, so we limit the size of
451 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400452 */
453 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000454 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500455 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400456 total_in = 0;
457 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400458
Chris Mason771ed682008-11-06 22:02:51 -0500459 /*
460 * we do compression for mount -o compress and when the
461 * inode has not been flagged as nocompress. This flag can
462 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400463 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800464 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400465 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400466 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800467 if (!pages) {
468 /* just bail out to the uncompressed code */
469 goto cont;
470 }
Chris Mason179e29e2007-11-01 11:28:41 -0400471
Li Zefan261507a02010-12-17 14:21:50 +0800472 if (BTRFS_I(inode)->force_compress)
473 compress_type = BTRFS_I(inode)->force_compress;
474
Chris Mason4adaa612013-03-26 13:07:00 -0400475 /*
476 * we need to call clear_page_dirty_for_io on each
477 * page in the range. Otherwise applications with the file
478 * mmap'd can wander in and change the page contents while
479 * we are compressing them.
480 *
481 * If the compression fails for any reason, we set the pages
482 * dirty again later on.
483 */
484 extent_range_clear_dirty_for_io(inode, start, end);
485 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800486 ret = btrfs_compress_pages(compress_type,
487 inode->i_mapping, start,
488 total_compressed, pages,
489 nr_pages, &nr_pages_ret,
490 &total_in,
491 &total_compressed,
492 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400493
494 if (!ret) {
495 unsigned long offset = total_compressed &
496 (PAGE_CACHE_SIZE - 1);
497 struct page *page = pages[nr_pages_ret - 1];
498 char *kaddr;
499
500 /* zero the tail end of the last page, we might be
501 * sending it down to disk
502 */
503 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800504 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400505 memset(kaddr + offset, 0,
506 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800507 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400508 }
509 will_compress = 1;
510 }
511 }
Li Zefan560f7d72011-09-08 10:22:01 +0800512cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400513 if (start == 0) {
514 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500515 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400516 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500517 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400518 */
Josef Bacik00361582013-08-14 14:02:47 -0400519 ret = cow_file_range_inline(root, inode, start, end,
520 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400521 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500522 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400523 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000524 total_compressed,
525 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400526 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100527 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400528 unsigned long clear_flags = EXTENT_DELALLOC |
529 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100530 unsigned long page_error_op;
531
Josef Bacik151a41b2013-07-29 13:22:24 -0400532 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100533 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400534
Chris Mason771ed682008-11-06 22:02:51 -0500535 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100536 * inline extent creation worked or returned error,
537 * we don't need to create any more async work items.
538 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500539 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400540 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400541 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400542 PAGE_CLEAR_DIRTY |
543 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100544 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400545 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400546 goto free_pages_out;
547 }
548 }
549
550 if (will_compress) {
551 /*
552 * we aren't doing an inline extent round the compressed size
553 * up to a block size boundary so the allocator does sane
554 * things
555 */
Qu Wenruofda28322013-02-26 08:10:22 +0000556 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400557
558 /*
559 * one last check to make sure the compression is really a
560 * win, compare the page count read with the blocks on disk
561 */
Qu Wenruofda28322013-02-26 08:10:22 +0000562 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400563 if (total_compressed >= total_in) {
564 will_compress = 0;
565 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400566 num_bytes = total_in;
567 }
568 }
569 if (!will_compress && pages) {
570 /*
571 * the compression code ran but failed to make things smaller,
572 * free any pages it allocated and our page pointer array
573 */
574 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400575 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400576 page_cache_release(pages[i]);
577 }
578 kfree(pages);
579 pages = NULL;
580 total_compressed = 0;
581 nr_pages_ret = 0;
582
583 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500584 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
585 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500586 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500587 }
Chris Masonc8b97812008-10-29 14:49:59 -0400588 }
Chris Mason771ed682008-11-06 22:02:51 -0500589 if (will_compress) {
590 *num_added += 1;
591
592 /* the async work queues will take care of doing actual
593 * allocation on disk for these compressed pages,
594 * and will submit them to the elevator.
595 */
596 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800597 total_compressed, pages, nr_pages_ret,
598 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500599
Yan, Zheng24ae6362010-12-06 07:02:36 +0000600 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500601 start += num_bytes;
602 pages = NULL;
603 cond_resched();
604 goto again;
605 }
606 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500607cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500608 /*
609 * No compression, but we still need to write the pages in
610 * the file we've been given so far. redirty the locked
611 * page if it corresponds to our extent and set things up
612 * for the async work queue to run cow_file_range to do
613 * the normal delalloc dance
614 */
615 if (page_offset(locked_page) >= start &&
616 page_offset(locked_page) <= end) {
617 __set_page_dirty_nobuffers(locked_page);
618 /* unlocked later on in the async handlers */
619 }
Chris Mason4adaa612013-03-26 13:07:00 -0400620 if (redirty)
621 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800622 add_async_extent(async_cow, start, end - start + 1,
623 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500624 *num_added += 1;
625 }
626
Filipe Mananac44f6492014-10-09 21:15:44 +0100627 return;
Chris Mason771ed682008-11-06 22:02:51 -0500628
629free_pages_out:
630 for (i = 0; i < nr_pages_ret; i++) {
631 WARN_ON(pages[i]->mapping);
632 page_cache_release(pages[i]);
633 }
Chris Masond3977122009-01-05 21:25:51 -0500634 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500635}
Chris Mason771ed682008-11-06 22:02:51 -0500636
Filipe Manana40ae8372014-10-06 22:14:24 +0100637static void free_async_extent_pages(struct async_extent *async_extent)
638{
639 int i;
640
641 if (!async_extent->pages)
642 return;
643
644 for (i = 0; i < async_extent->nr_pages; i++) {
645 WARN_ON(async_extent->pages[i]->mapping);
646 page_cache_release(async_extent->pages[i]);
647 }
648 kfree(async_extent->pages);
649 async_extent->nr_pages = 0;
650 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500651}
652
653/*
654 * phase two of compressed writeback. This is the ordered portion
655 * of the code, which only gets called in the order the work was
656 * queued. We walk all the async extents created by compress_file_range
657 * and send them down to the disk.
658 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100659static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500660 struct async_cow *async_cow)
661{
662 struct async_extent *async_extent;
663 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500664 struct btrfs_key ins;
665 struct extent_map *em;
666 struct btrfs_root *root = BTRFS_I(inode)->root;
667 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
668 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500669 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500670
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500671again:
Chris Masond3977122009-01-05 21:25:51 -0500672 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500673 async_extent = list_entry(async_cow->extents.next,
674 struct async_extent, list);
675 list_del(&async_extent->list);
676
677 io_tree = &BTRFS_I(inode)->io_tree;
678
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500679retry:
Chris Mason771ed682008-11-06 22:02:51 -0500680 /* did the compression code fall back to uncompressed IO? */
681 if (!async_extent->pages) {
682 int page_started = 0;
683 unsigned long nr_written = 0;
684
685 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000686 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100687 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500688
689 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500690 ret = cow_file_range(inode, async_cow->locked_page,
691 async_extent->start,
692 async_extent->start +
693 async_extent->ram_size - 1,
694 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500695
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100696 /* JDM XXX */
697
Chris Mason771ed682008-11-06 22:02:51 -0500698 /*
699 * if page_started, cow_file_range inserted an
700 * inline extent and took care of all the unlocking
701 * and IO for us. Otherwise, we need to submit
702 * all those pages down to the drive.
703 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500704 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500705 extent_write_locked_range(io_tree,
706 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500707 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500708 async_extent->ram_size - 1,
709 btrfs_get_extent,
710 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500711 else if (ret)
712 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500713 kfree(async_extent);
714 cond_resched();
715 continue;
716 }
717
718 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100719 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500720
Josef Bacik00361582013-08-14 14:02:47 -0400721 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500722 async_extent->compressed_size,
723 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800724 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500725 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100726 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500727
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400728 if (ret == -ENOSPC) {
729 unlock_extent(io_tree, async_extent->start,
730 async_extent->start +
731 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800732
733 /*
734 * we need to redirty the pages if we decide to
735 * fallback to uncompressed IO, otherwise we
736 * will not submit these pages down to lower
737 * layers.
738 */
739 extent_range_redirty_for_io(inode,
740 async_extent->start,
741 async_extent->start +
742 async_extent->ram_size - 1);
743
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100744 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400745 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500746 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500747 }
748
Yan, Zhengc2167752009-11-12 09:34:21 +0000749 /*
750 * here we're doing allocation and writeback of the
751 * compressed pages
752 */
753 btrfs_drop_extent_cache(inode, async_extent->start,
754 async_extent->start +
755 async_extent->ram_size - 1, 0);
756
David Sterba172ddd62011-04-21 00:48:27 +0200757 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000758 if (!em) {
759 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500760 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000761 }
Chris Mason771ed682008-11-06 22:02:51 -0500762 em->start = async_extent->start;
763 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500764 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400765 em->mod_start = em->start;
766 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500767
768 em->block_start = ins.objectid;
769 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500770 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400771 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500772 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800773 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500774 set_bit(EXTENT_FLAG_PINNED, &em->flags);
775 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400776 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500777
Chris Masond3977122009-01-05 21:25:51 -0500778 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400779 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400780 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400781 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500782 if (ret != -EEXIST) {
783 free_extent_map(em);
784 break;
785 }
786 btrfs_drop_extent_cache(inode, async_extent->start,
787 async_extent->start +
788 async_extent->ram_size - 1, 0);
789 }
790
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500791 if (ret)
792 goto out_free_reserve;
793
Li Zefan261507a02010-12-17 14:21:50 +0800794 ret = btrfs_add_ordered_extent_compress(inode,
795 async_extent->start,
796 ins.objectid,
797 async_extent->ram_size,
798 ins.offset,
799 BTRFS_ORDERED_COMPRESSED,
800 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100801 if (ret) {
802 btrfs_drop_extent_cache(inode, async_extent->start,
803 async_extent->start +
804 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500805 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100806 }
Chris Mason771ed682008-11-06 22:02:51 -0500807
Chris Mason771ed682008-11-06 22:02:51 -0500808 /*
809 * clear dirty, set writeback and unlock the pages.
810 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400811 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400812 async_extent->start +
813 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400814 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
815 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400816 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500817 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500818 async_extent->start,
819 async_extent->ram_size,
820 ins.objectid,
821 ins.offset, async_extent->pages,
822 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100823 if (ret) {
824 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
825 struct page *p = async_extent->pages[0];
826 const u64 start = async_extent->start;
827 const u64 end = start + async_extent->ram_size - 1;
828
829 p->mapping = inode->i_mapping;
830 tree->ops->writepage_end_io_hook(p, start, end,
831 NULL, 0);
832 p->mapping = NULL;
833 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
834 PAGE_END_WRITEBACK |
835 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100836 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100837 }
Chris Mason771ed682008-11-06 22:02:51 -0500838 alloc_hint = ins.objectid + ins.offset;
839 kfree(async_extent);
840 cond_resched();
841 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100842 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500843out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800844 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100845out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400846 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500847 async_extent->start +
848 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400849 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400850 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
851 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100852 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
853 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100854 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100855 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500856 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500857}
858
Josef Bacik4b46fce2010-05-23 11:00:55 -0400859static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
860 u64 num_bytes)
861{
862 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
863 struct extent_map *em;
864 u64 alloc_hint = 0;
865
866 read_lock(&em_tree->lock);
867 em = search_extent_mapping(em_tree, start, num_bytes);
868 if (em) {
869 /*
870 * if block start isn't an actual block number then find the
871 * first block in this inode and use that as a hint. If that
872 * block is also bogus then just don't worry about it.
873 */
874 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
875 free_extent_map(em);
876 em = search_extent_mapping(em_tree, 0, 0);
877 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
878 alloc_hint = em->block_start;
879 if (em)
880 free_extent_map(em);
881 } else {
882 alloc_hint = em->block_start;
883 free_extent_map(em);
884 }
885 }
886 read_unlock(&em_tree->lock);
887
888 return alloc_hint;
889}
890
Chris Mason771ed682008-11-06 22:02:51 -0500891/*
892 * when extent_io.c finds a delayed allocation range in the file,
893 * the call backs end up in this code. The basic idea is to
894 * allocate extents on disk for the range, and create ordered data structs
895 * in ram to track those extents.
896 *
897 * locked_page is the page that writepage had locked already. We use
898 * it to make sure we don't do extra locks or unlocks.
899 *
900 * *page_started is set to one if we unlock locked_page and do everything
901 * required to start IO on it. It may be clean and already done with
902 * IO when we return.
903 */
Josef Bacik00361582013-08-14 14:02:47 -0400904static noinline int cow_file_range(struct inode *inode,
905 struct page *locked_page,
906 u64 start, u64 end, int *page_started,
907 unsigned long *nr_written,
908 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500909{
Josef Bacik00361582013-08-14 14:02:47 -0400910 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500911 u64 alloc_hint = 0;
912 u64 num_bytes;
913 unsigned long ram_size;
914 u64 disk_num_bytes;
915 u64 cur_alloc_size;
916 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500917 struct btrfs_key ins;
918 struct extent_map *em;
919 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
920 int ret = 0;
921
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400922 if (btrfs_is_free_space_inode(inode)) {
923 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500924 ret = -EINVAL;
925 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400926 }
Chris Mason771ed682008-11-06 22:02:51 -0500927
Qu Wenruofda28322013-02-26 08:10:22 +0000928 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500929 num_bytes = max(blocksize, num_bytes);
930 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500931
Chris Mason4cb53002011-05-24 15:35:30 -0400932 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400933 if (num_bytes < 64 * 1024 &&
934 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400935 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400936
Chris Mason771ed682008-11-06 22:02:51 -0500937 if (start == 0) {
938 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400939 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
940 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500941 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400942 extent_clear_unlock_delalloc(inode, start, end, NULL,
943 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400944 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400945 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
946 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000947
Chris Mason771ed682008-11-06 22:02:51 -0500948 *nr_written = *nr_written +
949 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
950 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500951 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100952 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100953 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500954 }
955 }
Chris Masonc8b97812008-10-29 14:49:59 -0400956
957 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200958 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400959
Josef Bacik4b46fce2010-05-23 11:00:55 -0400960 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400961 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400962
Chris Masond3977122009-01-05 21:25:51 -0500963 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400964 unsigned long op;
965
Josef Bacik287a0ab2010-03-19 18:07:23 +0000966 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400967 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500968 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800969 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400970 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100971 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500972
David Sterba172ddd62011-04-21 00:48:27 +0200973 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000974 if (!em) {
975 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000976 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000977 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400978 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500979 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500980 ram_size = ins.offset;
981 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400982 em->mod_start = em->start;
983 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400984
Chris Masone6dcd2d2008-07-17 12:53:50 -0400985 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400986 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500987 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400988 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400989 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400990 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400991 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400992
Chris Masond3977122009-01-05 21:25:51 -0500993 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400994 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400995 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400996 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400997 if (ret != -EEXIST) {
998 free_extent_map(em);
999 break;
1000 }
1001 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001002 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001003 }
Liu Boace68ba2013-04-22 10:53:47 +00001004 if (ret)
1005 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001006
Chris Mason98d20f62008-04-14 09:46:10 -04001007 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001008 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001009 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001010 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001011 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001012
Yan Zheng17d217f2008-12-12 10:03:38 -05001013 if (root->root_key.objectid ==
1014 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1015 ret = btrfs_reloc_clone_csums(inode, start,
1016 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001017 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001018 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001019 }
1020
Chris Masond3977122009-01-05 21:25:51 -05001021 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001022 break;
Chris Masond3977122009-01-05 21:25:51 -05001023
Chris Masonc8b97812008-10-29 14:49:59 -04001024 /* we're not doing compressed IO, don't unlock the first
1025 * page (which the caller expects to stay locked), don't
1026 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001027 *
1028 * Do set the Private2 bit so we know this page was properly
1029 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001030 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001031 op = unlock ? PAGE_UNLOCK : 0;
1032 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001033
Josef Bacikc2790a22013-07-29 11:20:47 -04001034 extent_clear_unlock_delalloc(inode, start,
1035 start + ram_size - 1, locked_page,
1036 EXTENT_LOCKED | EXTENT_DELALLOC,
1037 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001038 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001039 num_bytes -= cur_alloc_size;
1040 alloc_hint = ins.objectid + ins.offset;
1041 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001042 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001043out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001044 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001045
Filipe Mananad9f85962014-08-25 10:43:00 +01001046out_drop_extent_cache:
1047 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001048out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001049 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001050out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001051 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001052 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1053 EXTENT_DELALLOC | EXTENT_DEFRAG,
1054 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1055 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001056 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001057}
Chris Masonc8b97812008-10-29 14:49:59 -04001058
Chris Mason771ed682008-11-06 22:02:51 -05001059/*
1060 * work queue call back to started compression on a file and pages
1061 */
1062static noinline void async_cow_start(struct btrfs_work *work)
1063{
1064 struct async_cow *async_cow;
1065 int num_added = 0;
1066 async_cow = container_of(work, struct async_cow, work);
1067
1068 compress_file_range(async_cow->inode, async_cow->locked_page,
1069 async_cow->start, async_cow->end, async_cow,
1070 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001071 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001072 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001073 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001074 }
Chris Mason771ed682008-11-06 22:02:51 -05001075}
1076
1077/*
1078 * work queue call back to submit previously compressed pages
1079 */
1080static noinline void async_cow_submit(struct btrfs_work *work)
1081{
1082 struct async_cow *async_cow;
1083 struct btrfs_root *root;
1084 unsigned long nr_pages;
1085
1086 async_cow = container_of(work, struct async_cow, work);
1087
1088 root = async_cow->root;
1089 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1090 PAGE_CACHE_SHIFT;
1091
Josef Bacik66657b32012-08-01 15:36:24 -04001092 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001093 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001094 waitqueue_active(&root->fs_info->async_submit_wait))
1095 wake_up(&root->fs_info->async_submit_wait);
1096
Chris Masond3977122009-01-05 21:25:51 -05001097 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001098 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001099}
Chris Masonc8b97812008-10-29 14:49:59 -04001100
Chris Mason771ed682008-11-06 22:02:51 -05001101static noinline void async_cow_free(struct btrfs_work *work)
1102{
1103 struct async_cow *async_cow;
1104 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001105 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001106 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001107 kfree(async_cow);
1108}
1109
1110static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1111 u64 start, u64 end, int *page_started,
1112 unsigned long *nr_written)
1113{
1114 struct async_cow *async_cow;
1115 struct btrfs_root *root = BTRFS_I(inode)->root;
1116 unsigned long nr_pages;
1117 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001118 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001119
Chris Masona3429ab2009-10-08 12:30:20 -04001120 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1121 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001122 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001123 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001124 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001125 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001126 async_cow->root = root;
1127 async_cow->locked_page = locked_page;
1128 async_cow->start = start;
1129
Wang Shilongf79707b2014-07-17 11:44:09 +08001130 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1131 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001132 cur_end = end;
1133 else
1134 cur_end = min(end, start + 512 * 1024 - 1);
1135
1136 async_cow->end = cur_end;
1137 INIT_LIST_HEAD(&async_cow->extents);
1138
Liu Bo9e0af232014-08-15 23:36:53 +08001139 btrfs_init_work(&async_cow->work,
1140 btrfs_delalloc_helper,
1141 async_cow_start, async_cow_submit,
1142 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001143
Chris Mason771ed682008-11-06 22:02:51 -05001144 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1145 PAGE_CACHE_SHIFT;
1146 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1147
Qu Wenruoafe3d242014-02-28 10:46:07 +08001148 btrfs_queue_work(root->fs_info->delalloc_workers,
1149 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001150
1151 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1152 wait_event(root->fs_info->async_submit_wait,
1153 (atomic_read(&root->fs_info->async_delalloc_pages) <
1154 limit));
1155 }
1156
Chris Masond3977122009-01-05 21:25:51 -05001157 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001158 atomic_read(&root->fs_info->async_delalloc_pages)) {
1159 wait_event(root->fs_info->async_submit_wait,
1160 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1161 0));
1162 }
1163
1164 *nr_written += nr_pages;
1165 start = cur_end + 1;
1166 }
1167 *page_started = 1;
1168 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001169}
1170
Chris Masond3977122009-01-05 21:25:51 -05001171static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001172 u64 bytenr, u64 num_bytes)
1173{
1174 int ret;
1175 struct btrfs_ordered_sum *sums;
1176 LIST_HEAD(list);
1177
Yan Zheng07d400a2009-01-06 11:42:00 -05001178 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001179 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001180 if (ret == 0 && list_empty(&list))
1181 return 0;
1182
1183 while (!list_empty(&list)) {
1184 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1185 list_del(&sums->list);
1186 kfree(sums);
1187 }
1188 return 1;
1189}
1190
Chris Masond352ac62008-09-29 15:18:18 -04001191/*
1192 * when nowcow writeback call back. This checks for snapshots or COW copies
1193 * of the extents that exist in the file, and COWs the file as required.
1194 *
1195 * If no cow copies or snapshots exist, we write directly to the existing
1196 * blocks on disk
1197 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001198static noinline int run_delalloc_nocow(struct inode *inode,
1199 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001200 u64 start, u64 end, int *page_started, int force,
1201 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001202{
Chris Masonbe20aa92007-12-17 20:14:01 -05001203 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001204 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001205 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001206 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001207 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001208 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001209 u64 cow_start;
1210 u64 cur_offset;
1211 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001212 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001213 u64 disk_bytenr;
1214 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001215 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001216 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001217 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001218 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001219 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001220 int nocow;
1221 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001222 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001223 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001224
1225 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001226 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001227 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1228 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001229 EXTENT_DO_ACCOUNTING |
1230 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001231 PAGE_CLEAR_DIRTY |
1232 PAGE_SET_WRITEBACK |
1233 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001234 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001235 }
Li Zefan82d59022011-04-20 10:33:24 +08001236
Liu Bo83eea1f2012-07-10 05:28:39 -06001237 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001238
1239 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001240 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001241 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001242 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001243
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001244 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001245 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1246 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001247 EXTENT_DO_ACCOUNTING |
1248 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001249 PAGE_CLEAR_DIRTY |
1250 PAGE_SET_WRITEBACK |
1251 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001252 btrfs_free_path(path);
1253 return PTR_ERR(trans);
1254 }
1255
Josef Bacik74b21072011-04-13 12:02:53 -04001256 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001257
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 cow_start = (u64)-1;
1259 cur_offset = start;
1260 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001261 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001263 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001264 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1266 leaf = path->nodes[0];
1267 btrfs_item_key_to_cpu(leaf, &found_key,
1268 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001269 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 found_key.type == BTRFS_EXTENT_DATA_KEY)
1271 path->slots[0]--;
1272 }
1273 check_prev = 0;
1274next_slot:
1275 leaf = path->nodes[0];
1276 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1277 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001278 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001279 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 if (ret > 0)
1281 break;
1282 leaf = path->nodes[0];
1283 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001284
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 nocow = 0;
1286 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001287 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001288 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001289
Li Zefan33345d012011-04-20 10:31:50 +08001290 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001291 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1292 found_key.offset > end)
1293 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001294
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 if (found_key.offset > cur_offset) {
1296 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001297 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001298 goto out_check;
1299 }
Chris Masonc31f8832008-01-08 15:46:31 -05001300
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 fi = btrfs_item_ptr(leaf, path->slots[0],
1302 struct btrfs_file_extent_item);
1303 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001304
Josef Bacikcc95bef2013-04-04 14:31:27 -04001305 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001306 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1307 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001309 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001310 extent_end = found_key.offset +
1311 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001312 disk_num_bytes =
1313 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 if (extent_end <= start) {
1315 path->slots[0]++;
1316 goto next_slot;
1317 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001318 if (disk_bytenr == 0)
1319 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001320 if (btrfs_file_extent_compression(leaf, fi) ||
1321 btrfs_file_extent_encryption(leaf, fi) ||
1322 btrfs_file_extent_other_encoding(leaf, fi))
1323 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001324 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1325 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001326 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001327 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001328 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001329 found_key.offset -
1330 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001331 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001332 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001333 disk_bytenr += cur_offset - found_key.offset;
1334 num_bytes = min(end + 1, extent_end) - cur_offset;
1335 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001336 * if there are pending snapshots for this root,
1337 * we fall into common COW way.
1338 */
1339 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001340 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001341 if (!err)
1342 goto out_check;
1343 }
1344 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001345 * force cow if csum exists in the range.
1346 * this ensure that csum for a given extent are
1347 * either valid or do not exist.
1348 */
1349 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1350 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001351 nocow = 1;
1352 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1353 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001354 btrfs_file_extent_inline_len(leaf,
1355 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001356 extent_end = ALIGN(extent_end, root->sectorsize);
1357 } else {
1358 BUG_ON(1);
1359 }
1360out_check:
1361 if (extent_end <= start) {
1362 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001363 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001364 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 goto next_slot;
1366 }
1367 if (!nocow) {
1368 if (cow_start == (u64)-1)
1369 cow_start = cur_offset;
1370 cur_offset = extent_end;
1371 if (cur_offset > end)
1372 break;
1373 path->slots[0]++;
1374 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001375 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001376
David Sterbab3b4aa72011-04-21 01:20:15 +02001377 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001378 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001379 ret = cow_file_range(inode, locked_page,
1380 cow_start, found_key.offset - 1,
1381 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001382 if (ret) {
1383 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001384 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001385 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001386 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001387 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001388 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001389
Yan Zhengd899e052008-10-30 14:25:28 -04001390 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1391 struct extent_map *em;
1392 struct extent_map_tree *em_tree;
1393 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001394 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001395 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001396 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001397 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001398 em->len = num_bytes;
1399 em->block_len = num_bytes;
1400 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001401 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001402 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001403 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001404 em->mod_start = em->start;
1405 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001406 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001407 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001408 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001409 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001410 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001411 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001412 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001413 if (ret != -EEXIST) {
1414 free_extent_map(em);
1415 break;
1416 }
1417 btrfs_drop_extent_cache(inode, em->start,
1418 em->start + em->len - 1, 0);
1419 }
1420 type = BTRFS_ORDERED_PREALLOC;
1421 } else {
1422 type = BTRFS_ORDERED_NOCOW;
1423 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001424
1425 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001426 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001427 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001428
Yan, Zhengefa56462010-05-16 10:49:59 -04001429 if (root->root_key.objectid ==
1430 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1431 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1432 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001433 if (ret) {
1434 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001435 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001436 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001437 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001438 }
1439
Josef Bacikc2790a22013-07-29 11:20:47 -04001440 extent_clear_unlock_delalloc(inode, cur_offset,
1441 cur_offset + num_bytes - 1,
1442 locked_page, EXTENT_LOCKED |
1443 EXTENT_DELALLOC, PAGE_UNLOCK |
1444 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001445 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001446 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001447 cur_offset = extent_end;
1448 if (cur_offset > end)
1449 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001450 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001451 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001452
Josef Bacik17ca04a2012-05-31 15:58:55 -04001453 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001454 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001455 cur_offset = end;
1456 }
1457
Yan Zheng80ff3852008-10-30 14:20:02 -04001458 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001459 ret = cow_file_range(inode, locked_page, cow_start, end,
1460 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001461 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001462 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001463 }
1464
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001465error:
Miao Xiea698d0752012-09-20 01:51:59 -06001466 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001467 if (!ret)
1468 ret = err;
1469
Josef Bacik17ca04a2012-05-31 15:58:55 -04001470 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001471 extent_clear_unlock_delalloc(inode, cur_offset, end,
1472 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001473 EXTENT_DELALLOC | EXTENT_DEFRAG |
1474 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1475 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001476 PAGE_SET_WRITEBACK |
1477 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001478 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001479 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001480}
1481
Wang Shilong47059d92014-07-03 18:22:07 +08001482static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1483{
1484
1485 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1486 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1487 return 0;
1488
1489 /*
1490 * @defrag_bytes is a hint value, no spinlock held here,
1491 * if is not zero, it means the file is defragging.
1492 * Force cow if given extent needs to be defragged.
1493 */
1494 if (BTRFS_I(inode)->defrag_bytes &&
1495 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1496 EXTENT_DEFRAG, 0, NULL))
1497 return 1;
1498
1499 return 0;
1500}
1501
Chris Masond352ac62008-09-29 15:18:18 -04001502/*
1503 * extent_io.c call back to do delayed allocation processing
1504 */
Chris Masonc8b97812008-10-29 14:49:59 -04001505static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001506 u64 start, u64 end, int *page_started,
1507 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001508{
Chris Masonbe20aa92007-12-17 20:14:01 -05001509 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001510 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001511
Wang Shilong47059d92014-07-03 18:22:07 +08001512 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001513 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001514 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001515 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001516 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001517 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001518 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001519 ret = cow_file_range(inode, locked_page, start, end,
1520 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001521 } else {
1522 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1523 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001524 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001525 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001526 }
Chris Masonb888db22007-08-27 16:49:44 -04001527 return ret;
1528}
1529
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001530static void btrfs_split_extent_hook(struct inode *inode,
1531 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001532{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001533 u64 size;
1534
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001535 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001536 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001537 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001538
Josef Bacikdcab6a32015-02-11 15:08:59 -05001539 size = orig->end - orig->start + 1;
1540 if (size > BTRFS_MAX_EXTENT_SIZE) {
1541 u64 num_extents;
1542 u64 new_size;
1543
1544 /*
1545 * We need the largest size of the remaining extent to see if we
1546 * need to add a new outstanding extent. Think of the following
1547 * case
1548 *
1549 * [MEAX_EXTENT_SIZEx2 - 4k][4k]
1550 *
1551 * The new_size would just be 4k and we'd think we had enough
1552 * outstanding extents for this if we only took one side of the
1553 * split, same goes for the other direction. We need to see if
1554 * the larger size still is the same amount of extents as the
1555 * original size, because if it is we need to add a new
1556 * outstanding extent. But if we split up and the larger size
1557 * is less than the original then we are good to go since we've
1558 * already accounted for the extra extent in our original
1559 * accounting.
1560 */
1561 new_size = orig->end - split + 1;
1562 if ((split - orig->start) > new_size)
1563 new_size = split - orig->start;
1564
1565 num_extents = div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1566 BTRFS_MAX_EXTENT_SIZE);
1567 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1568 BTRFS_MAX_EXTENT_SIZE) < num_extents)
1569 return;
1570 }
1571
Josef Bacik9e0baf62011-07-15 15:16:44 +00001572 spin_lock(&BTRFS_I(inode)->lock);
1573 BTRFS_I(inode)->outstanding_extents++;
1574 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001575}
1576
1577/*
1578 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1579 * extents so we can keep track of new extents that are just merged onto old
1580 * extents, such as when we are doing sequential writes, so we can properly
1581 * account for the metadata space we'll need.
1582 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001583static void btrfs_merge_extent_hook(struct inode *inode,
1584 struct extent_state *new,
1585 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001586{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001587 u64 new_size, old_size;
1588 u64 num_extents;
1589
Josef Bacik9ed74f22009-09-11 16:12:44 -04001590 /* not delalloc, ignore it */
1591 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001592 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001593
Josef Bacikdcab6a32015-02-11 15:08:59 -05001594 old_size = other->end - other->start + 1;
1595 new_size = old_size + (new->end - new->start + 1);
1596
1597 /* we're not bigger than the max, unreserve the space and go */
1598 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1599 spin_lock(&BTRFS_I(inode)->lock);
1600 BTRFS_I(inode)->outstanding_extents--;
1601 spin_unlock(&BTRFS_I(inode)->lock);
1602 return;
1603 }
1604
1605 /*
1606 * If we grew by another max_extent, just return, we want to keep that
1607 * reserved amount.
1608 */
1609 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1610 BTRFS_MAX_EXTENT_SIZE);
1611 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1612 BTRFS_MAX_EXTENT_SIZE) > num_extents)
1613 return;
1614
Josef Bacik9e0baf62011-07-15 15:16:44 +00001615 spin_lock(&BTRFS_I(inode)->lock);
1616 BTRFS_I(inode)->outstanding_extents--;
1617 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001618}
1619
Miao Xieeb73c1b2013-05-15 07:48:22 +00001620static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1621 struct inode *inode)
1622{
1623 spin_lock(&root->delalloc_lock);
1624 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1625 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1626 &root->delalloc_inodes);
1627 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1628 &BTRFS_I(inode)->runtime_flags);
1629 root->nr_delalloc_inodes++;
1630 if (root->nr_delalloc_inodes == 1) {
1631 spin_lock(&root->fs_info->delalloc_root_lock);
1632 BUG_ON(!list_empty(&root->delalloc_root));
1633 list_add_tail(&root->delalloc_root,
1634 &root->fs_info->delalloc_roots);
1635 spin_unlock(&root->fs_info->delalloc_root_lock);
1636 }
1637 }
1638 spin_unlock(&root->delalloc_lock);
1639}
1640
1641static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1642 struct inode *inode)
1643{
1644 spin_lock(&root->delalloc_lock);
1645 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1646 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1647 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1648 &BTRFS_I(inode)->runtime_flags);
1649 root->nr_delalloc_inodes--;
1650 if (!root->nr_delalloc_inodes) {
1651 spin_lock(&root->fs_info->delalloc_root_lock);
1652 BUG_ON(list_empty(&root->delalloc_root));
1653 list_del_init(&root->delalloc_root);
1654 spin_unlock(&root->fs_info->delalloc_root_lock);
1655 }
1656 }
1657 spin_unlock(&root->delalloc_lock);
1658}
1659
Chris Masond352ac62008-09-29 15:18:18 -04001660/*
1661 * extent_io.c set_bit_hook, used to track delayed allocation
1662 * bytes in this file, and to maintain the list of inodes that
1663 * have pending delalloc work to be done.
1664 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001665static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001666 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001667{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001668
Wang Shilong47059d92014-07-03 18:22:07 +08001669 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1670 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001671 /*
1672 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001673 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001674 * bit, which is only set or cleared with irqs on
1675 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001676 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001677 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001678 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001679 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001680
Josef Bacik9e0baf62011-07-15 15:16:44 +00001681 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001682 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001683 } else {
1684 spin_lock(&BTRFS_I(inode)->lock);
1685 BTRFS_I(inode)->outstanding_extents++;
1686 spin_unlock(&BTRFS_I(inode)->lock);
1687 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001688
Miao Xie963d6782013-01-29 10:10:51 +00001689 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1690 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001691 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001692 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001693 if (*bits & EXTENT_DEFRAG)
1694 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001695 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001696 &BTRFS_I(inode)->runtime_flags))
1697 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001698 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001699 }
Chris Mason291d6732008-01-29 15:55:23 -05001700}
1701
Chris Masond352ac62008-09-29 15:18:18 -04001702/*
1703 * extent_io.c clear_bit_hook, see set_bit_hook for why
1704 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001705static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001706 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001707 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001708{
Wang Shilong47059d92014-07-03 18:22:07 +08001709 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001710 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1711 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001712
1713 spin_lock(&BTRFS_I(inode)->lock);
1714 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1715 BTRFS_I(inode)->defrag_bytes -= len;
1716 spin_unlock(&BTRFS_I(inode)->lock);
1717
Chris Mason75eff682008-12-15 15:54:40 -05001718 /*
1719 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001720 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001721 * bit, which is only set or cleared with irqs on
1722 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001723 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001724 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001725 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001726
Josef Bacik9e0baf62011-07-15 15:16:44 +00001727 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001728 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001729 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1730 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001731 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001732 spin_unlock(&BTRFS_I(inode)->lock);
1733 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001734
Josef Bacikb6d08f02013-09-27 14:57:43 -04001735 /*
1736 * We don't reserve metadata space for space cache inodes so we
1737 * don't need to call dellalloc_release_metadata if there is an
1738 * error.
1739 */
1740 if (*bits & EXTENT_DO_ACCOUNTING &&
1741 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001742 btrfs_delalloc_release_metadata(inode, len);
1743
Josef Bacik0cb59c92010-07-02 12:14:14 -04001744 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001745 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001746 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001747
Miao Xie963d6782013-01-29 10:10:51 +00001748 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1749 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001750 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001751 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001752 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001753 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001754 &BTRFS_I(inode)->runtime_flags))
1755 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001756 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001757 }
Chris Mason291d6732008-01-29 15:55:23 -05001758}
1759
Chris Masond352ac62008-09-29 15:18:18 -04001760/*
1761 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1762 * we don't create bios that span stripes or chunks
1763 */
David Woodhouse64a16702009-07-15 23:29:37 +01001764int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001765 size_t size, struct bio *bio,
1766 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001767{
1768 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001769 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001770 u64 length = 0;
1771 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001772 int ret;
1773
Chris Mason771ed682008-11-06 22:02:51 -05001774 if (bio_flags & EXTENT_BIO_COMPRESSED)
1775 return 0;
1776
Kent Overstreet4f024f32013-10-11 15:44:27 -07001777 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001778 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001779 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001780 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001781 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001782 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001783 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001784 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001785 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001786}
1787
Chris Masond352ac62008-09-29 15:18:18 -04001788/*
1789 * in order to insert checksums into the metadata in large chunks,
1790 * we wait until bio submission time. All the pages in the bio are
1791 * checksummed and sums are attached onto the ordered extent record.
1792 *
1793 * At IO completion time the cums attached on the ordered extent record
1794 * are inserted into the btree
1795 */
Chris Masond3977122009-01-05 21:25:51 -05001796static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1797 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001798 unsigned long bio_flags,
1799 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001800{
Chris Mason065631f2008-02-20 12:07:25 -05001801 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001802 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001803
Chris Masond20f7042008-12-08 16:58:54 -05001804 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001805 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001806 return 0;
1807}
Chris Masone0156402008-04-16 11:15:20 -04001808
Chris Mason4a69a412008-11-06 22:03:00 -05001809/*
1810 * in order to insert checksums into the metadata in large chunks,
1811 * we wait until bio submission time. All the pages in the bio are
1812 * checksummed and sums are attached onto the ordered extent record.
1813 *
1814 * At IO completion time the cums attached on the ordered extent record
1815 * are inserted into the btree
1816 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001817static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001818 int mirror_num, unsigned long bio_flags,
1819 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001820{
1821 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001822 int ret;
1823
1824 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1825 if (ret)
1826 bio_endio(bio, ret);
1827 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001828}
1829
Chris Masond352ac62008-09-29 15:18:18 -04001830/*
Chris Masoncad321a2008-12-17 14:51:42 -05001831 * extent_io.c submission hook. This does the right thing for csum calculation
1832 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001833 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001834static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001835 int mirror_num, unsigned long bio_flags,
1836 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001837{
1838 struct btrfs_root *root = BTRFS_I(inode)->root;
1839 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001840 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001841 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001842 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001843
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001844 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001845
Liu Bo83eea1f2012-07-10 05:28:39 -06001846 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001847 metadata = 2;
1848
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001849 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001850 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1851 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001852 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001853
Chris Masond20f7042008-12-08 16:58:54 -05001854 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001855 ret = btrfs_submit_compressed_read(inode, bio,
1856 mirror_num,
1857 bio_flags);
1858 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001859 } else if (!skip_sum) {
1860 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1861 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001862 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001863 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001864 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001865 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001866 /* csum items have already been cloned */
1867 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1868 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001869 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001870 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001871 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001872 bio_flags, bio_offset,
1873 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001874 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001875 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001876 } else if (!skip_sum) {
1877 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1878 if (ret)
1879 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001880 }
1881
Chris Mason0b86a832008-03-24 15:01:56 -04001882mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001883 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1884
1885out:
1886 if (ret < 0)
1887 bio_endio(bio, ret);
1888 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001889}
Chris Mason6885f302008-02-20 16:11:05 -05001890
Chris Masond352ac62008-09-29 15:18:18 -04001891/*
1892 * given a list of ordered sums record them in the inode. This happens
1893 * at IO completion time based on sums calculated at bio submission time.
1894 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001895static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001896 struct inode *inode, u64 file_offset,
1897 struct list_head *list)
1898{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001899 struct btrfs_ordered_sum *sum;
1900
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001901 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001902 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001903 btrfs_csum_file_blocks(trans,
1904 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001905 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001906 }
1907 return 0;
1908}
1909
Josef Bacik2ac55d42010-02-03 19:33:23 +00001910int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1911 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001912{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001913 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001914 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001915 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001916}
1917
Chris Masond352ac62008-09-29 15:18:18 -04001918/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001919struct btrfs_writepage_fixup {
1920 struct page *page;
1921 struct btrfs_work work;
1922};
1923
Christoph Hellwigb2950862008-12-02 09:54:17 -05001924static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001925{
1926 struct btrfs_writepage_fixup *fixup;
1927 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001928 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001929 struct page *page;
1930 struct inode *inode;
1931 u64 page_start;
1932 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001933 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001934
1935 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1936 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001937again:
Chris Mason247e7432008-07-17 12:53:51 -04001938 lock_page(page);
1939 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1940 ClearPageChecked(page);
1941 goto out_page;
1942 }
1943
1944 inode = page->mapping->host;
1945 page_start = page_offset(page);
1946 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1947
Josef Bacik2ac55d42010-02-03 19:33:23 +00001948 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001949 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001950
1951 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001952 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001953 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001954
1955 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1956 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001957 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1958 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001959 unlock_page(page);
1960 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001961 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001962 goto again;
1963 }
Chris Mason247e7432008-07-17 12:53:51 -04001964
Jeff Mahoney87826df2012-02-15 16:23:57 +01001965 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1966 if (ret) {
1967 mapping_set_error(page->mapping, ret);
1968 end_extent_writepage(page, ret, page_start, page_end);
1969 ClearPageChecked(page);
1970 goto out;
1971 }
1972
Josef Bacik2ac55d42010-02-03 19:33:23 +00001973 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001974 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001975 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001976out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001977 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1978 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001979out_page:
1980 unlock_page(page);
1981 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001982 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001983}
1984
1985/*
1986 * There are a few paths in the higher layers of the kernel that directly
1987 * set the page dirty bit without asking the filesystem if it is a
1988 * good idea. This causes problems because we want to make sure COW
1989 * properly happens and the data=ordered rules are followed.
1990 *
Chris Masonc8b97812008-10-29 14:49:59 -04001991 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001992 * hasn't been properly setup for IO. We kick off an async process
1993 * to fix it up. The async helper will wait for ordered extents, set
1994 * the delalloc bit and make it safe to write the page.
1995 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001996static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001997{
1998 struct inode *inode = page->mapping->host;
1999 struct btrfs_writepage_fixup *fixup;
2000 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002001
Chris Mason8b62b722009-09-02 16:53:46 -04002002 /* this page is properly in the ordered list */
2003 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002004 return 0;
2005
2006 if (PageChecked(page))
2007 return -EAGAIN;
2008
2009 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2010 if (!fixup)
2011 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002012
Chris Mason247e7432008-07-17 12:53:51 -04002013 SetPageChecked(page);
2014 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002015 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2016 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002017 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002018 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002019 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002020}
2021
Yan Zhengd899e052008-10-30 14:25:28 -04002022static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2023 struct inode *inode, u64 file_pos,
2024 u64 disk_bytenr, u64 disk_num_bytes,
2025 u64 num_bytes, u64 ram_bytes,
2026 u8 compression, u8 encryption,
2027 u16 other_encoding, int extent_type)
2028{
2029 struct btrfs_root *root = BTRFS_I(inode)->root;
2030 struct btrfs_file_extent_item *fi;
2031 struct btrfs_path *path;
2032 struct extent_buffer *leaf;
2033 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002034 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002035 int ret;
2036
2037 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002038 if (!path)
2039 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002040
Chris Masona1ed8352009-09-11 12:27:37 -04002041 /*
2042 * we may be replacing one extent in the tree with another.
2043 * The new extent is pinned in the extent map, and we don't want
2044 * to drop it from the cache until it is completely in the btree.
2045 *
2046 * So, tell btrfs_drop_extents to leave this extent in the cache.
2047 * the caller is expected to unpin it and allow it to be merged
2048 * with the others.
2049 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002050 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2051 file_pos + num_bytes, NULL, 0,
2052 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002053 if (ret)
2054 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002055
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002056 if (!extent_inserted) {
2057 ins.objectid = btrfs_ino(inode);
2058 ins.offset = file_pos;
2059 ins.type = BTRFS_EXTENT_DATA_KEY;
2060
2061 path->leave_spinning = 1;
2062 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2063 sizeof(*fi));
2064 if (ret)
2065 goto out;
2066 }
Yan Zhengd899e052008-10-30 14:25:28 -04002067 leaf = path->nodes[0];
2068 fi = btrfs_item_ptr(leaf, path->slots[0],
2069 struct btrfs_file_extent_item);
2070 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2071 btrfs_set_file_extent_type(leaf, fi, extent_type);
2072 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2073 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2074 btrfs_set_file_extent_offset(leaf, fi, 0);
2075 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2076 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2077 btrfs_set_file_extent_compression(leaf, fi, compression);
2078 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2079 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002080
Yan Zhengd899e052008-10-30 14:25:28 -04002081 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002082 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002083
2084 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002085
2086 ins.objectid = disk_bytenr;
2087 ins.offset = disk_num_bytes;
2088 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002089 ret = btrfs_alloc_reserved_file_extent(trans, root,
2090 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002091 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002092out:
Yan Zhengd899e052008-10-30 14:25:28 -04002093 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002094
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002095 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002096}
2097
Liu Bo38c227d2013-01-29 03:18:40 +00002098/* snapshot-aware defrag */
2099struct sa_defrag_extent_backref {
2100 struct rb_node node;
2101 struct old_sa_defrag_extent *old;
2102 u64 root_id;
2103 u64 inum;
2104 u64 file_pos;
2105 u64 extent_offset;
2106 u64 num_bytes;
2107 u64 generation;
2108};
2109
2110struct old_sa_defrag_extent {
2111 struct list_head list;
2112 struct new_sa_defrag_extent *new;
2113
2114 u64 extent_offset;
2115 u64 bytenr;
2116 u64 offset;
2117 u64 len;
2118 int count;
2119};
2120
2121struct new_sa_defrag_extent {
2122 struct rb_root root;
2123 struct list_head head;
2124 struct btrfs_path *path;
2125 struct inode *inode;
2126 u64 file_pos;
2127 u64 len;
2128 u64 bytenr;
2129 u64 disk_len;
2130 u8 compress_type;
2131};
2132
2133static int backref_comp(struct sa_defrag_extent_backref *b1,
2134 struct sa_defrag_extent_backref *b2)
2135{
2136 if (b1->root_id < b2->root_id)
2137 return -1;
2138 else if (b1->root_id > b2->root_id)
2139 return 1;
2140
2141 if (b1->inum < b2->inum)
2142 return -1;
2143 else if (b1->inum > b2->inum)
2144 return 1;
2145
2146 if (b1->file_pos < b2->file_pos)
2147 return -1;
2148 else if (b1->file_pos > b2->file_pos)
2149 return 1;
2150
2151 /*
2152 * [------------------------------] ===> (a range of space)
2153 * |<--->| |<---->| =============> (fs/file tree A)
2154 * |<---------------------------->| ===> (fs/file tree B)
2155 *
2156 * A range of space can refer to two file extents in one tree while
2157 * refer to only one file extent in another tree.
2158 *
2159 * So we may process a disk offset more than one time(two extents in A)
2160 * and locate at the same extent(one extent in B), then insert two same
2161 * backrefs(both refer to the extent in B).
2162 */
2163 return 0;
2164}
2165
2166static void backref_insert(struct rb_root *root,
2167 struct sa_defrag_extent_backref *backref)
2168{
2169 struct rb_node **p = &root->rb_node;
2170 struct rb_node *parent = NULL;
2171 struct sa_defrag_extent_backref *entry;
2172 int ret;
2173
2174 while (*p) {
2175 parent = *p;
2176 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2177
2178 ret = backref_comp(backref, entry);
2179 if (ret < 0)
2180 p = &(*p)->rb_left;
2181 else
2182 p = &(*p)->rb_right;
2183 }
2184
2185 rb_link_node(&backref->node, parent, p);
2186 rb_insert_color(&backref->node, root);
2187}
2188
2189/*
2190 * Note the backref might has changed, and in this case we just return 0.
2191 */
2192static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2193 void *ctx)
2194{
2195 struct btrfs_file_extent_item *extent;
2196 struct btrfs_fs_info *fs_info;
2197 struct old_sa_defrag_extent *old = ctx;
2198 struct new_sa_defrag_extent *new = old->new;
2199 struct btrfs_path *path = new->path;
2200 struct btrfs_key key;
2201 struct btrfs_root *root;
2202 struct sa_defrag_extent_backref *backref;
2203 struct extent_buffer *leaf;
2204 struct inode *inode = new->inode;
2205 int slot;
2206 int ret;
2207 u64 extent_offset;
2208 u64 num_bytes;
2209
2210 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2211 inum == btrfs_ino(inode))
2212 return 0;
2213
2214 key.objectid = root_id;
2215 key.type = BTRFS_ROOT_ITEM_KEY;
2216 key.offset = (u64)-1;
2217
2218 fs_info = BTRFS_I(inode)->root->fs_info;
2219 root = btrfs_read_fs_root_no_name(fs_info, &key);
2220 if (IS_ERR(root)) {
2221 if (PTR_ERR(root) == -ENOENT)
2222 return 0;
2223 WARN_ON(1);
2224 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2225 inum, offset, root_id);
2226 return PTR_ERR(root);
2227 }
2228
2229 key.objectid = inum;
2230 key.type = BTRFS_EXTENT_DATA_KEY;
2231 if (offset > (u64)-1 << 32)
2232 key.offset = 0;
2233 else
2234 key.offset = offset;
2235
2236 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302237 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002238 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002239 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002240
2241 while (1) {
2242 cond_resched();
2243
2244 leaf = path->nodes[0];
2245 slot = path->slots[0];
2246
2247 if (slot >= btrfs_header_nritems(leaf)) {
2248 ret = btrfs_next_leaf(root, path);
2249 if (ret < 0) {
2250 goto out;
2251 } else if (ret > 0) {
2252 ret = 0;
2253 goto out;
2254 }
2255 continue;
2256 }
2257
2258 path->slots[0]++;
2259
2260 btrfs_item_key_to_cpu(leaf, &key, slot);
2261
2262 if (key.objectid > inum)
2263 goto out;
2264
2265 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2266 continue;
2267
2268 extent = btrfs_item_ptr(leaf, slot,
2269 struct btrfs_file_extent_item);
2270
2271 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2272 continue;
2273
Liu Boe68afa42013-07-01 22:13:26 +08002274 /*
2275 * 'offset' refers to the exact key.offset,
2276 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2277 * (key.offset - extent_offset).
2278 */
2279 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002280 continue;
2281
Liu Boe68afa42013-07-01 22:13:26 +08002282 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002283 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002284
Liu Bo38c227d2013-01-29 03:18:40 +00002285 if (extent_offset >= old->extent_offset + old->offset +
2286 old->len || extent_offset + num_bytes <=
2287 old->extent_offset + old->offset)
2288 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002289 break;
2290 }
2291
2292 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2293 if (!backref) {
2294 ret = -ENOENT;
2295 goto out;
2296 }
2297
2298 backref->root_id = root_id;
2299 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002300 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002301 backref->num_bytes = num_bytes;
2302 backref->extent_offset = extent_offset;
2303 backref->generation = btrfs_file_extent_generation(leaf, extent);
2304 backref->old = old;
2305 backref_insert(&new->root, backref);
2306 old->count++;
2307out:
2308 btrfs_release_path(path);
2309 WARN_ON(ret);
2310 return ret;
2311}
2312
2313static noinline bool record_extent_backrefs(struct btrfs_path *path,
2314 struct new_sa_defrag_extent *new)
2315{
2316 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2317 struct old_sa_defrag_extent *old, *tmp;
2318 int ret;
2319
2320 new->path = path;
2321
2322 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002323 ret = iterate_inodes_from_logical(old->bytenr +
2324 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002325 path, record_one_backref,
2326 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002327 if (ret < 0 && ret != -ENOENT)
2328 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002329
2330 /* no backref to be processed for this extent */
2331 if (!old->count) {
2332 list_del(&old->list);
2333 kfree(old);
2334 }
2335 }
2336
2337 if (list_empty(&new->head))
2338 return false;
2339
2340 return true;
2341}
2342
2343static int relink_is_mergable(struct extent_buffer *leaf,
2344 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002345 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002346{
Liu Bo116e0022013-08-02 16:30:40 +08002347 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002348 return 0;
2349
2350 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2351 return 0;
2352
Liu Bo116e0022013-08-02 16:30:40 +08002353 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2354 return 0;
2355
2356 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002357 btrfs_file_extent_other_encoding(leaf, fi))
2358 return 0;
2359
2360 return 1;
2361}
2362
2363/*
2364 * Note the backref might has changed, and in this case we just return 0.
2365 */
2366static noinline int relink_extent_backref(struct btrfs_path *path,
2367 struct sa_defrag_extent_backref *prev,
2368 struct sa_defrag_extent_backref *backref)
2369{
2370 struct btrfs_file_extent_item *extent;
2371 struct btrfs_file_extent_item *item;
2372 struct btrfs_ordered_extent *ordered;
2373 struct btrfs_trans_handle *trans;
2374 struct btrfs_fs_info *fs_info;
2375 struct btrfs_root *root;
2376 struct btrfs_key key;
2377 struct extent_buffer *leaf;
2378 struct old_sa_defrag_extent *old = backref->old;
2379 struct new_sa_defrag_extent *new = old->new;
2380 struct inode *src_inode = new->inode;
2381 struct inode *inode;
2382 struct extent_state *cached = NULL;
2383 int ret = 0;
2384 u64 start;
2385 u64 len;
2386 u64 lock_start;
2387 u64 lock_end;
2388 bool merge = false;
2389 int index;
2390
2391 if (prev && prev->root_id == backref->root_id &&
2392 prev->inum == backref->inum &&
2393 prev->file_pos + prev->num_bytes == backref->file_pos)
2394 merge = true;
2395
2396 /* step 1: get root */
2397 key.objectid = backref->root_id;
2398 key.type = BTRFS_ROOT_ITEM_KEY;
2399 key.offset = (u64)-1;
2400
2401 fs_info = BTRFS_I(src_inode)->root->fs_info;
2402 index = srcu_read_lock(&fs_info->subvol_srcu);
2403
2404 root = btrfs_read_fs_root_no_name(fs_info, &key);
2405 if (IS_ERR(root)) {
2406 srcu_read_unlock(&fs_info->subvol_srcu, index);
2407 if (PTR_ERR(root) == -ENOENT)
2408 return 0;
2409 return PTR_ERR(root);
2410 }
Liu Bo38c227d2013-01-29 03:18:40 +00002411
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002412 if (btrfs_root_readonly(root)) {
2413 srcu_read_unlock(&fs_info->subvol_srcu, index);
2414 return 0;
2415 }
2416
Liu Bo38c227d2013-01-29 03:18:40 +00002417 /* step 2: get inode */
2418 key.objectid = backref->inum;
2419 key.type = BTRFS_INODE_ITEM_KEY;
2420 key.offset = 0;
2421
2422 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2423 if (IS_ERR(inode)) {
2424 srcu_read_unlock(&fs_info->subvol_srcu, index);
2425 return 0;
2426 }
2427
2428 srcu_read_unlock(&fs_info->subvol_srcu, index);
2429
2430 /* step 3: relink backref */
2431 lock_start = backref->file_pos;
2432 lock_end = backref->file_pos + backref->num_bytes - 1;
2433 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2434 0, &cached);
2435
2436 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2437 if (ordered) {
2438 btrfs_put_ordered_extent(ordered);
2439 goto out_unlock;
2440 }
2441
2442 trans = btrfs_join_transaction(root);
2443 if (IS_ERR(trans)) {
2444 ret = PTR_ERR(trans);
2445 goto out_unlock;
2446 }
2447
2448 key.objectid = backref->inum;
2449 key.type = BTRFS_EXTENT_DATA_KEY;
2450 key.offset = backref->file_pos;
2451
2452 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2453 if (ret < 0) {
2454 goto out_free_path;
2455 } else if (ret > 0) {
2456 ret = 0;
2457 goto out_free_path;
2458 }
2459
2460 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2461 struct btrfs_file_extent_item);
2462
2463 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2464 backref->generation)
2465 goto out_free_path;
2466
2467 btrfs_release_path(path);
2468
2469 start = backref->file_pos;
2470 if (backref->extent_offset < old->extent_offset + old->offset)
2471 start += old->extent_offset + old->offset -
2472 backref->extent_offset;
2473
2474 len = min(backref->extent_offset + backref->num_bytes,
2475 old->extent_offset + old->offset + old->len);
2476 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2477
2478 ret = btrfs_drop_extents(trans, root, inode, start,
2479 start + len, 1);
2480 if (ret)
2481 goto out_free_path;
2482again:
2483 key.objectid = btrfs_ino(inode);
2484 key.type = BTRFS_EXTENT_DATA_KEY;
2485 key.offset = start;
2486
Liu Boa09a0a72013-03-11 09:20:58 +00002487 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002488 if (merge) {
2489 struct btrfs_file_extent_item *fi;
2490 u64 extent_len;
2491 struct btrfs_key found_key;
2492
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002493 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002494 if (ret < 0)
2495 goto out_free_path;
2496
2497 path->slots[0]--;
2498 leaf = path->nodes[0];
2499 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2500
2501 fi = btrfs_item_ptr(leaf, path->slots[0],
2502 struct btrfs_file_extent_item);
2503 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2504
Liu Bo116e0022013-08-02 16:30:40 +08002505 if (extent_len + found_key.offset == start &&
2506 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002507 btrfs_set_file_extent_num_bytes(leaf, fi,
2508 extent_len + len);
2509 btrfs_mark_buffer_dirty(leaf);
2510 inode_add_bytes(inode, len);
2511
2512 ret = 1;
2513 goto out_free_path;
2514 } else {
2515 merge = false;
2516 btrfs_release_path(path);
2517 goto again;
2518 }
2519 }
2520
2521 ret = btrfs_insert_empty_item(trans, root, path, &key,
2522 sizeof(*extent));
2523 if (ret) {
2524 btrfs_abort_transaction(trans, root, ret);
2525 goto out_free_path;
2526 }
2527
2528 leaf = path->nodes[0];
2529 item = btrfs_item_ptr(leaf, path->slots[0],
2530 struct btrfs_file_extent_item);
2531 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2532 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2533 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2534 btrfs_set_file_extent_num_bytes(leaf, item, len);
2535 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2536 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2537 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2538 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2539 btrfs_set_file_extent_encryption(leaf, item, 0);
2540 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2541
2542 btrfs_mark_buffer_dirty(leaf);
2543 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002544 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002545
2546 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2547 new->disk_len, 0,
2548 backref->root_id, backref->inum,
2549 new->file_pos, 0); /* start - extent_offset */
2550 if (ret) {
2551 btrfs_abort_transaction(trans, root, ret);
2552 goto out_free_path;
2553 }
2554
2555 ret = 1;
2556out_free_path:
2557 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002558 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002559 btrfs_end_transaction(trans, root);
2560out_unlock:
2561 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2562 &cached, GFP_NOFS);
2563 iput(inode);
2564 return ret;
2565}
2566
Liu Bo6f519562013-10-29 10:45:05 +08002567static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2568{
2569 struct old_sa_defrag_extent *old, *tmp;
2570
2571 if (!new)
2572 return;
2573
2574 list_for_each_entry_safe(old, tmp, &new->head, list) {
2575 list_del(&old->list);
2576 kfree(old);
2577 }
2578 kfree(new);
2579}
2580
Liu Bo38c227d2013-01-29 03:18:40 +00002581static void relink_file_extents(struct new_sa_defrag_extent *new)
2582{
2583 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002584 struct sa_defrag_extent_backref *backref;
2585 struct sa_defrag_extent_backref *prev = NULL;
2586 struct inode *inode;
2587 struct btrfs_root *root;
2588 struct rb_node *node;
2589 int ret;
2590
2591 inode = new->inode;
2592 root = BTRFS_I(inode)->root;
2593
2594 path = btrfs_alloc_path();
2595 if (!path)
2596 return;
2597
2598 if (!record_extent_backrefs(path, new)) {
2599 btrfs_free_path(path);
2600 goto out;
2601 }
2602 btrfs_release_path(path);
2603
2604 while (1) {
2605 node = rb_first(&new->root);
2606 if (!node)
2607 break;
2608 rb_erase(node, &new->root);
2609
2610 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2611
2612 ret = relink_extent_backref(path, prev, backref);
2613 WARN_ON(ret < 0);
2614
2615 kfree(prev);
2616
2617 if (ret == 1)
2618 prev = backref;
2619 else
2620 prev = NULL;
2621 cond_resched();
2622 }
2623 kfree(prev);
2624
2625 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002626out:
Liu Bo6f519562013-10-29 10:45:05 +08002627 free_sa_defrag_extent(new);
2628
Liu Bo38c227d2013-01-29 03:18:40 +00002629 atomic_dec(&root->fs_info->defrag_running);
2630 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002631}
2632
2633static struct new_sa_defrag_extent *
2634record_old_file_extents(struct inode *inode,
2635 struct btrfs_ordered_extent *ordered)
2636{
2637 struct btrfs_root *root = BTRFS_I(inode)->root;
2638 struct btrfs_path *path;
2639 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002640 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002641 struct new_sa_defrag_extent *new;
2642 int ret;
2643
2644 new = kmalloc(sizeof(*new), GFP_NOFS);
2645 if (!new)
2646 return NULL;
2647
2648 new->inode = inode;
2649 new->file_pos = ordered->file_offset;
2650 new->len = ordered->len;
2651 new->bytenr = ordered->start;
2652 new->disk_len = ordered->disk_len;
2653 new->compress_type = ordered->compress_type;
2654 new->root = RB_ROOT;
2655 INIT_LIST_HEAD(&new->head);
2656
2657 path = btrfs_alloc_path();
2658 if (!path)
2659 goto out_kfree;
2660
2661 key.objectid = btrfs_ino(inode);
2662 key.type = BTRFS_EXTENT_DATA_KEY;
2663 key.offset = new->file_pos;
2664
2665 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2666 if (ret < 0)
2667 goto out_free_path;
2668 if (ret > 0 && path->slots[0] > 0)
2669 path->slots[0]--;
2670
2671 /* find out all the old extents for the file range */
2672 while (1) {
2673 struct btrfs_file_extent_item *extent;
2674 struct extent_buffer *l;
2675 int slot;
2676 u64 num_bytes;
2677 u64 offset;
2678 u64 end;
2679 u64 disk_bytenr;
2680 u64 extent_offset;
2681
2682 l = path->nodes[0];
2683 slot = path->slots[0];
2684
2685 if (slot >= btrfs_header_nritems(l)) {
2686 ret = btrfs_next_leaf(root, path);
2687 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002688 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002689 else if (ret > 0)
2690 break;
2691 continue;
2692 }
2693
2694 btrfs_item_key_to_cpu(l, &key, slot);
2695
2696 if (key.objectid != btrfs_ino(inode))
2697 break;
2698 if (key.type != BTRFS_EXTENT_DATA_KEY)
2699 break;
2700 if (key.offset >= new->file_pos + new->len)
2701 break;
2702
2703 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2704
2705 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2706 if (key.offset + num_bytes < new->file_pos)
2707 goto next;
2708
2709 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2710 if (!disk_bytenr)
2711 goto next;
2712
2713 extent_offset = btrfs_file_extent_offset(l, extent);
2714
2715 old = kmalloc(sizeof(*old), GFP_NOFS);
2716 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002717 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002718
2719 offset = max(new->file_pos, key.offset);
2720 end = min(new->file_pos + new->len, key.offset + num_bytes);
2721
2722 old->bytenr = disk_bytenr;
2723 old->extent_offset = extent_offset;
2724 old->offset = offset - key.offset;
2725 old->len = end - offset;
2726 old->new = new;
2727 old->count = 0;
2728 list_add_tail(&old->list, &new->head);
2729next:
2730 path->slots[0]++;
2731 cond_resched();
2732 }
2733
2734 btrfs_free_path(path);
2735 atomic_inc(&root->fs_info->defrag_running);
2736
2737 return new;
2738
Liu Bo38c227d2013-01-29 03:18:40 +00002739out_free_path:
2740 btrfs_free_path(path);
2741out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002742 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002743 return NULL;
2744}
2745
Miao Xiee570fd22014-06-19 10:42:50 +08002746static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2747 u64 start, u64 len)
2748{
2749 struct btrfs_block_group_cache *cache;
2750
2751 cache = btrfs_lookup_block_group(root->fs_info, start);
2752 ASSERT(cache);
2753
2754 spin_lock(&cache->lock);
2755 cache->delalloc_bytes -= len;
2756 spin_unlock(&cache->lock);
2757
2758 btrfs_put_block_group(cache);
2759}
2760
Chris Masond352ac62008-09-29 15:18:18 -04002761/* as ordered data IO finishes, this gets called so we can finish
2762 * an ordered extent if the range of bytes in the file it covers are
2763 * fully written.
2764 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002765static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002766{
Josef Bacik5fd02042012-05-02 14:00:54 -04002767 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002768 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002769 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002770 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002771 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002772 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002773 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002774 int ret = 0;
2775 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002776 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002777 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002778
Liu Bo83eea1f2012-07-10 05:28:39 -06002779 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002780
Josef Bacik5fd02042012-05-02 14:00:54 -04002781 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2782 ret = -EIO;
2783 goto out;
2784 }
2785
Miao Xief6124962014-09-12 18:44:04 +08002786 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2787 ordered_extent->file_offset +
2788 ordered_extent->len - 1);
2789
Josef Bacik77cef2e2013-08-29 13:57:21 -04002790 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2791 truncated = true;
2792 logical_len = ordered_extent->truncated_len;
2793 /* Truncated the entire extent, don't bother adding */
2794 if (!logical_len)
2795 goto out;
2796 }
2797
Yan, Zhengc2167752009-11-12 09:34:21 +00002798 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002799 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002800 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2801 if (nolock)
2802 trans = btrfs_join_transaction_nolock(root);
2803 else
2804 trans = btrfs_join_transaction(root);
2805 if (IS_ERR(trans)) {
2806 ret = PTR_ERR(trans);
2807 trans = NULL;
2808 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002809 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002810 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2811 ret = btrfs_update_inode_fallback(trans, root, inode);
2812 if (ret) /* -ENOMEM or corruption */
2813 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002814 goto out;
2815 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002816
Josef Bacik2ac55d42010-02-03 19:33:23 +00002817 lock_extent_bits(io_tree, ordered_extent->file_offset,
2818 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002819 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002820
Liu Bo38c227d2013-01-29 03:18:40 +00002821 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2822 ordered_extent->file_offset + ordered_extent->len - 1,
2823 EXTENT_DEFRAG, 1, cached_state);
2824 if (ret) {
2825 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002826 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002827 /* the inode is shared */
2828 new = record_old_file_extents(inode, ordered_extent);
2829
2830 clear_extent_bit(io_tree, ordered_extent->file_offset,
2831 ordered_extent->file_offset + ordered_extent->len - 1,
2832 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2833 }
2834
Josef Bacik0cb59c92010-07-02 12:14:14 -04002835 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002836 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002837 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002838 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002839 if (IS_ERR(trans)) {
2840 ret = PTR_ERR(trans);
2841 trans = NULL;
2842 goto out_unlock;
2843 }
Chris Masona79b7d42014-05-22 16:18:52 -07002844
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002845 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002846
Chris Masonc8b97812008-10-29 14:49:59 -04002847 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002848 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002849 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002850 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002851 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002852 ordered_extent->file_offset,
2853 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002854 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002855 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002856 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002857 ret = insert_reserved_file_extent(trans, inode,
2858 ordered_extent->file_offset,
2859 ordered_extent->start,
2860 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002861 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002862 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002863 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002864 if (!ret)
2865 btrfs_release_delalloc_bytes(root,
2866 ordered_extent->start,
2867 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002868 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002869 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2870 ordered_extent->file_offset, ordered_extent->len,
2871 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002872 if (ret < 0) {
2873 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002874 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002875 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002876
Chris Masone6dcd2d2008-07-17 12:53:50 -04002877 add_pending_csums(trans, inode, ordered_extent->file_offset,
2878 &ordered_extent->list);
2879
Josef Bacik6c760c02012-11-09 10:53:21 -05002880 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2881 ret = btrfs_update_inode_fallback(trans, root, inode);
2882 if (ret) { /* -ENOMEM or corruption */
2883 btrfs_abort_transaction(trans, root, ret);
2884 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002885 }
2886 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002887out_unlock:
2888 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2889 ordered_extent->file_offset +
2890 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002891out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002892 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002893 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002894 if (trans)
2895 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002896
Josef Bacik77cef2e2013-08-29 13:57:21 -04002897 if (ret || truncated) {
2898 u64 start, end;
2899
2900 if (truncated)
2901 start = ordered_extent->file_offset + logical_len;
2902 else
2903 start = ordered_extent->file_offset;
2904 end = ordered_extent->file_offset + ordered_extent->len - 1;
2905 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2906
2907 /* Drop the cache for the part of the extent we didn't write. */
2908 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002909
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002910 /*
2911 * If the ordered extent had an IOERR or something else went
2912 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002913 * back to the allocator. We only free the extent in the
2914 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002915 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002916 if ((ret || !logical_len) &&
2917 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002918 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2919 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002920 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002921 }
2922
2923
Josef Bacik5fd02042012-05-02 14:00:54 -04002924 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002925 * This needs to be done to make sure anybody waiting knows we are done
2926 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002927 */
2928 btrfs_remove_ordered_extent(inode, ordered_extent);
2929
Liu Bo38c227d2013-01-29 03:18:40 +00002930 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002931 if (new) {
2932 if (ret) {
2933 free_sa_defrag_extent(new);
2934 atomic_dec(&root->fs_info->defrag_running);
2935 } else {
2936 relink_file_extents(new);
2937 }
2938 }
Liu Bo38c227d2013-01-29 03:18:40 +00002939
Chris Masone6dcd2d2008-07-17 12:53:50 -04002940 /* once for us */
2941 btrfs_put_ordered_extent(ordered_extent);
2942 /* once for the tree */
2943 btrfs_put_ordered_extent(ordered_extent);
2944
Josef Bacik5fd02042012-05-02 14:00:54 -04002945 return ret;
2946}
2947
2948static void finish_ordered_fn(struct btrfs_work *work)
2949{
2950 struct btrfs_ordered_extent *ordered_extent;
2951 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2952 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002953}
2954
Christoph Hellwigb2950862008-12-02 09:54:17 -05002955static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002956 struct extent_state *state, int uptodate)
2957{
Josef Bacik5fd02042012-05-02 14:00:54 -04002958 struct inode *inode = page->mapping->host;
2959 struct btrfs_root *root = BTRFS_I(inode)->root;
2960 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002961 struct btrfs_workqueue *wq;
2962 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002963
liubo1abe9b82011-03-24 11:18:59 +00002964 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2965
Chris Mason8b62b722009-09-02 16:53:46 -04002966 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002967 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2968 end - start + 1, uptodate))
2969 return 0;
2970
Liu Bo9e0af232014-08-15 23:36:53 +08002971 if (btrfs_is_free_space_inode(inode)) {
2972 wq = root->fs_info->endio_freespace_worker;
2973 func = btrfs_freespace_write_helper;
2974 } else {
2975 wq = root->fs_info->endio_write_workers;
2976 func = btrfs_endio_write_helper;
2977 }
Josef Bacik5fd02042012-05-02 14:00:54 -04002978
Liu Bo9e0af232014-08-15 23:36:53 +08002979 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
2980 NULL);
2981 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002982
2983 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002984}
2985
Miao Xiedc380ae2014-09-12 18:43:55 +08002986static int __readpage_endio_check(struct inode *inode,
2987 struct btrfs_io_bio *io_bio,
2988 int icsum, struct page *page,
2989 int pgoff, u64 start, size_t len)
2990{
2991 char *kaddr;
2992 u32 csum_expected;
2993 u32 csum = ~(u32)0;
2994 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2995 DEFAULT_RATELIMIT_BURST);
2996
2997 csum_expected = *(((u32 *)io_bio->csum) + icsum);
2998
2999 kaddr = kmap_atomic(page);
3000 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3001 btrfs_csum_final(csum, (char *)&csum);
3002 if (csum != csum_expected)
3003 goto zeroit;
3004
3005 kunmap_atomic(kaddr);
3006 return 0;
3007zeroit:
3008 if (__ratelimit(&_rs))
David Sterbaf0954c62014-12-19 18:38:44 +01003009 btrfs_warn(BTRFS_I(inode)->root->fs_info,
Miao Xiedc380ae2014-09-12 18:43:55 +08003010 "csum failed ino %llu off %llu csum %u expected csum %u",
3011 btrfs_ino(inode), start, csum, csum_expected);
3012 memset(kaddr + pgoff, 1, len);
3013 flush_dcache_page(page);
3014 kunmap_atomic(kaddr);
3015 if (csum_expected == 0)
3016 return 0;
3017 return -EIO;
3018}
3019
Chris Masond352ac62008-09-29 15:18:18 -04003020/*
Chris Masond352ac62008-09-29 15:18:18 -04003021 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003022 * if there's a match, we allow the bio to finish. If not, the code in
3023 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003024 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003025static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3026 u64 phy_offset, struct page *page,
3027 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003028{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003029 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003030 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003031 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003032 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003033
Chris Masond20f7042008-12-08 16:58:54 -05003034 if (PageChecked(page)) {
3035 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003036 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003037 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003038
3039 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003040 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003041
Yan Zheng17d217f2008-12-12 10:03:38 -05003042 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003043 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05003044 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
3045 GFP_NOFS);
3046 return 0;
3047 }
3048
Miao Xiefacc8a222013-07-25 19:22:34 +08003049 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003050 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3051 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003052}
Chris Masonb888db22007-08-27 16:49:44 -04003053
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003054struct delayed_iput {
3055 struct list_head list;
3056 struct inode *inode;
3057};
3058
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003059/* JDM: If this is fs-wide, why can't we add a pointer to
3060 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003061void btrfs_add_delayed_iput(struct inode *inode)
3062{
3063 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3064 struct delayed_iput *delayed;
3065
3066 if (atomic_add_unless(&inode->i_count, -1, 1))
3067 return;
3068
3069 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3070 delayed->inode = inode;
3071
3072 spin_lock(&fs_info->delayed_iput_lock);
3073 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3074 spin_unlock(&fs_info->delayed_iput_lock);
3075}
3076
3077void btrfs_run_delayed_iputs(struct btrfs_root *root)
3078{
3079 LIST_HEAD(list);
3080 struct btrfs_fs_info *fs_info = root->fs_info;
3081 struct delayed_iput *delayed;
3082 int empty;
3083
3084 spin_lock(&fs_info->delayed_iput_lock);
3085 empty = list_empty(&fs_info->delayed_iputs);
3086 spin_unlock(&fs_info->delayed_iput_lock);
3087 if (empty)
3088 return;
3089
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003090 spin_lock(&fs_info->delayed_iput_lock);
3091 list_splice_init(&fs_info->delayed_iputs, &list);
3092 spin_unlock(&fs_info->delayed_iput_lock);
3093
3094 while (!list_empty(&list)) {
3095 delayed = list_entry(list.next, struct delayed_iput, list);
3096 list_del(&delayed->list);
3097 iput(delayed->inode);
3098 kfree(delayed);
3099 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003100}
3101
Yan, Zhengd68fc572010-05-16 10:49:58 -04003102/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003103 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003104 * files in the subvolume, it removes orphan item and frees block_rsv
3105 * structure.
3106 */
3107void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3108 struct btrfs_root *root)
3109{
Josef Bacik90290e12011-12-02 15:44:12 -05003110 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003111 int ret;
3112
Josef Bacik8a35d952012-05-23 14:26:42 -04003113 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003114 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3115 return;
3116
Josef Bacik90290e12011-12-02 15:44:12 -05003117 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003118 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003119 spin_unlock(&root->orphan_lock);
3120 return;
3121 }
3122
3123 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3124 spin_unlock(&root->orphan_lock);
3125 return;
3126 }
3127
3128 block_rsv = root->orphan_block_rsv;
3129 root->orphan_block_rsv = NULL;
3130 spin_unlock(&root->orphan_lock);
3131
Miao Xie27cdeb72014-04-02 19:51:05 +08003132 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003133 btrfs_root_refs(&root->root_item) > 0) {
3134 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3135 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003136 if (ret)
3137 btrfs_abort_transaction(trans, root, ret);
3138 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003139 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3140 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003141 }
3142
Josef Bacik90290e12011-12-02 15:44:12 -05003143 if (block_rsv) {
3144 WARN_ON(block_rsv->size > 0);
3145 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003146 }
3147}
3148
3149/*
Josef Bacik7b128762008-07-24 12:17:14 -04003150 * This creates an orphan entry for the given inode in case something goes
3151 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003152 *
3153 * NOTE: caller of this function should reserve 5 units of metadata for
3154 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003155 */
3156int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3157{
3158 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003159 struct btrfs_block_rsv *block_rsv = NULL;
3160 int reserve = 0;
3161 int insert = 0;
3162 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003163
Yan, Zhengd68fc572010-05-16 10:49:58 -04003164 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003165 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003166 if (!block_rsv)
3167 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003168 }
3169
Yan, Zhengd68fc572010-05-16 10:49:58 -04003170 spin_lock(&root->orphan_lock);
3171 if (!root->orphan_block_rsv) {
3172 root->orphan_block_rsv = block_rsv;
3173 } else if (block_rsv) {
3174 btrfs_free_block_rsv(root, block_rsv);
3175 block_rsv = NULL;
3176 }
Josef Bacik7b128762008-07-24 12:17:14 -04003177
Josef Bacik8a35d952012-05-23 14:26:42 -04003178 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3179 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003180#if 0
3181 /*
3182 * For proper ENOSPC handling, we should do orphan
3183 * cleanup when mounting. But this introduces backward
3184 * compatibility issue.
3185 */
3186 if (!xchg(&root->orphan_item_inserted, 1))
3187 insert = 2;
3188 else
3189 insert = 1;
3190#endif
3191 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003192 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003193 }
Josef Bacik7b128762008-07-24 12:17:14 -04003194
Josef Bacik72ac3c02012-05-23 14:13:11 -04003195 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3196 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003197 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003198 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003199
Yan, Zhengd68fc572010-05-16 10:49:58 -04003200 /* grab metadata reservation from transaction handle */
3201 if (reserve) {
3202 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003203 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003204 }
3205
3206 /* insert an orphan item to track this unlinked/truncated file */
3207 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003208 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003209 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003210 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003211 if (reserve) {
3212 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3213 &BTRFS_I(inode)->runtime_flags);
3214 btrfs_orphan_release_metadata(inode);
3215 }
3216 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003217 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3218 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003219 btrfs_abort_transaction(trans, root, ret);
3220 return ret;
3221 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003222 }
3223 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003224 }
3225
3226 /* insert an orphan item to track subvolume contains orphan files */
3227 if (insert >= 2) {
3228 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3229 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003230 if (ret && ret != -EEXIST) {
3231 btrfs_abort_transaction(trans, root, ret);
3232 return ret;
3233 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003234 }
3235 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003236}
3237
3238/*
3239 * We have done the truncate/delete so we can go ahead and remove the orphan
3240 * item for this particular inode.
3241 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003242static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3243 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003244{
3245 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003246 int delete_item = 0;
3247 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003248 int ret = 0;
3249
Yan, Zhengd68fc572010-05-16 10:49:58 -04003250 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003251 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3252 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003253 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003254
Josef Bacik72ac3c02012-05-23 14:13:11 -04003255 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3256 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003257 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003258 spin_unlock(&root->orphan_lock);
3259
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003260 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003261 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003262 if (trans)
3263 ret = btrfs_del_orphan_item(trans, root,
3264 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003265 }
Josef Bacik7b128762008-07-24 12:17:14 -04003266
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003267 if (release_rsv)
3268 btrfs_orphan_release_metadata(inode);
3269
Josef Bacik4ef31a42013-08-13 14:10:08 -04003270 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003271}
3272
3273/*
3274 * this cleans up any orphans that may be left on the list from the last use
3275 * of this root.
3276 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003277int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003278{
3279 struct btrfs_path *path;
3280 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003281 struct btrfs_key key, found_key;
3282 struct btrfs_trans_handle *trans;
3283 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003284 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003285 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3286
Yan, Zhengd68fc572010-05-16 10:49:58 -04003287 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003288 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003289
3290 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003291 if (!path) {
3292 ret = -ENOMEM;
3293 goto out;
3294 }
Josef Bacik7b128762008-07-24 12:17:14 -04003295 path->reada = -1;
3296
3297 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003298 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003299 key.offset = (u64)-1;
3300
Josef Bacik7b128762008-07-24 12:17:14 -04003301 while (1) {
3302 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003303 if (ret < 0)
3304 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003305
3306 /*
3307 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003308 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003309 * find the key and see if we have stuff that matches
3310 */
3311 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003312 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003313 if (path->slots[0] == 0)
3314 break;
3315 path->slots[0]--;
3316 }
3317
3318 /* pull out the item */
3319 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003320 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3321
3322 /* make sure the item matches what we want */
3323 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3324 break;
David Sterba962a2982014-06-04 18:41:45 +02003325 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003326 break;
3327
3328 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003329 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003330
3331 /*
3332 * this is where we are basically btrfs_lookup, without the
3333 * crossing root thing. we store the inode number in the
3334 * offset of the orphan item.
3335 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003336
3337 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003338 btrfs_err(root->fs_info,
3339 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003340 ret = -EINVAL;
3341 goto out;
3342 }
3343
3344 last_objectid = found_key.offset;
3345
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003346 found_key.objectid = found_key.offset;
3347 found_key.type = BTRFS_INODE_ITEM_KEY;
3348 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003349 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303350 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003351 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003352 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003353
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003354 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3355 struct btrfs_root *dead_root;
3356 struct btrfs_fs_info *fs_info = root->fs_info;
3357 int is_dead_root = 0;
3358
3359 /*
3360 * this is an orphan in the tree root. Currently these
3361 * could come from 2 sources:
3362 * a) a snapshot deletion in progress
3363 * b) a free space cache inode
3364 * We need to distinguish those two, as the snapshot
3365 * orphan must not get deleted.
3366 * find_dead_roots already ran before us, so if this
3367 * is a snapshot deletion, we should find the root
3368 * in the dead_roots list
3369 */
3370 spin_lock(&fs_info->trans_lock);
3371 list_for_each_entry(dead_root, &fs_info->dead_roots,
3372 root_list) {
3373 if (dead_root->root_key.objectid ==
3374 found_key.objectid) {
3375 is_dead_root = 1;
3376 break;
3377 }
3378 }
3379 spin_unlock(&fs_info->trans_lock);
3380 if (is_dead_root) {
3381 /* prevent this orphan from being found again */
3382 key.offset = found_key.objectid - 1;
3383 continue;
3384 }
3385 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003386 /*
3387 * Inode is already gone but the orphan item is still there,
3388 * kill the orphan item.
3389 */
3390 if (ret == -ESTALE) {
3391 trans = btrfs_start_transaction(root, 1);
3392 if (IS_ERR(trans)) {
3393 ret = PTR_ERR(trans);
3394 goto out;
3395 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003396 btrfs_debug(root->fs_info, "auto deleting %Lu",
3397 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003398 ret = btrfs_del_orphan_item(trans, root,
3399 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003400 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003401 if (ret)
3402 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003403 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003404 }
Josef Bacik7b128762008-07-24 12:17:14 -04003405
Josef Bacik7b128762008-07-24 12:17:14 -04003406 /*
3407 * add this inode to the orphan list so btrfs_orphan_del does
3408 * the proper thing when we hit it
3409 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003410 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3411 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003412 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003413
Josef Bacik7b128762008-07-24 12:17:14 -04003414 /* if we have links, this was a truncate, lets do that */
3415 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303416 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003417 iput(inode);
3418 continue;
3419 }
Josef Bacik7b128762008-07-24 12:17:14 -04003420 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003421
3422 /* 1 for the orphan item deletion. */
3423 trans = btrfs_start_transaction(root, 1);
3424 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003425 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003426 ret = PTR_ERR(trans);
3427 goto out;
3428 }
3429 ret = btrfs_orphan_add(trans, inode);
3430 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003431 if (ret) {
3432 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003433 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003434 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003435
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003436 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003437 if (ret)
3438 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003439 } else {
3440 nr_unlink++;
3441 }
3442
3443 /* this will do delete_inode and everything for us */
3444 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003445 if (ret)
3446 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003447 }
Miao Xie3254c872011-11-10 20:45:05 -05003448 /* release the path since we're done with it */
3449 btrfs_release_path(path);
3450
Yan, Zhengd68fc572010-05-16 10:49:58 -04003451 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3452
3453 if (root->orphan_block_rsv)
3454 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3455 (u64)-1);
3456
Miao Xie27cdeb72014-04-02 19:51:05 +08003457 if (root->orphan_block_rsv ||
3458 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003459 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003460 if (!IS_ERR(trans))
3461 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003462 }
Josef Bacik7b128762008-07-24 12:17:14 -04003463
3464 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003465 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003466 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003467 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003468
3469out:
3470 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003471 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003472 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003473 btrfs_free_path(path);
3474 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003475}
3476
Chris Masond352ac62008-09-29 15:18:18 -04003477/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003478 * very simple check to peek ahead in the leaf looking for xattrs. If we
3479 * don't find any xattrs, we know there can't be any acls.
3480 *
3481 * slot is the slot the inode is in, objectid is the objectid of the inode
3482 */
3483static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003484 int slot, u64 objectid,
3485 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003486{
3487 u32 nritems = btrfs_header_nritems(leaf);
3488 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003489 static u64 xattr_access = 0;
3490 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003491 int scanned = 0;
3492
Josef Bacikf23b5a52013-06-19 10:16:26 -04003493 if (!xattr_access) {
3494 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3495 strlen(POSIX_ACL_XATTR_ACCESS));
3496 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3497 strlen(POSIX_ACL_XATTR_DEFAULT));
3498 }
3499
Chris Mason46a53cc2009-04-27 11:47:50 -04003500 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003501 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003502 while (slot < nritems) {
3503 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3504
3505 /* we found a different objectid, there must not be acls */
3506 if (found_key.objectid != objectid)
3507 return 0;
3508
3509 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003510 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003511 if (*first_xattr_slot == -1)
3512 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003513 if (found_key.offset == xattr_access ||
3514 found_key.offset == xattr_default)
3515 return 1;
3516 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003517
3518 /*
3519 * we found a key greater than an xattr key, there can't
3520 * be any acls later on
3521 */
3522 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3523 return 0;
3524
3525 slot++;
3526 scanned++;
3527
3528 /*
3529 * it goes inode, inode backrefs, xattrs, extents,
3530 * so if there are a ton of hard links to an inode there can
3531 * be a lot of backrefs. Don't waste time searching too hard,
3532 * this is just an optimization
3533 */
3534 if (scanned >= 8)
3535 break;
3536 }
3537 /* we hit the end of the leaf before we found an xattr or
3538 * something larger than an xattr. We have to assume the inode
3539 * has acls
3540 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003541 if (*first_xattr_slot == -1)
3542 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003543 return 1;
3544}
3545
3546/*
Chris Masond352ac62008-09-29 15:18:18 -04003547 * read an inode from the btree into the in-memory inode
3548 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003549static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003550{
3551 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003552 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003553 struct btrfs_inode_item *inode_item;
3554 struct btrfs_root *root = BTRFS_I(inode)->root;
3555 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003556 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003557 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003558 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003559 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003560 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003561 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003562
3563 ret = btrfs_fill_inode(inode, &rdev);
3564 if (!ret)
3565 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003566
3567 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003568 if (!path)
3569 goto make_bad;
3570
Chris Mason39279cc2007-06-12 06:35:45 -04003571 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003572
Chris Mason39279cc2007-06-12 06:35:45 -04003573 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003574 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003575 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003576
Chris Mason5f39d392007-10-15 16:14:19 -04003577 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003578
3579 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003580 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003581
Chris Mason5f39d392007-10-15 16:14:19 -04003582 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3583 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003584 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003585 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003586 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3587 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003588 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003589
David Sterbaa937b972014-12-12 17:39:12 +01003590 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3591 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003592
David Sterbaa937b972014-12-12 17:39:12 +01003593 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3594 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003595
David Sterbaa937b972014-12-12 17:39:12 +01003596 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3597 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003598
chandan r9cc97d62012-07-04 12:48:07 +05303599 BTRFS_I(inode)->i_otime.tv_sec =
3600 btrfs_timespec_sec(leaf, &inode_item->otime);
3601 BTRFS_I(inode)->i_otime.tv_nsec =
3602 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003603
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003604 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003605 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003606 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3607
3608 /*
3609 * If we were modified in the current generation and evicted from memory
3610 * and then re-read we need to do a full sync since we don't have any
3611 * idea about which extents were modified before we were evicted from
3612 * cache.
3613 */
3614 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3615 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3616 &BTRFS_I(inode)->runtime_flags);
3617
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003618 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003619 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003620 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003621 rdev = btrfs_inode_rdev(leaf, inode_item);
3622
Josef Bacikaec74772008-07-24 12:12:38 -04003623 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003624 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003625
3626cache_index:
3627 path->slots[0]++;
3628 if (inode->i_nlink != 1 ||
3629 path->slots[0] >= btrfs_header_nritems(leaf))
3630 goto cache_acl;
3631
3632 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3633 if (location.objectid != btrfs_ino(inode))
3634 goto cache_acl;
3635
3636 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3637 if (location.type == BTRFS_INODE_REF_KEY) {
3638 struct btrfs_inode_ref *ref;
3639
3640 ref = (struct btrfs_inode_ref *)ptr;
3641 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3642 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3643 struct btrfs_inode_extref *extref;
3644
3645 extref = (struct btrfs_inode_extref *)ptr;
3646 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3647 extref);
3648 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003649cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003650 /*
3651 * try to precache a NULL acl entry for files that don't have
3652 * any xattrs or acls
3653 */
Li Zefan33345d012011-04-20 10:31:50 +08003654 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003655 btrfs_ino(inode), &first_xattr_slot);
3656 if (first_xattr_slot != -1) {
3657 path->slots[0] = first_xattr_slot;
3658 ret = btrfs_load_inode_props(inode, path);
3659 if (ret)
3660 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003661 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003662 btrfs_ino(inode),
3663 root->root_key.objectid, ret);
3664 }
3665 btrfs_free_path(path);
3666
Al Viro72c04902009-06-24 16:58:48 -04003667 if (!maybe_acls)
3668 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003669
Chris Mason39279cc2007-06-12 06:35:45 -04003670 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003671 case S_IFREG:
3672 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003673 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003674 inode->i_fop = &btrfs_file_operations;
3675 inode->i_op = &btrfs_file_inode_operations;
3676 break;
3677 case S_IFDIR:
3678 inode->i_fop = &btrfs_dir_file_operations;
3679 if (root == root->fs_info->tree_root)
3680 inode->i_op = &btrfs_dir_ro_inode_operations;
3681 else
3682 inode->i_op = &btrfs_dir_inode_operations;
3683 break;
3684 case S_IFLNK:
3685 inode->i_op = &btrfs_symlink_inode_operations;
3686 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3687 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003688 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003689 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003690 init_special_inode(inode, inode->i_mode, rdev);
3691 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003692 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003693
3694 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003695 return;
3696
3697make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003698 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003699 make_bad_inode(inode);
3700}
3701
Chris Masond352ac62008-09-29 15:18:18 -04003702/*
3703 * given a leaf and an inode, copy the inode fields into the leaf
3704 */
Chris Masone02119d2008-09-05 16:13:11 -04003705static void fill_inode_item(struct btrfs_trans_handle *trans,
3706 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003707 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003708 struct inode *inode)
3709{
Liu Bo51fab692012-12-27 09:01:21 +00003710 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003711
Liu Bo51fab692012-12-27 09:01:21 +00003712 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003713
Liu Bo51fab692012-12-27 09:01:21 +00003714 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3715 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3716 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3717 &token);
3718 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3719 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003720
David Sterbaa937b972014-12-12 17:39:12 +01003721 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003722 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003723 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003724 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003725
David Sterbaa937b972014-12-12 17:39:12 +01003726 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003727 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003728 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003729 inode->i_mtime.tv_nsec, &token);
3730
David Sterbaa937b972014-12-12 17:39:12 +01003731 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003732 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003733 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003734 inode->i_ctime.tv_nsec, &token);
3735
chandan r9cc97d62012-07-04 12:48:07 +05303736 btrfs_set_token_timespec_sec(leaf, &item->otime,
3737 BTRFS_I(inode)->i_otime.tv_sec, &token);
3738 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3739 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3740
Liu Bo51fab692012-12-27 09:01:21 +00003741 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3742 &token);
3743 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3744 &token);
3745 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3746 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3747 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3748 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3749 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003750}
3751
Chris Masond352ac62008-09-29 15:18:18 -04003752/*
3753 * copy everything in the in-memory inode into the btree.
3754 */
Chris Mason21151332011-11-10 20:39:08 -05003755static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003756 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003757{
3758 struct btrfs_inode_item *inode_item;
3759 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003760 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003761 int ret;
3762
3763 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003764 if (!path)
3765 return -ENOMEM;
3766
Chris Masonb9473432009-03-13 11:00:37 -04003767 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003768 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3769 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003770 if (ret) {
3771 if (ret > 0)
3772 ret = -ENOENT;
3773 goto failed;
3774 }
3775
Chris Mason5f39d392007-10-15 16:14:19 -04003776 leaf = path->nodes[0];
3777 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003778 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003779
Chris Masone02119d2008-09-05 16:13:11 -04003780 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003781 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003782 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003783 ret = 0;
3784failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003785 btrfs_free_path(path);
3786 return ret;
3787}
3788
Chris Masond352ac62008-09-29 15:18:18 -04003789/*
Chris Mason21151332011-11-10 20:39:08 -05003790 * copy everything in the in-memory inode into the btree.
3791 */
3792noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3793 struct btrfs_root *root, struct inode *inode)
3794{
3795 int ret;
3796
3797 /*
3798 * If the inode is a free space inode, we can deadlock during commit
3799 * if we put it into the delayed code.
3800 *
3801 * The data relocation inode should also be directly updated
3802 * without delay
3803 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003804 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003805 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3806 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003807 btrfs_update_root_times(trans, root);
3808
Chris Mason21151332011-11-10 20:39:08 -05003809 ret = btrfs_delayed_update_inode(trans, root, inode);
3810 if (!ret)
3811 btrfs_set_inode_last_trans(trans, inode);
3812 return ret;
3813 }
3814
3815 return btrfs_update_inode_item(trans, root, inode);
3816}
3817
Josef Bacikbe6aef62012-10-22 15:43:12 -04003818noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3819 struct btrfs_root *root,
3820 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003821{
3822 int ret;
3823
3824 ret = btrfs_update_inode(trans, root, inode);
3825 if (ret == -ENOSPC)
3826 return btrfs_update_inode_item(trans, root, inode);
3827 return ret;
3828}
3829
3830/*
Chris Masond352ac62008-09-29 15:18:18 -04003831 * unlink helper that gets used here in inode.c and in the tree logging
3832 * recovery code. It remove a link in a directory with a given name, and
3833 * also drops the back refs in the inode to the directory
3834 */
Al Viro92986792011-03-04 17:14:37 +00003835static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3836 struct btrfs_root *root,
3837 struct inode *dir, struct inode *inode,
3838 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003839{
3840 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003841 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003842 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003843 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003844 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003845 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003846 u64 ino = btrfs_ino(inode);
3847 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003848
3849 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003850 if (!path) {
3851 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003852 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003853 }
3854
Chris Masonb9473432009-03-13 11:00:37 -04003855 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003856 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003857 name, name_len, -1);
3858 if (IS_ERR(di)) {
3859 ret = PTR_ERR(di);
3860 goto err;
3861 }
3862 if (!di) {
3863 ret = -ENOENT;
3864 goto err;
3865 }
Chris Mason5f39d392007-10-15 16:14:19 -04003866 leaf = path->nodes[0];
3867 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003868 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003869 if (ret)
3870 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003871 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003872
Miao Xie67de1172013-12-26 13:07:06 +08003873 /*
3874 * If we don't have dir index, we have to get it by looking up
3875 * the inode ref, since we get the inode ref, remove it directly,
3876 * it is unnecessary to do delayed deletion.
3877 *
3878 * But if we have dir index, needn't search inode ref to get it.
3879 * Since the inode ref is close to the inode item, it is better
3880 * that we delay to delete it, and just do this deletion when
3881 * we update the inode item.
3882 */
3883 if (BTRFS_I(inode)->dir_index) {
3884 ret = btrfs_delayed_delete_inode_ref(inode);
3885 if (!ret) {
3886 index = BTRFS_I(inode)->dir_index;
3887 goto skip_backref;
3888 }
3889 }
3890
Li Zefan33345d012011-04-20 10:31:50 +08003891 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3892 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003893 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003894 btrfs_info(root->fs_info,
3895 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003896 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003897 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003898 goto err;
3899 }
Miao Xie67de1172013-12-26 13:07:06 +08003900skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003901 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003902 if (ret) {
3903 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003904 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003905 }
Chris Mason39279cc2007-06-12 06:35:45 -04003906
Chris Masone02119d2008-09-05 16:13:11 -04003907 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003908 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003909 if (ret != 0 && ret != -ENOENT) {
3910 btrfs_abort_transaction(trans, root, ret);
3911 goto err;
3912 }
Chris Masone02119d2008-09-05 16:13:11 -04003913
3914 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3915 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003916 if (ret == -ENOENT)
3917 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003918 else if (ret)
3919 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003920err:
3921 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003922 if (ret)
3923 goto out;
3924
3925 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003926 inode_inc_iversion(inode);
3927 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003928 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003929 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003930out:
Chris Mason39279cc2007-06-12 06:35:45 -04003931 return ret;
3932}
3933
Al Viro92986792011-03-04 17:14:37 +00003934int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3935 struct btrfs_root *root,
3936 struct inode *dir, struct inode *inode,
3937 const char *name, int name_len)
3938{
3939 int ret;
3940 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3941 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003942 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003943 ret = btrfs_update_inode(trans, root, inode);
3944 }
3945 return ret;
3946}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003947
3948/*
3949 * helper to start transaction for unlink and rmdir.
3950 *
Josef Bacikd52be812013-05-29 14:54:47 -04003951 * unlink and rmdir are special in btrfs, they do not always free space, so
3952 * if we cannot make our reservations the normal way try and see if there is
3953 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3954 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003955 */
Josef Bacikd52be812013-05-29 14:54:47 -04003956static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003957{
3958 struct btrfs_trans_handle *trans;
3959 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003960 int ret;
3961
Josef Bacike70bea52011-10-11 14:18:24 -04003962 /*
3963 * 1 for the possible orphan item
3964 * 1 for the dir item
3965 * 1 for the dir index
3966 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003967 * 1 for the inode
3968 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003969 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003970 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3971 return trans;
3972
Josef Bacikd52be812013-05-29 14:54:47 -04003973 if (PTR_ERR(trans) == -ENOSPC) {
3974 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003975
Josef Bacikd52be812013-05-29 14:54:47 -04003976 trans = btrfs_start_transaction(root, 0);
3977 if (IS_ERR(trans))
3978 return trans;
3979 ret = btrfs_cond_migrate_bytes(root->fs_info,
3980 &root->fs_info->trans_block_rsv,
3981 num_bytes, 5);
3982 if (ret) {
3983 btrfs_end_transaction(trans, root);
3984 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003985 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003986 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003987 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003988 }
Josef Bacikd52be812013-05-29 14:54:47 -04003989 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003990}
3991
Chris Mason39279cc2007-06-12 06:35:45 -04003992static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3993{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003994 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003995 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003996 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003997 int ret;
3998
Josef Bacikd52be812013-05-29 14:54:47 -04003999 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004000 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004001 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004002
Chris Mason12fcfd22009-03-24 10:24:20 -04004003 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
4004
Chris Masone02119d2008-09-05 16:13:11 -04004005 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4006 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004007 if (ret)
4008 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004009
Yan, Zhenga22285a2010-05-16 10:48:46 -04004010 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004011 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004012 if (ret)
4013 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004014 }
Josef Bacik7b128762008-07-24 12:17:14 -04004015
Tsutomu Itohb5324022011-07-19 07:27:20 +00004016out:
Josef Bacikd52be812013-05-29 14:54:47 -04004017 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004018 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004019 return ret;
4020}
4021
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004022int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4023 struct btrfs_root *root,
4024 struct inode *dir, u64 objectid,
4025 const char *name, int name_len)
4026{
4027 struct btrfs_path *path;
4028 struct extent_buffer *leaf;
4029 struct btrfs_dir_item *di;
4030 struct btrfs_key key;
4031 u64 index;
4032 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004033 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004034
4035 path = btrfs_alloc_path();
4036 if (!path)
4037 return -ENOMEM;
4038
Li Zefan33345d012011-04-20 10:31:50 +08004039 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004040 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004041 if (IS_ERR_OR_NULL(di)) {
4042 if (!di)
4043 ret = -ENOENT;
4044 else
4045 ret = PTR_ERR(di);
4046 goto out;
4047 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004048
4049 leaf = path->nodes[0];
4050 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4051 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4052 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004053 if (ret) {
4054 btrfs_abort_transaction(trans, root, ret);
4055 goto out;
4056 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004057 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004058
4059 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4060 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004061 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004062 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004063 if (ret != -ENOENT) {
4064 btrfs_abort_transaction(trans, root, ret);
4065 goto out;
4066 }
Li Zefan33345d012011-04-20 10:31:50 +08004067 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004068 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004069 if (IS_ERR_OR_NULL(di)) {
4070 if (!di)
4071 ret = -ENOENT;
4072 else
4073 ret = PTR_ERR(di);
4074 btrfs_abort_transaction(trans, root, ret);
4075 goto out;
4076 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004077
4078 leaf = path->nodes[0];
4079 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004080 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004081 index = key.offset;
4082 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004083 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004084
Miao Xie16cdcec2011-04-22 18:12:22 +08004085 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004086 if (ret) {
4087 btrfs_abort_transaction(trans, root, ret);
4088 goto out;
4089 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004090
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004091 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004092 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004093 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004094 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004095 if (ret)
4096 btrfs_abort_transaction(trans, root, ret);
4097out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004098 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004099 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004100}
4101
Chris Mason39279cc2007-06-12 06:35:45 -04004102static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4103{
4104 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004105 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004106 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004107 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004108
David Sterbab3ae2442012-09-13 16:04:34 -06004109 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004110 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004111 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4112 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004113
Josef Bacikd52be812013-05-29 14:54:47 -04004114 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004115 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004116 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004117
Li Zefan33345d012011-04-20 10:31:50 +08004118 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004119 err = btrfs_unlink_subvol(trans, root, dir,
4120 BTRFS_I(inode)->location.objectid,
4121 dentry->d_name.name,
4122 dentry->d_name.len);
4123 goto out;
4124 }
4125
Josef Bacik7b128762008-07-24 12:17:14 -04004126 err = btrfs_orphan_add(trans, inode);
4127 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004128 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004129
Chris Mason39279cc2007-06-12 06:35:45 -04004130 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004131 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4132 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004133 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004134 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004135out:
Josef Bacikd52be812013-05-29 14:54:47 -04004136 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004137 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004138
Chris Mason39279cc2007-06-12 06:35:45 -04004139 return err;
4140}
4141
Chris Mason323ac952008-10-01 19:05:46 -04004142/*
Chris Mason39279cc2007-06-12 06:35:45 -04004143 * this can truncate away extent items, csum items and directory items.
4144 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004145 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004146 *
4147 * csum items that cross the new i_size are truncated to the new size
4148 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004149 *
4150 * min_type is the minimum key type to truncate down to. If set to 0, this
4151 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004152 */
Yan, Zheng80825102009-11-12 09:35:36 +00004153int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4154 struct btrfs_root *root,
4155 struct inode *inode,
4156 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004157{
Chris Mason39279cc2007-06-12 06:35:45 -04004158 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004159 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004160 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004161 struct btrfs_key key;
4162 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004163 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004164 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004165 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004166 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004167 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004168 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004169 int found_extent;
4170 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004171 int pending_del_nr = 0;
4172 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004173 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004174 int ret;
4175 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004176 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004177
4178 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004179
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004180 path = btrfs_alloc_path();
4181 if (!path)
4182 return -ENOMEM;
4183 path->reada = -1;
4184
Josef Bacik5dc562c2012-08-17 13:14:17 -04004185 /*
4186 * We want to drop from the next block forward in case this new size is
4187 * not block aligned since we will be keeping the last block of the
4188 * extent just the way it is.
4189 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004190 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4191 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004192 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4193 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004194
Miao Xie16cdcec2011-04-22 18:12:22 +08004195 /*
4196 * This function is also used to drop the items in the log tree before
4197 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4198 * it is used to drop the loged items. So we shouldn't kill the delayed
4199 * items.
4200 */
4201 if (min_type == 0 && root == BTRFS_I(inode)->root)
4202 btrfs_kill_delayed_inode_items(inode);
4203
Li Zefan33345d012011-04-20 10:31:50 +08004204 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004205 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004206 key.type = (u8)-1;
4207
Chris Mason85e21ba2008-01-29 15:11:36 -05004208search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004209 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004210 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004211 if (ret < 0) {
4212 err = ret;
4213 goto out;
4214 }
Chris Masond3977122009-01-05 21:25:51 -05004215
Chris Mason85e21ba2008-01-29 15:11:36 -05004216 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004217 /* there are no items in the tree for us to truncate, we're
4218 * done
4219 */
Yan, Zheng80825102009-11-12 09:35:36 +00004220 if (path->slots[0] == 0)
4221 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004222 path->slots[0]--;
4223 }
4224
Chris Masond3977122009-01-05 21:25:51 -05004225 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004226 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004227 leaf = path->nodes[0];
4228 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004229 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004230
Li Zefan33345d012011-04-20 10:31:50 +08004231 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004232 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004233
Chris Mason85e21ba2008-01-29 15:11:36 -05004234 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004235 break;
4236
Chris Mason5f39d392007-10-15 16:14:19 -04004237 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004238 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004239 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004240 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004241 extent_type = btrfs_file_extent_type(leaf, fi);
4242 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004243 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004244 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004245 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004246 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004247 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004248 }
Yan008630c2007-11-07 13:31:09 -05004249 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004250 }
Yan, Zheng80825102009-11-12 09:35:36 +00004251 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004252 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004253 } else {
4254 if (item_end < new_size)
4255 break;
4256 if (found_key.offset >= new_size)
4257 del_item = 1;
4258 else
4259 del_item = 0;
4260 }
Chris Mason39279cc2007-06-12 06:35:45 -04004261 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004262 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004263 if (found_type != BTRFS_EXTENT_DATA_KEY)
4264 goto delete;
4265
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004266 if (del_item)
4267 last_size = found_key.offset;
4268 else
4269 last_size = new_size;
4270
Chris Mason179e29e2007-11-01 11:28:41 -04004271 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004272 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004273 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004274 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004275 u64 orig_num_bytes =
4276 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004277 extent_num_bytes = ALIGN(new_size -
4278 found_key.offset,
4279 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004280 btrfs_set_file_extent_num_bytes(leaf, fi,
4281 extent_num_bytes);
4282 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004283 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004284 if (test_bit(BTRFS_ROOT_REF_COWS,
4285 &root->state) &&
4286 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004287 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004288 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004289 } else {
Chris Masondb945352007-10-15 16:15:53 -04004290 extent_num_bytes =
4291 btrfs_file_extent_disk_num_bytes(leaf,
4292 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004293 extent_offset = found_key.offset -
4294 btrfs_file_extent_offset(leaf, fi);
4295
Chris Mason39279cc2007-06-12 06:35:45 -04004296 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004297 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004298 if (extent_start != 0) {
4299 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004300 if (test_bit(BTRFS_ROOT_REF_COWS,
4301 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004302 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004303 }
4304 }
Chris Mason90692182008-02-08 13:49:28 -05004305 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004306 /*
4307 * we can't truncate inline items that have had
4308 * special encodings
4309 */
4310 if (!del_item &&
4311 btrfs_file_extent_compression(leaf, fi) == 0 &&
4312 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4313 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004314 u32 size = new_size - found_key.offset;
4315
Miao Xie27cdeb72014-04-02 19:51:05 +08004316 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004317 inode_sub_bytes(inode, item_end + 1 -
4318 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004319
4320 /*
4321 * update the ram bytes to properly reflect
4322 * the new size of our item
4323 */
4324 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004325 size =
4326 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004327 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004328 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4329 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004330 inode_sub_bytes(inode, item_end + 1 -
4331 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004332 }
Chris Mason39279cc2007-06-12 06:35:45 -04004333 }
Chris Mason179e29e2007-11-01 11:28:41 -04004334delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004335 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004336 if (!pending_del_nr) {
4337 /* no pending yet, add ourselves */
4338 pending_del_slot = path->slots[0];
4339 pending_del_nr = 1;
4340 } else if (pending_del_nr &&
4341 path->slots[0] + 1 == pending_del_slot) {
4342 /* hop on the pending chunk */
4343 pending_del_nr++;
4344 pending_del_slot = path->slots[0];
4345 } else {
Chris Masond3977122009-01-05 21:25:51 -05004346 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004347 }
Chris Mason39279cc2007-06-12 06:35:45 -04004348 } else {
4349 break;
4350 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004351 if (found_extent &&
4352 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4353 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004354 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004355 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004356 extent_num_bytes, 0,
4357 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004358 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004359 BUG_ON(ret);
4360 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004361
Yan, Zheng80825102009-11-12 09:35:36 +00004362 if (found_type == BTRFS_INODE_ITEM_KEY)
4363 break;
4364
4365 if (path->slots[0] == 0 ||
4366 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004367 if (pending_del_nr) {
4368 ret = btrfs_del_items(trans, root, path,
4369 pending_del_slot,
4370 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004371 if (ret) {
4372 btrfs_abort_transaction(trans,
4373 root, ret);
4374 goto error;
4375 }
Yan, Zheng80825102009-11-12 09:35:36 +00004376 pending_del_nr = 0;
4377 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004378 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004379 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004380 } else {
4381 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004382 }
Chris Mason39279cc2007-06-12 06:35:45 -04004383 }
Yan, Zheng80825102009-11-12 09:35:36 +00004384out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004385 if (pending_del_nr) {
4386 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4387 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004388 if (ret)
4389 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004390 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004391error:
Filipe Mananadac57052014-08-29 20:54:26 +01004392 if (last_size != (u64)-1 &&
4393 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004394 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004395 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004396 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004397}
4398
Chris Masona52d9a82007-08-27 16:49:44 -04004399/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004400 * btrfs_truncate_page - read, zero a chunk and write a page
4401 * @inode - inode that we're zeroing
4402 * @from - the offset to start zeroing
4403 * @len - the length to zero, 0 to zero the entire range respective to the
4404 * offset
4405 * @front - zero up to the offset instead of from the offset on
4406 *
4407 * This will find the page for the "from" offset and cow the page and zero the
4408 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004409 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004410int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4411 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004412{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004413 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004414 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004415 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4416 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004417 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004418 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004419 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004420 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4421 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4422 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004423 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004424 int ret = 0;
4425 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004426 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004427
Josef Bacik2aaa6652012-08-29 14:27:18 -04004428 if ((offset & (blocksize - 1)) == 0 &&
4429 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004430 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004431 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004432 if (ret)
4433 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004434
Chris Mason211c17f2008-05-15 09:13:45 -04004435again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004436 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004437 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004438 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004439 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004440 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004441 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004442
4443 page_start = page_offset(page);
4444 page_end = page_start + PAGE_CACHE_SIZE - 1;
4445
Chris Masona52d9a82007-08-27 16:49:44 -04004446 if (!PageUptodate(page)) {
4447 ret = btrfs_readpage(NULL, page);
4448 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004449 if (page->mapping != mapping) {
4450 unlock_page(page);
4451 page_cache_release(page);
4452 goto again;
4453 }
Chris Masona52d9a82007-08-27 16:49:44 -04004454 if (!PageUptodate(page)) {
4455 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004456 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004457 }
4458 }
Chris Mason211c17f2008-05-15 09:13:45 -04004459 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004460
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004461 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004462 set_page_extent_mapped(page);
4463
4464 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4465 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004466 unlock_extent_cached(io_tree, page_start, page_end,
4467 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004468 unlock_page(page);
4469 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004470 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004471 btrfs_put_ordered_extent(ordered);
4472 goto again;
4473 }
4474
Josef Bacik2ac55d42010-02-03 19:33:23 +00004475 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004476 EXTENT_DIRTY | EXTENT_DELALLOC |
4477 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004478 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004479
Josef Bacik2ac55d42010-02-03 19:33:23 +00004480 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4481 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004482 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004483 unlock_extent_cached(io_tree, page_start, page_end,
4484 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004485 goto out_unlock;
4486 }
4487
Chris Masone6dcd2d2008-07-17 12:53:50 -04004488 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004489 if (!len)
4490 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004491 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004492 if (front)
4493 memset(kaddr, 0, offset);
4494 else
4495 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004496 flush_dcache_page(page);
4497 kunmap(page);
4498 }
Chris Mason247e7432008-07-17 12:53:51 -04004499 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004500 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004501 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4502 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004503
Chris Mason89642222008-07-24 09:41:53 -04004504out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004505 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004506 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004507 unlock_page(page);
4508 page_cache_release(page);
4509out:
4510 return ret;
4511}
4512
Josef Bacik16e75492013-10-22 12:18:51 -04004513static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4514 u64 offset, u64 len)
4515{
4516 struct btrfs_trans_handle *trans;
4517 int ret;
4518
4519 /*
4520 * Still need to make sure the inode looks like it's been updated so
4521 * that any holes get logged if we fsync.
4522 */
4523 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4524 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4525 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4526 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4527 return 0;
4528 }
4529
4530 /*
4531 * 1 - for the one we're dropping
4532 * 1 - for the one we're adding
4533 * 1 - for updating the inode.
4534 */
4535 trans = btrfs_start_transaction(root, 3);
4536 if (IS_ERR(trans))
4537 return PTR_ERR(trans);
4538
4539 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4540 if (ret) {
4541 btrfs_abort_transaction(trans, root, ret);
4542 btrfs_end_transaction(trans, root);
4543 return ret;
4544 }
4545
4546 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4547 0, 0, len, 0, len, 0, 0, 0);
4548 if (ret)
4549 btrfs_abort_transaction(trans, root, ret);
4550 else
4551 btrfs_update_inode(trans, root, inode);
4552 btrfs_end_transaction(trans, root);
4553 return ret;
4554}
4555
Josef Bacik695a0d02011-03-04 15:46:53 -05004556/*
4557 * This function puts in dummy file extents for the area we're creating a hole
4558 * for. So if we are truncating this file to a larger size we need to insert
4559 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4560 * the range between oldsize and size
4561 */
Josef Bacika41ad392011-01-31 15:30:16 -05004562int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004563{
Yan Zheng9036c102008-10-30 14:19:41 -04004564 struct btrfs_root *root = BTRFS_I(inode)->root;
4565 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004566 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004567 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004568 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004569 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4570 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004571 u64 last_byte;
4572 u64 cur_offset;
4573 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004574 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004575
Josef Bacika71754f2013-06-17 17:14:39 -04004576 /*
4577 * If our size started in the middle of a page we need to zero out the
4578 * rest of the page before we expand the i_size, otherwise we could
4579 * expose stale data.
4580 */
4581 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4582 if (err)
4583 return err;
4584
Yan Zheng9036c102008-10-30 14:19:41 -04004585 if (size <= hole_start)
4586 return 0;
4587
Yan Zheng9036c102008-10-30 14:19:41 -04004588 while (1) {
4589 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004590
Josef Bacik2ac55d42010-02-03 19:33:23 +00004591 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004592 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004593 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4594 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004595 if (!ordered)
4596 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004597 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4598 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004599 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004600 btrfs_put_ordered_extent(ordered);
4601 }
4602
Yan Zheng9036c102008-10-30 14:19:41 -04004603 cur_offset = hole_start;
4604 while (1) {
4605 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4606 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004607 if (IS_ERR(em)) {
4608 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004609 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004610 break;
4611 }
Yan Zheng9036c102008-10-30 14:19:41 -04004612 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004613 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004614 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004615 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004616 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004617
Josef Bacik16e75492013-10-22 12:18:51 -04004618 err = maybe_insert_hole(root, inode, cur_offset,
4619 hole_size);
4620 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004621 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004622 btrfs_drop_extent_cache(inode, cur_offset,
4623 cur_offset + hole_size - 1, 0);
4624 hole_em = alloc_extent_map();
4625 if (!hole_em) {
4626 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4627 &BTRFS_I(inode)->runtime_flags);
4628 goto next;
4629 }
4630 hole_em->start = cur_offset;
4631 hole_em->len = hole_size;
4632 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004633
Josef Bacik5dc562c2012-08-17 13:14:17 -04004634 hole_em->block_start = EXTENT_MAP_HOLE;
4635 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004636 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004637 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004638 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4639 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004640 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004641
4642 while (1) {
4643 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004644 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004645 write_unlock(&em_tree->lock);
4646 if (err != -EEXIST)
4647 break;
4648 btrfs_drop_extent_cache(inode, cur_offset,
4649 cur_offset +
4650 hole_size - 1, 0);
4651 }
4652 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004653 }
Josef Bacik16e75492013-10-22 12:18:51 -04004654next:
Yan Zheng9036c102008-10-30 14:19:41 -04004655 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004656 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004657 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004658 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004659 break;
4660 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004661 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004662 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4663 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004664 return err;
4665}
4666
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004667static int wait_snapshoting_atomic_t(atomic_t *a)
4668{
4669 schedule();
4670 return 0;
4671}
4672
4673static void wait_for_snapshot_creation(struct btrfs_root *root)
4674{
4675 while (true) {
4676 int ret;
4677
4678 ret = btrfs_start_write_no_snapshoting(root);
4679 if (ret)
4680 break;
4681 wait_on_atomic_t(&root->will_be_snapshoted,
4682 wait_snapshoting_atomic_t,
4683 TASK_UNINTERRUPTIBLE);
4684 }
4685}
4686
Eric Sandeen3972f262013-01-12 02:57:22 +00004687static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004688{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004689 struct btrfs_root *root = BTRFS_I(inode)->root;
4690 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004691 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004692 loff_t newsize = attr->ia_size;
4693 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004694 int ret;
4695
Eric Sandeen3972f262013-01-12 02:57:22 +00004696 /*
4697 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4698 * special case where we need to update the times despite not having
4699 * these flags set. For all other operations the VFS set these flags
4700 * explicitly if it wants a timestamp update.
4701 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004702 if (newsize != oldsize) {
4703 inode_inc_iversion(inode);
4704 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4705 inode->i_ctime = inode->i_mtime =
4706 current_fs_time(inode->i_sb);
4707 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004708
Josef Bacika41ad392011-01-31 15:30:16 -05004709 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004710 truncate_pagecache(inode, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004711 /*
4712 * Don't do an expanding truncate while snapshoting is ongoing.
4713 * This is to ensure the snapshot captures a fully consistent
4714 * state of this file - if the snapshot captures this expanding
4715 * truncation, it must capture all writes that happened before
4716 * this truncation.
4717 */
4718 wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004719 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004720 if (ret) {
4721 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004722 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004723 }
Yan, Zheng80825102009-11-12 09:35:36 +00004724
Miao Xief4a2f4c2011-12-14 20:12:01 -05004725 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004726 if (IS_ERR(trans)) {
4727 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004728 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004729 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004730
4731 i_size_write(inode, newsize);
4732 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4733 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004734 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004735 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004736 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004737
Josef Bacika41ad392011-01-31 15:30:16 -05004738 /*
4739 * We're truncating a file that used to have good data down to
4740 * zero. Make sure it gets into the ordered flush list so that
4741 * any new writes get down to disk quickly.
4742 */
4743 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004744 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4745 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004746
Josef Bacikf3fe8202013-01-07 17:03:21 -05004747 /*
4748 * 1 for the orphan item we're going to add
4749 * 1 for the orphan item deletion.
4750 */
4751 trans = btrfs_start_transaction(root, 2);
4752 if (IS_ERR(trans))
4753 return PTR_ERR(trans);
4754
4755 /*
4756 * We need to do this in case we fail at _any_ point during the
4757 * actual truncate. Once we do the truncate_setsize we could
4758 * invalidate pages which forces any outstanding ordered io to
4759 * be instantly completed which will give us extents that need
4760 * to be truncated. If we fail to get an orphan inode down we
4761 * could have left over extents that were never meant to live,
4762 * so we need to garuntee from this point on that everything
4763 * will be consistent.
4764 */
4765 ret = btrfs_orphan_add(trans, inode);
4766 btrfs_end_transaction(trans, root);
4767 if (ret)
4768 return ret;
4769
Josef Bacika41ad392011-01-31 15:30:16 -05004770 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4771 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004772
4773 /* Disable nonlocked read DIO to avoid the end less truncate */
4774 btrfs_inode_block_unlocked_dio(inode);
4775 inode_dio_wait(inode);
4776 btrfs_inode_resume_unlocked_dio(inode);
4777
Josef Bacika41ad392011-01-31 15:30:16 -05004778 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004779 if (ret && inode->i_nlink) {
4780 int err;
4781
4782 /*
4783 * failed to truncate, disk_i_size is only adjusted down
4784 * as we remove extents, so it should represent the true
4785 * size of the inode, so reset the in memory size and
4786 * delete our orphan entry.
4787 */
4788 trans = btrfs_join_transaction(root);
4789 if (IS_ERR(trans)) {
4790 btrfs_orphan_del(NULL, inode);
4791 return ret;
4792 }
4793 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4794 err = btrfs_orphan_del(trans, inode);
4795 if (err)
4796 btrfs_abort_transaction(trans, root, err);
4797 btrfs_end_transaction(trans, root);
4798 }
Yan, Zheng80825102009-11-12 09:35:36 +00004799 }
4800
Josef Bacika41ad392011-01-31 15:30:16 -05004801 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004802}
4803
Chris Mason39279cc2007-06-12 06:35:45 -04004804static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4805{
4806 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004807 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004808 int err;
4809
Li Zefanb83cc962010-12-20 16:04:08 +08004810 if (btrfs_root_readonly(root))
4811 return -EROFS;
4812
Chris Mason39279cc2007-06-12 06:35:45 -04004813 err = inode_change_ok(inode, attr);
4814 if (err)
4815 return err;
4816
Chris Mason5a3f23d2009-03-31 13:27:11 -04004817 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004818 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004819 if (err)
4820 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004821 }
Yan Zheng9036c102008-10-30 14:19:41 -04004822
Christoph Hellwig10257742010-06-04 11:30:02 +02004823 if (attr->ia_valid) {
4824 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004825 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004826 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004827
Josef Bacik22c44fe2011-11-30 10:45:38 -05004828 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004829 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004830 }
4831
Chris Mason39279cc2007-06-12 06:35:45 -04004832 return err;
4833}
Chris Mason61295eb2008-01-14 16:24:38 -05004834
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004835/*
4836 * While truncating the inode pages during eviction, we get the VFS calling
4837 * btrfs_invalidatepage() against each page of the inode. This is slow because
4838 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4839 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4840 * extent_state structures over and over, wasting lots of time.
4841 *
4842 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4843 * those expensive operations on a per page basis and do only the ordered io
4844 * finishing, while we release here the extent_map and extent_state structures,
4845 * without the excessive merging and splitting.
4846 */
4847static void evict_inode_truncate_pages(struct inode *inode)
4848{
4849 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4850 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4851 struct rb_node *node;
4852
4853 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004854 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004855
4856 write_lock(&map_tree->lock);
4857 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4858 struct extent_map *em;
4859
4860 node = rb_first(&map_tree->map);
4861 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004862 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4863 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004864 remove_extent_mapping(map_tree, em);
4865 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004866 if (need_resched()) {
4867 write_unlock(&map_tree->lock);
4868 cond_resched();
4869 write_lock(&map_tree->lock);
4870 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004871 }
4872 write_unlock(&map_tree->lock);
4873
4874 spin_lock(&io_tree->lock);
4875 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4876 struct extent_state *state;
4877 struct extent_state *cached_state = NULL;
4878
4879 node = rb_first(&io_tree->state);
4880 state = rb_entry(node, struct extent_state, rb_node);
4881 atomic_inc(&state->refs);
4882 spin_unlock(&io_tree->lock);
4883
4884 lock_extent_bits(io_tree, state->start, state->end,
4885 0, &cached_state);
4886 clear_extent_bit(io_tree, state->start, state->end,
4887 EXTENT_LOCKED | EXTENT_DIRTY |
4888 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4889 EXTENT_DEFRAG, 1, 1,
4890 &cached_state, GFP_NOFS);
4891 free_extent_state(state);
4892
Filipe Manana7064dd52014-08-08 02:47:05 +01004893 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004894 spin_lock(&io_tree->lock);
4895 }
4896 spin_unlock(&io_tree->lock);
4897}
4898
Al Virobd555972010-06-07 11:35:40 -04004899void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004900{
4901 struct btrfs_trans_handle *trans;
4902 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004903 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004904 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004905 int ret;
4906
liubo1abe9b82011-03-24 11:18:59 +00004907 trace_btrfs_inode_evict(inode);
4908
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004909 evict_inode_truncate_pages(inode);
4910
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004911 if (inode->i_nlink &&
4912 ((btrfs_root_refs(&root->root_item) != 0 &&
4913 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4914 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004915 goto no_delete;
4916
Chris Mason39279cc2007-06-12 06:35:45 -04004917 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004918 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004919 goto no_delete;
4920 }
Al Virobd555972010-06-07 11:35:40 -04004921 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004922 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004923
Miao Xief6124962014-09-12 18:44:04 +08004924 btrfs_free_io_failure_record(inode, 0, (u64)-1);
4925
Yan, Zhengc71bf092009-11-12 09:34:40 +00004926 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004927 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004928 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004929 goto no_delete;
4930 }
4931
Yan, Zheng76dda932009-09-21 16:00:26 -04004932 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004933 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4934 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004935 goto no_delete;
4936 }
4937
Miao Xie0e8c36a2012-12-19 06:59:51 +00004938 ret = btrfs_commit_inode_delayed_inode(inode);
4939 if (ret) {
4940 btrfs_orphan_del(NULL, inode);
4941 goto no_delete;
4942 }
4943
Miao Xie66d8f3d2012-09-06 04:02:28 -06004944 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004945 if (!rsv) {
4946 btrfs_orphan_del(NULL, inode);
4947 goto no_delete;
4948 }
Josef Bacik4a338542011-08-29 11:01:31 -04004949 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004950 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004951 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004952
Chris Masondbe674a2008-07-17 12:54:05 -04004953 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004954
Josef Bacik4289a662011-08-05 13:22:24 -04004955 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004956 * This is a bit simpler than btrfs_truncate since we've already
4957 * reserved our space for our orphan item in the unlink, so we just
4958 * need to reserve some slack space in case we add bytes and update
4959 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004960 */
Yan, Zheng80825102009-11-12 09:35:36 +00004961 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004962 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4963 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004964
Josef Bacik726c35f2011-09-26 15:46:06 -04004965 /*
4966 * Try and steal from the global reserve since we will
4967 * likely not use this space anyway, we want to try as
4968 * hard as possible to get this to work.
4969 */
4970 if (ret)
4971 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4972
Yan, Zhengd68fc572010-05-16 10:49:58 -04004973 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004974 btrfs_warn(root->fs_info,
4975 "Could not get space for a delete, will truncate on mount %d",
4976 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004977 btrfs_orphan_del(NULL, inode);
4978 btrfs_free_block_rsv(root, rsv);
4979 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004980 }
4981
Miao Xie0e8c36a2012-12-19 06:59:51 +00004982 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004983 if (IS_ERR(trans)) {
4984 btrfs_orphan_del(NULL, inode);
4985 btrfs_free_block_rsv(root, rsv);
4986 goto no_delete;
4987 }
4988
4989 trans->block_rsv = rsv;
4990
Yan, Zhengd68fc572010-05-16 10:49:58 -04004991 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004992 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004993 break;
4994
Miao Xie8407aa42012-09-07 01:43:32 -06004995 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004996 btrfs_end_transaction(trans, root);
4997 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004998 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004999 }
5000
Josef Bacik4289a662011-08-05 13:22:24 -04005001 btrfs_free_block_rsv(root, rsv);
5002
Josef Bacik4ef31a42013-08-13 14:10:08 -04005003 /*
5004 * Errors here aren't a big deal, it just means we leave orphan items
5005 * in the tree. They will be cleaned up on the next mount.
5006 */
Yan, Zheng80825102009-11-12 09:35:36 +00005007 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005008 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005009 btrfs_orphan_del(trans, inode);
5010 } else {
5011 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005012 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005013
Josef Bacik4289a662011-08-05 13:22:24 -04005014 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005015 if (!(root == root->fs_info->tree_root ||
5016 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005017 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005018
Chris Mason54aa1f42007-06-22 14:16:25 -04005019 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005020 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005021no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005022 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005023 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005024 return;
Chris Mason39279cc2007-06-12 06:35:45 -04005025}
5026
5027/*
5028 * this returns the key found in the dir entry in the location pointer.
5029 * If no dir entries were found, location->objectid is 0.
5030 */
5031static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5032 struct btrfs_key *location)
5033{
5034 const char *name = dentry->d_name.name;
5035 int namelen = dentry->d_name.len;
5036 struct btrfs_dir_item *di;
5037 struct btrfs_path *path;
5038 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005039 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005040
5041 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005042 if (!path)
5043 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005044
Li Zefan33345d012011-04-20 10:31:50 +08005045 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005046 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005047 if (IS_ERR(di))
5048 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005049
David Sterbac7040052011-04-19 18:00:01 +02005050 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005051 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005052
Chris Mason5f39d392007-10-15 16:14:19 -04005053 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005054out:
Chris Mason39279cc2007-06-12 06:35:45 -04005055 btrfs_free_path(path);
5056 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005057out_err:
5058 location->objectid = 0;
5059 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005060}
5061
5062/*
5063 * when we hit a tree root in a directory, the btrfs part of the inode
5064 * needs to be changed to reflect the root directory of the tree root. This
5065 * is kind of like crossing a mount point.
5066 */
5067static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005068 struct inode *dir,
5069 struct dentry *dentry,
5070 struct btrfs_key *location,
5071 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005072{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005073 struct btrfs_path *path;
5074 struct btrfs_root *new_root;
5075 struct btrfs_root_ref *ref;
5076 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005077 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005078 int ret;
5079 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005080
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005081 path = btrfs_alloc_path();
5082 if (!path) {
5083 err = -ENOMEM;
5084 goto out;
5085 }
Chris Mason39279cc2007-06-12 06:35:45 -04005086
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005087 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005088 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5089 key.type = BTRFS_ROOT_REF_KEY;
5090 key.offset = location->objectid;
5091
5092 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5093 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005094 if (ret) {
5095 if (ret < 0)
5096 err = ret;
5097 goto out;
5098 }
Chris Mason39279cc2007-06-12 06:35:45 -04005099
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005100 leaf = path->nodes[0];
5101 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005102 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005103 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5104 goto out;
5105
5106 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5107 (unsigned long)(ref + 1),
5108 dentry->d_name.len);
5109 if (ret)
5110 goto out;
5111
David Sterbab3b4aa72011-04-21 01:20:15 +02005112 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005113
5114 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5115 if (IS_ERR(new_root)) {
5116 err = PTR_ERR(new_root);
5117 goto out;
5118 }
5119
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005120 *sub_root = new_root;
5121 location->objectid = btrfs_root_dirid(&new_root->root_item);
5122 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005123 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005124 err = 0;
5125out:
5126 btrfs_free_path(path);
5127 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005128}
5129
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005130static void inode_tree_add(struct inode *inode)
5131{
5132 struct btrfs_root *root = BTRFS_I(inode)->root;
5133 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005134 struct rb_node **p;
5135 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005136 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005137 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005138
Al Viro1d3382cb2010-10-23 15:19:20 -04005139 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005140 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005141 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005142 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005143 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005144 while (*p) {
5145 parent = *p;
5146 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5147
Li Zefan33345d012011-04-20 10:31:50 +08005148 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005149 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005150 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005151 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005152 else {
5153 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005154 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005155 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005156 RB_CLEAR_NODE(parent);
5157 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005158 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005159 }
5160 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005161 rb_link_node(new, parent, p);
5162 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005163 spin_unlock(&root->inode_lock);
5164}
5165
5166static void inode_tree_del(struct inode *inode)
5167{
5168 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005169 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005170
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005171 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005172 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005173 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005174 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005175 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005176 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005177 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005178
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005179 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005180 synchronize_srcu(&root->fs_info->subvol_srcu);
5181 spin_lock(&root->inode_lock);
5182 empty = RB_EMPTY_ROOT(&root->inode_tree);
5183 spin_unlock(&root->inode_lock);
5184 if (empty)
5185 btrfs_add_dead_root(root);
5186 }
5187}
5188
Jeff Mahoney143bede2012-03-01 14:56:26 +01005189void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005190{
5191 struct rb_node *node;
5192 struct rb_node *prev;
5193 struct btrfs_inode *entry;
5194 struct inode *inode;
5195 u64 objectid = 0;
5196
Liu Bo7813b3d2014-02-10 17:37:25 +08005197 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5198 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005199
5200 spin_lock(&root->inode_lock);
5201again:
5202 node = root->inode_tree.rb_node;
5203 prev = NULL;
5204 while (node) {
5205 prev = node;
5206 entry = rb_entry(node, struct btrfs_inode, rb_node);
5207
Li Zefan33345d012011-04-20 10:31:50 +08005208 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005209 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005210 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005211 node = node->rb_right;
5212 else
5213 break;
5214 }
5215 if (!node) {
5216 while (prev) {
5217 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005218 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005219 node = prev;
5220 break;
5221 }
5222 prev = rb_next(prev);
5223 }
5224 }
5225 while (node) {
5226 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005227 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005228 inode = igrab(&entry->vfs_inode);
5229 if (inode) {
5230 spin_unlock(&root->inode_lock);
5231 if (atomic_read(&inode->i_count) > 1)
5232 d_prune_aliases(inode);
5233 /*
Al Viro45321ac2010-06-07 13:43:19 -04005234 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005235 * the inode cache when its usage count
5236 * hits zero.
5237 */
5238 iput(inode);
5239 cond_resched();
5240 spin_lock(&root->inode_lock);
5241 goto again;
5242 }
5243
5244 if (cond_resched_lock(&root->inode_lock))
5245 goto again;
5246
5247 node = rb_next(node);
5248 }
5249 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005250}
5251
Chris Masone02119d2008-09-05 16:13:11 -04005252static int btrfs_init_locked_inode(struct inode *inode, void *p)
5253{
5254 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005255 inode->i_ino = args->location->objectid;
5256 memcpy(&BTRFS_I(inode)->location, args->location,
5257 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005258 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005259 return 0;
5260}
5261
5262static int btrfs_find_actor(struct inode *inode, void *opaque)
5263{
5264 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005265 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005266 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005267}
5268
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005269static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005270 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005271 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005272{
5273 struct inode *inode;
5274 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005275 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005276
Chris Mason90d3e592014-01-09 17:28:00 -08005277 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005278 args.root = root;
5279
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005280 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005281 btrfs_init_locked_inode,
5282 (void *)&args);
5283 return inode;
5284}
5285
Balaji Rao1a54ef82008-07-21 02:01:04 +05305286/* Get an inode object given its location and corresponding root.
5287 * Returns in *is_new if the inode was read from disk
5288 */
5289struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005290 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305291{
5292 struct inode *inode;
5293
Chris Mason90d3e592014-01-09 17:28:00 -08005294 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305295 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005296 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305297
5298 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305299 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005300 if (!is_bad_inode(inode)) {
5301 inode_tree_add(inode);
5302 unlock_new_inode(inode);
5303 if (new)
5304 *new = 1;
5305 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005306 unlock_new_inode(inode);
5307 iput(inode);
5308 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005309 }
5310 }
5311
Balaji Rao1a54ef82008-07-21 02:01:04 +05305312 return inode;
5313}
5314
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005315static struct inode *new_simple_dir(struct super_block *s,
5316 struct btrfs_key *key,
5317 struct btrfs_root *root)
5318{
5319 struct inode *inode = new_inode(s);
5320
5321 if (!inode)
5322 return ERR_PTR(-ENOMEM);
5323
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005324 BTRFS_I(inode)->root = root;
5325 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005326 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005327
5328 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005329 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005330 inode->i_fop = &simple_dir_operations;
5331 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
chandan r9cc97d62012-07-04 12:48:07 +05305332 inode->i_mtime = CURRENT_TIME;
5333 inode->i_atime = inode->i_mtime;
5334 inode->i_ctime = inode->i_mtime;
5335 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005336
5337 return inode;
5338}
5339
Chris Mason3de45862008-11-17 21:02:50 -05005340struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005341{
Chris Masond3977122009-01-05 21:25:51 -05005342 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005343 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005344 struct btrfs_root *sub_root = root;
5345 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005346 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005347 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005348
5349 if (dentry->d_name.len > BTRFS_NAME_LEN)
5350 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005351
Jeff Layton39e3c952012-11-28 11:30:53 -05005352 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005353 if (ret < 0)
5354 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005355
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005356 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005357 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005358
5359 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005360 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005361 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005362 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005363
5364 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5365
Yan, Zheng76dda932009-09-21 16:00:26 -04005366 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005367 ret = fixup_tree_root_location(root, dir, dentry,
5368 &location, &sub_root);
5369 if (ret < 0) {
5370 if (ret != -ENOENT)
5371 inode = ERR_PTR(ret);
5372 else
5373 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5374 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005375 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005376 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005377 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5378
Julia Lawall34d19ba2011-01-24 19:55:19 +00005379 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005380 down_read(&root->fs_info->cleanup_work_sem);
5381 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005382 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005383 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005384 if (ret) {
5385 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005386 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005387 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005388 }
5389
Chris Mason3de45862008-11-17 21:02:50 -05005390 return inode;
5391}
5392
Nick Pigginfe15ce42011-01-07 17:49:23 +11005393static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005394{
5395 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005396 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005397
Li Zefan848cce02012-02-21 17:04:28 +08005398 if (!inode && !IS_ROOT(dentry))
5399 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005400
Li Zefan848cce02012-02-21 17:04:28 +08005401 if (inode) {
5402 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005403 if (btrfs_root_refs(&root->root_item) == 0)
5404 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005405
5406 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5407 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005408 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005409 return 0;
5410}
5411
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005412static void btrfs_dentry_release(struct dentry *dentry)
5413{
Daeseok Youn944a4512014-04-14 15:37:02 +09005414 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005415}
5416
Chris Mason3de45862008-11-17 21:02:50 -05005417static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005418 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005419{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005420 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005421
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005422 inode = btrfs_lookup_dentry(dir, dentry);
5423 if (IS_ERR(inode)) {
5424 if (PTR_ERR(inode) == -ENOENT)
5425 inode = NULL;
5426 else
5427 return ERR_CAST(inode);
5428 }
5429
Al Viro41d28bc2014-10-12 22:24:21 -04005430 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005431}
5432
Miao Xie16cdcec2011-04-22 18:12:22 +08005433unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005434 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5435};
5436
Al Viro9cdda8d2013-05-22 16:48:09 -04005437static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005438{
Al Viro9cdda8d2013-05-22 16:48:09 -04005439 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005440 struct btrfs_root *root = BTRFS_I(inode)->root;
5441 struct btrfs_item *item;
5442 struct btrfs_dir_item *di;
5443 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005444 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005445 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005446 struct list_head ins_list;
5447 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005448 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005449 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005450 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005451 unsigned char d_type;
5452 int over = 0;
5453 u32 di_cur;
5454 u32 di_total;
5455 u32 di_len;
5456 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005457 char tmp_name[32];
5458 char *name_ptr;
5459 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005460 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005461
5462 /* FIXME, use a real flag for deciding about the key type */
5463 if (root->fs_info->tree_root == root)
5464 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005465
Al Viro9cdda8d2013-05-22 16:48:09 -04005466 if (!dir_emit_dots(file, ctx))
5467 return 0;
5468
David Woodhouse49593bf2008-08-17 17:08:36 +01005469 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005470 if (!path)
5471 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005472
Josef Bacik026fd312011-05-13 10:32:11 -04005473 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005474
Miao Xie16cdcec2011-04-22 18:12:22 +08005475 if (key_type == BTRFS_DIR_INDEX_KEY) {
5476 INIT_LIST_HEAD(&ins_list);
5477 INIT_LIST_HEAD(&del_list);
5478 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5479 }
5480
David Sterba962a2982014-06-04 18:41:45 +02005481 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005482 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005483 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005484
Chris Mason39279cc2007-06-12 06:35:45 -04005485 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5486 if (ret < 0)
5487 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005488
5489 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005490 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005491 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005492 if (slot >= btrfs_header_nritems(leaf)) {
5493 ret = btrfs_next_leaf(root, path);
5494 if (ret < 0)
5495 goto err;
5496 else if (ret > 0)
5497 break;
5498 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005499 }
Chris Mason3de45862008-11-17 21:02:50 -05005500
Ross Kirkdd3cc162013-09-16 15:58:09 +01005501 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005502 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5503
5504 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005505 break;
David Sterba962a2982014-06-04 18:41:45 +02005506 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005507 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005508 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005509 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005510 if (key_type == BTRFS_DIR_INDEX_KEY &&
5511 btrfs_should_delete_dir_index(&del_list,
5512 found_key.offset))
5513 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005514
Al Viro9cdda8d2013-05-22 16:48:09 -04005515 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005516 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005517
Chris Mason39279cc2007-06-12 06:35:45 -04005518 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5519 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005520 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005521
5522 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005523 struct btrfs_key location;
5524
Josef Bacik22a94d42011-03-16 16:47:17 -04005525 if (verify_dir_item(root, leaf, di))
5526 break;
5527
Chris Mason5f39d392007-10-15 16:14:19 -04005528 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005529 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005530 name_ptr = tmp_name;
5531 } else {
5532 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005533 if (!name_ptr) {
5534 ret = -ENOMEM;
5535 goto err;
5536 }
Chris Mason5f39d392007-10-15 16:14:19 -04005537 }
5538 read_extent_buffer(leaf, name_ptr,
5539 (unsigned long)(di + 1), name_len);
5540
5541 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5542 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005543
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005544
Chris Mason3de45862008-11-17 21:02:50 -05005545 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005546 * skip it.
5547 *
5548 * In contrast to old kernels, we insert the snapshot's
5549 * dir item and dir index after it has been created, so
5550 * we won't find a reference to our own snapshot. We
5551 * still keep the following code for backward
5552 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005553 */
5554 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5555 location.objectid == root->root_key.objectid) {
5556 over = 0;
5557 goto skip;
5558 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005559 over = !dir_emit(ctx, name_ptr, name_len,
5560 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005561
Chris Mason3de45862008-11-17 21:02:50 -05005562skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005563 if (name_ptr != tmp_name)
5564 kfree(name_ptr);
5565
Chris Mason39279cc2007-06-12 06:35:45 -04005566 if (over)
5567 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005568 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005569 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005570 di_cur += di_len;
5571 di = (struct btrfs_dir_item *)((char *)di + di_len);
5572 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005573next:
5574 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005575 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005576
Miao Xie16cdcec2011-04-22 18:12:22 +08005577 if (key_type == BTRFS_DIR_INDEX_KEY) {
5578 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005579 ctx->pos++;
5580 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005581 if (ret)
5582 goto nopos;
5583 }
5584
David Woodhouse49593bf2008-08-17 17:08:36 +01005585 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005586 ctx->pos++;
5587
5588 /*
5589 * Stop new entries from being returned after we return the last
5590 * entry.
5591 *
5592 * New directory entries are assigned a strictly increasing
5593 * offset. This means that new entries created during readdir
5594 * are *guaranteed* to be seen in the future by that readdir.
5595 * This has broken buggy programs which operate on names as
5596 * they're returned by readdir. Until we re-use freed offsets
5597 * we have this hack to stop new entries from being returned
5598 * under the assumption that they'll never reach this huge
5599 * offset.
5600 *
5601 * This is being careful not to overflow 32bit loff_t unless the
5602 * last entry requires it because doing so has broken 32bit apps
5603 * in the past.
5604 */
5605 if (key_type == BTRFS_DIR_INDEX_KEY) {
5606 if (ctx->pos >= INT_MAX)
5607 ctx->pos = LLONG_MAX;
5608 else
5609 ctx->pos = INT_MAX;
5610 }
Chris Mason39279cc2007-06-12 06:35:45 -04005611nopos:
5612 ret = 0;
5613err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005614 if (key_type == BTRFS_DIR_INDEX_KEY)
5615 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005616 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005617 return ret;
5618}
5619
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005620int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005621{
5622 struct btrfs_root *root = BTRFS_I(inode)->root;
5623 struct btrfs_trans_handle *trans;
5624 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005625 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005626
Josef Bacik72ac3c02012-05-23 14:13:11 -04005627 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005628 return 0;
5629
Liu Bo83eea1f2012-07-10 05:28:39 -06005630 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005631 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005632
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005633 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005634 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005635 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005636 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005637 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005638 if (IS_ERR(trans))
5639 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005640 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005641 }
5642 return ret;
5643}
5644
5645/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005646 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005647 * inode changes. But, it is most likely to find the inode in cache.
5648 * FIXME, needs more benchmarking...there are no reasons other than performance
5649 * to keep or drop this code.
5650 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005651static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005652{
5653 struct btrfs_root *root = BTRFS_I(inode)->root;
5654 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005655 int ret;
5656
Josef Bacik72ac3c02012-05-23 14:13:11 -04005657 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005658 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005659
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005660 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005661 if (IS_ERR(trans))
5662 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005663
5664 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005665 if (ret && ret == -ENOSPC) {
5666 /* whoops, lets try again with the full transaction */
5667 btrfs_end_transaction(trans, root);
5668 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005669 if (IS_ERR(trans))
5670 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005671
Chris Mason94b60442010-05-26 11:02:00 -04005672 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005673 }
Chris Mason39279cc2007-06-12 06:35:45 -04005674 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005675 if (BTRFS_I(inode)->delayed_node)
5676 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005677
5678 return ret;
5679}
5680
5681/*
5682 * This is a copy of file_update_time. We need this so we can return error on
5683 * ENOSPC for updating the inode in the case of file write and mmap writes.
5684 */
Josef Bacike41f9412012-03-26 09:46:47 -04005685static int btrfs_update_time(struct inode *inode, struct timespec *now,
5686 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005687{
Alexander Block2bc5565282012-06-15 09:49:33 +02005688 struct btrfs_root *root = BTRFS_I(inode)->root;
5689
5690 if (btrfs_root_readonly(root))
5691 return -EROFS;
5692
Josef Bacike41f9412012-03-26 09:46:47 -04005693 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005694 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005695 if (flags & S_CTIME)
5696 inode->i_ctime = *now;
5697 if (flags & S_MTIME)
5698 inode->i_mtime = *now;
5699 if (flags & S_ATIME)
5700 inode->i_atime = *now;
5701 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005702}
5703
Chris Masond352ac62008-09-29 15:18:18 -04005704/*
5705 * find the highest existing sequence number in a directory
5706 * and then set the in-memory index_cnt variable to reflect
5707 * free sequence numbers
5708 */
Josef Bacikaec74772008-07-24 12:12:38 -04005709static int btrfs_set_inode_index_count(struct inode *inode)
5710{
5711 struct btrfs_root *root = BTRFS_I(inode)->root;
5712 struct btrfs_key key, found_key;
5713 struct btrfs_path *path;
5714 struct extent_buffer *leaf;
5715 int ret;
5716
Li Zefan33345d012011-04-20 10:31:50 +08005717 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005718 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005719 key.offset = (u64)-1;
5720
5721 path = btrfs_alloc_path();
5722 if (!path)
5723 return -ENOMEM;
5724
5725 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5726 if (ret < 0)
5727 goto out;
5728 /* FIXME: we should be able to handle this */
5729 if (ret == 0)
5730 goto out;
5731 ret = 0;
5732
5733 /*
5734 * MAGIC NUMBER EXPLANATION:
5735 * since we search a directory based on f_pos we have to start at 2
5736 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5737 * else has to start at 2
5738 */
5739 if (path->slots[0] == 0) {
5740 BTRFS_I(inode)->index_cnt = 2;
5741 goto out;
5742 }
5743
5744 path->slots[0]--;
5745
5746 leaf = path->nodes[0];
5747 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5748
Li Zefan33345d012011-04-20 10:31:50 +08005749 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005750 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005751 BTRFS_I(inode)->index_cnt = 2;
5752 goto out;
5753 }
5754
5755 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5756out:
5757 btrfs_free_path(path);
5758 return ret;
5759}
5760
Chris Masond352ac62008-09-29 15:18:18 -04005761/*
5762 * helper to find a free sequence number in a given directory. This current
5763 * code is very simple, later versions will do smarter things in the btree
5764 */
Chris Mason3de45862008-11-17 21:02:50 -05005765int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005766{
5767 int ret = 0;
5768
5769 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005770 ret = btrfs_inode_delayed_dir_index_count(dir);
5771 if (ret) {
5772 ret = btrfs_set_inode_index_count(dir);
5773 if (ret)
5774 return ret;
5775 }
Josef Bacikaec74772008-07-24 12:12:38 -04005776 }
5777
Chris Mason00e4e6b2008-08-05 11:18:09 -04005778 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005779 BTRFS_I(dir)->index_cnt++;
5780
5781 return ret;
5782}
5783
Chris Masonb0d5d102014-09-08 13:08:51 -07005784static int btrfs_insert_inode_locked(struct inode *inode)
5785{
5786 struct btrfs_iget_args args;
5787 args.location = &BTRFS_I(inode)->location;
5788 args.root = BTRFS_I(inode)->root;
5789
5790 return insert_inode_locked4(inode,
5791 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5792 btrfs_find_actor, &args);
5793}
5794
Chris Mason39279cc2007-06-12 06:35:45 -04005795static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5796 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005797 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005798 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005799 u64 ref_objectid, u64 objectid,
5800 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005801{
5802 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005803 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005804 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005805 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005806 struct btrfs_inode_ref *ref;
5807 struct btrfs_key key[2];
5808 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005809 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005810 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005811 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005812
Chris Mason5f39d392007-10-15 16:14:19 -04005813 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005814 if (!path)
5815 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005816
Chris Mason39279cc2007-06-12 06:35:45 -04005817 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005818 if (!inode) {
5819 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005820 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005821 }
Chris Mason39279cc2007-06-12 06:35:45 -04005822
Li Zefan581bb052011-04-20 10:06:11 +08005823 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005824 * O_TMPFILE, set link count to 0, so that after this point,
5825 * we fill in an inode item with the correct link count.
5826 */
5827 if (!name)
5828 set_nlink(inode, 0);
5829
5830 /*
Li Zefan581bb052011-04-20 10:06:11 +08005831 * we have to initialize this early, so we can reclaim the inode
5832 * number if we fail afterwards in this function.
5833 */
5834 inode->i_ino = objectid;
5835
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005836 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005837 trace_btrfs_inode_request(dir);
5838
Chris Mason3de45862008-11-17 21:02:50 -05005839 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005840 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005841 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005842 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005843 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005844 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005845 } else if (dir) {
5846 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005847 }
5848 /*
5849 * index_cnt is ignored for everything but a dir,
5850 * btrfs_get_inode_index_count has an explanation for the magic
5851 * number
5852 */
5853 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005854 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005855 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005856 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005857 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005858
Josef Bacik5dc562c2012-08-17 13:14:17 -04005859 /*
5860 * We could have gotten an inode number from somebody who was fsynced
5861 * and then removed in this same transaction, so let's just set full
5862 * sync since it will be a full sync anyway and this will blow away the
5863 * old info in the log.
5864 */
5865 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5866
Chris Mason9c583092008-01-29 15:15:18 -05005867 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005868 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005869 key[0].offset = 0;
5870
Chris Mason9c583092008-01-29 15:15:18 -05005871 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005872
5873 if (name) {
5874 /*
5875 * Start new inodes with an inode_ref. This is slightly more
5876 * efficient for small numbers of hard links since they will
5877 * be packed into one item. Extended refs will kick in if we
5878 * add more hard links than can fit in the ref item.
5879 */
5880 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005881 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005882 key[1].offset = ref_objectid;
5883
5884 sizes[1] = name_len + sizeof(*ref);
5885 }
Chris Mason9c583092008-01-29 15:15:18 -05005886
Chris Masonb0d5d102014-09-08 13:08:51 -07005887 location = &BTRFS_I(inode)->location;
5888 location->objectid = objectid;
5889 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005890 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005891
5892 ret = btrfs_insert_inode_locked(inode);
5893 if (ret < 0)
5894 goto fail;
5895
Chris Masonb9473432009-03-13 11:00:37 -04005896 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005897 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005898 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005899 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005900
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005901 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005902 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05305903
5904 inode->i_mtime = CURRENT_TIME;
5905 inode->i_atime = inode->i_mtime;
5906 inode->i_ctime = inode->i_mtime;
5907 BTRFS_I(inode)->i_otime = inode->i_mtime;
5908
Chris Mason5f39d392007-10-15 16:14:19 -04005909 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5910 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005911 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5912 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005913 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005914
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005915 if (name) {
5916 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5917 struct btrfs_inode_ref);
5918 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5919 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5920 ptr = (unsigned long)(ref + 1);
5921 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5922 }
Chris Mason9c583092008-01-29 15:15:18 -05005923
Chris Mason5f39d392007-10-15 16:14:19 -04005924 btrfs_mark_buffer_dirty(path->nodes[0]);
5925 btrfs_free_path(path);
5926
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005927 btrfs_inherit_iflags(inode, dir);
5928
Al Viro569254b2011-07-24 17:08:40 -04005929 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005930 if (btrfs_test_opt(root, NODATASUM))
5931 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005932 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005933 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5934 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005935 }
5936
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005937 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005938
5939 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005940 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005941
Alexander Block8ea05e32012-07-25 17:35:53 +02005942 btrfs_update_root_times(trans, root);
5943
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005944 ret = btrfs_inode_inherit_props(trans, inode, dir);
5945 if (ret)
5946 btrfs_err(root->fs_info,
5947 "error inheriting props for ino %llu (root %llu): %d",
5948 btrfs_ino(inode), root->root_key.objectid, ret);
5949
Chris Mason39279cc2007-06-12 06:35:45 -04005950 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005951
5952fail_unlock:
5953 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005954fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005955 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005956 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005957 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005958 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005959 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005960}
5961
5962static inline u8 btrfs_inode_type(struct inode *inode)
5963{
5964 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5965}
5966
Chris Masond352ac62008-09-29 15:18:18 -04005967/*
5968 * utility function to add 'inode' into 'parent_inode' with
5969 * a give name and a given sequence number.
5970 * if 'add_backref' is true, also insert a backref from the
5971 * inode to the parent directory.
5972 */
Chris Masone02119d2008-09-05 16:13:11 -04005973int btrfs_add_link(struct btrfs_trans_handle *trans,
5974 struct inode *parent_inode, struct inode *inode,
5975 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005976{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005977 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005978 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005979 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005980 u64 ino = btrfs_ino(inode);
5981 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005982
Li Zefan33345d012011-04-20 10:31:50 +08005983 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005984 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5985 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005986 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02005987 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005988 key.offset = 0;
5989 }
Chris Mason39279cc2007-06-12 06:35:45 -04005990
Li Zefan33345d012011-04-20 10:31:50 +08005991 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005992 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5993 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005994 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005995 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005996 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5997 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005998 }
5999
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006000 /* Nothing to clean up yet */
6001 if (ret)
6002 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006003
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006004 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6005 parent_inode, &key,
6006 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006007 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006008 goto fail_dir_item;
6009 else if (ret) {
6010 btrfs_abort_transaction(trans, root, ret);
6011 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006012 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006013
6014 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6015 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006016 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006017 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
6018 ret = btrfs_update_inode(trans, root, parent_inode);
6019 if (ret)
6020 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006021 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006022
6023fail_dir_item:
6024 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6025 u64 local_index;
6026 int err;
6027 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6028 key.objectid, root->root_key.objectid,
6029 parent_ino, &local_index, name, name_len);
6030
6031 } else if (add_backref) {
6032 u64 local_index;
6033 int err;
6034
6035 err = btrfs_del_inode_ref(trans, root, name, name_len,
6036 ino, parent_ino, &local_index);
6037 }
6038 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006039}
6040
6041static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006042 struct inode *dir, struct dentry *dentry,
6043 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006044{
Josef Bacika1b075d2010-11-19 20:36:11 +00006045 int err = btrfs_add_link(trans, dir, inode,
6046 dentry->d_name.name, dentry->d_name.len,
6047 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006048 if (err > 0)
6049 err = -EEXIST;
6050 return err;
6051}
6052
Josef Bacik618e21d2007-07-11 10:18:17 -04006053static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006054 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006055{
6056 struct btrfs_trans_handle *trans;
6057 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006058 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006059 int err;
6060 int drop_inode = 0;
6061 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006062 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006063
6064 if (!new_valid_dev(rdev))
6065 return -EINVAL;
6066
Josef Bacik9ed74f22009-09-11 16:12:44 -04006067 /*
6068 * 2 for inode item and ref
6069 * 2 for dir items
6070 * 1 for xattr if selinux is on
6071 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006072 trans = btrfs_start_transaction(root, 5);
6073 if (IS_ERR(trans))
6074 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006075
Li Zefan581bb052011-04-20 10:06:11 +08006076 err = btrfs_find_free_ino(root, &objectid);
6077 if (err)
6078 goto out_unlock;
6079
Josef Bacikaec74772008-07-24 12:12:38 -04006080 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006081 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006082 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006083 if (IS_ERR(inode)) {
6084 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006085 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006086 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006087
Casey Schauflerad19db72011-12-15 10:09:07 -05006088 /*
6089 * If the active LSM wants to access the inode during
6090 * d_instantiate it needs these. Smack checks to see
6091 * if the filesystem supports xattrs by looking at the
6092 * ops vector.
6093 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006094 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006095 init_special_inode(inode, inode->i_mode, rdev);
6096
6097 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006098 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006099 goto out_unlock_inode;
6100
6101 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6102 if (err) {
6103 goto out_unlock_inode;
6104 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006105 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006106 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006107 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006108 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006109
Josef Bacik618e21d2007-07-11 10:18:17 -04006110out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006111 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006112 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006113 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006114 if (drop_inode) {
6115 inode_dec_link_count(inode);
6116 iput(inode);
6117 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006118 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006119
6120out_unlock_inode:
6121 drop_inode = 1;
6122 unlock_new_inode(inode);
6123 goto out_unlock;
6124
Josef Bacik618e21d2007-07-11 10:18:17 -04006125}
6126
Chris Mason39279cc2007-06-12 06:35:45 -04006127static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006128 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006129{
6130 struct btrfs_trans_handle *trans;
6131 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006132 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006133 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006134 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006135 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006136 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006137
Josef Bacik9ed74f22009-09-11 16:12:44 -04006138 /*
6139 * 2 for inode item and ref
6140 * 2 for dir items
6141 * 1 for xattr if selinux is on
6142 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006143 trans = btrfs_start_transaction(root, 5);
6144 if (IS_ERR(trans))
6145 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006146
Li Zefan581bb052011-04-20 10:06:11 +08006147 err = btrfs_find_free_ino(root, &objectid);
6148 if (err)
6149 goto out_unlock;
6150
Josef Bacikaec74772008-07-24 12:12:38 -04006151 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006152 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006153 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006154 if (IS_ERR(inode)) {
6155 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006156 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006157 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006158 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006159 /*
6160 * If the active LSM wants to access the inode during
6161 * d_instantiate it needs these. Smack checks to see
6162 * if the filesystem supports xattrs by looking at the
6163 * ops vector.
6164 */
6165 inode->i_fop = &btrfs_file_operations;
6166 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006167 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006168
6169 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6170 if (err)
6171 goto out_unlock_inode;
6172
6173 err = btrfs_update_inode(trans, root, inode);
6174 if (err)
6175 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006176
Josef Bacika1b075d2010-11-19 20:36:11 +00006177 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006178 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006179 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006180
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006181 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006182 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006183 d_instantiate(dentry, inode);
6184
Chris Mason39279cc2007-06-12 06:35:45 -04006185out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006186 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006187 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006188 inode_dec_link_count(inode);
6189 iput(inode);
6190 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006191 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006192 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006193 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006194
6195out_unlock_inode:
6196 unlock_new_inode(inode);
6197 goto out_unlock;
6198
Chris Mason39279cc2007-06-12 06:35:45 -04006199}
6200
6201static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6202 struct dentry *dentry)
6203{
6204 struct btrfs_trans_handle *trans;
6205 struct btrfs_root *root = BTRFS_I(dir)->root;
6206 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006207 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006208 int err;
6209 int drop_inode = 0;
6210
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006211 /* do not allow sys_link's with other subvols of the same device */
6212 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006213 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006214
Mark Fashehf1863732012-08-08 11:32:27 -07006215 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006216 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006217
Chris Mason3de45862008-11-17 21:02:50 -05006218 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006219 if (err)
6220 goto fail;
6221
Yan, Zhenga22285a2010-05-16 10:48:46 -04006222 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006223 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006224 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006225 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006226 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006227 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006228 if (IS_ERR(trans)) {
6229 err = PTR_ERR(trans);
6230 goto fail;
6231 }
Chris Mason5f39d392007-10-15 16:14:19 -04006232
Miao Xie67de1172013-12-26 13:07:06 +08006233 /* There are several dir indexes for this inode, clear the cache. */
6234 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006235 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006236 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006237 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006238 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006239 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006240
Josef Bacika1b075d2010-11-19 20:36:11 +00006241 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006242
Yan, Zhenga5719522009-09-24 09:17:31 -04006243 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006244 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006245 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006246 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006247 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006248 if (err)
6249 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006250 if (inode->i_nlink == 1) {
6251 /*
6252 * If new hard link count is 1, it's a file created
6253 * with open(2) O_TMPFILE flag.
6254 */
6255 err = btrfs_orphan_del(trans, inode);
6256 if (err)
6257 goto fail;
6258 }
Al Viro08c422c2011-12-23 07:58:13 -05006259 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006260 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006261 }
Chris Mason39279cc2007-06-12 06:35:45 -04006262
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006263 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006264 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006265fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006266 if (drop_inode) {
6267 inode_dec_link_count(inode);
6268 iput(inode);
6269 }
Liu Bob53d3f52012-11-14 14:34:34 +00006270 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006271 return err;
6272}
6273
Al Viro18bb1db2011-07-26 01:41:39 -04006274static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006275{
Chris Masonb9d86662008-05-02 16:13:49 -04006276 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006277 struct btrfs_trans_handle *trans;
6278 struct btrfs_root *root = BTRFS_I(dir)->root;
6279 int err = 0;
6280 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006281 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006282 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006283
Josef Bacik9ed74f22009-09-11 16:12:44 -04006284 /*
6285 * 2 items for inode and ref
6286 * 2 items for dir items
6287 * 1 for xattr if selinux is on
6288 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006289 trans = btrfs_start_transaction(root, 5);
6290 if (IS_ERR(trans))
6291 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006292
Li Zefan581bb052011-04-20 10:06:11 +08006293 err = btrfs_find_free_ino(root, &objectid);
6294 if (err)
6295 goto out_fail;
6296
Josef Bacikaec74772008-07-24 12:12:38 -04006297 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006298 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006299 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006300 if (IS_ERR(inode)) {
6301 err = PTR_ERR(inode);
6302 goto out_fail;
6303 }
Chris Mason5f39d392007-10-15 16:14:19 -04006304
Chris Mason39279cc2007-06-12 06:35:45 -04006305 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006306 /* these must be set before we unlock the inode */
6307 inode->i_op = &btrfs_dir_inode_operations;
6308 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006309
Eric Paris2a7dba32011-02-01 11:05:39 -05006310 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006311 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006312 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006313
Chris Masondbe674a2008-07-17 12:54:05 -04006314 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006315 err = btrfs_update_inode(trans, root, inode);
6316 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006317 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006318
Josef Bacika1b075d2010-11-19 20:36:11 +00006319 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6320 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006321 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006322 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006323
Chris Mason39279cc2007-06-12 06:35:45 -04006324 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006325 /*
6326 * mkdir is special. We're unlocking after we call d_instantiate
6327 * to avoid a race with nfsd calling d_instantiate.
6328 */
6329 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006330 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006331
6332out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006333 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006334 if (drop_on_err) {
6335 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006336 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006337 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006338 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006339 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006340 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006341
6342out_fail_inode:
6343 unlock_new_inode(inode);
6344 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006345}
6346
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006347/* Find next extent map of a given extent map, caller needs to ensure locks */
6348static struct extent_map *next_extent_map(struct extent_map *em)
6349{
6350 struct rb_node *next;
6351
6352 next = rb_next(&em->rb_node);
6353 if (!next)
6354 return NULL;
6355 return container_of(next, struct extent_map, rb_node);
6356}
6357
6358static struct extent_map *prev_extent_map(struct extent_map *em)
6359{
6360 struct rb_node *prev;
6361
6362 prev = rb_prev(&em->rb_node);
6363 if (!prev)
6364 return NULL;
6365 return container_of(prev, struct extent_map, rb_node);
6366}
6367
Chris Masond352ac62008-09-29 15:18:18 -04006368/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006369 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006370 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006371 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006372 */
Chris Mason3b951512008-04-17 11:29:12 -04006373static int merge_extent_mapping(struct extent_map_tree *em_tree,
6374 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006375 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006376 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006377{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006378 struct extent_map *prev;
6379 struct extent_map *next;
6380 u64 start;
6381 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006382 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006383
Chris Masone6dcd2d2008-07-17 12:53:50 -04006384 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006385
6386 if (existing->start > map_start) {
6387 next = existing;
6388 prev = prev_extent_map(next);
6389 } else {
6390 prev = existing;
6391 next = next_extent_map(prev);
6392 }
6393
6394 start = prev ? extent_map_end(prev) : em->start;
6395 start = max_t(u64, start, em->start);
6396 end = next ? next->start : extent_map_end(em);
6397 end = min_t(u64, end, extent_map_end(em));
6398 start_diff = start - em->start;
6399 em->start = start;
6400 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006401 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6402 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006403 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006404 em->block_len -= start_diff;
6405 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006406 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006407}
6408
Chris Masonc8b97812008-10-29 14:49:59 -04006409static noinline int uncompress_inline(struct btrfs_path *path,
6410 struct inode *inode, struct page *page,
6411 size_t pg_offset, u64 extent_offset,
6412 struct btrfs_file_extent_item *item)
6413{
6414 int ret;
6415 struct extent_buffer *leaf = path->nodes[0];
6416 char *tmp;
6417 size_t max_size;
6418 unsigned long inline_size;
6419 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006420 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006421
6422 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006423 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006424 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6425 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006426 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006427 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006428 if (!tmp)
6429 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006430 ptr = btrfs_file_extent_inline_start(item);
6431
6432 read_extent_buffer(leaf, tmp, ptr, inline_size);
6433
Chris Mason5b050f02008-11-11 09:34:41 -05006434 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006435 ret = btrfs_decompress(compress_type, tmp, page,
6436 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006437 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006438 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006439}
6440
Chris Masond352ac62008-09-29 15:18:18 -04006441/*
6442 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006443 * the ugly parts come from merging extents from the disk with the in-ram
6444 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006445 * where the in-ram extents might be locked pending data=ordered completion.
6446 *
6447 * This also copies inline extents directly into the page.
6448 */
Chris Masond3977122009-01-05 21:25:51 -05006449
Chris Masona52d9a82007-08-27 16:49:44 -04006450struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006451 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006452 int create)
6453{
6454 int ret;
6455 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006456 u64 extent_start = 0;
6457 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006458 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006459 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006460 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006461 struct btrfs_root *root = BTRFS_I(inode)->root;
6462 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006463 struct extent_buffer *leaf;
6464 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006465 struct extent_map *em = NULL;
6466 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006467 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006468 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006469 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006470
Chris Masona52d9a82007-08-27 16:49:44 -04006471again:
Chris Mason890871b2009-09-02 16:24:52 -04006472 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006473 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006474 if (em)
6475 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006476 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006477
Chris Masona52d9a82007-08-27 16:49:44 -04006478 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006479 if (em->start > start || em->start + em->len <= start)
6480 free_extent_map(em);
6481 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006482 free_extent_map(em);
6483 else
6484 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006485 }
David Sterba172ddd62011-04-21 00:48:27 +02006486 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006487 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006488 err = -ENOMEM;
6489 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006490 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006491 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006492 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006493 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006494 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006495 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006496
6497 if (!path) {
6498 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006499 if (!path) {
6500 err = -ENOMEM;
6501 goto out;
6502 }
6503 /*
6504 * Chances are we'll be called again, so go ahead and do
6505 * readahead
6506 */
6507 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006508 }
6509
Chris Mason179e29e2007-11-01 11:28:41 -04006510 ret = btrfs_lookup_file_extent(trans, root, path,
6511 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006512 if (ret < 0) {
6513 err = ret;
6514 goto out;
6515 }
6516
6517 if (ret != 0) {
6518 if (path->slots[0] == 0)
6519 goto not_found;
6520 path->slots[0]--;
6521 }
6522
Chris Mason5f39d392007-10-15 16:14:19 -04006523 leaf = path->nodes[0];
6524 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006525 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006526 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006527 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006528 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006529 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006530 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006531 /*
6532 * If we backup past the first extent we want to move forward
6533 * and see if there is an extent in front of us, otherwise we'll
6534 * say there is a hole for our whole search range which can
6535 * cause problems.
6536 */
6537 extent_end = start;
6538 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006539 }
6540
Chris Mason5f39d392007-10-15 16:14:19 -04006541 found_type = btrfs_file_extent_type(leaf, item);
6542 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006543 if (found_type == BTRFS_FILE_EXTENT_REG ||
6544 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006545 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006546 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006547 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6548 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006549 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006550 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006551 }
Josef Bacik25a50342013-10-14 12:08:38 -04006552next:
Yan Zheng9036c102008-10-30 14:19:41 -04006553 if (start >= extent_end) {
6554 path->slots[0]++;
6555 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6556 ret = btrfs_next_leaf(root, path);
6557 if (ret < 0) {
6558 err = ret;
6559 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006560 }
Yan Zheng9036c102008-10-30 14:19:41 -04006561 if (ret > 0)
6562 goto not_found;
6563 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006564 }
Yan Zheng9036c102008-10-30 14:19:41 -04006565 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6566 if (found_key.objectid != objectid ||
6567 found_key.type != BTRFS_EXTENT_DATA_KEY)
6568 goto not_found;
6569 if (start + len <= found_key.offset)
6570 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006571 if (start > found_key.offset)
6572 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006573 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006574 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006575 em->len = found_key.offset - start;
6576 goto not_found_em;
6577 }
6578
Filipe Manana7ffbb592014-06-09 03:48:05 +01006579 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6580
Yan Zhengd899e052008-10-30 14:25:28 -04006581 if (found_type == BTRFS_FILE_EXTENT_REG ||
6582 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006583 goto insert;
6584 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006585 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006586 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006587 size_t size;
6588 size_t extent_offset;
6589 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006590
Filipe Manana7ffbb592014-06-09 03:48:05 +01006591 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006592 goto out;
Yan689f9342007-10-29 11:41:07 -04006593
Chris Mason514ac8a2014-01-03 21:07:00 -08006594 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006595 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006596 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006597 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006598 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006599 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006600 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006601 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006602 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006603 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006604 if (btrfs_file_extent_compression(leaf, item) !=
6605 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006606 ret = uncompress_inline(path, inode, page,
6607 pg_offset,
6608 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006609 if (ret) {
6610 err = ret;
6611 goto out;
6612 }
Chris Masonc8b97812008-10-29 14:49:59 -04006613 } else {
6614 map = kmap(page);
6615 read_extent_buffer(leaf, map + pg_offset, ptr,
6616 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006617 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6618 memset(map + pg_offset + copy_size, 0,
6619 PAGE_CACHE_SIZE - pg_offset -
6620 copy_size);
6621 }
Chris Masonc8b97812008-10-29 14:49:59 -04006622 kunmap(page);
6623 }
Chris Mason179e29e2007-11-01 11:28:41 -04006624 flush_dcache_page(page);
6625 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006626 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006627 if (!trans) {
6628 kunmap(page);
6629 free_extent_map(em);
6630 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006631
David Sterbab3b4aa72011-04-21 01:20:15 +02006632 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006633 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006634
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006635 if (IS_ERR(trans))
6636 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006637 goto again;
6638 }
Chris Masonc8b97812008-10-29 14:49:59 -04006639 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006640 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006641 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006642 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006643 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006644 }
Chris Masond1310b22008-01-24 16:13:08 -05006645 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006646 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006647 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006648 }
6649not_found:
6650 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006651 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006652 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006653not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006654 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006655 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006656insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006657 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006658 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006659 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006660 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006661 err = -EIO;
6662 goto out;
6663 }
Chris Masond1310b22008-01-24 16:13:08 -05006664
6665 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006666 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006667 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006668 /* it is possible that someone inserted the extent into the tree
6669 * while we had the lock dropped. It is also possible that
6670 * an overlapping map exists in the tree
6671 */
Chris Masona52d9a82007-08-27 16:49:44 -04006672 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006673 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006674
6675 ret = 0;
6676
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006677 existing = search_extent_mapping(em_tree, start, len);
6678 /*
6679 * existing will always be non-NULL, since there must be
6680 * extent causing the -EEXIST.
6681 */
6682 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006683 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006684 /*
6685 * The existing extent map is the one nearest to
6686 * the [start, start + len) range which overlaps
6687 */
6688 err = merge_extent_mapping(em_tree, existing,
6689 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006690 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006691 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006692 free_extent_map(em);
6693 em = NULL;
6694 }
6695 } else {
6696 free_extent_map(em);
6697 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006698 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006699 }
Chris Masona52d9a82007-08-27 16:49:44 -04006700 }
Chris Mason890871b2009-09-02 16:24:52 -04006701 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006702out:
liubo1abe9b82011-03-24 11:18:59 +00006703
Steven Rostedt4cd8587c2013-11-14 22:57:29 -05006704 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006705
Chris Masonf4219502008-07-22 11:18:09 -04006706 if (path)
6707 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006708 if (trans) {
6709 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006710 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006711 err = ret;
6712 }
Chris Masona52d9a82007-08-27 16:49:44 -04006713 if (err) {
6714 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006715 return ERR_PTR(err);
6716 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006717 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006718 return em;
6719}
6720
Chris Masonec29ed52011-02-23 16:23:20 -05006721struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6722 size_t pg_offset, u64 start, u64 len,
6723 int create)
6724{
6725 struct extent_map *em;
6726 struct extent_map *hole_em = NULL;
6727 u64 range_start = start;
6728 u64 end;
6729 u64 found;
6730 u64 found_end;
6731 int err = 0;
6732
6733 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6734 if (IS_ERR(em))
6735 return em;
6736 if (em) {
6737 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006738 * if our em maps to
6739 * - a hole or
6740 * - a pre-alloc extent,
6741 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006742 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006743 if (em->block_start != EXTENT_MAP_HOLE &&
6744 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006745 return em;
6746 else
6747 hole_em = em;
6748 }
6749
6750 /* check to see if we've wrapped (len == -1 or similar) */
6751 end = start + len;
6752 if (end < start)
6753 end = (u64)-1;
6754 else
6755 end -= 1;
6756
6757 em = NULL;
6758
6759 /* ok, we didn't find anything, lets look for delalloc */
6760 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6761 end, len, EXTENT_DELALLOC, 1);
6762 found_end = range_start + found;
6763 if (found_end < range_start)
6764 found_end = (u64)-1;
6765
6766 /*
6767 * we didn't find anything useful, return
6768 * the original results from get_extent()
6769 */
6770 if (range_start > end || found_end <= start) {
6771 em = hole_em;
6772 hole_em = NULL;
6773 goto out;
6774 }
6775
6776 /* adjust the range_start to make sure it doesn't
6777 * go backwards from the start they passed in
6778 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306779 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006780 found = found_end - range_start;
6781
6782 if (found > 0) {
6783 u64 hole_start = start;
6784 u64 hole_len = len;
6785
David Sterba172ddd62011-04-21 00:48:27 +02006786 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006787 if (!em) {
6788 err = -ENOMEM;
6789 goto out;
6790 }
6791 /*
6792 * when btrfs_get_extent can't find anything it
6793 * returns one huge hole
6794 *
6795 * make sure what it found really fits our range, and
6796 * adjust to make sure it is based on the start from
6797 * the caller
6798 */
6799 if (hole_em) {
6800 u64 calc_end = extent_map_end(hole_em);
6801
6802 if (calc_end <= start || (hole_em->start > end)) {
6803 free_extent_map(hole_em);
6804 hole_em = NULL;
6805 } else {
6806 hole_start = max(hole_em->start, start);
6807 hole_len = calc_end - hole_start;
6808 }
6809 }
6810 em->bdev = NULL;
6811 if (hole_em && range_start > hole_start) {
6812 /* our hole starts before our delalloc, so we
6813 * have to return just the parts of the hole
6814 * that go until the delalloc starts
6815 */
6816 em->len = min(hole_len,
6817 range_start - hole_start);
6818 em->start = hole_start;
6819 em->orig_start = hole_start;
6820 /*
6821 * don't adjust block start at all,
6822 * it is fixed at EXTENT_MAP_HOLE
6823 */
6824 em->block_start = hole_em->block_start;
6825 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006826 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6827 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006828 } else {
6829 em->start = range_start;
6830 em->len = found;
6831 em->orig_start = range_start;
6832 em->block_start = EXTENT_MAP_DELALLOC;
6833 em->block_len = found;
6834 }
6835 } else if (hole_em) {
6836 return hole_em;
6837 }
6838out:
6839
6840 free_extent_map(hole_em);
6841 if (err) {
6842 free_extent_map(em);
6843 return ERR_PTR(err);
6844 }
6845 return em;
6846}
6847
Josef Bacik4b46fce2010-05-23 11:00:55 -04006848static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6849 u64 start, u64 len)
6850{
6851 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006852 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006853 struct btrfs_key ins;
6854 u64 alloc_hint;
6855 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006856
Josef Bacik4b46fce2010-05-23 11:00:55 -04006857 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006858 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08006859 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006860 if (ret)
6861 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006862
Josef Bacik70c8a912012-10-11 16:54:30 -04006863 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006864 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006865 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08006866 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006867 return em;
6868 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006869
6870 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6871 ins.offset, ins.offset, 0);
6872 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08006873 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006874 free_extent_map(em);
6875 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006876 }
Josef Bacik00361582013-08-14 14:02:47 -04006877
Josef Bacik4b46fce2010-05-23 11:00:55 -04006878 return em;
6879}
6880
Chris Mason46bfbb52010-05-26 11:04:10 -04006881/*
6882 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6883 * block must be cow'd
6884 */
Josef Bacik00361582013-08-14 14:02:47 -04006885noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006886 u64 *orig_start, u64 *orig_block_len,
6887 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006888{
Josef Bacik00361582013-08-14 14:02:47 -04006889 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006890 struct btrfs_path *path;
6891 int ret;
6892 struct extent_buffer *leaf;
6893 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006894 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006895 struct btrfs_file_extent_item *fi;
6896 struct btrfs_key key;
6897 u64 disk_bytenr;
6898 u64 backref_offset;
6899 u64 extent_end;
6900 u64 num_bytes;
6901 int slot;
6902 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006903 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006904
Chris Mason46bfbb52010-05-26 11:04:10 -04006905 path = btrfs_alloc_path();
6906 if (!path)
6907 return -ENOMEM;
6908
Josef Bacik00361582013-08-14 14:02:47 -04006909 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006910 offset, 0);
6911 if (ret < 0)
6912 goto out;
6913
6914 slot = path->slots[0];
6915 if (ret == 1) {
6916 if (slot == 0) {
6917 /* can't find the item, must cow */
6918 ret = 0;
6919 goto out;
6920 }
6921 slot--;
6922 }
6923 ret = 0;
6924 leaf = path->nodes[0];
6925 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006926 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006927 key.type != BTRFS_EXTENT_DATA_KEY) {
6928 /* not our file or wrong item type, must cow */
6929 goto out;
6930 }
6931
6932 if (key.offset > offset) {
6933 /* Wrong offset, must cow */
6934 goto out;
6935 }
6936
6937 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6938 found_type = btrfs_file_extent_type(leaf, fi);
6939 if (found_type != BTRFS_FILE_EXTENT_REG &&
6940 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6941 /* not a regular extent, must cow */
6942 goto out;
6943 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006944
6945 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6946 goto out;
6947
Miao Xiee77751a2013-12-27 21:11:50 +08006948 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6949 if (extent_end <= offset)
6950 goto out;
6951
Chris Mason46bfbb52010-05-26 11:04:10 -04006952 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006953 if (disk_bytenr == 0)
6954 goto out;
6955
6956 if (btrfs_file_extent_compression(leaf, fi) ||
6957 btrfs_file_extent_encryption(leaf, fi) ||
6958 btrfs_file_extent_other_encoding(leaf, fi))
6959 goto out;
6960
Chris Mason46bfbb52010-05-26 11:04:10 -04006961 backref_offset = btrfs_file_extent_offset(leaf, fi);
6962
Josef Bacik7ee9e442013-06-21 16:37:03 -04006963 if (orig_start) {
6964 *orig_start = key.offset - backref_offset;
6965 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6966 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6967 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006968
Chris Mason46bfbb52010-05-26 11:04:10 -04006969 if (btrfs_extent_readonly(root, disk_bytenr))
6970 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006971
6972 num_bytes = min(offset + *len, extent_end) - offset;
6973 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6974 u64 range_end;
6975
6976 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6977 ret = test_range_bit(io_tree, offset, range_end,
6978 EXTENT_DELALLOC, 0, NULL);
6979 if (ret) {
6980 ret = -EAGAIN;
6981 goto out;
6982 }
6983 }
6984
Josef Bacik1bda19e2013-10-18 12:10:36 -04006985 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006986
6987 /*
6988 * look for other files referencing this extent, if we
6989 * find any we must cow
6990 */
Josef Bacik00361582013-08-14 14:02:47 -04006991 trans = btrfs_join_transaction(root);
6992 if (IS_ERR(trans)) {
6993 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006994 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006995 }
6996
6997 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6998 key.offset - backref_offset, disk_bytenr);
6999 btrfs_end_transaction(trans, root);
7000 if (ret) {
7001 ret = 0;
7002 goto out;
7003 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007004
7005 /*
7006 * adjust disk_bytenr and num_bytes to cover just the bytes
7007 * in this extent we are about to write. If there
7008 * are any csums in that range we have to cow in order
7009 * to keep the csums correct
7010 */
7011 disk_bytenr += backref_offset;
7012 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007013 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7014 goto out;
7015 /*
7016 * all of the above have passed, it is safe to overwrite this extent
7017 * without cow
7018 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007019 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007020 ret = 1;
7021out:
7022 btrfs_free_path(path);
7023 return ret;
7024}
7025
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007026bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7027{
7028 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7029 int found = false;
7030 void **pagep = NULL;
7031 struct page *page = NULL;
7032 int start_idx;
7033 int end_idx;
7034
7035 start_idx = start >> PAGE_CACHE_SHIFT;
7036
7037 /*
7038 * end is the last byte in the last page. end == start is legal
7039 */
7040 end_idx = end >> PAGE_CACHE_SHIFT;
7041
7042 rcu_read_lock();
7043
7044 /* Most of the code in this while loop is lifted from
7045 * find_get_page. It's been modified to begin searching from a
7046 * page and return just the first page found in that range. If the
7047 * found idx is less than or equal to the end idx then we know that
7048 * a page exists. If no pages are found or if those pages are
7049 * outside of the range then we're fine (yay!) */
7050 while (page == NULL &&
7051 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7052 page = radix_tree_deref_slot(pagep);
7053 if (unlikely(!page))
7054 break;
7055
7056 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007057 if (radix_tree_deref_retry(page)) {
7058 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007059 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007060 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007061 /*
7062 * Otherwise, shmem/tmpfs must be storing a swap entry
7063 * here as an exceptional entry: so return it without
7064 * attempting to raise page count.
7065 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007066 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007067 break; /* TODO: Is this relevant for this use case? */
7068 }
7069
Filipe Manana91405152014-06-05 13:22:24 +01007070 if (!page_cache_get_speculative(page)) {
7071 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007072 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007073 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007074
7075 /*
7076 * Has the page moved?
7077 * This is part of the lockless pagecache protocol. See
7078 * include/linux/pagemap.h for details.
7079 */
7080 if (unlikely(page != *pagep)) {
7081 page_cache_release(page);
7082 page = NULL;
7083 }
7084 }
7085
7086 if (page) {
7087 if (page->index <= end_idx)
7088 found = true;
7089 page_cache_release(page);
7090 }
7091
7092 rcu_read_unlock();
7093 return found;
7094}
7095
Josef Bacikeb838e72012-07-31 16:28:48 -04007096static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7097 struct extent_state **cached_state, int writing)
7098{
7099 struct btrfs_ordered_extent *ordered;
7100 int ret = 0;
7101
7102 while (1) {
7103 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7104 0, cached_state);
7105 /*
7106 * We're concerned with the entire range that we're going to be
7107 * doing DIO to, so we need to make sure theres no ordered
7108 * extents in this range.
7109 */
7110 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7111 lockend - lockstart + 1);
7112
7113 /*
7114 * We need to make sure there are no buffered pages in this
7115 * range either, we could have raced between the invalidate in
7116 * generic_file_direct_write and locking the extent. The
7117 * invalidate needs to happen so that reads after a write do not
7118 * get stale data.
7119 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007120 if (!ordered &&
7121 (!writing ||
7122 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007123 break;
7124
7125 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7126 cached_state, GFP_NOFS);
7127
7128 if (ordered) {
7129 btrfs_start_ordered_extent(inode, ordered, 1);
7130 btrfs_put_ordered_extent(ordered);
7131 } else {
7132 /* Screw you mmap */
Filipe Manana728404d2014-10-10 09:43:11 +01007133 ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
Filipe Manana075bdbd2014-10-09 21:18:55 +01007134 if (ret)
7135 break;
7136 ret = filemap_fdatawait_range(inode->i_mapping,
7137 lockstart,
7138 lockend);
Josef Bacikeb838e72012-07-31 16:28:48 -04007139 if (ret)
7140 break;
7141
7142 /*
7143 * If we found a page that couldn't be invalidated just
7144 * fall back to buffered.
7145 */
7146 ret = invalidate_inode_pages2_range(inode->i_mapping,
7147 lockstart >> PAGE_CACHE_SHIFT,
7148 lockend >> PAGE_CACHE_SHIFT);
7149 if (ret)
7150 break;
7151 }
7152
7153 cond_resched();
7154 }
7155
7156 return ret;
7157}
7158
Josef Bacik69ffb542012-09-11 15:40:07 -04007159static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7160 u64 len, u64 orig_start,
7161 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007162 u64 orig_block_len, u64 ram_bytes,
7163 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007164{
7165 struct extent_map_tree *em_tree;
7166 struct extent_map *em;
7167 struct btrfs_root *root = BTRFS_I(inode)->root;
7168 int ret;
7169
7170 em_tree = &BTRFS_I(inode)->extent_tree;
7171 em = alloc_extent_map();
7172 if (!em)
7173 return ERR_PTR(-ENOMEM);
7174
7175 em->start = start;
7176 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007177 em->mod_start = start;
7178 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007179 em->len = len;
7180 em->block_len = block_len;
7181 em->block_start = block_start;
7182 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007183 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007184 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007185 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007186 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7187 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007188 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007189
7190 do {
7191 btrfs_drop_extent_cache(inode, em->start,
7192 em->start + em->len - 1, 0);
7193 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007194 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007195 write_unlock(&em_tree->lock);
7196 } while (ret == -EEXIST);
7197
7198 if (ret) {
7199 free_extent_map(em);
7200 return ERR_PTR(ret);
7201 }
7202
7203 return em;
7204}
7205
7206
Josef Bacik4b46fce2010-05-23 11:00:55 -04007207static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7208 struct buffer_head *bh_result, int create)
7209{
7210 struct extent_map *em;
7211 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007212 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007213 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007214 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007215 u64 len = bh_result->b_size;
Josef Bacik3e05bde2015-02-11 15:08:57 -05007216 u64 orig_len = len;
Josef Bacikeb838e72012-07-31 16:28:48 -04007217 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007218 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007219
Miao Xie172a5042013-02-21 02:48:22 -07007220 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007221 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007222 else
Josef Bacikc3298612012-08-03 16:49:19 -04007223 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007224
Josef Bacikc3298612012-08-03 16:49:19 -04007225 lockstart = start;
7226 lockend = start + len - 1;
7227
Josef Bacikeb838e72012-07-31 16:28:48 -04007228 /*
7229 * If this errors out it's because we couldn't invalidate pagecache for
7230 * this range and we need to fallback to buffered.
7231 */
7232 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7233 return -ENOTBLK;
7234
Josef Bacik4b46fce2010-05-23 11:00:55 -04007235 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007236 if (IS_ERR(em)) {
7237 ret = PTR_ERR(em);
7238 goto unlock_err;
7239 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007240
7241 /*
7242 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7243 * io. INLINE is special, and we could probably kludge it in here, but
7244 * it's still buffered so for safety lets just fall back to the generic
7245 * buffered path.
7246 *
7247 * For COMPRESSED we _have_ to read the entire extent in so we can
7248 * decompress it, so there will be buffering required no matter what we
7249 * do, so go ahead and fallback to buffered.
7250 *
7251 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7252 * to buffered IO. Don't blame me, this is the price we pay for using
7253 * the generic code.
7254 */
7255 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7256 em->block_start == EXTENT_MAP_INLINE) {
7257 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007258 ret = -ENOTBLK;
7259 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007260 }
7261
7262 /* Just a good old fashioned hole, return */
7263 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7264 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7265 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007266 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007267 }
7268
7269 /*
7270 * We don't allocate a new extent in the following cases
7271 *
7272 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7273 * existing extent.
7274 * 2) The extent is marked as PREALLOC. We're good to go here and can
7275 * just use the extent.
7276 *
7277 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007278 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007279 len = min(len, em->len - (start - em->start));
7280 lockstart = start + len;
7281 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007282 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007283
7284 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7285 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7286 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007287 int type;
7288 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04007289 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007290
7291 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7292 type = BTRFS_ORDERED_PREALLOC;
7293 else
7294 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007295 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007296 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007297
Josef Bacik00361582013-08-14 14:02:47 -04007298 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007299 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007300 if (type == BTRFS_ORDERED_PREALLOC) {
7301 free_extent_map(em);
7302 em = create_pinned_em(inode, start, len,
7303 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007304 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007305 orig_block_len,
7306 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007307 if (IS_ERR(em)) {
7308 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007309 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007310 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007311 }
7312
Chris Mason46bfbb52010-05-26 11:04:10 -04007313 ret = btrfs_add_ordered_extent_dio(inode, start,
7314 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007315 if (ret) {
7316 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007317 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007318 }
7319 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007320 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007321 }
Josef Bacik00361582013-08-14 14:02:47 -04007322
Chris Mason46bfbb52010-05-26 11:04:10 -04007323 /*
7324 * this will cow the extent, reset the len in case we changed
7325 * it above
7326 */
7327 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007328 free_extent_map(em);
7329 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007330 if (IS_ERR(em)) {
7331 ret = PTR_ERR(em);
7332 goto unlock_err;
7333 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007334 len = min(len, em->len - (start - em->start));
7335unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007336 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7337 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007338 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007339 bh_result->b_bdev = em->bdev;
7340 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007341 if (create) {
7342 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7343 set_buffer_new(bh_result);
7344
7345 /*
7346 * Need to update the i_size under the extent lock so buffered
7347 * readers will get the updated i_size when we unlock.
7348 */
7349 if (start + len > i_size_read(inode))
7350 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007351
Josef Bacik3e05bde2015-02-11 15:08:57 -05007352 if (len < orig_len) {
7353 spin_lock(&BTRFS_I(inode)->lock);
7354 BTRFS_I(inode)->outstanding_extents++;
7355 spin_unlock(&BTRFS_I(inode)->lock);
7356 }
Josef Bacik32667892015-02-11 15:08:58 -05007357 btrfs_free_reserved_data_space(inode, len);
Josef Bacikc3473e82012-06-19 10:59:00 -04007358 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007359
Josef Bacikeb838e72012-07-31 16:28:48 -04007360 /*
7361 * In the case of write we need to clear and unlock the entire range,
7362 * in the case of read we need to unlock only the end area that we
7363 * aren't using if there is any left over space.
7364 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007365 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007366 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7367 lockend, unlock_bits, 1, 0,
7368 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007369 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007370 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007371 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007372
Josef Bacik4b46fce2010-05-23 11:00:55 -04007373 free_extent_map(em);
7374
7375 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007376
7377unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007378 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7379 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7380 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007381}
7382
Miao Xie8b110e32014-09-12 18:44:03 +08007383static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7384 int rw, int mirror_num)
7385{
7386 struct btrfs_root *root = BTRFS_I(inode)->root;
7387 int ret;
7388
7389 BUG_ON(rw & REQ_WRITE);
7390
7391 bio_get(bio);
7392
7393 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7394 BTRFS_WQ_ENDIO_DIO_REPAIR);
7395 if (ret)
7396 goto err;
7397
7398 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7399err:
7400 bio_put(bio);
7401 return ret;
7402}
7403
7404static int btrfs_check_dio_repairable(struct inode *inode,
7405 struct bio *failed_bio,
7406 struct io_failure_record *failrec,
7407 int failed_mirror)
7408{
7409 int num_copies;
7410
7411 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7412 failrec->logical, failrec->len);
7413 if (num_copies == 1) {
7414 /*
7415 * we only have a single copy of the data, so don't bother with
7416 * all the retry and error correction code that follows. no
7417 * matter what the error is, it is very likely to persist.
7418 */
7419 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7420 num_copies, failrec->this_mirror, failed_mirror);
7421 return 0;
7422 }
7423
7424 failrec->failed_mirror = failed_mirror;
7425 failrec->this_mirror++;
7426 if (failrec->this_mirror == failed_mirror)
7427 failrec->this_mirror++;
7428
7429 if (failrec->this_mirror > num_copies) {
7430 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7431 num_copies, failrec->this_mirror, failed_mirror);
7432 return 0;
7433 }
7434
7435 return 1;
7436}
7437
7438static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7439 struct page *page, u64 start, u64 end,
7440 int failed_mirror, bio_end_io_t *repair_endio,
7441 void *repair_arg)
7442{
7443 struct io_failure_record *failrec;
7444 struct bio *bio;
7445 int isector;
7446 int read_mode;
7447 int ret;
7448
7449 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7450
7451 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7452 if (ret)
7453 return ret;
7454
7455 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7456 failed_mirror);
7457 if (!ret) {
7458 free_io_failure(inode, failrec);
7459 return -EIO;
7460 }
7461
7462 if (failed_bio->bi_vcnt > 1)
7463 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7464 else
7465 read_mode = READ_SYNC;
7466
7467 isector = start - btrfs_io_bio(failed_bio)->logical;
7468 isector >>= inode->i_sb->s_blocksize_bits;
7469 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7470 0, isector, repair_endio, repair_arg);
7471 if (!bio) {
7472 free_io_failure(inode, failrec);
7473 return -EIO;
7474 }
7475
7476 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7477 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7478 read_mode, failrec->this_mirror, failrec->in_validation);
7479
7480 ret = submit_dio_repair_bio(inode, bio, read_mode,
7481 failrec->this_mirror);
7482 if (ret) {
7483 free_io_failure(inode, failrec);
7484 bio_put(bio);
7485 }
7486
7487 return ret;
7488}
7489
7490struct btrfs_retry_complete {
7491 struct completion done;
7492 struct inode *inode;
7493 u64 start;
7494 int uptodate;
7495};
7496
7497static void btrfs_retry_endio_nocsum(struct bio *bio, int err)
7498{
7499 struct btrfs_retry_complete *done = bio->bi_private;
7500 struct bio_vec *bvec;
7501 int i;
7502
7503 if (err)
7504 goto end;
7505
7506 done->uptodate = 1;
7507 bio_for_each_segment_all(bvec, bio, i)
7508 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7509end:
7510 complete(&done->done);
7511 bio_put(bio);
7512}
7513
7514static int __btrfs_correct_data_nocsum(struct inode *inode,
7515 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007516{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007517 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007518 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007519 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007520 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007521 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007522
Miao Xiec1dc0892014-09-12 18:43:56 +08007523 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007524 done.inode = inode;
7525
7526 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7527try_again:
7528 done.uptodate = 0;
7529 done.start = start;
7530 init_completion(&done.done);
7531
7532 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7533 start + bvec->bv_len - 1,
7534 io_bio->mirror_num,
7535 btrfs_retry_endio_nocsum, &done);
7536 if (ret)
7537 return ret;
7538
7539 wait_for_completion(&done.done);
7540
7541 if (!done.uptodate) {
7542 /* We might have another mirror, so try again */
7543 goto try_again;
7544 }
7545
7546 start += bvec->bv_len;
7547 }
7548
7549 return 0;
7550}
7551
7552static void btrfs_retry_endio(struct bio *bio, int err)
7553{
7554 struct btrfs_retry_complete *done = bio->bi_private;
7555 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7556 struct bio_vec *bvec;
7557 int uptodate;
7558 int ret;
7559 int i;
7560
7561 if (err)
7562 goto end;
7563
7564 uptodate = 1;
7565 bio_for_each_segment_all(bvec, bio, i) {
7566 ret = __readpage_endio_check(done->inode, io_bio, i,
7567 bvec->bv_page, 0,
7568 done->start, bvec->bv_len);
7569 if (!ret)
7570 clean_io_failure(done->inode, done->start,
7571 bvec->bv_page, 0);
7572 else
7573 uptodate = 0;
7574 }
7575
7576 done->uptodate = uptodate;
7577end:
7578 complete(&done->done);
7579 bio_put(bio);
7580}
7581
7582static int __btrfs_subio_endio_read(struct inode *inode,
7583 struct btrfs_io_bio *io_bio, int err)
7584{
7585 struct bio_vec *bvec;
7586 struct btrfs_retry_complete done;
7587 u64 start;
7588 u64 offset = 0;
7589 int i;
7590 int ret;
7591
7592 err = 0;
7593 start = io_bio->logical;
7594 done.inode = inode;
7595
Miao Xiec1dc0892014-09-12 18:43:56 +08007596 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007597 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7598 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007599 if (likely(!ret))
7600 goto next;
7601try_again:
7602 done.uptodate = 0;
7603 done.start = start;
7604 init_completion(&done.done);
7605
7606 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7607 start + bvec->bv_len - 1,
7608 io_bio->mirror_num,
7609 btrfs_retry_endio, &done);
7610 if (ret) {
7611 err = ret;
7612 goto next;
7613 }
7614
7615 wait_for_completion(&done.done);
7616
7617 if (!done.uptodate) {
7618 /* We might have another mirror, so try again */
7619 goto try_again;
7620 }
7621next:
7622 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007623 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007624 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007625
7626 return err;
7627}
7628
Miao Xie8b110e32014-09-12 18:44:03 +08007629static int btrfs_subio_endio_read(struct inode *inode,
7630 struct btrfs_io_bio *io_bio, int err)
7631{
7632 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7633
7634 if (skip_csum) {
7635 if (unlikely(err))
7636 return __btrfs_correct_data_nocsum(inode, io_bio);
7637 else
7638 return 0;
7639 } else {
7640 return __btrfs_subio_endio_read(inode, io_bio, err);
7641 }
7642}
7643
Miao Xiec1dc0892014-09-12 18:43:56 +08007644static void btrfs_endio_direct_read(struct bio *bio, int err)
7645{
7646 struct btrfs_dio_private *dip = bio->bi_private;
7647 struct inode *inode = dip->inode;
7648 struct bio *dio_bio;
7649 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7650
Miao Xie8b110e32014-09-12 18:44:03 +08007651 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7652 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007653
Josef Bacik4b46fce2010-05-23 11:00:55 -04007654 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007655 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007656 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007657
Josef Bacik4b46fce2010-05-23 11:00:55 -04007658 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007659
7660 /* If we had a csum failure make sure to clear the uptodate flag */
7661 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007662 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7663 dio_end_io(dio_bio, err);
Miao Xie23ea8e52014-09-12 18:43:54 +08007664
7665 if (io_bio->end_io)
7666 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007667 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007668}
7669
7670static void btrfs_endio_direct_write(struct bio *bio, int err)
7671{
7672 struct btrfs_dio_private *dip = bio->bi_private;
7673 struct inode *inode = dip->inode;
7674 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007675 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007676 u64 ordered_offset = dip->logical_offset;
7677 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007678 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007679 int ret;
7680
7681 if (err)
7682 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007683again:
7684 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7685 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007686 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007687 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007688 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007689
Liu Bo9e0af232014-08-15 23:36:53 +08007690 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7691 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007692 btrfs_queue_work(root->fs_info->endio_write_workers,
7693 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007694out_test:
7695 /*
7696 * our bio might span multiple ordered extents. If we haven't
7697 * completed the accounting for the whole dio, go back and try again
7698 */
7699 if (ordered_offset < dip->logical_offset + dip->bytes) {
7700 ordered_bytes = dip->logical_offset + dip->bytes -
7701 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007702 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007703 goto again;
7704 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007705out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007706 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007707
Josef Bacik4b46fce2010-05-23 11:00:55 -04007708 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007709
7710 /* If we had an error make sure to clear the uptodate flag */
7711 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007712 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7713 dio_end_io(dio_bio, err);
7714 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007715}
7716
Chris Masoneaf25d92010-05-25 09:48:28 -04007717static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7718 struct bio *bio, int mirror_num,
7719 unsigned long bio_flags, u64 offset)
7720{
7721 int ret;
7722 struct btrfs_root *root = BTRFS_I(inode)->root;
7723 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007724 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007725 return 0;
7726}
7727
Miao Xiee65e1532010-11-22 03:04:43 +00007728static void btrfs_end_dio_bio(struct bio *bio, int err)
7729{
7730 struct btrfs_dio_private *dip = bio->bi_private;
7731
Miao Xie8b110e32014-09-12 18:44:03 +08007732 if (err)
7733 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
7734 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7735 btrfs_ino(dip->inode), bio->bi_rw,
7736 (unsigned long long)bio->bi_iter.bi_sector,
7737 bio->bi_iter.bi_size, err);
7738
7739 if (dip->subio_endio)
7740 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007741
7742 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007743 dip->errors = 1;
7744
7745 /*
7746 * before atomic variable goto zero, we must make sure
7747 * dip->errors is perceived to be set.
7748 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007749 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007750 }
7751
7752 /* if there are more bios still pending for this dio, just exit */
7753 if (!atomic_dec_and_test(&dip->pending_bios))
7754 goto out;
7755
Chris Mason9be33952013-05-17 18:30:14 -04007756 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007757 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007758 } else {
7759 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007760 bio_endio(dip->orig_bio, 0);
7761 }
7762out:
7763 bio_put(bio);
7764}
7765
7766static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7767 u64 first_sector, gfp_t gfp_flags)
7768{
7769 int nr_vecs = bio_get_nr_vecs(bdev);
7770 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7771}
7772
Miao Xiec1dc0892014-09-12 18:43:56 +08007773static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
7774 struct inode *inode,
7775 struct btrfs_dio_private *dip,
7776 struct bio *bio,
7777 u64 file_offset)
7778{
7779 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7780 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
7781 int ret;
7782
7783 /*
7784 * We load all the csum data we need when we submit
7785 * the first bio to reduce the csum tree search and
7786 * contention.
7787 */
7788 if (dip->logical_offset == file_offset) {
7789 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
7790 file_offset);
7791 if (ret)
7792 return ret;
7793 }
7794
7795 if (bio == dip->orig_bio)
7796 return 0;
7797
7798 file_offset -= dip->logical_offset;
7799 file_offset >>= inode->i_sb->s_blocksize_bits;
7800 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
7801
7802 return 0;
7803}
7804
Miao Xiee65e1532010-11-22 03:04:43 +00007805static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7806 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007807 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007808{
Miao Xiefacc8a222013-07-25 19:22:34 +08007809 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007810 int write = rw & REQ_WRITE;
7811 struct btrfs_root *root = BTRFS_I(inode)->root;
7812 int ret;
7813
Josef Bacikb812ce22012-11-16 13:56:32 -05007814 if (async_submit)
7815 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7816
Miao Xiee65e1532010-11-22 03:04:43 +00007817 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007818
7819 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02007820 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7821 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007822 if (ret)
7823 goto err;
7824 }
Miao Xiee65e1532010-11-22 03:04:43 +00007825
Josef Bacik1ae39932011-04-06 14:41:34 -04007826 if (skip_sum)
7827 goto map;
7828
7829 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007830 ret = btrfs_wq_submit_bio(root->fs_info,
7831 inode, rw, bio, 0, 0,
7832 file_offset,
7833 __btrfs_submit_bio_start_direct_io,
7834 __btrfs_submit_bio_done);
7835 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007836 } else if (write) {
7837 /*
7838 * If we aren't doing async submit, calculate the csum of the
7839 * bio now.
7840 */
7841 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7842 if (ret)
7843 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007844 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08007845 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
7846 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007847 if (ret)
7848 goto err;
7849 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007850map:
7851 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007852err:
7853 bio_put(bio);
7854 return ret;
7855}
7856
7857static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7858 int skip_sum)
7859{
7860 struct inode *inode = dip->inode;
7861 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007862 struct bio *bio;
7863 struct bio *orig_bio = dip->orig_bio;
7864 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007865 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007866 u64 file_offset = dip->logical_offset;
7867 u64 submit_len = 0;
7868 u64 map_length;
7869 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08007870 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04007871 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007872
Kent Overstreet4f024f32013-10-11 15:44:27 -07007873 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007874 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007875 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007876 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007877 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08007878
Kent Overstreet4f024f32013-10-11 15:44:27 -07007879 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007880 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007881 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04007882 goto submit;
7883 }
7884
David Woodhouse53b381b2013-01-29 18:40:14 -05007885 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08007886 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05007887 async_submit = 0;
7888 else
7889 async_submit = 1;
7890
Josef Bacik02f57c72011-04-06 14:25:44 -04007891 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7892 if (!bio)
7893 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08007894
Josef Bacik02f57c72011-04-06 14:25:44 -04007895 bio->bi_private = dip;
7896 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007897 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04007898 atomic_inc(&dip->pending_bios);
7899
Miao Xiee65e1532010-11-22 03:04:43 +00007900 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02007901 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00007902 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02007903 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00007904 /*
7905 * inc the count before we submit the bio so
7906 * we know the end IO handler won't happen before
7907 * we inc the count. Otherwise, the dip might get freed
7908 * before we're done setting it up
7909 */
7910 atomic_inc(&dip->pending_bios);
7911 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7912 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007913 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007914 if (ret) {
7915 bio_put(bio);
7916 atomic_dec(&dip->pending_bios);
7917 goto out_err;
7918 }
7919
Miao Xiee65e1532010-11-22 03:04:43 +00007920 start_sector += submit_len >> 9;
7921 file_offset += submit_len;
7922
7923 submit_len = 0;
7924 nr_pages = 0;
7925
7926 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7927 start_sector, GFP_NOFS);
7928 if (!bio)
7929 goto out_err;
7930 bio->bi_private = dip;
7931 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007932 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007933
Kent Overstreet4f024f32013-10-11 15:44:27 -07007934 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007935 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007936 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007937 &map_length, NULL, 0);
7938 if (ret) {
7939 bio_put(bio);
7940 goto out_err;
7941 }
7942 } else {
7943 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307944 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007945 bvec++;
7946 }
7947 }
7948
Josef Bacik02f57c72011-04-06 14:25:44 -04007949submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007950 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007951 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007952 if (!ret)
7953 return 0;
7954
7955 bio_put(bio);
7956out_err:
7957 dip->errors = 1;
7958 /*
7959 * before atomic variable goto zero, we must
7960 * make sure dip->errors is perceived to be set.
7961 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007962 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007963 if (atomic_dec_and_test(&dip->pending_bios))
7964 bio_io_error(dip->orig_bio);
7965
7966 /* bio_end_io() will handle error, so we needn't return it */
7967 return 0;
7968}
7969
Chris Mason9be33952013-05-17 18:30:14 -04007970static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7971 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007972{
7973 struct btrfs_root *root = BTRFS_I(inode)->root;
7974 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007975 struct bio *io_bio;
Miao Xie23ea8e52014-09-12 18:43:54 +08007976 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007977 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007978 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007979 int ret = 0;
7980
7981 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7982
Chris Mason9be33952013-05-17 18:30:14 -04007983 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007984 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007985 ret = -ENOMEM;
7986 goto free_ordered;
7987 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007988
Miao Xiec1dc0892014-09-12 18:43:56 +08007989 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007990 if (!dip) {
7991 ret = -ENOMEM;
7992 goto free_io_bio;
7993 }
7994
7995 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007996 dip->inode = inode;
7997 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007998 dip->bytes = dio_bio->bi_iter.bi_size;
7999 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008000 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008001 dip->orig_bio = io_bio;
8002 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008003 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008004 btrfs_bio = btrfs_io_bio(io_bio);
8005 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008006
Miao Xiec1dc0892014-09-12 18:43:56 +08008007 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008008 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008009 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008010 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008011 dip->subio_endio = btrfs_subio_endio_read;
8012 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008013
Miao Xiee65e1532010-11-22 03:04:43 +00008014 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8015 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008016 return;
Chris Mason9be33952013-05-17 18:30:14 -04008017
Miao Xie23ea8e52014-09-12 18:43:54 +08008018 if (btrfs_bio->end_io)
8019 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008020free_io_bio:
8021 bio_put(io_bio);
8022
Josef Bacik4b46fce2010-05-23 11:00:55 -04008023free_ordered:
8024 /*
8025 * If this is a write, we need to clean up the reserved space and kill
8026 * the ordered extent.
8027 */
8028 if (write) {
8029 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05008030 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008031 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
8032 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
8033 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08008034 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008035 btrfs_put_ordered_extent(ordered);
8036 btrfs_put_ordered_extent(ordered);
8037 }
Chris Mason9be33952013-05-17 18:30:14 -04008038 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008039}
8040
Chris Mason5a5f79b2010-05-26 21:33:37 -04008041static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008042 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008043{
8044 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008045 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008046 unsigned blocksize_mask = root->sectorsize - 1;
8047 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008048
8049 if (offset & blocksize_mask)
8050 goto out;
8051
Al Viro28060d52014-03-22 05:15:17 -04008052 if (iov_iter_alignment(iter) & blocksize_mask)
8053 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008054
Al Viro28060d52014-03-22 05:15:17 -04008055 /* If this is a write we don't need to check anymore */
8056 if (rw & WRITE)
8057 return 0;
8058 /*
8059 * Check to make sure we don't have duplicate iov_base's in this
8060 * iovec, if so return EINVAL, otherwise we'll get csum errors
8061 * when reading back.
8062 */
8063 for (seg = 0; seg < iter->nr_segs; seg++) {
8064 for (i = seg + 1; i < iter->nr_segs; i++) {
8065 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008066 goto out;
8067 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008068 }
8069 retval = 0;
8070out:
8071 return retval;
8072}
Josef Bacikeb838e72012-07-31 16:28:48 -04008073
Chris Mason16432982008-04-10 10:23:21 -04008074static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05008075 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04008076{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008077 struct file *file = iocb->ki_filp;
8078 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00008079 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008080 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008081 bool wakeup = true;
8082 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008083 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008084
Al Viro28060d52014-03-22 05:15:17 -04008085 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008086 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008087
Miao Xie38851cc2013-02-08 07:04:11 +00008088 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008089 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008090
Josef Bacik0e267c42013-07-02 10:38:02 -04008091 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008092 * The generic stuff only does filemap_write_and_wait_range, which
8093 * isn't enough if we've written compressed pages to this area, so
8094 * we need to flush the dirty pages again to make absolutely sure
8095 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008096 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008097 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008098 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8099 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008100 filemap_fdatawrite_range(inode->i_mapping, offset,
8101 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008102
Miao Xie09348562013-02-07 10:12:07 +00008103 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008104 /*
8105 * If the write DIO is beyond the EOF, we need update
8106 * the isize, but it is protected by i_mutex. So we can
8107 * not unlock the i_mutex at this case.
8108 */
8109 if (offset + count <= inode->i_size) {
8110 mutex_unlock(&inode->i_mutex);
8111 relock = true;
8112 }
Miao Xie09348562013-02-07 10:12:07 +00008113 ret = btrfs_delalloc_reserve_space(inode, count);
8114 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008115 goto out;
David Sterbaee39b432014-09-30 01:33:33 +02008116 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8117 &BTRFS_I(inode)->runtime_flags)) {
Miao Xie38851cc2013-02-08 07:04:11 +00008118 inode_dio_done(inode);
8119 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8120 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008121 }
8122
8123 ret = __blockdev_direct_IO(rw, iocb, inode,
8124 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05008125 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00008126 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00008127 if (rw & WRITE) {
8128 if (ret < 0 && ret != -EIOCBQUEUED)
8129 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07008130 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00008131 btrfs_delalloc_release_space(inode,
8132 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008133 }
Miao Xie38851cc2013-02-08 07:04:11 +00008134out:
Miao Xie2e60a512013-02-08 07:01:08 +00008135 if (wakeup)
8136 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008137 if (relock)
8138 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008139
8140 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008141}
8142
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008143#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8144
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008145static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8146 __u64 start, __u64 len)
8147{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008148 int ret;
8149
8150 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8151 if (ret)
8152 return ret;
8153
Chris Masonec29ed52011-02-23 16:23:20 -05008154 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008155}
8156
Chris Mason9ebefb182007-06-15 13:50:00 -04008157int btrfs_readpage(struct file *file, struct page *page)
8158{
Chris Masond1310b22008-01-24 16:13:08 -05008159 struct extent_io_tree *tree;
8160 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008161 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008162}
Chris Mason1832a6d2007-12-21 16:27:21 -05008163
Chris Mason39279cc2007-06-12 06:35:45 -04008164static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8165{
Chris Masond1310b22008-01-24 16:13:08 -05008166 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04008167
8168
8169 if (current->flags & PF_MEMALLOC) {
8170 redirty_page_for_writepage(wbc, page);
8171 unlock_page(page);
8172 return 0;
8173 }
Chris Masond1310b22008-01-24 16:13:08 -05008174 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008175 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8176}
Chris Mason39279cc2007-06-12 06:35:45 -04008177
Eric Sandeen48a3b632013-04-25 20:41:01 +00008178static int btrfs_writepages(struct address_space *mapping,
8179 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008180{
Chris Masond1310b22008-01-24 16:13:08 -05008181 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008182
Chris Masond1310b22008-01-24 16:13:08 -05008183 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008184 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8185}
8186
Chris Mason3ab2fb52007-11-08 10:59:22 -05008187static int
8188btrfs_readpages(struct file *file, struct address_space *mapping,
8189 struct list_head *pages, unsigned nr_pages)
8190{
Chris Masond1310b22008-01-24 16:13:08 -05008191 struct extent_io_tree *tree;
8192 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008193 return extent_readpages(tree, mapping, pages, nr_pages,
8194 btrfs_get_extent);
8195}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008196static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008197{
Chris Masond1310b22008-01-24 16:13:08 -05008198 struct extent_io_tree *tree;
8199 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008200 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008201
Chris Masond1310b22008-01-24 16:13:08 -05008202 tree = &BTRFS_I(page->mapping->host)->io_tree;
8203 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008204 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008205 if (ret == 1) {
8206 ClearPagePrivate(page);
8207 set_page_private(page, 0);
8208 page_cache_release(page);
8209 }
8210 return ret;
8211}
Chris Mason39279cc2007-06-12 06:35:45 -04008212
Chris Masone6dcd2d2008-07-17 12:53:50 -04008213static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8214{
Chris Mason98509cf2008-09-11 15:51:43 -04008215 if (PageWriteback(page) || PageDirty(page))
8216 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008217 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008218}
8219
Lukas Czernerd47992f2013-05-21 23:17:23 -04008220static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8221 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008222{
Josef Bacik5fd02042012-05-02 14:00:54 -04008223 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008224 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008225 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008226 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008227 u64 page_start = page_offset(page);
8228 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008229 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008230
Chris Mason8b62b722009-09-02 16:53:46 -04008231 /*
8232 * we have the page locked, so new writeback can't start,
8233 * and the dirty bit won't be cleared while we are here.
8234 *
8235 * Wait for IO on this page so that we can safely clear
8236 * the PagePrivate2 bit and do ordered accounting
8237 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008238 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008239
Josef Bacik5fd02042012-05-02 14:00:54 -04008240 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008241 if (offset) {
8242 btrfs_releasepage(page, GFP_NOFS);
8243 return;
8244 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008245
8246 if (!inode_evicting)
8247 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8248 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008249 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008250 /*
8251 * IO on this page will never be started, so we need
8252 * to account for any ordered extents now
8253 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008254 if (!inode_evicting)
8255 clear_extent_bit(tree, page_start, page_end,
8256 EXTENT_DIRTY | EXTENT_DELALLOC |
8257 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8258 EXTENT_DEFRAG, 1, 0, &cached_state,
8259 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008260 /*
8261 * whoever cleared the private bit is responsible
8262 * for the finish_ordered_io
8263 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008264 if (TestClearPagePrivate2(page)) {
8265 struct btrfs_ordered_inode_tree *tree;
8266 u64 new_len;
8267
8268 tree = &BTRFS_I(inode)->ordered_tree;
8269
8270 spin_lock_irq(&tree->lock);
8271 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8272 new_len = page_start - ordered->file_offset;
8273 if (new_len < ordered->truncated_len)
8274 ordered->truncated_len = new_len;
8275 spin_unlock_irq(&tree->lock);
8276
8277 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8278 page_start,
8279 PAGE_CACHE_SIZE, 1))
8280 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008281 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008282 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008283 if (!inode_evicting) {
8284 cached_state = NULL;
8285 lock_extent_bits(tree, page_start, page_end, 0,
8286 &cached_state);
8287 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008288 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008289
8290 if (!inode_evicting) {
8291 clear_extent_bit(tree, page_start, page_end,
8292 EXTENT_LOCKED | EXTENT_DIRTY |
8293 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8294 EXTENT_DEFRAG, 1, 1,
8295 &cached_state, GFP_NOFS);
8296
8297 __btrfs_releasepage(page, GFP_NOFS);
8298 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008299
Chris Mason4a096752008-07-21 10:29:44 -04008300 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008301 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008302 ClearPagePrivate(page);
8303 set_page_private(page, 0);
8304 page_cache_release(page);
8305 }
Chris Mason39279cc2007-06-12 06:35:45 -04008306}
8307
Chris Mason9ebefb182007-06-15 13:50:00 -04008308/*
8309 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8310 * called from a page fault handler when a page is first dirtied. Hence we must
8311 * be careful to check for EOF conditions here. We set the page up correctly
8312 * for a written page which means we get ENOSPC checking when writing into
8313 * holes and correct delalloc and unwritten extent mapping on filesystems that
8314 * support these features.
8315 *
8316 * We are not allowed to take the i_mutex here so we have to play games to
8317 * protect against truncate races as the page could now be beyond EOF. Because
8318 * vmtruncate() writes the inode size before removing pages, once we have the
8319 * page lock we can determine safely if the page is beyond EOF. If it is not
8320 * beyond EOF, then the page is guaranteed safe against truncation until we
8321 * unlock the page.
8322 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008323int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008324{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008325 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008326 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008327 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008328 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8329 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008330 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008331 char *kaddr;
8332 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008333 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008334 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008335 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008336 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008337 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008338
Jan Karab2b5ef52012-06-12 16:20:45 +02008339 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04008340 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008341 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008342 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008343 reserved = 1;
8344 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008345 if (ret) {
8346 if (ret == -ENOMEM)
8347 ret = VM_FAULT_OOM;
8348 else /* -ENOSPC, -EIO, etc */
8349 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008350 if (reserved)
8351 goto out;
8352 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008353 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008354
Nick Piggin56a76f82009-03-31 15:23:23 -07008355 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008356again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008357 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008358 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008359 page_start = page_offset(page);
8360 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04008361
Chris Mason9ebefb182007-06-15 13:50:00 -04008362 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008363 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008364 /* page got truncated out from underneath us */
8365 goto out_unlock;
8366 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008367 wait_on_page_writeback(page);
8368
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008369 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008370 set_page_extent_mapped(page);
8371
Chris Masoneb84ae02008-07-17 13:53:27 -04008372 /*
8373 * we can't set the delalloc bits if there are pending ordered
8374 * extents. Drop our locks and wait for them to finish
8375 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008376 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8377 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008378 unlock_extent_cached(io_tree, page_start, page_end,
8379 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008380 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008381 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008382 btrfs_put_ordered_extent(ordered);
8383 goto again;
8384 }
8385
Josef Bacikfbf19082009-10-01 17:10:23 -04008386 /*
8387 * XXX - page_mkwrite gets called every time the page is dirtied, even
8388 * if it was already dirty, so for space accounting reasons we need to
8389 * clear any delalloc bits for the range we are fixing to save. There
8390 * is probably a better way to do this, but for now keep consistent with
8391 * prepare_pages in the normal write path.
8392 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008393 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008394 EXTENT_DIRTY | EXTENT_DELALLOC |
8395 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008396 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008397
Josef Bacik2ac55d42010-02-03 19:33:23 +00008398 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8399 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008400 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008401 unlock_extent_cached(io_tree, page_start, page_end,
8402 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008403 ret = VM_FAULT_SIGBUS;
8404 goto out_unlock;
8405 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008406 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008407
8408 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008409 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008410 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008411 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008412 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008413
Chris Masone6dcd2d2008-07-17 12:53:50 -04008414 if (zero_start != PAGE_CACHE_SIZE) {
8415 kaddr = kmap(page);
8416 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8417 flush_dcache_page(page);
8418 kunmap(page);
8419 }
Chris Mason247e7432008-07-17 12:53:51 -04008420 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008421 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008422 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008423
Chris Mason257c62e2009-10-13 13:21:08 -04008424 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8425 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008426 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008427
Josef Bacik2ac55d42010-02-03 19:33:23 +00008428 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008429
8430out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008431 if (!ret) {
8432 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008433 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008434 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008435 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008436out:
Josef Bacikec39e182012-01-12 19:10:12 -05008437 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008438out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008439 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008440 return ret;
8441}
8442
Josef Bacika41ad392011-01-31 15:30:16 -05008443static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008444{
8445 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008446 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008447 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008448 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008449 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008450 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008451 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008452
Josef Bacik0ef8b722013-10-25 16:13:35 -04008453 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8454 (u64)-1);
8455 if (ret)
8456 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008457
Josef Bacikfcb80c22011-05-03 10:40:22 -04008458 /*
8459 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8460 * 3 things going on here
8461 *
8462 * 1) We need to reserve space for our orphan item and the space to
8463 * delete our orphan item. Lord knows we don't want to have a dangling
8464 * orphan item because we didn't reserve space to remove it.
8465 *
8466 * 2) We need to reserve space to update our inode.
8467 *
8468 * 3) We need to have something to cache all the space that is going to
8469 * be free'd up by the truncate operation, but also have some slack
8470 * space reserved in case it uses space during the truncate (thank you
8471 * very much snapshotting).
8472 *
8473 * And we need these to all be seperate. The fact is we can use alot of
8474 * space doing the truncate, and we have no earthly idea how much space
8475 * we will use, so we need the truncate reservation to be seperate so it
8476 * doesn't end up using space reserved for updating the inode or
8477 * removing the orphan item. We also need to be able to stop the
8478 * transaction and start a new one, which means we need to be able to
8479 * update the inode several times, and we have no idea of knowing how
8480 * many times that will be, so we can't just reserve 1 item for the
8481 * entirety of the opration, so that has to be done seperately as well.
8482 * Then there is the orphan item, which does indeed need to be held on
8483 * to for the whole operation, and we need nobody to touch this reserved
8484 * space except the orphan code.
8485 *
8486 * So that leaves us with
8487 *
8488 * 1) root->orphan_block_rsv - for the orphan deletion.
8489 * 2) rsv - for the truncate reservation, which we will steal from the
8490 * transaction reservation.
8491 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8492 * updating the inode.
8493 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008494 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008495 if (!rsv)
8496 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008497 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008498 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008499
Josef Bacik907cbce2011-08-08 13:46:15 -04008500 /*
Josef Bacik07127182011-08-19 10:29:59 -04008501 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008502 * 1 for updating the inode.
8503 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008504 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008505 if (IS_ERR(trans)) {
8506 err = PTR_ERR(trans);
8507 goto out;
8508 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008509
Josef Bacik907cbce2011-08-08 13:46:15 -04008510 /* Migrate the slack space for the truncate to our reserve */
8511 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8512 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008513 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008514
Chris Mason5a3f23d2009-03-31 13:27:11 -04008515 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008516 * So if we truncate and then write and fsync we normally would just
8517 * write the extents that changed, which is a problem if we need to
8518 * first truncate that entire inode. So set this flag so we write out
8519 * all of the extents in the inode to the sync log so we're completely
8520 * safe.
8521 */
8522 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008523 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008524
Yan, Zheng80825102009-11-12 09:35:36 +00008525 while (1) {
8526 ret = btrfs_truncate_inode_items(trans, root, inode,
8527 inode->i_size,
8528 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008529 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05008530 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008531 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008532 }
Chris Mason39279cc2007-06-12 06:35:45 -04008533
Josef Bacikfcb80c22011-05-03 10:40:22 -04008534 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008535 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008536 if (ret) {
8537 err = ret;
8538 break;
8539 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008540
Yan, Zheng80825102009-11-12 09:35:36 +00008541 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008542 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008543
8544 trans = btrfs_start_transaction(root, 2);
8545 if (IS_ERR(trans)) {
8546 ret = err = PTR_ERR(trans);
8547 trans = NULL;
8548 break;
8549 }
8550
8551 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8552 rsv, min_size);
8553 BUG_ON(ret); /* shouldn't happen */
8554 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008555 }
8556
8557 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008558 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008559 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008560 if (ret)
8561 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008562 }
8563
Chris Mason917c16b2011-11-08 14:49:59 -05008564 if (trans) {
8565 trans->block_rsv = &root->fs_info->trans_block_rsv;
8566 ret = btrfs_update_inode(trans, root, inode);
8567 if (ret && !err)
8568 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008569
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008570 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008571 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008572 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008573
8574out:
8575 btrfs_free_block_rsv(root, rsv);
8576
Josef Bacik3893e332011-01-31 16:03:11 -05008577 if (ret && !err)
8578 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008579
Josef Bacik3893e332011-01-31 16:03:11 -05008580 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008581}
8582
Sven Wegener3b963622008-06-09 21:57:42 -04008583/*
Chris Masond352ac62008-09-29 15:18:18 -04008584 * create a new subvolume directory/inode (helper for the ioctl).
8585 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008586int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008587 struct btrfs_root *new_root,
8588 struct btrfs_root *parent_root,
8589 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008590{
Chris Mason39279cc2007-06-12 06:35:45 -04008591 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008592 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008593 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008594
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008595 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8596 new_dirid, new_dirid,
8597 S_IFDIR | (~current_umask() & S_IRWXUGO),
8598 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008599 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008600 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008601 inode->i_op = &btrfs_dir_inode_operations;
8602 inode->i_fop = &btrfs_dir_file_operations;
8603
Miklos Szeredibfe86842011-10-28 14:13:29 +02008604 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008605 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008606 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008607
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008608 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8609 if (err)
8610 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008611 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008612 new_root->root_key.objectid, err);
8613
Yan, Zheng76dda932009-09-21 16:00:26 -04008614 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008615
Yan, Zheng76dda932009-09-21 16:00:26 -04008616 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008617 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008618}
8619
Chris Mason39279cc2007-06-12 06:35:45 -04008620struct inode *btrfs_alloc_inode(struct super_block *sb)
8621{
8622 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008623 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008624
8625 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8626 if (!ei)
8627 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008628
8629 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008630 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008631 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008632 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008633 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008634 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008635 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008636 ei->disk_i_size = 0;
8637 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008638 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008639 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008640 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008641 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008642 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008643
Josef Bacik9e0baf62011-07-15 15:16:44 +00008644 spin_lock_init(&ei->lock);
8645 ei->outstanding_extents = 0;
8646 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008647
Josef Bacik72ac3c02012-05-23 14:13:11 -04008648 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008649 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008650
Miao Xie16cdcec2011-04-22 18:12:22 +08008651 ei->delayed_node = NULL;
8652
chandan r9cc97d62012-07-04 12:48:07 +05308653 ei->i_otime.tv_sec = 0;
8654 ei->i_otime.tv_nsec = 0;
8655
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008656 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008657 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008658 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8659 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008660 ei->io_tree.track_uptodate = 1;
8661 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008662 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008663 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008664 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008665 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008666 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008667 RB_CLEAR_NODE(&ei->rb_node);
8668
8669 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008670}
8671
Josef Bacikaaedb552013-10-11 14:44:09 -04008672#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8673void btrfs_test_destroy_inode(struct inode *inode)
8674{
8675 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8676 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8677}
8678#endif
8679
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008680static void btrfs_i_callback(struct rcu_head *head)
8681{
8682 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008683 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8684}
8685
Chris Mason39279cc2007-06-12 06:35:45 -04008686void btrfs_destroy_inode(struct inode *inode)
8687{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008688 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008689 struct btrfs_root *root = BTRFS_I(inode)->root;
8690
Al Virob3d9b7a2012-06-09 13:51:19 -04008691 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008692 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008693 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8694 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008695 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8696 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008697 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008698
Chris Mason5a3f23d2009-03-31 13:27:11 -04008699 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008700 * This can happen where we create an inode, but somebody else also
8701 * created the same inode and we need to destroy the one we already
8702 * created.
8703 */
8704 if (!root)
8705 goto free;
8706
Josef Bacik8a35d952012-05-23 14:26:42 -04008707 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8708 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008709 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008710 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008711 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008712 }
Josef Bacik7b128762008-07-24 12:17:14 -04008713
Chris Masond3977122009-01-05 21:25:51 -05008714 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008715 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8716 if (!ordered)
8717 break;
8718 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008719 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008720 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008721 btrfs_remove_ordered_extent(inode, ordered);
8722 btrfs_put_ordered_extent(ordered);
8723 btrfs_put_ordered_extent(ordered);
8724 }
8725 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008726 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008727 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008728free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008729 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008730}
8731
Al Viro45321ac2010-06-07 13:43:19 -04008732int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008733{
8734 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008735
Naohiro Aota6379ef92013-06-06 09:56:34 +00008736 if (root == NULL)
8737 return 1;
8738
Liu Bofa6ac872013-02-20 14:10:23 +00008739 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008740 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008741 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008742 else
Al Viro45321ac2010-06-07 13:43:19 -04008743 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008744}
8745
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008746static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008747{
8748 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8749
8750 inode_init_once(&ei->vfs_inode);
8751}
8752
8753void btrfs_destroy_cachep(void)
8754{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008755 /*
8756 * Make sure all delayed rcu free inodes are flushed before we
8757 * destroy cache.
8758 */
8759 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008760 if (btrfs_inode_cachep)
8761 kmem_cache_destroy(btrfs_inode_cachep);
8762 if (btrfs_trans_handle_cachep)
8763 kmem_cache_destroy(btrfs_trans_handle_cachep);
8764 if (btrfs_transaction_cachep)
8765 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008766 if (btrfs_path_cachep)
8767 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008768 if (btrfs_free_space_cachep)
8769 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008770 if (btrfs_delalloc_work_cachep)
8771 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008772}
8773
8774int btrfs_init_cachep(void)
8775{
David Sterba837e1972012-09-07 03:00:48 -06008776 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008777 sizeof(struct btrfs_inode), 0,
8778 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008779 if (!btrfs_inode_cachep)
8780 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008781
David Sterba837e1972012-09-07 03:00:48 -06008782 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008783 sizeof(struct btrfs_trans_handle), 0,
8784 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008785 if (!btrfs_trans_handle_cachep)
8786 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008787
David Sterba837e1972012-09-07 03:00:48 -06008788 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008789 sizeof(struct btrfs_transaction), 0,
8790 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008791 if (!btrfs_transaction_cachep)
8792 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008793
David Sterba837e1972012-09-07 03:00:48 -06008794 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008795 sizeof(struct btrfs_path), 0,
8796 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008797 if (!btrfs_path_cachep)
8798 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008799
David Sterba837e1972012-09-07 03:00:48 -06008800 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008801 sizeof(struct btrfs_free_space), 0,
8802 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8803 if (!btrfs_free_space_cachep)
8804 goto fail;
8805
Miao Xie8ccf6f192012-10-25 09:28:04 +00008806 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8807 sizeof(struct btrfs_delalloc_work), 0,
8808 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8809 NULL);
8810 if (!btrfs_delalloc_work_cachep)
8811 goto fail;
8812
Chris Mason39279cc2007-06-12 06:35:45 -04008813 return 0;
8814fail:
8815 btrfs_destroy_cachep();
8816 return -ENOMEM;
8817}
8818
8819static int btrfs_getattr(struct vfsmount *mnt,
8820 struct dentry *dentry, struct kstat *stat)
8821{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008822 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008823 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008824 u32 blocksize = inode->i_sb->s_blocksize;
8825
Chris Mason39279cc2007-06-12 06:35:45 -04008826 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008827 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008828 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008829
8830 spin_lock(&BTRFS_I(inode)->lock);
8831 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8832 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008833 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008834 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008835 return 0;
8836}
8837
Chris Masond3977122009-01-05 21:25:51 -05008838static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8839 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008840{
8841 struct btrfs_trans_handle *trans;
8842 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008843 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008844 struct inode *new_inode = new_dentry->d_inode;
8845 struct inode *old_inode = old_dentry->d_inode;
8846 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008847 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008848 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008849 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008850 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008851
Li Zefan33345d012011-04-20 10:31:50 +08008852 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008853 return -EPERM;
8854
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008855 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008856 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008857 return -EXDEV;
8858
Li Zefan33345d012011-04-20 10:31:50 +08008859 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8860 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008861 return -ENOTEMPTY;
8862
Chris Mason39279cc2007-06-12 06:35:45 -04008863 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008864 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008865 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008866
8867
8868 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008869 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008870 new_dentry->d_name.name,
8871 new_dentry->d_name.len);
8872
8873 if (ret) {
8874 if (ret == -EEXIST) {
8875 /* we shouldn't get
8876 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308877 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008878 return ret;
8879 }
8880 } else {
8881 /* maybe -EOVERFLOW */
8882 return ret;
8883 }
8884 }
8885 ret = 0;
8886
Chris Mason5a3f23d2009-03-31 13:27:11 -04008887 /*
Chris Mason8d875f92014-08-12 10:47:42 -07008888 * we're using rename to replace one file with another. Start IO on it
8889 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04008890 */
Chris Mason8d875f92014-08-12 10:47:42 -07008891 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04008892 filemap_flush(old_inode->i_mapping);
8893
Yan, Zheng76dda932009-09-21 16:00:26 -04008894 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008895 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008896 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008897 /*
8898 * We want to reserve the absolute worst case amount of items. So if
8899 * both inodes are subvols and we need to unlink them then that would
8900 * require 4 item modifications, but if they are both normal inodes it
8901 * would require 5 item modifications, so we'll assume their normal
8902 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8903 * should cover the worst case number of items we'll modify.
8904 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008905 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008906 if (IS_ERR(trans)) {
8907 ret = PTR_ERR(trans);
8908 goto out_notrans;
8909 }
Chris Mason5f39d392007-10-15 16:14:19 -04008910
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008911 if (dest != root)
8912 btrfs_record_root_in_trans(trans, dest);
8913
Yan, Zhenga5719522009-09-24 09:17:31 -04008914 ret = btrfs_set_inode_index(new_dir, &index);
8915 if (ret)
8916 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008917
Miao Xie67de1172013-12-26 13:07:06 +08008918 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008919 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008920 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08008921 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008922 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008923 ret = btrfs_insert_inode_ref(trans, dest,
8924 new_dentry->d_name.name,
8925 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008926 old_ino,
8927 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008928 if (ret)
8929 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008930 /*
8931 * this is an ugly little race, but the rename is required
8932 * to make sure that if we crash, the inode is either at the
8933 * old name or the new one. pinning the log transaction lets
8934 * us make sure we don't allow a log commit to come in after
8935 * we unlink the name but before we add the new name back in.
8936 */
8937 btrfs_pin_log_trans(root);
8938 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04008939
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008940 inode_inc_iversion(old_dir);
8941 inode_inc_iversion(new_dir);
8942 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008943 old_dir->i_ctime = old_dir->i_mtime = ctime;
8944 new_dir->i_ctime = new_dir->i_mtime = ctime;
8945 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008946
Chris Mason12fcfd22009-03-24 10:24:20 -04008947 if (old_dentry->d_parent != new_dentry->d_parent)
8948 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8949
Li Zefan33345d012011-04-20 10:31:50 +08008950 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008951 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8952 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8953 old_dentry->d_name.name,
8954 old_dentry->d_name.len);
8955 } else {
Al Viro92986792011-03-04 17:14:37 +00008956 ret = __btrfs_unlink_inode(trans, root, old_dir,
8957 old_dentry->d_inode,
8958 old_dentry->d_name.name,
8959 old_dentry->d_name.len);
8960 if (!ret)
8961 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008962 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008963 if (ret) {
8964 btrfs_abort_transaction(trans, root, ret);
8965 goto out_fail;
8966 }
Chris Mason39279cc2007-06-12 06:35:45 -04008967
8968 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008969 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008970 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008971 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008972 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8973 root_objectid = BTRFS_I(new_inode)->location.objectid;
8974 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8975 root_objectid,
8976 new_dentry->d_name.name,
8977 new_dentry->d_name.len);
8978 BUG_ON(new_inode->i_nlink == 0);
8979 } else {
8980 ret = btrfs_unlink_inode(trans, dest, new_dir,
8981 new_dentry->d_inode,
8982 new_dentry->d_name.name,
8983 new_dentry->d_name.len);
8984 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008985 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008986 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008987 if (ret) {
8988 btrfs_abort_transaction(trans, root, ret);
8989 goto out_fail;
8990 }
Chris Mason39279cc2007-06-12 06:35:45 -04008991 }
Josef Bacikaec74772008-07-24 12:12:38 -04008992
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008993 ret = btrfs_add_link(trans, new_dir, old_inode,
8994 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008995 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008996 if (ret) {
8997 btrfs_abort_transaction(trans, root, ret);
8998 goto out_fail;
8999 }
Chris Mason39279cc2007-06-12 06:35:45 -04009000
Miao Xie67de1172013-12-26 13:07:06 +08009001 if (old_inode->i_nlink == 1)
9002 BTRFS_I(old_inode)->dir_index = index;
9003
Li Zefan33345d012011-04-20 10:31:50 +08009004 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04009005 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00009006 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009007 btrfs_end_log_trans(root);
9008 }
Chris Mason39279cc2007-06-12 06:35:45 -04009009out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009010 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009011out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009012 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009013 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009014
Chris Mason39279cc2007-06-12 06:35:45 -04009015 return ret;
9016}
9017
Miklos Szeredi80ace852014-07-23 15:15:32 +02009018static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9019 struct inode *new_dir, struct dentry *new_dentry,
9020 unsigned int flags)
9021{
9022 if (flags & ~RENAME_NOREPLACE)
9023 return -EINVAL;
9024
9025 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
9026}
9027
Miao Xie8ccf6f192012-10-25 09:28:04 +00009028static void btrfs_run_delalloc_work(struct btrfs_work *work)
9029{
9030 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009031 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009032
9033 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9034 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009035 inode = delalloc_work->inode;
9036 if (delalloc_work->wait) {
9037 btrfs_wait_ordered_range(inode, 0, (u64)-1);
9038 } else {
9039 filemap_flush(inode->i_mapping);
9040 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9041 &BTRFS_I(inode)->runtime_flags))
9042 filemap_flush(inode->i_mapping);
9043 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00009044
9045 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009046 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009047 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009048 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009049 complete(&delalloc_work->completion);
9050}
9051
9052struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
9053 int wait, int delay_iput)
9054{
9055 struct btrfs_delalloc_work *work;
9056
9057 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
9058 if (!work)
9059 return NULL;
9060
9061 init_completion(&work->completion);
9062 INIT_LIST_HEAD(&work->list);
9063 work->inode = inode;
9064 work->wait = wait;
9065 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009066 WARN_ON_ONCE(!inode);
9067 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9068 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009069
9070 return work;
9071}
9072
9073void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9074{
9075 wait_for_completion(&work->completion);
9076 kmem_cache_free(btrfs_delalloc_work_cachep, work);
9077}
9078
Chris Masond352ac62008-09-29 15:18:18 -04009079/*
9080 * some fairly slow code that needs optimization. This walks the list
9081 * of all the inodes with pending delalloc and forces them to disk.
9082 */
Miao Xie6c255e62014-03-06 13:55:01 +08009083static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9084 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009085{
Chris Masonea8c2812008-08-04 23:17:27 -04009086 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009087 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009088 struct btrfs_delalloc_work *work, *next;
9089 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009090 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009091 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009092
Miao Xie8ccf6f192012-10-25 09:28:04 +00009093 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009094 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009095
Miao Xie573bfb72014-03-06 13:55:03 +08009096 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009097 spin_lock(&root->delalloc_lock);
9098 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009099 while (!list_empty(&splice)) {
9100 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009101 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009102
Miao Xieeb73c1b2013-05-15 07:48:22 +00009103 list_move_tail(&binode->delalloc_inodes,
9104 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009105 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009106 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009107 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009108 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009109 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009110 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009111
9112 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009113 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009114 if (delay_iput)
9115 btrfs_add_delayed_iput(inode);
9116 else
9117 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009118 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009119 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009120 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009121 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009122 btrfs_queue_work(root->fs_info->flush_workers,
9123 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009124 ret++;
9125 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009126 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009127 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009128 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009129 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009130 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009131
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009132out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009133 list_for_each_entry_safe(work, next, &works, list) {
9134 list_del_init(&work->list);
9135 btrfs_wait_and_free_delalloc_work(work);
9136 }
9137
Miao Xieeb73c1b2013-05-15 07:48:22 +00009138 if (!list_empty_careful(&splice)) {
9139 spin_lock(&root->delalloc_lock);
9140 list_splice_tail(&splice, &root->delalloc_inodes);
9141 spin_unlock(&root->delalloc_lock);
9142 }
Miao Xie573bfb72014-03-06 13:55:03 +08009143 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009144 return ret;
9145}
9146
9147int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9148{
9149 int ret;
9150
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009151 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009152 return -EROFS;
9153
Miao Xie6c255e62014-03-06 13:55:01 +08009154 ret = __start_delalloc_inodes(root, delay_iput, -1);
9155 if (ret > 0)
9156 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009157 /*
9158 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009159 * we have to make sure the IO is actually started and that
9160 * ordered extents get created before we return
9161 */
9162 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009163 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009164 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009165 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009166 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9167 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009168 }
9169 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009170 return ret;
9171}
9172
Miao Xie6c255e62014-03-06 13:55:01 +08009173int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9174 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009175{
9176 struct btrfs_root *root;
9177 struct list_head splice;
9178 int ret;
9179
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009180 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009181 return -EROFS;
9182
9183 INIT_LIST_HEAD(&splice);
9184
Miao Xie573bfb72014-03-06 13:55:03 +08009185 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009186 spin_lock(&fs_info->delalloc_root_lock);
9187 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009188 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009189 root = list_first_entry(&splice, struct btrfs_root,
9190 delalloc_root);
9191 root = btrfs_grab_fs_root(root);
9192 BUG_ON(!root);
9193 list_move_tail(&root->delalloc_root,
9194 &fs_info->delalloc_roots);
9195 spin_unlock(&fs_info->delalloc_root_lock);
9196
Miao Xie6c255e62014-03-06 13:55:01 +08009197 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009198 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009199 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009200 goto out;
9201
Miao Xie6c255e62014-03-06 13:55:01 +08009202 if (nr != -1) {
9203 nr -= ret;
9204 WARN_ON(nr < 0);
9205 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009206 spin_lock(&fs_info->delalloc_root_lock);
9207 }
9208 spin_unlock(&fs_info->delalloc_root_lock);
9209
Miao Xie6c255e62014-03-06 13:55:01 +08009210 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009211 atomic_inc(&fs_info->async_submit_draining);
9212 while (atomic_read(&fs_info->nr_async_submits) ||
9213 atomic_read(&fs_info->async_delalloc_pages)) {
9214 wait_event(fs_info->async_submit_wait,
9215 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9216 atomic_read(&fs_info->async_delalloc_pages) == 0));
9217 }
9218 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009219out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009220 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009221 spin_lock(&fs_info->delalloc_root_lock);
9222 list_splice_tail(&splice, &fs_info->delalloc_roots);
9223 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009224 }
Miao Xie573bfb72014-03-06 13:55:03 +08009225 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009226 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009227}
9228
Chris Mason39279cc2007-06-12 06:35:45 -04009229static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9230 const char *symname)
9231{
9232 struct btrfs_trans_handle *trans;
9233 struct btrfs_root *root = BTRFS_I(dir)->root;
9234 struct btrfs_path *path;
9235 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009236 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009237 int err;
9238 int drop_inode = 0;
9239 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309240 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009241 int name_len;
9242 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009243 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009244 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009245 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009246
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009247 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009248 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9249 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009250
Josef Bacik9ed74f22009-09-11 16:12:44 -04009251 /*
9252 * 2 items for inode item and ref
9253 * 2 items for dir items
9254 * 1 item for xattr if selinux is on
9255 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009256 trans = btrfs_start_transaction(root, 5);
9257 if (IS_ERR(trans))
9258 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009259
Li Zefan581bb052011-04-20 10:06:11 +08009260 err = btrfs_find_free_ino(root, &objectid);
9261 if (err)
9262 goto out_unlock;
9263
Josef Bacikaec74772008-07-24 12:12:38 -04009264 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009265 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04009266 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009267 if (IS_ERR(inode)) {
9268 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009269 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009270 }
Chris Mason39279cc2007-06-12 06:35:45 -04009271
Casey Schauflerad19db72011-12-15 10:09:07 -05009272 /*
9273 * If the active LSM wants to access the inode during
9274 * d_instantiate it needs these. Smack checks to see
9275 * if the filesystem supports xattrs by looking at the
9276 * ops vector.
9277 */
9278 inode->i_fop = &btrfs_file_operations;
9279 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009280 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009281 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9282
9283 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9284 if (err)
9285 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009286
Josef Bacika1b075d2010-11-19 20:36:11 +00009287 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009288 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009289 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009290
9291 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009292 if (!path) {
9293 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009294 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009295 }
Li Zefan33345d012011-04-20 10:31:50 +08009296 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009297 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009298 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009299 datasize = btrfs_file_extent_calc_inline_size(name_len);
9300 err = btrfs_insert_empty_item(trans, root, path, &key,
9301 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009302 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009303 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009304 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009305 }
Chris Mason5f39d392007-10-15 16:14:19 -04009306 leaf = path->nodes[0];
9307 ei = btrfs_item_ptr(leaf, path->slots[0],
9308 struct btrfs_file_extent_item);
9309 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9310 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009311 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009312 btrfs_set_file_extent_encryption(leaf, ei, 0);
9313 btrfs_set_file_extent_compression(leaf, ei, 0);
9314 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9315 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9316
Chris Mason39279cc2007-06-12 06:35:45 -04009317 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009318 write_extent_buffer(leaf, symname, ptr, name_len);
9319 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009320 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009321
Chris Mason39279cc2007-06-12 06:35:45 -04009322 inode->i_op = &btrfs_symlink_inode_operations;
9323 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -04009324 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009325 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009326 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009327 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009328 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009329 goto out_unlock_inode;
9330 }
9331
9332 unlock_new_inode(inode);
9333 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009334
9335out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009336 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009337 if (drop_inode) {
9338 inode_dec_link_count(inode);
9339 iput(inode);
9340 }
Liu Bob53d3f52012-11-14 14:34:34 +00009341 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009342 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009343
9344out_unlock_inode:
9345 drop_inode = 1;
9346 unlock_new_inode(inode);
9347 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009348}
Chris Mason16432982008-04-10 10:23:21 -04009349
Josef Bacik0af3d002010-06-21 14:48:16 -04009350static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9351 u64 start, u64 num_bytes, u64 min_size,
9352 loff_t actual_len, u64 *alloc_hint,
9353 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009354{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009355 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9356 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009357 struct btrfs_root *root = BTRFS_I(inode)->root;
9358 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009359 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009360 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009361 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009362 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009363 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009364
Josef Bacik0af3d002010-06-21 14:48:16 -04009365 if (trans)
9366 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009367 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009368 if (own_trans) {
9369 trans = btrfs_start_transaction(root, 3);
9370 if (IS_ERR(trans)) {
9371 ret = PTR_ERR(trans);
9372 break;
9373 }
Yan Zhengd899e052008-10-30 14:25:28 -04009374 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009375
Chris Mason154ea282013-03-05 11:11:26 -05009376 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9377 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009378 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009379 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009380 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009381 if (own_trans)
9382 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009383 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009384 }
9385
Yan Zhengd899e052008-10-30 14:25:28 -04009386 ret = insert_reserved_file_extent(trans, inode,
9387 cur_offset, ins.objectid,
9388 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009389 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009390 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009391 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009392 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009393 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009394 btrfs_abort_transaction(trans, root, ret);
9395 if (own_trans)
9396 btrfs_end_transaction(trans, root);
9397 break;
9398 }
Chris Masona1ed8352009-09-11 12:27:37 -04009399 btrfs_drop_extent_cache(inode, cur_offset,
9400 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009401
Josef Bacik5dc562c2012-08-17 13:14:17 -04009402 em = alloc_extent_map();
9403 if (!em) {
9404 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9405 &BTRFS_I(inode)->runtime_flags);
9406 goto next;
9407 }
9408
9409 em->start = cur_offset;
9410 em->orig_start = cur_offset;
9411 em->len = ins.offset;
9412 em->block_start = ins.objectid;
9413 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009414 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009415 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009416 em->bdev = root->fs_info->fs_devices->latest_bdev;
9417 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9418 em->generation = trans->transid;
9419
9420 while (1) {
9421 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009422 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009423 write_unlock(&em_tree->lock);
9424 if (ret != -EEXIST)
9425 break;
9426 btrfs_drop_extent_cache(inode, cur_offset,
9427 cur_offset + ins.offset - 1,
9428 0);
9429 }
9430 free_extent_map(em);
9431next:
Yan Zhengd899e052008-10-30 14:25:28 -04009432 num_bytes -= ins.offset;
9433 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009434 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009435
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009436 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009437 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009438 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009439 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009440 (actual_len > inode->i_size) &&
9441 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009442 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009443 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009444 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009445 i_size = cur_offset;
9446 i_size_write(inode, i_size);
9447 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009448 }
9449
Yan Zhengd899e052008-10-30 14:25:28 -04009450 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009451
9452 if (ret) {
9453 btrfs_abort_transaction(trans, root, ret);
9454 if (own_trans)
9455 btrfs_end_transaction(trans, root);
9456 break;
9457 }
Yan Zhengd899e052008-10-30 14:25:28 -04009458
Josef Bacik0af3d002010-06-21 14:48:16 -04009459 if (own_trans)
9460 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009461 }
Yan Zhengd899e052008-10-30 14:25:28 -04009462 return ret;
9463}
9464
Josef Bacik0af3d002010-06-21 14:48:16 -04009465int btrfs_prealloc_file_range(struct inode *inode, int mode,
9466 u64 start, u64 num_bytes, u64 min_size,
9467 loff_t actual_len, u64 *alloc_hint)
9468{
9469 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9470 min_size, actual_len, alloc_hint,
9471 NULL);
9472}
9473
9474int btrfs_prealloc_file_range_trans(struct inode *inode,
9475 struct btrfs_trans_handle *trans, int mode,
9476 u64 start, u64 num_bytes, u64 min_size,
9477 loff_t actual_len, u64 *alloc_hint)
9478{
9479 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9480 min_size, actual_len, alloc_hint, trans);
9481}
9482
Chris Masone6dcd2d2008-07-17 12:53:50 -04009483static int btrfs_set_page_dirty(struct page *page)
9484{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009485 return __set_page_dirty_nobuffers(page);
9486}
9487
Al Viro10556cb2011-06-20 19:28:19 -04009488static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009489{
Li Zefanb83cc962010-12-20 16:04:08 +08009490 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009491 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009492
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009493 if (mask & MAY_WRITE &&
9494 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9495 if (btrfs_root_readonly(root))
9496 return -EROFS;
9497 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9498 return -EACCES;
9499 }
Al Viro2830ba72011-06-20 19:16:29 -04009500 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009501}
Chris Mason39279cc2007-06-12 06:35:45 -04009502
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009503static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9504{
9505 struct btrfs_trans_handle *trans;
9506 struct btrfs_root *root = BTRFS_I(dir)->root;
9507 struct inode *inode = NULL;
9508 u64 objectid;
9509 u64 index;
9510 int ret = 0;
9511
9512 /*
9513 * 5 units required for adding orphan entry
9514 */
9515 trans = btrfs_start_transaction(root, 5);
9516 if (IS_ERR(trans))
9517 return PTR_ERR(trans);
9518
9519 ret = btrfs_find_free_ino(root, &objectid);
9520 if (ret)
9521 goto out;
9522
9523 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9524 btrfs_ino(dir), objectid, mode, &index);
9525 if (IS_ERR(inode)) {
9526 ret = PTR_ERR(inode);
9527 inode = NULL;
9528 goto out;
9529 }
9530
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009531 inode->i_fop = &btrfs_file_operations;
9532 inode->i_op = &btrfs_file_inode_operations;
9533
9534 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009535 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9536
Chris Masonb0d5d102014-09-08 13:08:51 -07009537 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9538 if (ret)
9539 goto out_inode;
9540
9541 ret = btrfs_update_inode(trans, root, inode);
9542 if (ret)
9543 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009544 ret = btrfs_orphan_add(trans, inode);
9545 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009546 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009547
Filipe Manana5762b5c2014-08-01 00:10:32 +01009548 /*
9549 * We set number of links to 0 in btrfs_new_inode(), and here we set
9550 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9551 * through:
9552 *
9553 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9554 */
9555 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009556 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009557 d_tmpfile(dentry, inode);
9558 mark_inode_dirty(inode);
9559
9560out:
9561 btrfs_end_transaction(trans, root);
9562 if (ret)
9563 iput(inode);
9564 btrfs_balance_delayed_items(root);
9565 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009566 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009567
9568out_inode:
9569 unlock_new_inode(inode);
9570 goto out;
9571
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009572}
9573
Filipe Mananab38ef712014-11-13 17:01:45 +00009574/* Inspired by filemap_check_errors() */
9575int btrfs_inode_check_errors(struct inode *inode)
9576{
9577 int ret = 0;
9578
9579 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
9580 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
9581 ret = -ENOSPC;
9582 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
9583 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
9584 ret = -EIO;
9585
9586 return ret;
9587}
9588
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009589static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009590 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009591 .lookup = btrfs_lookup,
9592 .create = btrfs_create,
9593 .unlink = btrfs_unlink,
9594 .link = btrfs_link,
9595 .mkdir = btrfs_mkdir,
9596 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009597 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009598 .symlink = btrfs_symlink,
9599 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009600 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009601 .setxattr = btrfs_setxattr,
9602 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009603 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009604 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009605 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009606 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009607 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009608 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009609 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009610};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009611static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009612 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009613 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009614 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009615 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009616 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009617};
Yan, Zheng76dda932009-09-21 16:00:26 -04009618
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009619static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009620 .llseek = generic_file_llseek,
9621 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009622 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009623 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009624#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009625 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009626#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009627 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009628 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009629};
9630
Chris Masond1310b22008-01-24 16:13:08 -05009631static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009632 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009633 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009634 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009635 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009636 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009637 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009638 .set_bit_hook = btrfs_set_bit_hook,
9639 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009640 .merge_extent_hook = btrfs_merge_extent_hook,
9641 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009642};
9643
Chris Mason35054392009-01-21 13:11:13 -05009644/*
9645 * btrfs doesn't support the bmap operation because swapfiles
9646 * use bmap to make a mapping of extents in the file. They assume
9647 * these extents won't change over the life of the file and they
9648 * use the bmap result to do IO directly to the drive.
9649 *
9650 * the btrfs bmap call would return logical addresses that aren't
9651 * suitable for IO and they also will change frequently as COW
9652 * operations happen. So, swapfile + btrfs == corruption.
9653 *
9654 * For now we're avoiding this by dropping bmap.
9655 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009656static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009657 .readpage = btrfs_readpage,
9658 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04009659 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009660 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009661 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009662 .invalidatepage = btrfs_invalidatepage,
9663 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009664 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009665 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009666};
9667
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009668static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009669 .readpage = btrfs_readpage,
9670 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009671 .invalidatepage = btrfs_invalidatepage,
9672 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009673};
9674
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009675static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009676 .getattr = btrfs_getattr,
9677 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009678 .setxattr = btrfs_setxattr,
9679 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009680 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009681 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009682 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009683 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009684 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009685 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009686 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009687};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009688static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009689 .getattr = btrfs_getattr,
9690 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009691 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009692 .setxattr = btrfs_setxattr,
9693 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009694 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009695 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009696 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009697 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009698 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009699};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009700static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009701 .readlink = generic_readlink,
9702 .follow_link = page_follow_link_light,
9703 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009704 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009705 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009706 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009707 .setxattr = btrfs_setxattr,
9708 .getxattr = btrfs_getxattr,
9709 .listxattr = btrfs_listxattr,
9710 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009711 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009712};
Yan, Zheng76dda932009-09-21 16:00:26 -04009713
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009714const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009715 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009716 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009717};