blob: fcb9a38fc9d35ff62df6e16161d39779d0833d3f [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 */
Chris Mason771ed682008-11-06 22:02:51 -0500385static noinline int compress_file_range(struct inode *inode,
386 struct page *locked_page,
387 u64 start, u64 end,
388 struct async_cow *async_cow,
389 int *num_added)
Chris Masonb888db2b2007-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 Masonb888db2b2007-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
Wang Shilong68bb4622014-04-01 18:01:42 +0800414 /*
415 * skip compression for a small file range(<=blocksize) that
416 * isn't an inline extent, since it dosen't save disk space at all.
417 */
418 if ((end - start + 1) <= blocksize &&
419 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
420 goto cleanup_and_bail_uncompressed;
421
Chris Mason42dc7ba2008-12-15 11:44:56 -0500422 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400423again:
424 will_compress = 0;
425 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
426 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
427
Chris Masonf03d9301f2009-02-04 09:31:06 -0500428 /*
429 * we don't want to send crud past the end of i_size through
430 * compression, that's just a waste of CPU time. So, if the
431 * end of the file is before the start of our current
432 * requested range of bytes, we bail out to the uncompressed
433 * cleanup code that can deal with all of this.
434 *
435 * It isn't really the fastest way to fix things, but this is a
436 * very uncommon corner.
437 */
438 if (actual_end <= start)
439 goto cleanup_and_bail_uncompressed;
440
Chris Masonc8b97812008-10-29 14:49:59 -0400441 total_compressed = actual_end - start;
442
443 /* 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;
530 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
531
Chris Mason771ed682008-11-06 22:02:51 -0500532 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100533 * inline extent creation worked or returned error,
534 * we don't need to create any more async work items.
535 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500536 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400537 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400538 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400539 PAGE_CLEAR_DIRTY |
540 PAGE_SET_WRITEBACK |
541 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400542 goto free_pages_out;
543 }
544 }
545
546 if (will_compress) {
547 /*
548 * we aren't doing an inline extent round the compressed size
549 * up to a block size boundary so the allocator does sane
550 * things
551 */
Qu Wenruofda28322013-02-26 08:10:22 +0000552 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400553
554 /*
555 * one last check to make sure the compression is really a
556 * win, compare the page count read with the blocks on disk
557 */
Qu Wenruofda28322013-02-26 08:10:22 +0000558 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400559 if (total_compressed >= total_in) {
560 will_compress = 0;
561 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400562 num_bytes = total_in;
563 }
564 }
565 if (!will_compress && pages) {
566 /*
567 * the compression code ran but failed to make things smaller,
568 * free any pages it allocated and our page pointer array
569 */
570 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400571 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400572 page_cache_release(pages[i]);
573 }
574 kfree(pages);
575 pages = NULL;
576 total_compressed = 0;
577 nr_pages_ret = 0;
578
579 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500580 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
581 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500582 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500583 }
Chris Masonc8b97812008-10-29 14:49:59 -0400584 }
Chris Mason771ed682008-11-06 22:02:51 -0500585 if (will_compress) {
586 *num_added += 1;
587
588 /* the async work queues will take care of doing actual
589 * allocation on disk for these compressed pages,
590 * and will submit them to the elevator.
591 */
592 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800593 total_compressed, pages, nr_pages_ret,
594 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500595
Yan, Zheng24ae6362010-12-06 07:02:36 +0000596 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500597 start += num_bytes;
598 pages = NULL;
599 cond_resched();
600 goto again;
601 }
602 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500603cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500604 /*
605 * No compression, but we still need to write the pages in
606 * the file we've been given so far. redirty the locked
607 * page if it corresponds to our extent and set things up
608 * for the async work queue to run cow_file_range to do
609 * the normal delalloc dance
610 */
611 if (page_offset(locked_page) >= start &&
612 page_offset(locked_page) <= end) {
613 __set_page_dirty_nobuffers(locked_page);
614 /* unlocked later on in the async handlers */
615 }
Chris Mason4adaa612013-03-26 13:07:00 -0400616 if (redirty)
617 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800618 add_async_extent(async_cow, start, end - start + 1,
619 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500620 *num_added += 1;
621 }
622
623out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100624 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500625
626free_pages_out:
627 for (i = 0; i < nr_pages_ret; i++) {
628 WARN_ON(pages[i]->mapping);
629 page_cache_release(pages[i]);
630 }
Chris Masond3977122009-01-05 21:25:51 -0500631 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500632
633 goto out;
634}
635
636/*
637 * phase two of compressed writeback. This is the ordered portion
638 * of the code, which only gets called in the order the work was
639 * queued. We walk all the async extents created by compress_file_range
640 * and send them down to the disk.
641 */
642static noinline int submit_compressed_extents(struct inode *inode,
643 struct async_cow *async_cow)
644{
645 struct async_extent *async_extent;
646 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500647 struct btrfs_key ins;
648 struct extent_map *em;
649 struct btrfs_root *root = BTRFS_I(inode)->root;
650 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
651 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500652 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500653
654 if (list_empty(&async_cow->extents))
655 return 0;
656
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500657again:
Chris Masond3977122009-01-05 21:25:51 -0500658 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500659 async_extent = list_entry(async_cow->extents.next,
660 struct async_extent, list);
661 list_del(&async_extent->list);
662
663 io_tree = &BTRFS_I(inode)->io_tree;
664
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500665retry:
Chris Mason771ed682008-11-06 22:02:51 -0500666 /* did the compression code fall back to uncompressed IO? */
667 if (!async_extent->pages) {
668 int page_started = 0;
669 unsigned long nr_written = 0;
670
671 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000672 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100673 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500674
675 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500676 ret = cow_file_range(inode, async_cow->locked_page,
677 async_extent->start,
678 async_extent->start +
679 async_extent->ram_size - 1,
680 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500681
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100682 /* JDM XXX */
683
Chris Mason771ed682008-11-06 22:02:51 -0500684 /*
685 * if page_started, cow_file_range inserted an
686 * inline extent and took care of all the unlocking
687 * and IO for us. Otherwise, we need to submit
688 * all those pages down to the drive.
689 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500690 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500691 extent_write_locked_range(io_tree,
692 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500693 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500694 async_extent->ram_size - 1,
695 btrfs_get_extent,
696 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500697 else if (ret)
698 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500699 kfree(async_extent);
700 cond_resched();
701 continue;
702 }
703
704 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100705 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500706
Josef Bacik00361582013-08-14 14:02:47 -0400707 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500708 async_extent->compressed_size,
709 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800710 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500711 if (ret) {
712 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500713
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500714 for (i = 0; i < async_extent->nr_pages; i++) {
715 WARN_ON(async_extent->pages[i]->mapping);
716 page_cache_release(async_extent->pages[i]);
717 }
718 kfree(async_extent->pages);
719 async_extent->nr_pages = 0;
720 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500721
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400722 if (ret == -ENOSPC) {
723 unlock_extent(io_tree, async_extent->start,
724 async_extent->start +
725 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800726
727 /*
728 * we need to redirty the pages if we decide to
729 * fallback to uncompressed IO, otherwise we
730 * will not submit these pages down to lower
731 * layers.
732 */
733 extent_range_redirty_for_io(inode,
734 async_extent->start,
735 async_extent->start +
736 async_extent->ram_size - 1);
737
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100738 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400739 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500740 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500741 }
742
Yan, Zhengc2167752009-11-12 09:34:21 +0000743 /*
744 * here we're doing allocation and writeback of the
745 * compressed pages
746 */
747 btrfs_drop_extent_cache(inode, async_extent->start,
748 async_extent->start +
749 async_extent->ram_size - 1, 0);
750
David Sterba172ddd62011-04-21 00:48:27 +0200751 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000752 if (!em) {
753 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500754 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000755 }
Chris Mason771ed682008-11-06 22:02:51 -0500756 em->start = async_extent->start;
757 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500758 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400759 em->mod_start = em->start;
760 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500761
762 em->block_start = ins.objectid;
763 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500764 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400765 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500766 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800767 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500768 set_bit(EXTENT_FLAG_PINNED, &em->flags);
769 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400770 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500771
Chris Masond3977122009-01-05 21:25:51 -0500772 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400773 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400774 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400775 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500776 if (ret != -EEXIST) {
777 free_extent_map(em);
778 break;
779 }
780 btrfs_drop_extent_cache(inode, async_extent->start,
781 async_extent->start +
782 async_extent->ram_size - 1, 0);
783 }
784
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500785 if (ret)
786 goto out_free_reserve;
787
Li Zefan261507a02010-12-17 14:21:50 +0800788 ret = btrfs_add_ordered_extent_compress(inode,
789 async_extent->start,
790 ins.objectid,
791 async_extent->ram_size,
792 ins.offset,
793 BTRFS_ORDERED_COMPRESSED,
794 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100795 if (ret) {
796 btrfs_drop_extent_cache(inode, async_extent->start,
797 async_extent->start +
798 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500799 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100800 }
Chris Mason771ed682008-11-06 22:02:51 -0500801
Chris Mason771ed682008-11-06 22:02:51 -0500802 /*
803 * clear dirty, set writeback and unlock the pages.
804 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400805 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400806 async_extent->start +
807 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400808 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
809 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400810 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500811 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500812 async_extent->start,
813 async_extent->ram_size,
814 ins.objectid,
815 ins.offset, async_extent->pages,
816 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500817 alloc_hint = ins.objectid + ins.offset;
818 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500819 if (ret)
820 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500821 cond_resched();
822 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100823 ret = 0;
824out:
825 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500826out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800827 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100828out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400829 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500830 async_extent->start +
831 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400832 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400833 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
834 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100835 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
836 PAGE_SET_ERROR);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100837 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500838 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500839}
840
Josef Bacik4b46fce2010-05-23 11:00:55 -0400841static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
842 u64 num_bytes)
843{
844 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
845 struct extent_map *em;
846 u64 alloc_hint = 0;
847
848 read_lock(&em_tree->lock);
849 em = search_extent_mapping(em_tree, start, num_bytes);
850 if (em) {
851 /*
852 * if block start isn't an actual block number then find the
853 * first block in this inode and use that as a hint. If that
854 * block is also bogus then just don't worry about it.
855 */
856 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
857 free_extent_map(em);
858 em = search_extent_mapping(em_tree, 0, 0);
859 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
860 alloc_hint = em->block_start;
861 if (em)
862 free_extent_map(em);
863 } else {
864 alloc_hint = em->block_start;
865 free_extent_map(em);
866 }
867 }
868 read_unlock(&em_tree->lock);
869
870 return alloc_hint;
871}
872
Chris Mason771ed682008-11-06 22:02:51 -0500873/*
874 * when extent_io.c finds a delayed allocation range in the file,
875 * the call backs end up in this code. The basic idea is to
876 * allocate extents on disk for the range, and create ordered data structs
877 * in ram to track those extents.
878 *
879 * locked_page is the page that writepage had locked already. We use
880 * it to make sure we don't do extra locks or unlocks.
881 *
882 * *page_started is set to one if we unlock locked_page and do everything
883 * required to start IO on it. It may be clean and already done with
884 * IO when we return.
885 */
Josef Bacik00361582013-08-14 14:02:47 -0400886static noinline int cow_file_range(struct inode *inode,
887 struct page *locked_page,
888 u64 start, u64 end, int *page_started,
889 unsigned long *nr_written,
890 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500891{
Josef Bacik00361582013-08-14 14:02:47 -0400892 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500893 u64 alloc_hint = 0;
894 u64 num_bytes;
895 unsigned long ram_size;
896 u64 disk_num_bytes;
897 u64 cur_alloc_size;
898 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500899 struct btrfs_key ins;
900 struct extent_map *em;
901 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
902 int ret = 0;
903
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400904 if (btrfs_is_free_space_inode(inode)) {
905 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500906 ret = -EINVAL;
907 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400908 }
Chris Mason771ed682008-11-06 22:02:51 -0500909
Qu Wenruofda28322013-02-26 08:10:22 +0000910 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500911 num_bytes = max(blocksize, num_bytes);
912 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500913
Chris Mason4cb53002011-05-24 15:35:30 -0400914 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400915 if (num_bytes < 64 * 1024 &&
916 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400917 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400918
Chris Mason771ed682008-11-06 22:02:51 -0500919 if (start == 0) {
920 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400921 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
922 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500923 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400924 extent_clear_unlock_delalloc(inode, start, end, NULL,
925 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400926 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400927 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
928 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000929
Chris Mason771ed682008-11-06 22:02:51 -0500930 *nr_written = *nr_written +
931 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
932 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500933 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100934 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100935 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500936 }
937 }
Chris Masonc8b97812008-10-29 14:49:59 -0400938
939 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200940 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400941
Josef Bacik4b46fce2010-05-23 11:00:55 -0400942 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400943 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400944
Chris Masond3977122009-01-05 21:25:51 -0500945 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400946 unsigned long op;
947
Josef Bacik287a0ab2010-03-19 18:07:23 +0000948 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400949 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500950 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800951 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400952 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100953 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500954
David Sterba172ddd62011-04-21 00:48:27 +0200955 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000956 if (!em) {
957 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000958 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000959 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400960 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500961 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500962 ram_size = ins.offset;
963 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400964 em->mod_start = em->start;
965 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400966
Chris Masone6dcd2d2008-07-17 12:53:50 -0400967 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400968 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500969 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400970 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400971 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400972 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400973 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400974
Chris Masond3977122009-01-05 21:25:51 -0500975 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400976 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400977 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400978 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400979 if (ret != -EEXIST) {
980 free_extent_map(em);
981 break;
982 }
983 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400984 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400985 }
Liu Boace68ba2013-04-22 10:53:47 +0000986 if (ret)
987 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400988
Chris Mason98d20f62008-04-14 09:46:10 -0400989 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400990 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500991 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000992 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +0100993 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -0400994
Yan Zheng17d217f2008-12-12 10:03:38 -0500995 if (root->root_key.objectid ==
996 BTRFS_DATA_RELOC_TREE_OBJECTID) {
997 ret = btrfs_reloc_clone_csums(inode, start,
998 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -0400999 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001000 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001001 }
1002
Chris Masond3977122009-01-05 21:25:51 -05001003 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001004 break;
Chris Masond3977122009-01-05 21:25:51 -05001005
Chris Masonc8b97812008-10-29 14:49:59 -04001006 /* we're not doing compressed IO, don't unlock the first
1007 * page (which the caller expects to stay locked), don't
1008 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001009 *
1010 * Do set the Private2 bit so we know this page was properly
1011 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001012 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001013 op = unlock ? PAGE_UNLOCK : 0;
1014 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001015
Josef Bacikc2790a22013-07-29 11:20:47 -04001016 extent_clear_unlock_delalloc(inode, start,
1017 start + ram_size - 1, locked_page,
1018 EXTENT_LOCKED | EXTENT_DELALLOC,
1019 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001020 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001021 num_bytes -= cur_alloc_size;
1022 alloc_hint = ins.objectid + ins.offset;
1023 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -04001024 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001025out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001026 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001027
Filipe Mananad9f85962014-08-25 10:43:00 +01001028out_drop_extent_cache:
1029 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001030out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001031 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001032out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001033 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001034 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1035 EXTENT_DELALLOC | EXTENT_DEFRAG,
1036 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1037 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001038 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001039}
Chris Masonc8b97812008-10-29 14:49:59 -04001040
Chris Mason771ed682008-11-06 22:02:51 -05001041/*
1042 * work queue call back to started compression on a file and pages
1043 */
1044static noinline void async_cow_start(struct btrfs_work *work)
1045{
1046 struct async_cow *async_cow;
1047 int num_added = 0;
1048 async_cow = container_of(work, struct async_cow, work);
1049
1050 compress_file_range(async_cow->inode, async_cow->locked_page,
1051 async_cow->start, async_cow->end, async_cow,
1052 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001053 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001054 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001055 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001056 }
Chris Mason771ed682008-11-06 22:02:51 -05001057}
1058
1059/*
1060 * work queue call back to submit previously compressed pages
1061 */
1062static noinline void async_cow_submit(struct btrfs_work *work)
1063{
1064 struct async_cow *async_cow;
1065 struct btrfs_root *root;
1066 unsigned long nr_pages;
1067
1068 async_cow = container_of(work, struct async_cow, work);
1069
1070 root = async_cow->root;
1071 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1072 PAGE_CACHE_SHIFT;
1073
Josef Bacik66657b32012-08-01 15:36:24 -04001074 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001075 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001076 waitqueue_active(&root->fs_info->async_submit_wait))
1077 wake_up(&root->fs_info->async_submit_wait);
1078
Chris Masond3977122009-01-05 21:25:51 -05001079 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001080 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001081}
Chris Masonc8b97812008-10-29 14:49:59 -04001082
Chris Mason771ed682008-11-06 22:02:51 -05001083static noinline void async_cow_free(struct btrfs_work *work)
1084{
1085 struct async_cow *async_cow;
1086 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001087 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001088 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001089 kfree(async_cow);
1090}
1091
1092static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1093 u64 start, u64 end, int *page_started,
1094 unsigned long *nr_written)
1095{
1096 struct async_cow *async_cow;
1097 struct btrfs_root *root = BTRFS_I(inode)->root;
1098 unsigned long nr_pages;
1099 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001100 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001101
Chris Masona3429ab2009-10-08 12:30:20 -04001102 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1103 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001104 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001105 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001106 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001107 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001108 async_cow->root = root;
1109 async_cow->locked_page = locked_page;
1110 async_cow->start = start;
1111
Wang Shilongf79707b2014-07-17 11:44:09 +08001112 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1113 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001114 cur_end = end;
1115 else
1116 cur_end = min(end, start + 512 * 1024 - 1);
1117
1118 async_cow->end = cur_end;
1119 INIT_LIST_HEAD(&async_cow->extents);
1120
Liu Bo9e0af232014-08-15 23:36:53 +08001121 btrfs_init_work(&async_cow->work,
1122 btrfs_delalloc_helper,
1123 async_cow_start, async_cow_submit,
1124 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001125
Chris Mason771ed682008-11-06 22:02:51 -05001126 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1127 PAGE_CACHE_SHIFT;
1128 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1129
Qu Wenruoafe3d242014-02-28 10:46:07 +08001130 btrfs_queue_work(root->fs_info->delalloc_workers,
1131 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001132
1133 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1134 wait_event(root->fs_info->async_submit_wait,
1135 (atomic_read(&root->fs_info->async_delalloc_pages) <
1136 limit));
1137 }
1138
Chris Masond3977122009-01-05 21:25:51 -05001139 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001140 atomic_read(&root->fs_info->async_delalloc_pages)) {
1141 wait_event(root->fs_info->async_submit_wait,
1142 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1143 0));
1144 }
1145
1146 *nr_written += nr_pages;
1147 start = cur_end + 1;
1148 }
1149 *page_started = 1;
1150 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001151}
1152
Chris Masond3977122009-01-05 21:25:51 -05001153static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001154 u64 bytenr, u64 num_bytes)
1155{
1156 int ret;
1157 struct btrfs_ordered_sum *sums;
1158 LIST_HEAD(list);
1159
Yan Zheng07d400a2009-01-06 11:42:00 -05001160 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001161 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001162 if (ret == 0 && list_empty(&list))
1163 return 0;
1164
1165 while (!list_empty(&list)) {
1166 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1167 list_del(&sums->list);
1168 kfree(sums);
1169 }
1170 return 1;
1171}
1172
Chris Masond352ac62008-09-29 15:18:18 -04001173/*
1174 * when nowcow writeback call back. This checks for snapshots or COW copies
1175 * of the extents that exist in the file, and COWs the file as required.
1176 *
1177 * If no cow copies or snapshots exist, we write directly to the existing
1178 * blocks on disk
1179 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001180static noinline int run_delalloc_nocow(struct inode *inode,
1181 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001182 u64 start, u64 end, int *page_started, int force,
1183 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001184{
Chris Masonbe20aa92007-12-17 20:14:01 -05001185 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001186 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001187 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001188 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001189 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001190 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001191 u64 cow_start;
1192 u64 cur_offset;
1193 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001194 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001195 u64 disk_bytenr;
1196 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001197 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001198 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001199 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001200 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001201 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001202 int nocow;
1203 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001204 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001205 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001206
1207 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001208 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001209 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1210 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001211 EXTENT_DO_ACCOUNTING |
1212 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001213 PAGE_CLEAR_DIRTY |
1214 PAGE_SET_WRITEBACK |
1215 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001216 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001217 }
Li Zefan82d59022011-04-20 10:33:24 +08001218
Liu Bo83eea1f2012-07-10 05:28:39 -06001219 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001220
1221 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001222 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001223 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001224 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001225
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001226 if (IS_ERR(trans)) {
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);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001234 btrfs_free_path(path);
1235 return PTR_ERR(trans);
1236 }
1237
Josef Bacik74b21072011-04-13 12:02:53 -04001238 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001239
Yan Zheng80ff3852008-10-30 14:20:02 -04001240 cow_start = (u64)-1;
1241 cur_offset = start;
1242 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001243 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001244 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001245 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001246 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001247 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1248 leaf = path->nodes[0];
1249 btrfs_item_key_to_cpu(leaf, &found_key,
1250 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001251 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001252 found_key.type == BTRFS_EXTENT_DATA_KEY)
1253 path->slots[0]--;
1254 }
1255 check_prev = 0;
1256next_slot:
1257 leaf = path->nodes[0];
1258 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1259 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001260 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001261 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 if (ret > 0)
1263 break;
1264 leaf = path->nodes[0];
1265 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001266
Yan Zheng80ff3852008-10-30 14:20:02 -04001267 nocow = 0;
1268 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001269 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001271
Li Zefan33345d012011-04-20 10:31:50 +08001272 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001273 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1274 found_key.offset > end)
1275 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001276
Yan Zheng80ff3852008-10-30 14:20:02 -04001277 if (found_key.offset > cur_offset) {
1278 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001279 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 goto out_check;
1281 }
Chris Masonc31f8832008-01-08 15:46:31 -05001282
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 fi = btrfs_item_ptr(leaf, path->slots[0],
1284 struct btrfs_file_extent_item);
1285 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001286
Josef Bacikcc95bef2013-04-04 14:31:27 -04001287 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001288 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1289 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001291 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 extent_end = found_key.offset +
1293 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001294 disk_num_bytes =
1295 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 if (extent_end <= start) {
1297 path->slots[0]++;
1298 goto next_slot;
1299 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001300 if (disk_bytenr == 0)
1301 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001302 if (btrfs_file_extent_compression(leaf, fi) ||
1303 btrfs_file_extent_encryption(leaf, fi) ||
1304 btrfs_file_extent_other_encoding(leaf, fi))
1305 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001306 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1307 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001308 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001310 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001311 found_key.offset -
1312 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001313 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001314 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001315 disk_bytenr += cur_offset - found_key.offset;
1316 num_bytes = min(end + 1, extent_end) - cur_offset;
1317 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001318 * if there are pending snapshots for this root,
1319 * we fall into common COW way.
1320 */
1321 if (!nolock) {
1322 err = btrfs_start_nocow_write(root);
1323 if (!err)
1324 goto out_check;
1325 }
1326 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001327 * force cow if csum exists in the range.
1328 * this ensure that csum for a given extent are
1329 * either valid or do not exist.
1330 */
1331 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1332 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001333 nocow = 1;
1334 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1335 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001336 btrfs_file_extent_inline_len(leaf,
1337 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 extent_end = ALIGN(extent_end, root->sectorsize);
1339 } else {
1340 BUG_ON(1);
1341 }
1342out_check:
1343 if (extent_end <= start) {
1344 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001345 if (!nolock && nocow)
1346 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 goto next_slot;
1348 }
1349 if (!nocow) {
1350 if (cow_start == (u64)-1)
1351 cow_start = cur_offset;
1352 cur_offset = extent_end;
1353 if (cur_offset > end)
1354 break;
1355 path->slots[0]++;
1356 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001357 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001358
David Sterbab3b4aa72011-04-21 01:20:15 +02001359 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001361 ret = cow_file_range(inode, locked_page,
1362 cow_start, found_key.offset - 1,
1363 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001364 if (ret) {
1365 if (!nolock && nocow)
1366 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001367 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001368 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001369 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001370 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001371
Yan Zhengd899e052008-10-30 14:25:28 -04001372 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1373 struct extent_map *em;
1374 struct extent_map_tree *em_tree;
1375 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001376 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001377 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001378 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001379 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001380 em->len = num_bytes;
1381 em->block_len = num_bytes;
1382 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001383 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001384 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001385 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001386 em->mod_start = em->start;
1387 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001388 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001389 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001390 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001391 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001392 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001393 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001394 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001395 if (ret != -EEXIST) {
1396 free_extent_map(em);
1397 break;
1398 }
1399 btrfs_drop_extent_cache(inode, em->start,
1400 em->start + em->len - 1, 0);
1401 }
1402 type = BTRFS_ORDERED_PREALLOC;
1403 } else {
1404 type = BTRFS_ORDERED_NOCOW;
1405 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001406
1407 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001408 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001409 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001410
Yan, Zhengefa56462010-05-16 10:49:59 -04001411 if (root->root_key.objectid ==
1412 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1413 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1414 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001415 if (ret) {
1416 if (!nolock && nocow)
1417 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001418 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001419 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001420 }
1421
Josef Bacikc2790a22013-07-29 11:20:47 -04001422 extent_clear_unlock_delalloc(inode, cur_offset,
1423 cur_offset + num_bytes - 1,
1424 locked_page, EXTENT_LOCKED |
1425 EXTENT_DELALLOC, PAGE_UNLOCK |
1426 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001427 if (!nolock && nocow)
1428 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001429 cur_offset = extent_end;
1430 if (cur_offset > end)
1431 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001432 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001433 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001434
Josef Bacik17ca04a2012-05-31 15:58:55 -04001435 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001436 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001437 cur_offset = end;
1438 }
1439
Yan Zheng80ff3852008-10-30 14:20:02 -04001440 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001441 ret = cow_file_range(inode, locked_page, cow_start, end,
1442 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001443 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001444 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001445 }
1446
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001447error:
Miao Xiea698d0752012-09-20 01:51:59 -06001448 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001449 if (!ret)
1450 ret = err;
1451
Josef Bacik17ca04a2012-05-31 15:58:55 -04001452 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001453 extent_clear_unlock_delalloc(inode, cur_offset, end,
1454 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001455 EXTENT_DELALLOC | EXTENT_DEFRAG |
1456 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1457 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001458 PAGE_SET_WRITEBACK |
1459 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001460 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001461 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001462}
1463
Wang Shilong47059d92014-07-03 18:22:07 +08001464static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1465{
1466
1467 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1468 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1469 return 0;
1470
1471 /*
1472 * @defrag_bytes is a hint value, no spinlock held here,
1473 * if is not zero, it means the file is defragging.
1474 * Force cow if given extent needs to be defragged.
1475 */
1476 if (BTRFS_I(inode)->defrag_bytes &&
1477 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1478 EXTENT_DEFRAG, 0, NULL))
1479 return 1;
1480
1481 return 0;
1482}
1483
Chris Masond352ac62008-09-29 15:18:18 -04001484/*
1485 * extent_io.c call back to do delayed allocation processing
1486 */
Chris Masonc8b97812008-10-29 14:49:59 -04001487static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001488 u64 start, u64 end, int *page_started,
1489 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001490{
Chris Masonbe20aa92007-12-17 20:14:01 -05001491 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001492 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001493
Wang Shilong47059d92014-07-03 18:22:07 +08001494 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001495 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001496 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001497 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001498 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001499 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001500 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001501 ret = cow_file_range(inode, locked_page, start, end,
1502 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001503 } else {
1504 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1505 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001506 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001507 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001508 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001509 return ret;
1510}
1511
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001512static void btrfs_split_extent_hook(struct inode *inode,
1513 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001514{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001515 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001516 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001517 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001518
Josef Bacik9e0baf62011-07-15 15:16:44 +00001519 spin_lock(&BTRFS_I(inode)->lock);
1520 BTRFS_I(inode)->outstanding_extents++;
1521 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001522}
1523
1524/*
1525 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1526 * extents so we can keep track of new extents that are just merged onto old
1527 * extents, such as when we are doing sequential writes, so we can properly
1528 * account for the metadata space we'll need.
1529 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001530static void btrfs_merge_extent_hook(struct inode *inode,
1531 struct extent_state *new,
1532 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001533{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001534 /* not delalloc, ignore it */
1535 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001536 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001537
Josef Bacik9e0baf62011-07-15 15:16:44 +00001538 spin_lock(&BTRFS_I(inode)->lock);
1539 BTRFS_I(inode)->outstanding_extents--;
1540 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001541}
1542
Miao Xieeb73c1b2013-05-15 07:48:22 +00001543static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1544 struct inode *inode)
1545{
1546 spin_lock(&root->delalloc_lock);
1547 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1548 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1549 &root->delalloc_inodes);
1550 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1551 &BTRFS_I(inode)->runtime_flags);
1552 root->nr_delalloc_inodes++;
1553 if (root->nr_delalloc_inodes == 1) {
1554 spin_lock(&root->fs_info->delalloc_root_lock);
1555 BUG_ON(!list_empty(&root->delalloc_root));
1556 list_add_tail(&root->delalloc_root,
1557 &root->fs_info->delalloc_roots);
1558 spin_unlock(&root->fs_info->delalloc_root_lock);
1559 }
1560 }
1561 spin_unlock(&root->delalloc_lock);
1562}
1563
1564static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1565 struct inode *inode)
1566{
1567 spin_lock(&root->delalloc_lock);
1568 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1569 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1570 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1571 &BTRFS_I(inode)->runtime_flags);
1572 root->nr_delalloc_inodes--;
1573 if (!root->nr_delalloc_inodes) {
1574 spin_lock(&root->fs_info->delalloc_root_lock);
1575 BUG_ON(list_empty(&root->delalloc_root));
1576 list_del_init(&root->delalloc_root);
1577 spin_unlock(&root->fs_info->delalloc_root_lock);
1578 }
1579 }
1580 spin_unlock(&root->delalloc_lock);
1581}
1582
Chris Masond352ac62008-09-29 15:18:18 -04001583/*
1584 * extent_io.c set_bit_hook, used to track delayed allocation
1585 * bytes in this file, and to maintain the list of inodes that
1586 * have pending delalloc work to be done.
1587 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001588static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001589 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001590{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001591
Wang Shilong47059d92014-07-03 18:22:07 +08001592 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1593 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001594 /*
1595 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001596 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001597 * bit, which is only set or cleared with irqs on
1598 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001599 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001600 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001601 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001602 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001603
Josef Bacik9e0baf62011-07-15 15:16:44 +00001604 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001605 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001606 } else {
1607 spin_lock(&BTRFS_I(inode)->lock);
1608 BTRFS_I(inode)->outstanding_extents++;
1609 spin_unlock(&BTRFS_I(inode)->lock);
1610 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001611
Miao Xie963d6782013-01-29 10:10:51 +00001612 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1613 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001614 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001615 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001616 if (*bits & EXTENT_DEFRAG)
1617 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001618 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001619 &BTRFS_I(inode)->runtime_flags))
1620 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001621 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001622 }
Chris Mason291d6732008-01-29 15:55:23 -05001623}
1624
Chris Masond352ac62008-09-29 15:18:18 -04001625/*
1626 * extent_io.c clear_bit_hook, see set_bit_hook for why
1627 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001628static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001629 struct extent_state *state,
1630 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001631{
Wang Shilong47059d92014-07-03 18:22:07 +08001632 u64 len = state->end + 1 - state->start;
1633
1634 spin_lock(&BTRFS_I(inode)->lock);
1635 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1636 BTRFS_I(inode)->defrag_bytes -= len;
1637 spin_unlock(&BTRFS_I(inode)->lock);
1638
Chris Mason75eff682008-12-15 15:54:40 -05001639 /*
1640 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001641 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001642 * bit, which is only set or cleared with irqs on
1643 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001644 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001645 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001646 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001647
Josef Bacik9e0baf62011-07-15 15:16:44 +00001648 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001649 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001650 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1651 spin_lock(&BTRFS_I(inode)->lock);
1652 BTRFS_I(inode)->outstanding_extents--;
1653 spin_unlock(&BTRFS_I(inode)->lock);
1654 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001655
Josef Bacikb6d08f02013-09-27 14:57:43 -04001656 /*
1657 * We don't reserve metadata space for space cache inodes so we
1658 * don't need to call dellalloc_release_metadata if there is an
1659 * error.
1660 */
1661 if (*bits & EXTENT_DO_ACCOUNTING &&
1662 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001663 btrfs_delalloc_release_metadata(inode, len);
1664
Josef Bacik0cb59c92010-07-02 12:14:14 -04001665 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001666 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001667 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001668
Miao Xie963d6782013-01-29 10:10:51 +00001669 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1670 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001671 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001672 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001673 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001674 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001675 &BTRFS_I(inode)->runtime_flags))
1676 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001677 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001678 }
Chris Mason291d6732008-01-29 15:55:23 -05001679}
1680
Chris Masond352ac62008-09-29 15:18:18 -04001681/*
1682 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1683 * we don't create bios that span stripes or chunks
1684 */
David Woodhouse64a16702009-07-15 23:29:37 +01001685int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001686 size_t size, struct bio *bio,
1687 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001688{
1689 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001690 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001691 u64 length = 0;
1692 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001693 int ret;
1694
Chris Mason771ed682008-11-06 22:02:51 -05001695 if (bio_flags & EXTENT_BIO_COMPRESSED)
1696 return 0;
1697
Kent Overstreet4f024f32013-10-11 15:44:27 -07001698 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001699 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001700 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001701 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001702 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001703 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001704 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001705 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001706 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001707}
1708
Chris Masond352ac62008-09-29 15:18:18 -04001709/*
1710 * in order to insert checksums into the metadata in large chunks,
1711 * we wait until bio submission time. All the pages in the bio are
1712 * checksummed and sums are attached onto the ordered extent record.
1713 *
1714 * At IO completion time the cums attached on the ordered extent record
1715 * are inserted into the btree
1716 */
Chris Masond3977122009-01-05 21:25:51 -05001717static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1718 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001719 unsigned long bio_flags,
1720 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001721{
Chris Mason065631f2008-02-20 12:07:25 -05001722 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001723 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001724
Chris Masond20f7042008-12-08 16:58:54 -05001725 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001726 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001727 return 0;
1728}
Chris Masone0156402008-04-16 11:15:20 -04001729
Chris Mason4a69a412008-11-06 22:03:00 -05001730/*
1731 * in order to insert checksums into the metadata in large chunks,
1732 * we wait until bio submission time. All the pages in the bio are
1733 * checksummed and sums are attached onto the ordered extent record.
1734 *
1735 * At IO completion time the cums attached on the ordered extent record
1736 * are inserted into the btree
1737 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001738static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001739 int mirror_num, unsigned long bio_flags,
1740 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001741{
1742 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001743 int ret;
1744
1745 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1746 if (ret)
1747 bio_endio(bio, ret);
1748 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001749}
1750
Chris Masond352ac62008-09-29 15:18:18 -04001751/*
Chris Masoncad321a2008-12-17 14:51:42 -05001752 * extent_io.c submission hook. This does the right thing for csum calculation
1753 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001754 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001755static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001756 int mirror_num, unsigned long bio_flags,
1757 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001758{
1759 struct btrfs_root *root = BTRFS_I(inode)->root;
1760 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001761 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001762 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001763 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001764
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001765 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001766
Liu Bo83eea1f2012-07-10 05:28:39 -06001767 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001768 metadata = 2;
1769
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001770 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001771 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1772 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001773 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001774
Chris Masond20f7042008-12-08 16:58:54 -05001775 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001776 ret = btrfs_submit_compressed_read(inode, bio,
1777 mirror_num,
1778 bio_flags);
1779 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001780 } else if (!skip_sum) {
1781 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1782 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001783 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001784 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001785 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001786 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001787 /* csum items have already been cloned */
1788 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1789 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001790 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001791 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001792 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001793 bio_flags, bio_offset,
1794 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001795 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001796 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001797 } else if (!skip_sum) {
1798 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1799 if (ret)
1800 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001801 }
1802
Chris Mason0b86a832008-03-24 15:01:56 -04001803mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001804 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1805
1806out:
1807 if (ret < 0)
1808 bio_endio(bio, ret);
1809 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001810}
Chris Mason6885f302008-02-20 16:11:05 -05001811
Chris Masond352ac62008-09-29 15:18:18 -04001812/*
1813 * given a list of ordered sums record them in the inode. This happens
1814 * at IO completion time based on sums calculated at bio submission time.
1815 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001816static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001817 struct inode *inode, u64 file_offset,
1818 struct list_head *list)
1819{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001820 struct btrfs_ordered_sum *sum;
1821
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001822 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001823 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001824 btrfs_csum_file_blocks(trans,
1825 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001826 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001827 }
1828 return 0;
1829}
1830
Josef Bacik2ac55d42010-02-03 19:33:23 +00001831int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1832 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001833{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001834 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001835 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001836 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001837}
1838
Chris Masond352ac62008-09-29 15:18:18 -04001839/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001840struct btrfs_writepage_fixup {
1841 struct page *page;
1842 struct btrfs_work work;
1843};
1844
Christoph Hellwigb2950862008-12-02 09:54:17 -05001845static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001846{
1847 struct btrfs_writepage_fixup *fixup;
1848 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001849 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001850 struct page *page;
1851 struct inode *inode;
1852 u64 page_start;
1853 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001854 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001855
1856 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1857 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001858again:
Chris Mason247e7432008-07-17 12:53:51 -04001859 lock_page(page);
1860 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1861 ClearPageChecked(page);
1862 goto out_page;
1863 }
1864
1865 inode = page->mapping->host;
1866 page_start = page_offset(page);
1867 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1868
Josef Bacik2ac55d42010-02-03 19:33:23 +00001869 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001870 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001871
1872 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001873 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001874 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001875
1876 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1877 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001878 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1879 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001880 unlock_page(page);
1881 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001882 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001883 goto again;
1884 }
Chris Mason247e7432008-07-17 12:53:51 -04001885
Jeff Mahoney87826df2012-02-15 16:23:57 +01001886 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1887 if (ret) {
1888 mapping_set_error(page->mapping, ret);
1889 end_extent_writepage(page, ret, page_start, page_end);
1890 ClearPageChecked(page);
1891 goto out;
1892 }
1893
Josef Bacik2ac55d42010-02-03 19:33:23 +00001894 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001895 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001896 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001897out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001898 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1899 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001900out_page:
1901 unlock_page(page);
1902 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001903 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001904}
1905
1906/*
1907 * There are a few paths in the higher layers of the kernel that directly
1908 * set the page dirty bit without asking the filesystem if it is a
1909 * good idea. This causes problems because we want to make sure COW
1910 * properly happens and the data=ordered rules are followed.
1911 *
Chris Masonc8b97812008-10-29 14:49:59 -04001912 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001913 * hasn't been properly setup for IO. We kick off an async process
1914 * to fix it up. The async helper will wait for ordered extents, set
1915 * the delalloc bit and make it safe to write the page.
1916 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001917static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001918{
1919 struct inode *inode = page->mapping->host;
1920 struct btrfs_writepage_fixup *fixup;
1921 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001922
Chris Mason8b62b722009-09-02 16:53:46 -04001923 /* this page is properly in the ordered list */
1924 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001925 return 0;
1926
1927 if (PageChecked(page))
1928 return -EAGAIN;
1929
1930 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1931 if (!fixup)
1932 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001933
Chris Mason247e7432008-07-17 12:53:51 -04001934 SetPageChecked(page);
1935 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08001936 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
1937 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04001938 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08001939 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001940 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001941}
1942
Yan Zhengd899e052008-10-30 14:25:28 -04001943static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1944 struct inode *inode, u64 file_pos,
1945 u64 disk_bytenr, u64 disk_num_bytes,
1946 u64 num_bytes, u64 ram_bytes,
1947 u8 compression, u8 encryption,
1948 u16 other_encoding, int extent_type)
1949{
1950 struct btrfs_root *root = BTRFS_I(inode)->root;
1951 struct btrfs_file_extent_item *fi;
1952 struct btrfs_path *path;
1953 struct extent_buffer *leaf;
1954 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001955 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001956 int ret;
1957
1958 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001959 if (!path)
1960 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001961
Chris Masona1ed8352009-09-11 12:27:37 -04001962 /*
1963 * we may be replacing one extent in the tree with another.
1964 * The new extent is pinned in the extent map, and we don't want
1965 * to drop it from the cache until it is completely in the btree.
1966 *
1967 * So, tell btrfs_drop_extents to leave this extent in the cache.
1968 * the caller is expected to unpin it and allow it to be merged
1969 * with the others.
1970 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001971 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1972 file_pos + num_bytes, NULL, 0,
1973 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001974 if (ret)
1975 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001976
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001977 if (!extent_inserted) {
1978 ins.objectid = btrfs_ino(inode);
1979 ins.offset = file_pos;
1980 ins.type = BTRFS_EXTENT_DATA_KEY;
1981
1982 path->leave_spinning = 1;
1983 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1984 sizeof(*fi));
1985 if (ret)
1986 goto out;
1987 }
Yan Zhengd899e052008-10-30 14:25:28 -04001988 leaf = path->nodes[0];
1989 fi = btrfs_item_ptr(leaf, path->slots[0],
1990 struct btrfs_file_extent_item);
1991 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1992 btrfs_set_file_extent_type(leaf, fi, extent_type);
1993 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1994 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1995 btrfs_set_file_extent_offset(leaf, fi, 0);
1996 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1997 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1998 btrfs_set_file_extent_compression(leaf, fi, compression);
1999 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2000 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002001
Yan Zhengd899e052008-10-30 14:25:28 -04002002 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002003 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002004
2005 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002006
2007 ins.objectid = disk_bytenr;
2008 ins.offset = disk_num_bytes;
2009 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002010 ret = btrfs_alloc_reserved_file_extent(trans, root,
2011 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002012 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002013out:
Yan Zhengd899e052008-10-30 14:25:28 -04002014 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002015
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002016 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002017}
2018
Liu Bo38c227d2013-01-29 03:18:40 +00002019/* snapshot-aware defrag */
2020struct sa_defrag_extent_backref {
2021 struct rb_node node;
2022 struct old_sa_defrag_extent *old;
2023 u64 root_id;
2024 u64 inum;
2025 u64 file_pos;
2026 u64 extent_offset;
2027 u64 num_bytes;
2028 u64 generation;
2029};
2030
2031struct old_sa_defrag_extent {
2032 struct list_head list;
2033 struct new_sa_defrag_extent *new;
2034
2035 u64 extent_offset;
2036 u64 bytenr;
2037 u64 offset;
2038 u64 len;
2039 int count;
2040};
2041
2042struct new_sa_defrag_extent {
2043 struct rb_root root;
2044 struct list_head head;
2045 struct btrfs_path *path;
2046 struct inode *inode;
2047 u64 file_pos;
2048 u64 len;
2049 u64 bytenr;
2050 u64 disk_len;
2051 u8 compress_type;
2052};
2053
2054static int backref_comp(struct sa_defrag_extent_backref *b1,
2055 struct sa_defrag_extent_backref *b2)
2056{
2057 if (b1->root_id < b2->root_id)
2058 return -1;
2059 else if (b1->root_id > b2->root_id)
2060 return 1;
2061
2062 if (b1->inum < b2->inum)
2063 return -1;
2064 else if (b1->inum > b2->inum)
2065 return 1;
2066
2067 if (b1->file_pos < b2->file_pos)
2068 return -1;
2069 else if (b1->file_pos > b2->file_pos)
2070 return 1;
2071
2072 /*
2073 * [------------------------------] ===> (a range of space)
2074 * |<--->| |<---->| =============> (fs/file tree A)
2075 * |<---------------------------->| ===> (fs/file tree B)
2076 *
2077 * A range of space can refer to two file extents in one tree while
2078 * refer to only one file extent in another tree.
2079 *
2080 * So we may process a disk offset more than one time(two extents in A)
2081 * and locate at the same extent(one extent in B), then insert two same
2082 * backrefs(both refer to the extent in B).
2083 */
2084 return 0;
2085}
2086
2087static void backref_insert(struct rb_root *root,
2088 struct sa_defrag_extent_backref *backref)
2089{
2090 struct rb_node **p = &root->rb_node;
2091 struct rb_node *parent = NULL;
2092 struct sa_defrag_extent_backref *entry;
2093 int ret;
2094
2095 while (*p) {
2096 parent = *p;
2097 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2098
2099 ret = backref_comp(backref, entry);
2100 if (ret < 0)
2101 p = &(*p)->rb_left;
2102 else
2103 p = &(*p)->rb_right;
2104 }
2105
2106 rb_link_node(&backref->node, parent, p);
2107 rb_insert_color(&backref->node, root);
2108}
2109
2110/*
2111 * Note the backref might has changed, and in this case we just return 0.
2112 */
2113static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2114 void *ctx)
2115{
2116 struct btrfs_file_extent_item *extent;
2117 struct btrfs_fs_info *fs_info;
2118 struct old_sa_defrag_extent *old = ctx;
2119 struct new_sa_defrag_extent *new = old->new;
2120 struct btrfs_path *path = new->path;
2121 struct btrfs_key key;
2122 struct btrfs_root *root;
2123 struct sa_defrag_extent_backref *backref;
2124 struct extent_buffer *leaf;
2125 struct inode *inode = new->inode;
2126 int slot;
2127 int ret;
2128 u64 extent_offset;
2129 u64 num_bytes;
2130
2131 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2132 inum == btrfs_ino(inode))
2133 return 0;
2134
2135 key.objectid = root_id;
2136 key.type = BTRFS_ROOT_ITEM_KEY;
2137 key.offset = (u64)-1;
2138
2139 fs_info = BTRFS_I(inode)->root->fs_info;
2140 root = btrfs_read_fs_root_no_name(fs_info, &key);
2141 if (IS_ERR(root)) {
2142 if (PTR_ERR(root) == -ENOENT)
2143 return 0;
2144 WARN_ON(1);
2145 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2146 inum, offset, root_id);
2147 return PTR_ERR(root);
2148 }
2149
2150 key.objectid = inum;
2151 key.type = BTRFS_EXTENT_DATA_KEY;
2152 if (offset > (u64)-1 << 32)
2153 key.offset = 0;
2154 else
2155 key.offset = offset;
2156
2157 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302158 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002159 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002160 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002161
2162 while (1) {
2163 cond_resched();
2164
2165 leaf = path->nodes[0];
2166 slot = path->slots[0];
2167
2168 if (slot >= btrfs_header_nritems(leaf)) {
2169 ret = btrfs_next_leaf(root, path);
2170 if (ret < 0) {
2171 goto out;
2172 } else if (ret > 0) {
2173 ret = 0;
2174 goto out;
2175 }
2176 continue;
2177 }
2178
2179 path->slots[0]++;
2180
2181 btrfs_item_key_to_cpu(leaf, &key, slot);
2182
2183 if (key.objectid > inum)
2184 goto out;
2185
2186 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2187 continue;
2188
2189 extent = btrfs_item_ptr(leaf, slot,
2190 struct btrfs_file_extent_item);
2191
2192 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2193 continue;
2194
Liu Boe68afa42013-07-01 22:13:26 +08002195 /*
2196 * 'offset' refers to the exact key.offset,
2197 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2198 * (key.offset - extent_offset).
2199 */
2200 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002201 continue;
2202
Liu Boe68afa42013-07-01 22:13:26 +08002203 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002204 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002205
Liu Bo38c227d2013-01-29 03:18:40 +00002206 if (extent_offset >= old->extent_offset + old->offset +
2207 old->len || extent_offset + num_bytes <=
2208 old->extent_offset + old->offset)
2209 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002210 break;
2211 }
2212
2213 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2214 if (!backref) {
2215 ret = -ENOENT;
2216 goto out;
2217 }
2218
2219 backref->root_id = root_id;
2220 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002221 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002222 backref->num_bytes = num_bytes;
2223 backref->extent_offset = extent_offset;
2224 backref->generation = btrfs_file_extent_generation(leaf, extent);
2225 backref->old = old;
2226 backref_insert(&new->root, backref);
2227 old->count++;
2228out:
2229 btrfs_release_path(path);
2230 WARN_ON(ret);
2231 return ret;
2232}
2233
2234static noinline bool record_extent_backrefs(struct btrfs_path *path,
2235 struct new_sa_defrag_extent *new)
2236{
2237 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2238 struct old_sa_defrag_extent *old, *tmp;
2239 int ret;
2240
2241 new->path = path;
2242
2243 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002244 ret = iterate_inodes_from_logical(old->bytenr +
2245 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002246 path, record_one_backref,
2247 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002248 if (ret < 0 && ret != -ENOENT)
2249 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002250
2251 /* no backref to be processed for this extent */
2252 if (!old->count) {
2253 list_del(&old->list);
2254 kfree(old);
2255 }
2256 }
2257
2258 if (list_empty(&new->head))
2259 return false;
2260
2261 return true;
2262}
2263
2264static int relink_is_mergable(struct extent_buffer *leaf,
2265 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002266 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002267{
Liu Bo116e0022013-08-02 16:30:40 +08002268 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002269 return 0;
2270
2271 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2272 return 0;
2273
Liu Bo116e0022013-08-02 16:30:40 +08002274 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2275 return 0;
2276
2277 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002278 btrfs_file_extent_other_encoding(leaf, fi))
2279 return 0;
2280
2281 return 1;
2282}
2283
2284/*
2285 * Note the backref might has changed, and in this case we just return 0.
2286 */
2287static noinline int relink_extent_backref(struct btrfs_path *path,
2288 struct sa_defrag_extent_backref *prev,
2289 struct sa_defrag_extent_backref *backref)
2290{
2291 struct btrfs_file_extent_item *extent;
2292 struct btrfs_file_extent_item *item;
2293 struct btrfs_ordered_extent *ordered;
2294 struct btrfs_trans_handle *trans;
2295 struct btrfs_fs_info *fs_info;
2296 struct btrfs_root *root;
2297 struct btrfs_key key;
2298 struct extent_buffer *leaf;
2299 struct old_sa_defrag_extent *old = backref->old;
2300 struct new_sa_defrag_extent *new = old->new;
2301 struct inode *src_inode = new->inode;
2302 struct inode *inode;
2303 struct extent_state *cached = NULL;
2304 int ret = 0;
2305 u64 start;
2306 u64 len;
2307 u64 lock_start;
2308 u64 lock_end;
2309 bool merge = false;
2310 int index;
2311
2312 if (prev && prev->root_id == backref->root_id &&
2313 prev->inum == backref->inum &&
2314 prev->file_pos + prev->num_bytes == backref->file_pos)
2315 merge = true;
2316
2317 /* step 1: get root */
2318 key.objectid = backref->root_id;
2319 key.type = BTRFS_ROOT_ITEM_KEY;
2320 key.offset = (u64)-1;
2321
2322 fs_info = BTRFS_I(src_inode)->root->fs_info;
2323 index = srcu_read_lock(&fs_info->subvol_srcu);
2324
2325 root = btrfs_read_fs_root_no_name(fs_info, &key);
2326 if (IS_ERR(root)) {
2327 srcu_read_unlock(&fs_info->subvol_srcu, index);
2328 if (PTR_ERR(root) == -ENOENT)
2329 return 0;
2330 return PTR_ERR(root);
2331 }
Liu Bo38c227d2013-01-29 03:18:40 +00002332
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002333 if (btrfs_root_readonly(root)) {
2334 srcu_read_unlock(&fs_info->subvol_srcu, index);
2335 return 0;
2336 }
2337
Liu Bo38c227d2013-01-29 03:18:40 +00002338 /* step 2: get inode */
2339 key.objectid = backref->inum;
2340 key.type = BTRFS_INODE_ITEM_KEY;
2341 key.offset = 0;
2342
2343 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2344 if (IS_ERR(inode)) {
2345 srcu_read_unlock(&fs_info->subvol_srcu, index);
2346 return 0;
2347 }
2348
2349 srcu_read_unlock(&fs_info->subvol_srcu, index);
2350
2351 /* step 3: relink backref */
2352 lock_start = backref->file_pos;
2353 lock_end = backref->file_pos + backref->num_bytes - 1;
2354 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2355 0, &cached);
2356
2357 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2358 if (ordered) {
2359 btrfs_put_ordered_extent(ordered);
2360 goto out_unlock;
2361 }
2362
2363 trans = btrfs_join_transaction(root);
2364 if (IS_ERR(trans)) {
2365 ret = PTR_ERR(trans);
2366 goto out_unlock;
2367 }
2368
2369 key.objectid = backref->inum;
2370 key.type = BTRFS_EXTENT_DATA_KEY;
2371 key.offset = backref->file_pos;
2372
2373 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2374 if (ret < 0) {
2375 goto out_free_path;
2376 } else if (ret > 0) {
2377 ret = 0;
2378 goto out_free_path;
2379 }
2380
2381 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2382 struct btrfs_file_extent_item);
2383
2384 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2385 backref->generation)
2386 goto out_free_path;
2387
2388 btrfs_release_path(path);
2389
2390 start = backref->file_pos;
2391 if (backref->extent_offset < old->extent_offset + old->offset)
2392 start += old->extent_offset + old->offset -
2393 backref->extent_offset;
2394
2395 len = min(backref->extent_offset + backref->num_bytes,
2396 old->extent_offset + old->offset + old->len);
2397 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2398
2399 ret = btrfs_drop_extents(trans, root, inode, start,
2400 start + len, 1);
2401 if (ret)
2402 goto out_free_path;
2403again:
2404 key.objectid = btrfs_ino(inode);
2405 key.type = BTRFS_EXTENT_DATA_KEY;
2406 key.offset = start;
2407
Liu Boa09a0a72013-03-11 09:20:58 +00002408 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002409 if (merge) {
2410 struct btrfs_file_extent_item *fi;
2411 u64 extent_len;
2412 struct btrfs_key found_key;
2413
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002414 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002415 if (ret < 0)
2416 goto out_free_path;
2417
2418 path->slots[0]--;
2419 leaf = path->nodes[0];
2420 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2421
2422 fi = btrfs_item_ptr(leaf, path->slots[0],
2423 struct btrfs_file_extent_item);
2424 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2425
Liu Bo116e0022013-08-02 16:30:40 +08002426 if (extent_len + found_key.offset == start &&
2427 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002428 btrfs_set_file_extent_num_bytes(leaf, fi,
2429 extent_len + len);
2430 btrfs_mark_buffer_dirty(leaf);
2431 inode_add_bytes(inode, len);
2432
2433 ret = 1;
2434 goto out_free_path;
2435 } else {
2436 merge = false;
2437 btrfs_release_path(path);
2438 goto again;
2439 }
2440 }
2441
2442 ret = btrfs_insert_empty_item(trans, root, path, &key,
2443 sizeof(*extent));
2444 if (ret) {
2445 btrfs_abort_transaction(trans, root, ret);
2446 goto out_free_path;
2447 }
2448
2449 leaf = path->nodes[0];
2450 item = btrfs_item_ptr(leaf, path->slots[0],
2451 struct btrfs_file_extent_item);
2452 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2453 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2454 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2455 btrfs_set_file_extent_num_bytes(leaf, item, len);
2456 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2457 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2458 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2459 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2460 btrfs_set_file_extent_encryption(leaf, item, 0);
2461 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2462
2463 btrfs_mark_buffer_dirty(leaf);
2464 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002465 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002466
2467 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2468 new->disk_len, 0,
2469 backref->root_id, backref->inum,
2470 new->file_pos, 0); /* start - extent_offset */
2471 if (ret) {
2472 btrfs_abort_transaction(trans, root, ret);
2473 goto out_free_path;
2474 }
2475
2476 ret = 1;
2477out_free_path:
2478 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002479 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002480 btrfs_end_transaction(trans, root);
2481out_unlock:
2482 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2483 &cached, GFP_NOFS);
2484 iput(inode);
2485 return ret;
2486}
2487
Liu Bo6f519562013-10-29 10:45:05 +08002488static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2489{
2490 struct old_sa_defrag_extent *old, *tmp;
2491
2492 if (!new)
2493 return;
2494
2495 list_for_each_entry_safe(old, tmp, &new->head, list) {
2496 list_del(&old->list);
2497 kfree(old);
2498 }
2499 kfree(new);
2500}
2501
Liu Bo38c227d2013-01-29 03:18:40 +00002502static void relink_file_extents(struct new_sa_defrag_extent *new)
2503{
2504 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002505 struct sa_defrag_extent_backref *backref;
2506 struct sa_defrag_extent_backref *prev = NULL;
2507 struct inode *inode;
2508 struct btrfs_root *root;
2509 struct rb_node *node;
2510 int ret;
2511
2512 inode = new->inode;
2513 root = BTRFS_I(inode)->root;
2514
2515 path = btrfs_alloc_path();
2516 if (!path)
2517 return;
2518
2519 if (!record_extent_backrefs(path, new)) {
2520 btrfs_free_path(path);
2521 goto out;
2522 }
2523 btrfs_release_path(path);
2524
2525 while (1) {
2526 node = rb_first(&new->root);
2527 if (!node)
2528 break;
2529 rb_erase(node, &new->root);
2530
2531 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2532
2533 ret = relink_extent_backref(path, prev, backref);
2534 WARN_ON(ret < 0);
2535
2536 kfree(prev);
2537
2538 if (ret == 1)
2539 prev = backref;
2540 else
2541 prev = NULL;
2542 cond_resched();
2543 }
2544 kfree(prev);
2545
2546 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002547out:
Liu Bo6f519562013-10-29 10:45:05 +08002548 free_sa_defrag_extent(new);
2549
Liu Bo38c227d2013-01-29 03:18:40 +00002550 atomic_dec(&root->fs_info->defrag_running);
2551 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002552}
2553
2554static struct new_sa_defrag_extent *
2555record_old_file_extents(struct inode *inode,
2556 struct btrfs_ordered_extent *ordered)
2557{
2558 struct btrfs_root *root = BTRFS_I(inode)->root;
2559 struct btrfs_path *path;
2560 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002561 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002562 struct new_sa_defrag_extent *new;
2563 int ret;
2564
2565 new = kmalloc(sizeof(*new), GFP_NOFS);
2566 if (!new)
2567 return NULL;
2568
2569 new->inode = inode;
2570 new->file_pos = ordered->file_offset;
2571 new->len = ordered->len;
2572 new->bytenr = ordered->start;
2573 new->disk_len = ordered->disk_len;
2574 new->compress_type = ordered->compress_type;
2575 new->root = RB_ROOT;
2576 INIT_LIST_HEAD(&new->head);
2577
2578 path = btrfs_alloc_path();
2579 if (!path)
2580 goto out_kfree;
2581
2582 key.objectid = btrfs_ino(inode);
2583 key.type = BTRFS_EXTENT_DATA_KEY;
2584 key.offset = new->file_pos;
2585
2586 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2587 if (ret < 0)
2588 goto out_free_path;
2589 if (ret > 0 && path->slots[0] > 0)
2590 path->slots[0]--;
2591
2592 /* find out all the old extents for the file range */
2593 while (1) {
2594 struct btrfs_file_extent_item *extent;
2595 struct extent_buffer *l;
2596 int slot;
2597 u64 num_bytes;
2598 u64 offset;
2599 u64 end;
2600 u64 disk_bytenr;
2601 u64 extent_offset;
2602
2603 l = path->nodes[0];
2604 slot = path->slots[0];
2605
2606 if (slot >= btrfs_header_nritems(l)) {
2607 ret = btrfs_next_leaf(root, path);
2608 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002609 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002610 else if (ret > 0)
2611 break;
2612 continue;
2613 }
2614
2615 btrfs_item_key_to_cpu(l, &key, slot);
2616
2617 if (key.objectid != btrfs_ino(inode))
2618 break;
2619 if (key.type != BTRFS_EXTENT_DATA_KEY)
2620 break;
2621 if (key.offset >= new->file_pos + new->len)
2622 break;
2623
2624 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2625
2626 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2627 if (key.offset + num_bytes < new->file_pos)
2628 goto next;
2629
2630 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2631 if (!disk_bytenr)
2632 goto next;
2633
2634 extent_offset = btrfs_file_extent_offset(l, extent);
2635
2636 old = kmalloc(sizeof(*old), GFP_NOFS);
2637 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002638 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002639
2640 offset = max(new->file_pos, key.offset);
2641 end = min(new->file_pos + new->len, key.offset + num_bytes);
2642
2643 old->bytenr = disk_bytenr;
2644 old->extent_offset = extent_offset;
2645 old->offset = offset - key.offset;
2646 old->len = end - offset;
2647 old->new = new;
2648 old->count = 0;
2649 list_add_tail(&old->list, &new->head);
2650next:
2651 path->slots[0]++;
2652 cond_resched();
2653 }
2654
2655 btrfs_free_path(path);
2656 atomic_inc(&root->fs_info->defrag_running);
2657
2658 return new;
2659
Liu Bo38c227d2013-01-29 03:18:40 +00002660out_free_path:
2661 btrfs_free_path(path);
2662out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002663 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002664 return NULL;
2665}
2666
Miao Xiee570fd22014-06-19 10:42:50 +08002667static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2668 u64 start, u64 len)
2669{
2670 struct btrfs_block_group_cache *cache;
2671
2672 cache = btrfs_lookup_block_group(root->fs_info, start);
2673 ASSERT(cache);
2674
2675 spin_lock(&cache->lock);
2676 cache->delalloc_bytes -= len;
2677 spin_unlock(&cache->lock);
2678
2679 btrfs_put_block_group(cache);
2680}
2681
Chris Masond352ac62008-09-29 15:18:18 -04002682/* as ordered data IO finishes, this gets called so we can finish
2683 * an ordered extent if the range of bytes in the file it covers are
2684 * fully written.
2685 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002686static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002687{
Josef Bacik5fd02042012-05-02 14:00:54 -04002688 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002689 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002690 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002691 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002692 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002693 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002694 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002695 int ret = 0;
2696 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002697 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002698 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002699
Liu Bo83eea1f2012-07-10 05:28:39 -06002700 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002701
Josef Bacik5fd02042012-05-02 14:00:54 -04002702 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2703 ret = -EIO;
2704 goto out;
2705 }
2706
Miao Xief6124962014-09-12 18:44:04 +08002707 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2708 ordered_extent->file_offset +
2709 ordered_extent->len - 1);
2710
Josef Bacik77cef2e2013-08-29 13:57:21 -04002711 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2712 truncated = true;
2713 logical_len = ordered_extent->truncated_len;
2714 /* Truncated the entire extent, don't bother adding */
2715 if (!logical_len)
2716 goto out;
2717 }
2718
Yan, Zhengc2167752009-11-12 09:34:21 +00002719 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002720 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002721 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2722 if (nolock)
2723 trans = btrfs_join_transaction_nolock(root);
2724 else
2725 trans = btrfs_join_transaction(root);
2726 if (IS_ERR(trans)) {
2727 ret = PTR_ERR(trans);
2728 trans = NULL;
2729 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002730 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002731 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2732 ret = btrfs_update_inode_fallback(trans, root, inode);
2733 if (ret) /* -ENOMEM or corruption */
2734 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002735 goto out;
2736 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002737
Josef Bacik2ac55d42010-02-03 19:33:23 +00002738 lock_extent_bits(io_tree, ordered_extent->file_offset,
2739 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002740 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002741
Liu Bo38c227d2013-01-29 03:18:40 +00002742 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2743 ordered_extent->file_offset + ordered_extent->len - 1,
2744 EXTENT_DEFRAG, 1, cached_state);
2745 if (ret) {
2746 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002747 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002748 /* the inode is shared */
2749 new = record_old_file_extents(inode, ordered_extent);
2750
2751 clear_extent_bit(io_tree, ordered_extent->file_offset,
2752 ordered_extent->file_offset + ordered_extent->len - 1,
2753 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2754 }
2755
Josef Bacik0cb59c92010-07-02 12:14:14 -04002756 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002757 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002758 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002759 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002760 if (IS_ERR(trans)) {
2761 ret = PTR_ERR(trans);
2762 trans = NULL;
2763 goto out_unlock;
2764 }
Chris Masona79b7d42014-05-22 16:18:52 -07002765
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002766 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002767
Chris Masonc8b97812008-10-29 14:49:59 -04002768 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002769 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002770 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002771 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002772 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002773 ordered_extent->file_offset,
2774 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002775 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002776 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002777 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002778 ret = insert_reserved_file_extent(trans, inode,
2779 ordered_extent->file_offset,
2780 ordered_extent->start,
2781 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002782 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002783 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002784 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002785 if (!ret)
2786 btrfs_release_delalloc_bytes(root,
2787 ordered_extent->start,
2788 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002789 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002790 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2791 ordered_extent->file_offset, ordered_extent->len,
2792 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002793 if (ret < 0) {
2794 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002795 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002796 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002797
Chris Masone6dcd2d2008-07-17 12:53:50 -04002798 add_pending_csums(trans, inode, ordered_extent->file_offset,
2799 &ordered_extent->list);
2800
Josef Bacik6c760c02012-11-09 10:53:21 -05002801 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2802 ret = btrfs_update_inode_fallback(trans, root, inode);
2803 if (ret) { /* -ENOMEM or corruption */
2804 btrfs_abort_transaction(trans, root, ret);
2805 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002806 }
2807 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002808out_unlock:
2809 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2810 ordered_extent->file_offset +
2811 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002812out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002813 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002814 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002815 if (trans)
2816 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002817
Josef Bacik77cef2e2013-08-29 13:57:21 -04002818 if (ret || truncated) {
2819 u64 start, end;
2820
2821 if (truncated)
2822 start = ordered_extent->file_offset + logical_len;
2823 else
2824 start = ordered_extent->file_offset;
2825 end = ordered_extent->file_offset + ordered_extent->len - 1;
2826 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2827
2828 /* Drop the cache for the part of the extent we didn't write. */
2829 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002830
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002831 /*
2832 * If the ordered extent had an IOERR or something else went
2833 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002834 * back to the allocator. We only free the extent in the
2835 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002836 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002837 if ((ret || !logical_len) &&
2838 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002839 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2840 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002841 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002842 }
2843
2844
Josef Bacik5fd02042012-05-02 14:00:54 -04002845 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002846 * This needs to be done to make sure anybody waiting knows we are done
2847 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002848 */
2849 btrfs_remove_ordered_extent(inode, ordered_extent);
2850
Liu Bo38c227d2013-01-29 03:18:40 +00002851 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002852 if (new) {
2853 if (ret) {
2854 free_sa_defrag_extent(new);
2855 atomic_dec(&root->fs_info->defrag_running);
2856 } else {
2857 relink_file_extents(new);
2858 }
2859 }
Liu Bo38c227d2013-01-29 03:18:40 +00002860
Chris Masone6dcd2d2008-07-17 12:53:50 -04002861 /* once for us */
2862 btrfs_put_ordered_extent(ordered_extent);
2863 /* once for the tree */
2864 btrfs_put_ordered_extent(ordered_extent);
2865
Josef Bacik5fd02042012-05-02 14:00:54 -04002866 return ret;
2867}
2868
2869static void finish_ordered_fn(struct btrfs_work *work)
2870{
2871 struct btrfs_ordered_extent *ordered_extent;
2872 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2873 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002874}
2875
Christoph Hellwigb2950862008-12-02 09:54:17 -05002876static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002877 struct extent_state *state, int uptodate)
2878{
Josef Bacik5fd02042012-05-02 14:00:54 -04002879 struct inode *inode = page->mapping->host;
2880 struct btrfs_root *root = BTRFS_I(inode)->root;
2881 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002882 struct btrfs_workqueue *wq;
2883 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002884
liubo1abe9b82011-03-24 11:18:59 +00002885 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2886
Chris Mason8b62b722009-09-02 16:53:46 -04002887 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002888 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2889 end - start + 1, uptodate))
2890 return 0;
2891
Liu Bo9e0af232014-08-15 23:36:53 +08002892 if (btrfs_is_free_space_inode(inode)) {
2893 wq = root->fs_info->endio_freespace_worker;
2894 func = btrfs_freespace_write_helper;
2895 } else {
2896 wq = root->fs_info->endio_write_workers;
2897 func = btrfs_endio_write_helper;
2898 }
Josef Bacik5fd02042012-05-02 14:00:54 -04002899
Liu Bo9e0af232014-08-15 23:36:53 +08002900 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
2901 NULL);
2902 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002903
2904 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002905}
2906
Miao Xiedc380ae2014-09-12 18:43:55 +08002907static int __readpage_endio_check(struct inode *inode,
2908 struct btrfs_io_bio *io_bio,
2909 int icsum, struct page *page,
2910 int pgoff, u64 start, size_t len)
2911{
2912 char *kaddr;
2913 u32 csum_expected;
2914 u32 csum = ~(u32)0;
2915 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2916 DEFAULT_RATELIMIT_BURST);
2917
2918 csum_expected = *(((u32 *)io_bio->csum) + icsum);
2919
2920 kaddr = kmap_atomic(page);
2921 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
2922 btrfs_csum_final(csum, (char *)&csum);
2923 if (csum != csum_expected)
2924 goto zeroit;
2925
2926 kunmap_atomic(kaddr);
2927 return 0;
2928zeroit:
2929 if (__ratelimit(&_rs))
2930 btrfs_info(BTRFS_I(inode)->root->fs_info,
2931 "csum failed ino %llu off %llu csum %u expected csum %u",
2932 btrfs_ino(inode), start, csum, csum_expected);
2933 memset(kaddr + pgoff, 1, len);
2934 flush_dcache_page(page);
2935 kunmap_atomic(kaddr);
2936 if (csum_expected == 0)
2937 return 0;
2938 return -EIO;
2939}
2940
Chris Masond352ac62008-09-29 15:18:18 -04002941/*
Chris Masond352ac62008-09-29 15:18:18 -04002942 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002943 * if there's a match, we allow the bio to finish. If not, the code in
2944 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002945 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002946static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2947 u64 phy_offset, struct page *page,
2948 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002949{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002950 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002951 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002952 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002953 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002954
Chris Masond20f7042008-12-08 16:58:54 -05002955 if (PageChecked(page)) {
2956 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002957 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002958 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002959
2960 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002961 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002962
Yan Zheng17d217f2008-12-12 10:03:38 -05002963 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002964 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002965 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2966 GFP_NOFS);
2967 return 0;
2968 }
2969
Miao Xiefacc8a222013-07-25 19:22:34 +08002970 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08002971 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
2972 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002973}
Chris Masonb888db2b2007-08-27 16:49:44 -04002974
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002975struct delayed_iput {
2976 struct list_head list;
2977 struct inode *inode;
2978};
2979
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002980/* JDM: If this is fs-wide, why can't we add a pointer to
2981 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002982void btrfs_add_delayed_iput(struct inode *inode)
2983{
2984 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2985 struct delayed_iput *delayed;
2986
2987 if (atomic_add_unless(&inode->i_count, -1, 1))
2988 return;
2989
2990 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2991 delayed->inode = inode;
2992
2993 spin_lock(&fs_info->delayed_iput_lock);
2994 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2995 spin_unlock(&fs_info->delayed_iput_lock);
2996}
2997
2998void btrfs_run_delayed_iputs(struct btrfs_root *root)
2999{
3000 LIST_HEAD(list);
3001 struct btrfs_fs_info *fs_info = root->fs_info;
3002 struct delayed_iput *delayed;
3003 int empty;
3004
3005 spin_lock(&fs_info->delayed_iput_lock);
3006 empty = list_empty(&fs_info->delayed_iputs);
3007 spin_unlock(&fs_info->delayed_iput_lock);
3008 if (empty)
3009 return;
3010
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003011 spin_lock(&fs_info->delayed_iput_lock);
3012 list_splice_init(&fs_info->delayed_iputs, &list);
3013 spin_unlock(&fs_info->delayed_iput_lock);
3014
3015 while (!list_empty(&list)) {
3016 delayed = list_entry(list.next, struct delayed_iput, list);
3017 list_del(&delayed->list);
3018 iput(delayed->inode);
3019 kfree(delayed);
3020 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003021}
3022
Yan, Zhengd68fc572010-05-16 10:49:58 -04003023/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003024 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003025 * files in the subvolume, it removes orphan item and frees block_rsv
3026 * structure.
3027 */
3028void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3029 struct btrfs_root *root)
3030{
Josef Bacik90290e12011-12-02 15:44:12 -05003031 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003032 int ret;
3033
Josef Bacik8a35d952012-05-23 14:26:42 -04003034 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003035 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3036 return;
3037
Josef Bacik90290e12011-12-02 15:44:12 -05003038 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003039 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003040 spin_unlock(&root->orphan_lock);
3041 return;
3042 }
3043
3044 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3045 spin_unlock(&root->orphan_lock);
3046 return;
3047 }
3048
3049 block_rsv = root->orphan_block_rsv;
3050 root->orphan_block_rsv = NULL;
3051 spin_unlock(&root->orphan_lock);
3052
Miao Xie27cdeb72014-04-02 19:51:05 +08003053 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003054 btrfs_root_refs(&root->root_item) > 0) {
3055 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3056 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003057 if (ret)
3058 btrfs_abort_transaction(trans, root, ret);
3059 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003060 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3061 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003062 }
3063
Josef Bacik90290e12011-12-02 15:44:12 -05003064 if (block_rsv) {
3065 WARN_ON(block_rsv->size > 0);
3066 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003067 }
3068}
3069
3070/*
Josef Bacik7b128762008-07-24 12:17:14 -04003071 * This creates an orphan entry for the given inode in case something goes
3072 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003073 *
3074 * NOTE: caller of this function should reserve 5 units of metadata for
3075 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003076 */
3077int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3078{
3079 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003080 struct btrfs_block_rsv *block_rsv = NULL;
3081 int reserve = 0;
3082 int insert = 0;
3083 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003084
Yan, Zhengd68fc572010-05-16 10:49:58 -04003085 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003086 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003087 if (!block_rsv)
3088 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003089 }
3090
Yan, Zhengd68fc572010-05-16 10:49:58 -04003091 spin_lock(&root->orphan_lock);
3092 if (!root->orphan_block_rsv) {
3093 root->orphan_block_rsv = block_rsv;
3094 } else if (block_rsv) {
3095 btrfs_free_block_rsv(root, block_rsv);
3096 block_rsv = NULL;
3097 }
Josef Bacik7b128762008-07-24 12:17:14 -04003098
Josef Bacik8a35d952012-05-23 14:26:42 -04003099 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3100 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003101#if 0
3102 /*
3103 * For proper ENOSPC handling, we should do orphan
3104 * cleanup when mounting. But this introduces backward
3105 * compatibility issue.
3106 */
3107 if (!xchg(&root->orphan_item_inserted, 1))
3108 insert = 2;
3109 else
3110 insert = 1;
3111#endif
3112 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003113 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003114 }
Josef Bacik7b128762008-07-24 12:17:14 -04003115
Josef Bacik72ac3c02012-05-23 14:13:11 -04003116 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3117 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003118 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003119 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003120
Yan, Zhengd68fc572010-05-16 10:49:58 -04003121 /* grab metadata reservation from transaction handle */
3122 if (reserve) {
3123 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003124 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003125 }
3126
3127 /* insert an orphan item to track this unlinked/truncated file */
3128 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003129 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003130 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003131 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003132 if (reserve) {
3133 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3134 &BTRFS_I(inode)->runtime_flags);
3135 btrfs_orphan_release_metadata(inode);
3136 }
3137 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003138 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3139 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003140 btrfs_abort_transaction(trans, root, ret);
3141 return ret;
3142 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003143 }
3144 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003145 }
3146
3147 /* insert an orphan item to track subvolume contains orphan files */
3148 if (insert >= 2) {
3149 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3150 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003151 if (ret && ret != -EEXIST) {
3152 btrfs_abort_transaction(trans, root, ret);
3153 return ret;
3154 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003155 }
3156 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003157}
3158
3159/*
3160 * We have done the truncate/delete so we can go ahead and remove the orphan
3161 * item for this particular inode.
3162 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003163static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3164 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003165{
3166 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003167 int delete_item = 0;
3168 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003169 int ret = 0;
3170
Yan, Zhengd68fc572010-05-16 10:49:58 -04003171 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003172 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3173 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003174 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003175
Josef Bacik72ac3c02012-05-23 14:13:11 -04003176 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3177 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003178 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003179 spin_unlock(&root->orphan_lock);
3180
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003181 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003182 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003183 if (trans)
3184 ret = btrfs_del_orphan_item(trans, root,
3185 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003186 }
Josef Bacik7b128762008-07-24 12:17:14 -04003187
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003188 if (release_rsv)
3189 btrfs_orphan_release_metadata(inode);
3190
Josef Bacik4ef31a42013-08-13 14:10:08 -04003191 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003192}
3193
3194/*
3195 * this cleans up any orphans that may be left on the list from the last use
3196 * of this root.
3197 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003198int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003199{
3200 struct btrfs_path *path;
3201 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003202 struct btrfs_key key, found_key;
3203 struct btrfs_trans_handle *trans;
3204 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003205 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003206 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3207
Yan, Zhengd68fc572010-05-16 10:49:58 -04003208 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003209 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003210
3211 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003212 if (!path) {
3213 ret = -ENOMEM;
3214 goto out;
3215 }
Josef Bacik7b128762008-07-24 12:17:14 -04003216 path->reada = -1;
3217
3218 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003219 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003220 key.offset = (u64)-1;
3221
Josef Bacik7b128762008-07-24 12:17:14 -04003222 while (1) {
3223 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003224 if (ret < 0)
3225 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003226
3227 /*
3228 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003229 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003230 * find the key and see if we have stuff that matches
3231 */
3232 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003233 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003234 if (path->slots[0] == 0)
3235 break;
3236 path->slots[0]--;
3237 }
3238
3239 /* pull out the item */
3240 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003241 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3242
3243 /* make sure the item matches what we want */
3244 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3245 break;
David Sterba962a2982014-06-04 18:41:45 +02003246 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003247 break;
3248
3249 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003250 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003251
3252 /*
3253 * this is where we are basically btrfs_lookup, without the
3254 * crossing root thing. we store the inode number in the
3255 * offset of the orphan item.
3256 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003257
3258 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003259 btrfs_err(root->fs_info,
3260 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003261 ret = -EINVAL;
3262 goto out;
3263 }
3264
3265 last_objectid = found_key.offset;
3266
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003267 found_key.objectid = found_key.offset;
3268 found_key.type = BTRFS_INODE_ITEM_KEY;
3269 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003270 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303271 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003272 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003273 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003274
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003275 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3276 struct btrfs_root *dead_root;
3277 struct btrfs_fs_info *fs_info = root->fs_info;
3278 int is_dead_root = 0;
3279
3280 /*
3281 * this is an orphan in the tree root. Currently these
3282 * could come from 2 sources:
3283 * a) a snapshot deletion in progress
3284 * b) a free space cache inode
3285 * We need to distinguish those two, as the snapshot
3286 * orphan must not get deleted.
3287 * find_dead_roots already ran before us, so if this
3288 * is a snapshot deletion, we should find the root
3289 * in the dead_roots list
3290 */
3291 spin_lock(&fs_info->trans_lock);
3292 list_for_each_entry(dead_root, &fs_info->dead_roots,
3293 root_list) {
3294 if (dead_root->root_key.objectid ==
3295 found_key.objectid) {
3296 is_dead_root = 1;
3297 break;
3298 }
3299 }
3300 spin_unlock(&fs_info->trans_lock);
3301 if (is_dead_root) {
3302 /* prevent this orphan from being found again */
3303 key.offset = found_key.objectid - 1;
3304 continue;
3305 }
3306 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003307 /*
3308 * Inode is already gone but the orphan item is still there,
3309 * kill the orphan item.
3310 */
3311 if (ret == -ESTALE) {
3312 trans = btrfs_start_transaction(root, 1);
3313 if (IS_ERR(trans)) {
3314 ret = PTR_ERR(trans);
3315 goto out;
3316 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003317 btrfs_debug(root->fs_info, "auto deleting %Lu",
3318 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003319 ret = btrfs_del_orphan_item(trans, root,
3320 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003321 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003322 if (ret)
3323 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003324 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003325 }
Josef Bacik7b128762008-07-24 12:17:14 -04003326
Josef Bacik7b128762008-07-24 12:17:14 -04003327 /*
3328 * add this inode to the orphan list so btrfs_orphan_del does
3329 * the proper thing when we hit it
3330 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003331 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3332 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003333 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003334
Josef Bacik7b128762008-07-24 12:17:14 -04003335 /* if we have links, this was a truncate, lets do that */
3336 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303337 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003338 iput(inode);
3339 continue;
3340 }
Josef Bacik7b128762008-07-24 12:17:14 -04003341 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003342
3343 /* 1 for the orphan item deletion. */
3344 trans = btrfs_start_transaction(root, 1);
3345 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003346 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003347 ret = PTR_ERR(trans);
3348 goto out;
3349 }
3350 ret = btrfs_orphan_add(trans, inode);
3351 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003352 if (ret) {
3353 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003354 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003355 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003356
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003357 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003358 if (ret)
3359 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003360 } else {
3361 nr_unlink++;
3362 }
3363
3364 /* this will do delete_inode and everything for us */
3365 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003366 if (ret)
3367 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003368 }
Miao Xie3254c872011-11-10 20:45:05 -05003369 /* release the path since we're done with it */
3370 btrfs_release_path(path);
3371
Yan, Zhengd68fc572010-05-16 10:49:58 -04003372 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3373
3374 if (root->orphan_block_rsv)
3375 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3376 (u64)-1);
3377
Miao Xie27cdeb72014-04-02 19:51:05 +08003378 if (root->orphan_block_rsv ||
3379 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003380 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003381 if (!IS_ERR(trans))
3382 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003383 }
Josef Bacik7b128762008-07-24 12:17:14 -04003384
3385 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003386 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003387 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003388 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003389
3390out:
3391 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003392 btrfs_crit(root->fs_info,
3393 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003394 btrfs_free_path(path);
3395 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003396}
3397
Chris Masond352ac62008-09-29 15:18:18 -04003398/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003399 * very simple check to peek ahead in the leaf looking for xattrs. If we
3400 * don't find any xattrs, we know there can't be any acls.
3401 *
3402 * slot is the slot the inode is in, objectid is the objectid of the inode
3403 */
3404static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003405 int slot, u64 objectid,
3406 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003407{
3408 u32 nritems = btrfs_header_nritems(leaf);
3409 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003410 static u64 xattr_access = 0;
3411 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003412 int scanned = 0;
3413
Josef Bacikf23b5a52013-06-19 10:16:26 -04003414 if (!xattr_access) {
3415 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3416 strlen(POSIX_ACL_XATTR_ACCESS));
3417 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3418 strlen(POSIX_ACL_XATTR_DEFAULT));
3419 }
3420
Chris Mason46a53cc2009-04-27 11:47:50 -04003421 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003422 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003423 while (slot < nritems) {
3424 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3425
3426 /* we found a different objectid, there must not be acls */
3427 if (found_key.objectid != objectid)
3428 return 0;
3429
3430 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003431 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003432 if (*first_xattr_slot == -1)
3433 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003434 if (found_key.offset == xattr_access ||
3435 found_key.offset == xattr_default)
3436 return 1;
3437 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003438
3439 /*
3440 * we found a key greater than an xattr key, there can't
3441 * be any acls later on
3442 */
3443 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3444 return 0;
3445
3446 slot++;
3447 scanned++;
3448
3449 /*
3450 * it goes inode, inode backrefs, xattrs, extents,
3451 * so if there are a ton of hard links to an inode there can
3452 * be a lot of backrefs. Don't waste time searching too hard,
3453 * this is just an optimization
3454 */
3455 if (scanned >= 8)
3456 break;
3457 }
3458 /* we hit the end of the leaf before we found an xattr or
3459 * something larger than an xattr. We have to assume the inode
3460 * has acls
3461 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003462 if (*first_xattr_slot == -1)
3463 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003464 return 1;
3465}
3466
3467/*
Chris Masond352ac62008-09-29 15:18:18 -04003468 * read an inode from the btree into the in-memory inode
3469 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003470static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003471{
3472 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003473 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003474 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003475 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003476 struct btrfs_root *root = BTRFS_I(inode)->root;
3477 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003478 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003479 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003480 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003481 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003482 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003483 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003484
3485 ret = btrfs_fill_inode(inode, &rdev);
3486 if (!ret)
3487 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003488
3489 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003490 if (!path)
3491 goto make_bad;
3492
Chris Mason39279cc2007-06-12 06:35:45 -04003493 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003494
Chris Mason39279cc2007-06-12 06:35:45 -04003495 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003496 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003497 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003498
Chris Mason5f39d392007-10-15 16:14:19 -04003499 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003500
3501 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003502 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003503
Chris Mason5f39d392007-10-15 16:14:19 -04003504 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3505 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003506 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003507 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003508 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3509 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003510 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003511
3512 tspec = btrfs_inode_atime(inode_item);
3513 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3514 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3515
3516 tspec = btrfs_inode_mtime(inode_item);
3517 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3518 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3519
3520 tspec = btrfs_inode_ctime(inode_item);
3521 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3522 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3523
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003524 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003525 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003526 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3527
3528 /*
3529 * If we were modified in the current generation and evicted from memory
3530 * and then re-read we need to do a full sync since we don't have any
3531 * idea about which extents were modified before we were evicted from
3532 * cache.
3533 */
3534 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3535 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3536 &BTRFS_I(inode)->runtime_flags);
3537
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003538 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003539 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003540 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003541 rdev = btrfs_inode_rdev(leaf, inode_item);
3542
Josef Bacikaec74772008-07-24 12:12:38 -04003543 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003544 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003545
3546cache_index:
3547 path->slots[0]++;
3548 if (inode->i_nlink != 1 ||
3549 path->slots[0] >= btrfs_header_nritems(leaf))
3550 goto cache_acl;
3551
3552 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3553 if (location.objectid != btrfs_ino(inode))
3554 goto cache_acl;
3555
3556 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3557 if (location.type == BTRFS_INODE_REF_KEY) {
3558 struct btrfs_inode_ref *ref;
3559
3560 ref = (struct btrfs_inode_ref *)ptr;
3561 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3562 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3563 struct btrfs_inode_extref *extref;
3564
3565 extref = (struct btrfs_inode_extref *)ptr;
3566 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3567 extref);
3568 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003569cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003570 /*
3571 * try to precache a NULL acl entry for files that don't have
3572 * any xattrs or acls
3573 */
Li Zefan33345d012011-04-20 10:31:50 +08003574 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003575 btrfs_ino(inode), &first_xattr_slot);
3576 if (first_xattr_slot != -1) {
3577 path->slots[0] = first_xattr_slot;
3578 ret = btrfs_load_inode_props(inode, path);
3579 if (ret)
3580 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003581 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003582 btrfs_ino(inode),
3583 root->root_key.objectid, ret);
3584 }
3585 btrfs_free_path(path);
3586
Al Viro72c04902009-06-24 16:58:48 -04003587 if (!maybe_acls)
3588 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003589
Chris Mason39279cc2007-06-12 06:35:45 -04003590 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003591 case S_IFREG:
3592 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003593 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003594 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003595 inode->i_fop = &btrfs_file_operations;
3596 inode->i_op = &btrfs_file_inode_operations;
3597 break;
3598 case S_IFDIR:
3599 inode->i_fop = &btrfs_dir_file_operations;
3600 if (root == root->fs_info->tree_root)
3601 inode->i_op = &btrfs_dir_ro_inode_operations;
3602 else
3603 inode->i_op = &btrfs_dir_inode_operations;
3604 break;
3605 case S_IFLNK:
3606 inode->i_op = &btrfs_symlink_inode_operations;
3607 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003608 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003609 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003610 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003611 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003612 init_special_inode(inode, inode->i_mode, rdev);
3613 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003614 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003615
3616 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003617 return;
3618
3619make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003620 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003621 make_bad_inode(inode);
3622}
3623
Chris Masond352ac62008-09-29 15:18:18 -04003624/*
3625 * given a leaf and an inode, copy the inode fields into the leaf
3626 */
Chris Masone02119d2008-09-05 16:13:11 -04003627static void fill_inode_item(struct btrfs_trans_handle *trans,
3628 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003629 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003630 struct inode *inode)
3631{
Liu Bo51fab692012-12-27 09:01:21 +00003632 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003633
Liu Bo51fab692012-12-27 09:01:21 +00003634 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003635
Liu Bo51fab692012-12-27 09:01:21 +00003636 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3637 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3638 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3639 &token);
3640 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3641 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003642
Liu Bo51fab692012-12-27 09:01:21 +00003643 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3644 inode->i_atime.tv_sec, &token);
3645 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3646 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003647
Liu Bo51fab692012-12-27 09:01:21 +00003648 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3649 inode->i_mtime.tv_sec, &token);
3650 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3651 inode->i_mtime.tv_nsec, &token);
3652
3653 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3654 inode->i_ctime.tv_sec, &token);
3655 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3656 inode->i_ctime.tv_nsec, &token);
3657
3658 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3659 &token);
3660 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3661 &token);
3662 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3663 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3664 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3665 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3666 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003667}
3668
Chris Masond352ac62008-09-29 15:18:18 -04003669/*
3670 * copy everything in the in-memory inode into the btree.
3671 */
Chris Mason21151332011-11-10 20:39:08 -05003672static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003673 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003674{
3675 struct btrfs_inode_item *inode_item;
3676 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003677 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003678 int ret;
3679
3680 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003681 if (!path)
3682 return -ENOMEM;
3683
Chris Masonb9473432009-03-13 11:00:37 -04003684 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003685 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3686 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003687 if (ret) {
3688 if (ret > 0)
3689 ret = -ENOENT;
3690 goto failed;
3691 }
3692
Chris Mason5f39d392007-10-15 16:14:19 -04003693 leaf = path->nodes[0];
3694 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003695 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003696
Chris Masone02119d2008-09-05 16:13:11 -04003697 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003698 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003699 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003700 ret = 0;
3701failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003702 btrfs_free_path(path);
3703 return ret;
3704}
3705
Chris Masond352ac62008-09-29 15:18:18 -04003706/*
Chris Mason21151332011-11-10 20:39:08 -05003707 * copy everything in the in-memory inode into the btree.
3708 */
3709noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3710 struct btrfs_root *root, struct inode *inode)
3711{
3712 int ret;
3713
3714 /*
3715 * If the inode is a free space inode, we can deadlock during commit
3716 * if we put it into the delayed code.
3717 *
3718 * The data relocation inode should also be directly updated
3719 * without delay
3720 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003721 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003722 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3723 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003724 btrfs_update_root_times(trans, root);
3725
Chris Mason21151332011-11-10 20:39:08 -05003726 ret = btrfs_delayed_update_inode(trans, root, inode);
3727 if (!ret)
3728 btrfs_set_inode_last_trans(trans, inode);
3729 return ret;
3730 }
3731
3732 return btrfs_update_inode_item(trans, root, inode);
3733}
3734
Josef Bacikbe6aef62012-10-22 15:43:12 -04003735noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3736 struct btrfs_root *root,
3737 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003738{
3739 int ret;
3740
3741 ret = btrfs_update_inode(trans, root, inode);
3742 if (ret == -ENOSPC)
3743 return btrfs_update_inode_item(trans, root, inode);
3744 return ret;
3745}
3746
3747/*
Chris Masond352ac62008-09-29 15:18:18 -04003748 * unlink helper that gets used here in inode.c and in the tree logging
3749 * recovery code. It remove a link in a directory with a given name, and
3750 * also drops the back refs in the inode to the directory
3751 */
Al Viro92986792011-03-04 17:14:37 +00003752static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3753 struct btrfs_root *root,
3754 struct inode *dir, struct inode *inode,
3755 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003756{
3757 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003758 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003759 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003760 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003761 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003762 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003763 u64 ino = btrfs_ino(inode);
3764 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003765
3766 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003767 if (!path) {
3768 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003769 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003770 }
3771
Chris Masonb9473432009-03-13 11:00:37 -04003772 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003773 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003774 name, name_len, -1);
3775 if (IS_ERR(di)) {
3776 ret = PTR_ERR(di);
3777 goto err;
3778 }
3779 if (!di) {
3780 ret = -ENOENT;
3781 goto err;
3782 }
Chris Mason5f39d392007-10-15 16:14:19 -04003783 leaf = path->nodes[0];
3784 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003785 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003786 if (ret)
3787 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003788 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003789
Miao Xie67de1172013-12-26 13:07:06 +08003790 /*
3791 * If we don't have dir index, we have to get it by looking up
3792 * the inode ref, since we get the inode ref, remove it directly,
3793 * it is unnecessary to do delayed deletion.
3794 *
3795 * But if we have dir index, needn't search inode ref to get it.
3796 * Since the inode ref is close to the inode item, it is better
3797 * that we delay to delete it, and just do this deletion when
3798 * we update the inode item.
3799 */
3800 if (BTRFS_I(inode)->dir_index) {
3801 ret = btrfs_delayed_delete_inode_ref(inode);
3802 if (!ret) {
3803 index = BTRFS_I(inode)->dir_index;
3804 goto skip_backref;
3805 }
3806 }
3807
Li Zefan33345d012011-04-20 10:31:50 +08003808 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3809 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003810 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003811 btrfs_info(root->fs_info,
3812 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003813 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003814 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003815 goto err;
3816 }
Miao Xie67de1172013-12-26 13:07:06 +08003817skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003818 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003819 if (ret) {
3820 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003821 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003822 }
Chris Mason39279cc2007-06-12 06:35:45 -04003823
Chris Masone02119d2008-09-05 16:13:11 -04003824 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003825 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003826 if (ret != 0 && ret != -ENOENT) {
3827 btrfs_abort_transaction(trans, root, ret);
3828 goto err;
3829 }
Chris Masone02119d2008-09-05 16:13:11 -04003830
3831 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3832 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003833 if (ret == -ENOENT)
3834 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003835 else if (ret)
3836 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003837err:
3838 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003839 if (ret)
3840 goto out;
3841
3842 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003843 inode_inc_iversion(inode);
3844 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003845 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003846 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003847out:
Chris Mason39279cc2007-06-12 06:35:45 -04003848 return ret;
3849}
3850
Al Viro92986792011-03-04 17:14:37 +00003851int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3852 struct btrfs_root *root,
3853 struct inode *dir, struct inode *inode,
3854 const char *name, int name_len)
3855{
3856 int ret;
3857 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3858 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003859 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003860 ret = btrfs_update_inode(trans, root, inode);
3861 }
3862 return ret;
3863}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003864
3865/*
3866 * helper to start transaction for unlink and rmdir.
3867 *
Josef Bacikd52be812013-05-29 14:54:47 -04003868 * unlink and rmdir are special in btrfs, they do not always free space, so
3869 * if we cannot make our reservations the normal way try and see if there is
3870 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3871 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003872 */
Josef Bacikd52be812013-05-29 14:54:47 -04003873static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003874{
3875 struct btrfs_trans_handle *trans;
3876 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003877 int ret;
3878
Josef Bacike70bea52011-10-11 14:18:24 -04003879 /*
3880 * 1 for the possible orphan item
3881 * 1 for the dir item
3882 * 1 for the dir index
3883 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003884 * 1 for the inode
3885 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003886 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003887 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3888 return trans;
3889
Josef Bacikd52be812013-05-29 14:54:47 -04003890 if (PTR_ERR(trans) == -ENOSPC) {
3891 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003892
Josef Bacikd52be812013-05-29 14:54:47 -04003893 trans = btrfs_start_transaction(root, 0);
3894 if (IS_ERR(trans))
3895 return trans;
3896 ret = btrfs_cond_migrate_bytes(root->fs_info,
3897 &root->fs_info->trans_block_rsv,
3898 num_bytes, 5);
3899 if (ret) {
3900 btrfs_end_transaction(trans, root);
3901 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003902 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003903 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003904 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003905 }
Josef Bacikd52be812013-05-29 14:54:47 -04003906 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003907}
3908
Chris Mason39279cc2007-06-12 06:35:45 -04003909static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3910{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003911 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003912 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003913 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003914 int ret;
3915
Josef Bacikd52be812013-05-29 14:54:47 -04003916 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003917 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003918 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003919
Chris Mason12fcfd22009-03-24 10:24:20 -04003920 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3921
Chris Masone02119d2008-09-05 16:13:11 -04003922 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3923 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003924 if (ret)
3925 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003926
Yan, Zhenga22285a2010-05-16 10:48:46 -04003927 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003928 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003929 if (ret)
3930 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003931 }
Josef Bacik7b128762008-07-24 12:17:14 -04003932
Tsutomu Itohb5324022011-07-19 07:27:20 +00003933out:
Josef Bacikd52be812013-05-29 14:54:47 -04003934 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003935 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003936 return ret;
3937}
3938
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003939int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3940 struct btrfs_root *root,
3941 struct inode *dir, u64 objectid,
3942 const char *name, int name_len)
3943{
3944 struct btrfs_path *path;
3945 struct extent_buffer *leaf;
3946 struct btrfs_dir_item *di;
3947 struct btrfs_key key;
3948 u64 index;
3949 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003950 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003951
3952 path = btrfs_alloc_path();
3953 if (!path)
3954 return -ENOMEM;
3955
Li Zefan33345d012011-04-20 10:31:50 +08003956 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003957 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003958 if (IS_ERR_OR_NULL(di)) {
3959 if (!di)
3960 ret = -ENOENT;
3961 else
3962 ret = PTR_ERR(di);
3963 goto out;
3964 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003965
3966 leaf = path->nodes[0];
3967 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3968 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3969 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003970 if (ret) {
3971 btrfs_abort_transaction(trans, root, ret);
3972 goto out;
3973 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003974 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003975
3976 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3977 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003978 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003979 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003980 if (ret != -ENOENT) {
3981 btrfs_abort_transaction(trans, root, ret);
3982 goto out;
3983 }
Li Zefan33345d012011-04-20 10:31:50 +08003984 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003985 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003986 if (IS_ERR_OR_NULL(di)) {
3987 if (!di)
3988 ret = -ENOENT;
3989 else
3990 ret = PTR_ERR(di);
3991 btrfs_abort_transaction(trans, root, ret);
3992 goto out;
3993 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003994
3995 leaf = path->nodes[0];
3996 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003997 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003998 index = key.offset;
3999 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004000 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004001
Miao Xie16cdcec2011-04-22 18:12:22 +08004002 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004003 if (ret) {
4004 btrfs_abort_transaction(trans, root, ret);
4005 goto out;
4006 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004007
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004008 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004009 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004010 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004011 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004012 if (ret)
4013 btrfs_abort_transaction(trans, root, ret);
4014out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004015 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004016 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004017}
4018
Chris Mason39279cc2007-06-12 06:35:45 -04004019static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4020{
4021 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004022 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004023 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004024 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004025
David Sterbab3ae2442012-09-13 16:04:34 -06004026 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004027 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004028 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4029 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004030
Josef Bacikd52be812013-05-29 14:54:47 -04004031 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004032 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004033 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004034
Li Zefan33345d012011-04-20 10:31:50 +08004035 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004036 err = btrfs_unlink_subvol(trans, root, dir,
4037 BTRFS_I(inode)->location.objectid,
4038 dentry->d_name.name,
4039 dentry->d_name.len);
4040 goto out;
4041 }
4042
Josef Bacik7b128762008-07-24 12:17:14 -04004043 err = btrfs_orphan_add(trans, inode);
4044 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004045 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004046
Chris Mason39279cc2007-06-12 06:35:45 -04004047 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004048 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4049 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004050 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004051 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004052out:
Josef Bacikd52be812013-05-29 14:54:47 -04004053 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004054 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004055
Chris Mason39279cc2007-06-12 06:35:45 -04004056 return err;
4057}
4058
Chris Mason323ac952008-10-01 19:05:46 -04004059/*
Chris Mason39279cc2007-06-12 06:35:45 -04004060 * this can truncate away extent items, csum items and directory items.
4061 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004062 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004063 *
4064 * csum items that cross the new i_size are truncated to the new size
4065 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004066 *
4067 * min_type is the minimum key type to truncate down to. If set to 0, this
4068 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004069 */
Yan, Zheng80825102009-11-12 09:35:36 +00004070int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4071 struct btrfs_root *root,
4072 struct inode *inode,
4073 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004074{
Chris Mason39279cc2007-06-12 06:35:45 -04004075 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004076 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004077 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004078 struct btrfs_key key;
4079 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004080 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004081 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004082 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004083 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004084 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004085 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004086 int found_extent;
4087 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004088 int pending_del_nr = 0;
4089 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004090 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004091 int ret;
4092 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004093 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004094
4095 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004096
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004097 path = btrfs_alloc_path();
4098 if (!path)
4099 return -ENOMEM;
4100 path->reada = -1;
4101
Josef Bacik5dc562c2012-08-17 13:14:17 -04004102 /*
4103 * We want to drop from the next block forward in case this new size is
4104 * not block aligned since we will be keeping the last block of the
4105 * extent just the way it is.
4106 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004107 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4108 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004109 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4110 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004111
Miao Xie16cdcec2011-04-22 18:12:22 +08004112 /*
4113 * This function is also used to drop the items in the log tree before
4114 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4115 * it is used to drop the loged items. So we shouldn't kill the delayed
4116 * items.
4117 */
4118 if (min_type == 0 && root == BTRFS_I(inode)->root)
4119 btrfs_kill_delayed_inode_items(inode);
4120
Li Zefan33345d012011-04-20 10:31:50 +08004121 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004122 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004123 key.type = (u8)-1;
4124
Chris Mason85e21ba2008-01-29 15:11:36 -05004125search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004126 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004127 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004128 if (ret < 0) {
4129 err = ret;
4130 goto out;
4131 }
Chris Masond3977122009-01-05 21:25:51 -05004132
Chris Mason85e21ba2008-01-29 15:11:36 -05004133 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004134 /* there are no items in the tree for us to truncate, we're
4135 * done
4136 */
Yan, Zheng80825102009-11-12 09:35:36 +00004137 if (path->slots[0] == 0)
4138 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004139 path->slots[0]--;
4140 }
4141
Chris Masond3977122009-01-05 21:25:51 -05004142 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004143 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004144 leaf = path->nodes[0];
4145 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004146 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004147
Li Zefan33345d012011-04-20 10:31:50 +08004148 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004149 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004150
Chris Mason85e21ba2008-01-29 15:11:36 -05004151 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004152 break;
4153
Chris Mason5f39d392007-10-15 16:14:19 -04004154 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004155 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004156 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004157 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004158 extent_type = btrfs_file_extent_type(leaf, fi);
4159 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004160 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004161 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004162 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004163 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004164 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004165 }
Yan008630c2007-11-07 13:31:09 -05004166 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004167 }
Yan, Zheng80825102009-11-12 09:35:36 +00004168 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004169 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004170 } else {
4171 if (item_end < new_size)
4172 break;
4173 if (found_key.offset >= new_size)
4174 del_item = 1;
4175 else
4176 del_item = 0;
4177 }
Chris Mason39279cc2007-06-12 06:35:45 -04004178 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004179 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004180 if (found_type != BTRFS_EXTENT_DATA_KEY)
4181 goto delete;
4182
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004183 if (del_item)
4184 last_size = found_key.offset;
4185 else
4186 last_size = new_size;
4187
Chris Mason179e29e2007-11-01 11:28:41 -04004188 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004189 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004190 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004191 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004192 u64 orig_num_bytes =
4193 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004194 extent_num_bytes = ALIGN(new_size -
4195 found_key.offset,
4196 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004197 btrfs_set_file_extent_num_bytes(leaf, fi,
4198 extent_num_bytes);
4199 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004200 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004201 if (test_bit(BTRFS_ROOT_REF_COWS,
4202 &root->state) &&
4203 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004204 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004205 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004206 } else {
Chris Masondb945352007-10-15 16:15:53 -04004207 extent_num_bytes =
4208 btrfs_file_extent_disk_num_bytes(leaf,
4209 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004210 extent_offset = found_key.offset -
4211 btrfs_file_extent_offset(leaf, fi);
4212
Chris Mason39279cc2007-06-12 06:35:45 -04004213 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004214 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004215 if (extent_start != 0) {
4216 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004217 if (test_bit(BTRFS_ROOT_REF_COWS,
4218 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004219 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004220 }
4221 }
Chris Mason90692182008-02-08 13:49:28 -05004222 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004223 /*
4224 * we can't truncate inline items that have had
4225 * special encodings
4226 */
4227 if (!del_item &&
4228 btrfs_file_extent_compression(leaf, fi) == 0 &&
4229 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4230 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004231 u32 size = new_size - found_key.offset;
4232
Miao Xie27cdeb72014-04-02 19:51:05 +08004233 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004234 inode_sub_bytes(inode, item_end + 1 -
4235 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004236
4237 /*
4238 * update the ram bytes to properly reflect
4239 * the new size of our item
4240 */
4241 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004242 size =
4243 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004244 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004245 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4246 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004247 inode_sub_bytes(inode, item_end + 1 -
4248 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004249 }
Chris Mason39279cc2007-06-12 06:35:45 -04004250 }
Chris Mason179e29e2007-11-01 11:28:41 -04004251delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004252 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004253 if (!pending_del_nr) {
4254 /* no pending yet, add ourselves */
4255 pending_del_slot = path->slots[0];
4256 pending_del_nr = 1;
4257 } else if (pending_del_nr &&
4258 path->slots[0] + 1 == pending_del_slot) {
4259 /* hop on the pending chunk */
4260 pending_del_nr++;
4261 pending_del_slot = path->slots[0];
4262 } else {
Chris Masond3977122009-01-05 21:25:51 -05004263 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004264 }
Chris Mason39279cc2007-06-12 06:35:45 -04004265 } else {
4266 break;
4267 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004268 if (found_extent &&
4269 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4270 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004271 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004272 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004273 extent_num_bytes, 0,
4274 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004275 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004276 BUG_ON(ret);
4277 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004278
Yan, Zheng80825102009-11-12 09:35:36 +00004279 if (found_type == BTRFS_INODE_ITEM_KEY)
4280 break;
4281
4282 if (path->slots[0] == 0 ||
4283 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004284 if (pending_del_nr) {
4285 ret = btrfs_del_items(trans, root, path,
4286 pending_del_slot,
4287 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004288 if (ret) {
4289 btrfs_abort_transaction(trans,
4290 root, ret);
4291 goto error;
4292 }
Yan, Zheng80825102009-11-12 09:35:36 +00004293 pending_del_nr = 0;
4294 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004295 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004296 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004297 } else {
4298 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004299 }
Chris Mason39279cc2007-06-12 06:35:45 -04004300 }
Yan, Zheng80825102009-11-12 09:35:36 +00004301out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004302 if (pending_del_nr) {
4303 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4304 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004305 if (ret)
4306 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004307 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004308error:
Filipe Mananadac57052014-08-29 20:54:26 +01004309 if (last_size != (u64)-1 &&
4310 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004311 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004312 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004313 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004314}
4315
Chris Masona52d9a82007-08-27 16:49:44 -04004316/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004317 * btrfs_truncate_page - read, zero a chunk and write a page
4318 * @inode - inode that we're zeroing
4319 * @from - the offset to start zeroing
4320 * @len - the length to zero, 0 to zero the entire range respective to the
4321 * offset
4322 * @front - zero up to the offset instead of from the offset on
4323 *
4324 * This will find the page for the "from" offset and cow the page and zero the
4325 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004326 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004327int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4328 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004329{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004330 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004331 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004332 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4333 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004334 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004335 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004336 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004337 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4338 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4339 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004340 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004341 int ret = 0;
4342 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004343 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004344
Josef Bacik2aaa6652012-08-29 14:27:18 -04004345 if ((offset & (blocksize - 1)) == 0 &&
4346 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004347 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004348 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004349 if (ret)
4350 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004351
Chris Mason211c17f2008-05-15 09:13:45 -04004352again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004353 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004354 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004355 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004356 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004357 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004358 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004359
4360 page_start = page_offset(page);
4361 page_end = page_start + PAGE_CACHE_SIZE - 1;
4362
Chris Masona52d9a82007-08-27 16:49:44 -04004363 if (!PageUptodate(page)) {
4364 ret = btrfs_readpage(NULL, page);
4365 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004366 if (page->mapping != mapping) {
4367 unlock_page(page);
4368 page_cache_release(page);
4369 goto again;
4370 }
Chris Masona52d9a82007-08-27 16:49:44 -04004371 if (!PageUptodate(page)) {
4372 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004373 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004374 }
4375 }
Chris Mason211c17f2008-05-15 09:13:45 -04004376 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004377
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004378 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004379 set_page_extent_mapped(page);
4380
4381 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4382 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004383 unlock_extent_cached(io_tree, page_start, page_end,
4384 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004385 unlock_page(page);
4386 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004387 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004388 btrfs_put_ordered_extent(ordered);
4389 goto again;
4390 }
4391
Josef Bacik2ac55d42010-02-03 19:33:23 +00004392 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004393 EXTENT_DIRTY | EXTENT_DELALLOC |
4394 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004395 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004396
Josef Bacik2ac55d42010-02-03 19:33:23 +00004397 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4398 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004399 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004400 unlock_extent_cached(io_tree, page_start, page_end,
4401 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004402 goto out_unlock;
4403 }
4404
Chris Masone6dcd2d2008-07-17 12:53:50 -04004405 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004406 if (!len)
4407 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004408 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004409 if (front)
4410 memset(kaddr, 0, offset);
4411 else
4412 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004413 flush_dcache_page(page);
4414 kunmap(page);
4415 }
Chris Mason247e7432008-07-17 12:53:51 -04004416 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004417 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004418 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4419 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004420
Chris Mason89642222008-07-24 09:41:53 -04004421out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004422 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004423 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004424 unlock_page(page);
4425 page_cache_release(page);
4426out:
4427 return ret;
4428}
4429
Josef Bacik16e75492013-10-22 12:18:51 -04004430static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4431 u64 offset, u64 len)
4432{
4433 struct btrfs_trans_handle *trans;
4434 int ret;
4435
4436 /*
4437 * Still need to make sure the inode looks like it's been updated so
4438 * that any holes get logged if we fsync.
4439 */
4440 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4441 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4442 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4443 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4444 return 0;
4445 }
4446
4447 /*
4448 * 1 - for the one we're dropping
4449 * 1 - for the one we're adding
4450 * 1 - for updating the inode.
4451 */
4452 trans = btrfs_start_transaction(root, 3);
4453 if (IS_ERR(trans))
4454 return PTR_ERR(trans);
4455
4456 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4457 if (ret) {
4458 btrfs_abort_transaction(trans, root, ret);
4459 btrfs_end_transaction(trans, root);
4460 return ret;
4461 }
4462
4463 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4464 0, 0, len, 0, len, 0, 0, 0);
4465 if (ret)
4466 btrfs_abort_transaction(trans, root, ret);
4467 else
4468 btrfs_update_inode(trans, root, inode);
4469 btrfs_end_transaction(trans, root);
4470 return ret;
4471}
4472
Josef Bacik695a0d02011-03-04 15:46:53 -05004473/*
4474 * This function puts in dummy file extents for the area we're creating a hole
4475 * for. So if we are truncating this file to a larger size we need to insert
4476 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4477 * the range between oldsize and size
4478 */
Josef Bacika41ad392011-01-31 15:30:16 -05004479int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004480{
Yan Zheng9036c102008-10-30 14:19:41 -04004481 struct btrfs_root *root = BTRFS_I(inode)->root;
4482 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004483 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004484 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004485 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004486 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4487 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004488 u64 last_byte;
4489 u64 cur_offset;
4490 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004491 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004492
Josef Bacika71754f2013-06-17 17:14:39 -04004493 /*
4494 * If our size started in the middle of a page we need to zero out the
4495 * rest of the page before we expand the i_size, otherwise we could
4496 * expose stale data.
4497 */
4498 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4499 if (err)
4500 return err;
4501
Yan Zheng9036c102008-10-30 14:19:41 -04004502 if (size <= hole_start)
4503 return 0;
4504
Yan Zheng9036c102008-10-30 14:19:41 -04004505 while (1) {
4506 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004507
Josef Bacik2ac55d42010-02-03 19:33:23 +00004508 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004509 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004510 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4511 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004512 if (!ordered)
4513 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004514 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4515 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004516 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004517 btrfs_put_ordered_extent(ordered);
4518 }
4519
Yan Zheng9036c102008-10-30 14:19:41 -04004520 cur_offset = hole_start;
4521 while (1) {
4522 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4523 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004524 if (IS_ERR(em)) {
4525 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004526 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004527 break;
4528 }
Yan Zheng9036c102008-10-30 14:19:41 -04004529 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004530 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004531 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004532 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004533 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004534
Josef Bacik16e75492013-10-22 12:18:51 -04004535 err = maybe_insert_hole(root, inode, cur_offset,
4536 hole_size);
4537 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004538 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004539 btrfs_drop_extent_cache(inode, cur_offset,
4540 cur_offset + hole_size - 1, 0);
4541 hole_em = alloc_extent_map();
4542 if (!hole_em) {
4543 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4544 &BTRFS_I(inode)->runtime_flags);
4545 goto next;
4546 }
4547 hole_em->start = cur_offset;
4548 hole_em->len = hole_size;
4549 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004550
Josef Bacik5dc562c2012-08-17 13:14:17 -04004551 hole_em->block_start = EXTENT_MAP_HOLE;
4552 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004553 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004554 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004555 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4556 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004557 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004558
4559 while (1) {
4560 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004561 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004562 write_unlock(&em_tree->lock);
4563 if (err != -EEXIST)
4564 break;
4565 btrfs_drop_extent_cache(inode, cur_offset,
4566 cur_offset +
4567 hole_size - 1, 0);
4568 }
4569 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004570 }
Josef Bacik16e75492013-10-22 12:18:51 -04004571next:
Yan Zheng9036c102008-10-30 14:19:41 -04004572 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004573 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004574 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004575 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004576 break;
4577 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004578 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004579 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4580 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004581 return err;
4582}
4583
Eric Sandeen3972f262013-01-12 02:57:22 +00004584static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004585{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004586 struct btrfs_root *root = BTRFS_I(inode)->root;
4587 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004588 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004589 loff_t newsize = attr->ia_size;
4590 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004591 int ret;
4592
Eric Sandeen3972f262013-01-12 02:57:22 +00004593 /*
4594 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4595 * special case where we need to update the times despite not having
4596 * these flags set. For all other operations the VFS set these flags
4597 * explicitly if it wants a timestamp update.
4598 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004599 if (newsize != oldsize) {
4600 inode_inc_iversion(inode);
4601 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4602 inode->i_ctime = inode->i_mtime =
4603 current_fs_time(inode->i_sb);
4604 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004605
Josef Bacika41ad392011-01-31 15:30:16 -05004606 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004607 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004608 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004609 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004610 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004611
Miao Xief4a2f4c2011-12-14 20:12:01 -05004612 trans = btrfs_start_transaction(root, 1);
4613 if (IS_ERR(trans))
4614 return PTR_ERR(trans);
4615
4616 i_size_write(inode, newsize);
4617 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4618 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004619 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004620 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004621
Josef Bacika41ad392011-01-31 15:30:16 -05004622 /*
4623 * We're truncating a file that used to have good data down to
4624 * zero. Make sure it gets into the ordered flush list so that
4625 * any new writes get down to disk quickly.
4626 */
4627 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004628 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4629 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004630
Josef Bacikf3fe8202013-01-07 17:03:21 -05004631 /*
4632 * 1 for the orphan item we're going to add
4633 * 1 for the orphan item deletion.
4634 */
4635 trans = btrfs_start_transaction(root, 2);
4636 if (IS_ERR(trans))
4637 return PTR_ERR(trans);
4638
4639 /*
4640 * We need to do this in case we fail at _any_ point during the
4641 * actual truncate. Once we do the truncate_setsize we could
4642 * invalidate pages which forces any outstanding ordered io to
4643 * be instantly completed which will give us extents that need
4644 * to be truncated. If we fail to get an orphan inode down we
4645 * could have left over extents that were never meant to live,
4646 * so we need to garuntee from this point on that everything
4647 * will be consistent.
4648 */
4649 ret = btrfs_orphan_add(trans, inode);
4650 btrfs_end_transaction(trans, root);
4651 if (ret)
4652 return ret;
4653
Josef Bacika41ad392011-01-31 15:30:16 -05004654 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4655 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004656
4657 /* Disable nonlocked read DIO to avoid the end less truncate */
4658 btrfs_inode_block_unlocked_dio(inode);
4659 inode_dio_wait(inode);
4660 btrfs_inode_resume_unlocked_dio(inode);
4661
Josef Bacika41ad392011-01-31 15:30:16 -05004662 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004663 if (ret && inode->i_nlink) {
4664 int err;
4665
4666 /*
4667 * failed to truncate, disk_i_size is only adjusted down
4668 * as we remove extents, so it should represent the true
4669 * size of the inode, so reset the in memory size and
4670 * delete our orphan entry.
4671 */
4672 trans = btrfs_join_transaction(root);
4673 if (IS_ERR(trans)) {
4674 btrfs_orphan_del(NULL, inode);
4675 return ret;
4676 }
4677 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4678 err = btrfs_orphan_del(trans, inode);
4679 if (err)
4680 btrfs_abort_transaction(trans, root, err);
4681 btrfs_end_transaction(trans, root);
4682 }
Yan, Zheng80825102009-11-12 09:35:36 +00004683 }
4684
Josef Bacika41ad392011-01-31 15:30:16 -05004685 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004686}
4687
Chris Mason39279cc2007-06-12 06:35:45 -04004688static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4689{
4690 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004691 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004692 int err;
4693
Li Zefanb83cc962010-12-20 16:04:08 +08004694 if (btrfs_root_readonly(root))
4695 return -EROFS;
4696
Chris Mason39279cc2007-06-12 06:35:45 -04004697 err = inode_change_ok(inode, attr);
4698 if (err)
4699 return err;
4700
Chris Mason5a3f23d2009-03-31 13:27:11 -04004701 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004702 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004703 if (err)
4704 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004705 }
Yan Zheng9036c102008-10-30 14:19:41 -04004706
Christoph Hellwig10257742010-06-04 11:30:02 +02004707 if (attr->ia_valid) {
4708 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004709 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004710 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004711
Josef Bacik22c44fe2011-11-30 10:45:38 -05004712 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004713 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004714 }
4715
Chris Mason39279cc2007-06-12 06:35:45 -04004716 return err;
4717}
Chris Mason61295eb2008-01-14 16:24:38 -05004718
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004719/*
4720 * While truncating the inode pages during eviction, we get the VFS calling
4721 * btrfs_invalidatepage() against each page of the inode. This is slow because
4722 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4723 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4724 * extent_state structures over and over, wasting lots of time.
4725 *
4726 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4727 * those expensive operations on a per page basis and do only the ordered io
4728 * finishing, while we release here the extent_map and extent_state structures,
4729 * without the excessive merging and splitting.
4730 */
4731static void evict_inode_truncate_pages(struct inode *inode)
4732{
4733 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4734 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4735 struct rb_node *node;
4736
4737 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004738 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004739
4740 write_lock(&map_tree->lock);
4741 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4742 struct extent_map *em;
4743
4744 node = rb_first(&map_tree->map);
4745 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004746 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4747 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004748 remove_extent_mapping(map_tree, em);
4749 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004750 if (need_resched()) {
4751 write_unlock(&map_tree->lock);
4752 cond_resched();
4753 write_lock(&map_tree->lock);
4754 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004755 }
4756 write_unlock(&map_tree->lock);
4757
4758 spin_lock(&io_tree->lock);
4759 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4760 struct extent_state *state;
4761 struct extent_state *cached_state = NULL;
4762
4763 node = rb_first(&io_tree->state);
4764 state = rb_entry(node, struct extent_state, rb_node);
4765 atomic_inc(&state->refs);
4766 spin_unlock(&io_tree->lock);
4767
4768 lock_extent_bits(io_tree, state->start, state->end,
4769 0, &cached_state);
4770 clear_extent_bit(io_tree, state->start, state->end,
4771 EXTENT_LOCKED | EXTENT_DIRTY |
4772 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4773 EXTENT_DEFRAG, 1, 1,
4774 &cached_state, GFP_NOFS);
4775 free_extent_state(state);
4776
Filipe Manana7064dd52014-08-08 02:47:05 +01004777 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004778 spin_lock(&io_tree->lock);
4779 }
4780 spin_unlock(&io_tree->lock);
4781}
4782
Al Virobd555972010-06-07 11:35:40 -04004783void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004784{
4785 struct btrfs_trans_handle *trans;
4786 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004787 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004788 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004789 int ret;
4790
liubo1abe9b82011-03-24 11:18:59 +00004791 trace_btrfs_inode_evict(inode);
4792
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004793 evict_inode_truncate_pages(inode);
4794
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004795 if (inode->i_nlink &&
4796 ((btrfs_root_refs(&root->root_item) != 0 &&
4797 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4798 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004799 goto no_delete;
4800
Chris Mason39279cc2007-06-12 06:35:45 -04004801 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004802 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004803 goto no_delete;
4804 }
Al Virobd555972010-06-07 11:35:40 -04004805 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004806 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004807
Miao Xief6124962014-09-12 18:44:04 +08004808 btrfs_free_io_failure_record(inode, 0, (u64)-1);
4809
Yan, Zhengc71bf092009-11-12 09:34:40 +00004810 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004811 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004812 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004813 goto no_delete;
4814 }
4815
Yan, Zheng76dda932009-09-21 16:00:26 -04004816 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004817 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4818 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004819 goto no_delete;
4820 }
4821
Miao Xie0e8c36a2012-12-19 06:59:51 +00004822 ret = btrfs_commit_inode_delayed_inode(inode);
4823 if (ret) {
4824 btrfs_orphan_del(NULL, inode);
4825 goto no_delete;
4826 }
4827
Miao Xie66d8f3d2012-09-06 04:02:28 -06004828 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004829 if (!rsv) {
4830 btrfs_orphan_del(NULL, inode);
4831 goto no_delete;
4832 }
Josef Bacik4a338542011-08-29 11:01:31 -04004833 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004834 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004835 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004836
Chris Masondbe674a2008-07-17 12:54:05 -04004837 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004838
Josef Bacik4289a662011-08-05 13:22:24 -04004839 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004840 * This is a bit simpler than btrfs_truncate since we've already
4841 * reserved our space for our orphan item in the unlink, so we just
4842 * need to reserve some slack space in case we add bytes and update
4843 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004844 */
Yan, Zheng80825102009-11-12 09:35:36 +00004845 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004846 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4847 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004848
Josef Bacik726c35f2011-09-26 15:46:06 -04004849 /*
4850 * Try and steal from the global reserve since we will
4851 * likely not use this space anyway, we want to try as
4852 * hard as possible to get this to work.
4853 */
4854 if (ret)
4855 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4856
Yan, Zhengd68fc572010-05-16 10:49:58 -04004857 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004858 btrfs_warn(root->fs_info,
4859 "Could not get space for a delete, will truncate on mount %d",
4860 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004861 btrfs_orphan_del(NULL, inode);
4862 btrfs_free_block_rsv(root, rsv);
4863 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004864 }
4865
Miao Xie0e8c36a2012-12-19 06:59:51 +00004866 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004867 if (IS_ERR(trans)) {
4868 btrfs_orphan_del(NULL, inode);
4869 btrfs_free_block_rsv(root, rsv);
4870 goto no_delete;
4871 }
4872
4873 trans->block_rsv = rsv;
4874
Yan, Zhengd68fc572010-05-16 10:49:58 -04004875 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004876 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004877 break;
4878
Miao Xie8407aa42012-09-07 01:43:32 -06004879 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004880 btrfs_end_transaction(trans, root);
4881 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004882 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004883 }
4884
Josef Bacik4289a662011-08-05 13:22:24 -04004885 btrfs_free_block_rsv(root, rsv);
4886
Josef Bacik4ef31a42013-08-13 14:10:08 -04004887 /*
4888 * Errors here aren't a big deal, it just means we leave orphan items
4889 * in the tree. They will be cleaned up on the next mount.
4890 */
Yan, Zheng80825102009-11-12 09:35:36 +00004891 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004892 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004893 btrfs_orphan_del(trans, inode);
4894 } else {
4895 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004896 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004897
Josef Bacik4289a662011-08-05 13:22:24 -04004898 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004899 if (!(root == root->fs_info->tree_root ||
4900 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004901 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004902
Chris Mason54aa1f42007-06-22 14:16:25 -04004903 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004904 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004905no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004906 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004907 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004908 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004909}
4910
4911/*
4912 * this returns the key found in the dir entry in the location pointer.
4913 * If no dir entries were found, location->objectid is 0.
4914 */
4915static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4916 struct btrfs_key *location)
4917{
4918 const char *name = dentry->d_name.name;
4919 int namelen = dentry->d_name.len;
4920 struct btrfs_dir_item *di;
4921 struct btrfs_path *path;
4922 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004923 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004924
4925 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004926 if (!path)
4927 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004928
Li Zefan33345d012011-04-20 10:31:50 +08004929 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004930 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004931 if (IS_ERR(di))
4932 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004933
David Sterbac7040052011-04-19 18:00:01 +02004934 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004935 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004936
Chris Mason5f39d392007-10-15 16:14:19 -04004937 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004938out:
Chris Mason39279cc2007-06-12 06:35:45 -04004939 btrfs_free_path(path);
4940 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004941out_err:
4942 location->objectid = 0;
4943 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004944}
4945
4946/*
4947 * when we hit a tree root in a directory, the btrfs part of the inode
4948 * needs to be changed to reflect the root directory of the tree root. This
4949 * is kind of like crossing a mount point.
4950 */
4951static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004952 struct inode *dir,
4953 struct dentry *dentry,
4954 struct btrfs_key *location,
4955 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004956{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004957 struct btrfs_path *path;
4958 struct btrfs_root *new_root;
4959 struct btrfs_root_ref *ref;
4960 struct extent_buffer *leaf;
4961 int ret;
4962 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004963
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004964 path = btrfs_alloc_path();
4965 if (!path) {
4966 err = -ENOMEM;
4967 goto out;
4968 }
Chris Mason39279cc2007-06-12 06:35:45 -04004969
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004970 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004971 ret = btrfs_find_item(root->fs_info->tree_root, path,
4972 BTRFS_I(dir)->root->root_key.objectid,
4973 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004974 if (ret) {
4975 if (ret < 0)
4976 err = ret;
4977 goto out;
4978 }
Chris Mason39279cc2007-06-12 06:35:45 -04004979
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004980 leaf = path->nodes[0];
4981 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004982 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004983 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4984 goto out;
4985
4986 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4987 (unsigned long)(ref + 1),
4988 dentry->d_name.len);
4989 if (ret)
4990 goto out;
4991
David Sterbab3b4aa72011-04-21 01:20:15 +02004992 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004993
4994 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4995 if (IS_ERR(new_root)) {
4996 err = PTR_ERR(new_root);
4997 goto out;
4998 }
4999
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005000 *sub_root = new_root;
5001 location->objectid = btrfs_root_dirid(&new_root->root_item);
5002 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005003 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005004 err = 0;
5005out:
5006 btrfs_free_path(path);
5007 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005008}
5009
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005010static void inode_tree_add(struct inode *inode)
5011{
5012 struct btrfs_root *root = BTRFS_I(inode)->root;
5013 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005014 struct rb_node **p;
5015 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005016 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005017 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005018
Al Viro1d3382cb2010-10-23 15:19:20 -04005019 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005020 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005021 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005022 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005023 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005024 while (*p) {
5025 parent = *p;
5026 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5027
Li Zefan33345d012011-04-20 10:31:50 +08005028 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005029 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005030 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005031 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005032 else {
5033 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005034 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005035 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005036 RB_CLEAR_NODE(parent);
5037 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005038 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005039 }
5040 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005041 rb_link_node(new, parent, p);
5042 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005043 spin_unlock(&root->inode_lock);
5044}
5045
5046static void inode_tree_del(struct inode *inode)
5047{
5048 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005049 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005050
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005051 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005052 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005053 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005054 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005055 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005056 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005057 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005058
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005059 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005060 synchronize_srcu(&root->fs_info->subvol_srcu);
5061 spin_lock(&root->inode_lock);
5062 empty = RB_EMPTY_ROOT(&root->inode_tree);
5063 spin_unlock(&root->inode_lock);
5064 if (empty)
5065 btrfs_add_dead_root(root);
5066 }
5067}
5068
Jeff Mahoney143bede2012-03-01 14:56:26 +01005069void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005070{
5071 struct rb_node *node;
5072 struct rb_node *prev;
5073 struct btrfs_inode *entry;
5074 struct inode *inode;
5075 u64 objectid = 0;
5076
Liu Bo7813b3d2014-02-10 17:37:25 +08005077 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5078 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005079
5080 spin_lock(&root->inode_lock);
5081again:
5082 node = root->inode_tree.rb_node;
5083 prev = NULL;
5084 while (node) {
5085 prev = node;
5086 entry = rb_entry(node, struct btrfs_inode, rb_node);
5087
Li Zefan33345d012011-04-20 10:31:50 +08005088 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005089 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005090 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005091 node = node->rb_right;
5092 else
5093 break;
5094 }
5095 if (!node) {
5096 while (prev) {
5097 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005098 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005099 node = prev;
5100 break;
5101 }
5102 prev = rb_next(prev);
5103 }
5104 }
5105 while (node) {
5106 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005107 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005108 inode = igrab(&entry->vfs_inode);
5109 if (inode) {
5110 spin_unlock(&root->inode_lock);
5111 if (atomic_read(&inode->i_count) > 1)
5112 d_prune_aliases(inode);
5113 /*
Al Viro45321ac2010-06-07 13:43:19 -04005114 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005115 * the inode cache when its usage count
5116 * hits zero.
5117 */
5118 iput(inode);
5119 cond_resched();
5120 spin_lock(&root->inode_lock);
5121 goto again;
5122 }
5123
5124 if (cond_resched_lock(&root->inode_lock))
5125 goto again;
5126
5127 node = rb_next(node);
5128 }
5129 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005130}
5131
Chris Masone02119d2008-09-05 16:13:11 -04005132static int btrfs_init_locked_inode(struct inode *inode, void *p)
5133{
5134 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005135 inode->i_ino = args->location->objectid;
5136 memcpy(&BTRFS_I(inode)->location, args->location,
5137 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005138 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005139 return 0;
5140}
5141
5142static int btrfs_find_actor(struct inode *inode, void *opaque)
5143{
5144 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005145 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005146 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005147}
5148
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005149static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005150 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005151 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005152{
5153 struct inode *inode;
5154 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005155 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005156
Chris Mason90d3e592014-01-09 17:28:00 -08005157 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005158 args.root = root;
5159
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005160 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005161 btrfs_init_locked_inode,
5162 (void *)&args);
5163 return inode;
5164}
5165
Balaji Rao1a54ef82008-07-21 02:01:04 +05305166/* Get an inode object given its location and corresponding root.
5167 * Returns in *is_new if the inode was read from disk
5168 */
5169struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005170 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305171{
5172 struct inode *inode;
5173
Chris Mason90d3e592014-01-09 17:28:00 -08005174 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305175 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005176 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305177
5178 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305179 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005180 if (!is_bad_inode(inode)) {
5181 inode_tree_add(inode);
5182 unlock_new_inode(inode);
5183 if (new)
5184 *new = 1;
5185 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005186 unlock_new_inode(inode);
5187 iput(inode);
5188 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005189 }
5190 }
5191
Balaji Rao1a54ef82008-07-21 02:01:04 +05305192 return inode;
5193}
5194
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005195static struct inode *new_simple_dir(struct super_block *s,
5196 struct btrfs_key *key,
5197 struct btrfs_root *root)
5198{
5199 struct inode *inode = new_inode(s);
5200
5201 if (!inode)
5202 return ERR_PTR(-ENOMEM);
5203
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005204 BTRFS_I(inode)->root = root;
5205 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005206 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005207
5208 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005209 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005210 inode->i_fop = &simple_dir_operations;
5211 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5212 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5213
5214 return inode;
5215}
5216
Chris Mason3de45862008-11-17 21:02:50 -05005217struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005218{
Chris Masond3977122009-01-05 21:25:51 -05005219 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005220 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005221 struct btrfs_root *sub_root = root;
5222 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005223 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005224 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005225
5226 if (dentry->d_name.len > BTRFS_NAME_LEN)
5227 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005228
Jeff Layton39e3c952012-11-28 11:30:53 -05005229 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005230 if (ret < 0)
5231 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005232
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005233 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005234 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005235
5236 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005237 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005238 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005239 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005240
5241 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5242
Yan, Zheng76dda932009-09-21 16:00:26 -04005243 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005244 ret = fixup_tree_root_location(root, dir, dentry,
5245 &location, &sub_root);
5246 if (ret < 0) {
5247 if (ret != -ENOENT)
5248 inode = ERR_PTR(ret);
5249 else
5250 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5251 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005252 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005253 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005254 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5255
Julia Lawall34d19ba2011-01-24 19:55:19 +00005256 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005257 down_read(&root->fs_info->cleanup_work_sem);
5258 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005259 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005260 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005261 if (ret) {
5262 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005263 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005264 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005265 }
5266
Chris Mason3de45862008-11-17 21:02:50 -05005267 return inode;
5268}
5269
Nick Pigginfe15ce42011-01-07 17:49:23 +11005270static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005271{
5272 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005273 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005274
Li Zefan848cce02012-02-21 17:04:28 +08005275 if (!inode && !IS_ROOT(dentry))
5276 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005277
Li Zefan848cce02012-02-21 17:04:28 +08005278 if (inode) {
5279 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005280 if (btrfs_root_refs(&root->root_item) == 0)
5281 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005282
5283 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5284 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005285 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005286 return 0;
5287}
5288
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005289static void btrfs_dentry_release(struct dentry *dentry)
5290{
Daeseok Youn944a4512014-04-14 15:37:02 +09005291 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005292}
5293
Chris Mason3de45862008-11-17 21:02:50 -05005294static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005295 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005296{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005297 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005298
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005299 inode = btrfs_lookup_dentry(dir, dentry);
5300 if (IS_ERR(inode)) {
5301 if (PTR_ERR(inode) == -ENOENT)
5302 inode = NULL;
5303 else
5304 return ERR_CAST(inode);
5305 }
5306
Josef Bacik3a0dfa62014-02-14 13:43:48 -05005307 return d_materialise_unique(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005308}
5309
Miao Xie16cdcec2011-04-22 18:12:22 +08005310unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005311 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5312};
5313
Al Viro9cdda8d2013-05-22 16:48:09 -04005314static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005315{
Al Viro9cdda8d2013-05-22 16:48:09 -04005316 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005317 struct btrfs_root *root = BTRFS_I(inode)->root;
5318 struct btrfs_item *item;
5319 struct btrfs_dir_item *di;
5320 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005321 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005322 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005323 struct list_head ins_list;
5324 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005325 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005326 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005327 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005328 unsigned char d_type;
5329 int over = 0;
5330 u32 di_cur;
5331 u32 di_total;
5332 u32 di_len;
5333 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005334 char tmp_name[32];
5335 char *name_ptr;
5336 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005337 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005338
5339 /* FIXME, use a real flag for deciding about the key type */
5340 if (root->fs_info->tree_root == root)
5341 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005342
Al Viro9cdda8d2013-05-22 16:48:09 -04005343 if (!dir_emit_dots(file, ctx))
5344 return 0;
5345
David Woodhouse49593bf2008-08-17 17:08:36 +01005346 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005347 if (!path)
5348 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005349
Josef Bacik026fd312011-05-13 10:32:11 -04005350 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005351
Miao Xie16cdcec2011-04-22 18:12:22 +08005352 if (key_type == BTRFS_DIR_INDEX_KEY) {
5353 INIT_LIST_HEAD(&ins_list);
5354 INIT_LIST_HEAD(&del_list);
5355 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5356 }
5357
David Sterba962a2982014-06-04 18:41:45 +02005358 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005359 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005360 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005361
Chris Mason39279cc2007-06-12 06:35:45 -04005362 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5363 if (ret < 0)
5364 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005365
5366 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005367 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005368 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005369 if (slot >= btrfs_header_nritems(leaf)) {
5370 ret = btrfs_next_leaf(root, path);
5371 if (ret < 0)
5372 goto err;
5373 else if (ret > 0)
5374 break;
5375 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005376 }
Chris Mason3de45862008-11-17 21:02:50 -05005377
Ross Kirkdd3cc162013-09-16 15:58:09 +01005378 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005379 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5380
5381 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005382 break;
David Sterba962a2982014-06-04 18:41:45 +02005383 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005384 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005385 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005386 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005387 if (key_type == BTRFS_DIR_INDEX_KEY &&
5388 btrfs_should_delete_dir_index(&del_list,
5389 found_key.offset))
5390 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005391
Al Viro9cdda8d2013-05-22 16:48:09 -04005392 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005393 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005394
Chris Mason39279cc2007-06-12 06:35:45 -04005395 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5396 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005397 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005398
5399 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005400 struct btrfs_key location;
5401
Josef Bacik22a94d42011-03-16 16:47:17 -04005402 if (verify_dir_item(root, leaf, di))
5403 break;
5404
Chris Mason5f39d392007-10-15 16:14:19 -04005405 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005406 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005407 name_ptr = tmp_name;
5408 } else {
5409 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005410 if (!name_ptr) {
5411 ret = -ENOMEM;
5412 goto err;
5413 }
Chris Mason5f39d392007-10-15 16:14:19 -04005414 }
5415 read_extent_buffer(leaf, name_ptr,
5416 (unsigned long)(di + 1), name_len);
5417
5418 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5419 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005420
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005421
Chris Mason3de45862008-11-17 21:02:50 -05005422 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005423 * skip it.
5424 *
5425 * In contrast to old kernels, we insert the snapshot's
5426 * dir item and dir index after it has been created, so
5427 * we won't find a reference to our own snapshot. We
5428 * still keep the following code for backward
5429 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005430 */
5431 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5432 location.objectid == root->root_key.objectid) {
5433 over = 0;
5434 goto skip;
5435 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005436 over = !dir_emit(ctx, name_ptr, name_len,
5437 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005438
Chris Mason3de45862008-11-17 21:02:50 -05005439skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005440 if (name_ptr != tmp_name)
5441 kfree(name_ptr);
5442
Chris Mason39279cc2007-06-12 06:35:45 -04005443 if (over)
5444 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005445 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005446 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005447 di_cur += di_len;
5448 di = (struct btrfs_dir_item *)((char *)di + di_len);
5449 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005450next:
5451 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005452 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005453
Miao Xie16cdcec2011-04-22 18:12:22 +08005454 if (key_type == BTRFS_DIR_INDEX_KEY) {
5455 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005456 ctx->pos++;
5457 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005458 if (ret)
5459 goto nopos;
5460 }
5461
David Woodhouse49593bf2008-08-17 17:08:36 +01005462 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005463 ctx->pos++;
5464
5465 /*
5466 * Stop new entries from being returned after we return the last
5467 * entry.
5468 *
5469 * New directory entries are assigned a strictly increasing
5470 * offset. This means that new entries created during readdir
5471 * are *guaranteed* to be seen in the future by that readdir.
5472 * This has broken buggy programs which operate on names as
5473 * they're returned by readdir. Until we re-use freed offsets
5474 * we have this hack to stop new entries from being returned
5475 * under the assumption that they'll never reach this huge
5476 * offset.
5477 *
5478 * This is being careful not to overflow 32bit loff_t unless the
5479 * last entry requires it because doing so has broken 32bit apps
5480 * in the past.
5481 */
5482 if (key_type == BTRFS_DIR_INDEX_KEY) {
5483 if (ctx->pos >= INT_MAX)
5484 ctx->pos = LLONG_MAX;
5485 else
5486 ctx->pos = INT_MAX;
5487 }
Chris Mason39279cc2007-06-12 06:35:45 -04005488nopos:
5489 ret = 0;
5490err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005491 if (key_type == BTRFS_DIR_INDEX_KEY)
5492 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005493 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005494 return ret;
5495}
5496
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005497int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005498{
5499 struct btrfs_root *root = BTRFS_I(inode)->root;
5500 struct btrfs_trans_handle *trans;
5501 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005502 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005503
Josef Bacik72ac3c02012-05-23 14:13:11 -04005504 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005505 return 0;
5506
Liu Bo83eea1f2012-07-10 05:28:39 -06005507 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005508 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005509
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005510 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005511 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005512 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005513 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005514 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005515 if (IS_ERR(trans))
5516 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005517 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005518 }
5519 return ret;
5520}
5521
5522/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005523 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005524 * inode changes. But, it is most likely to find the inode in cache.
5525 * FIXME, needs more benchmarking...there are no reasons other than performance
5526 * to keep or drop this code.
5527 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005528static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005529{
5530 struct btrfs_root *root = BTRFS_I(inode)->root;
5531 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005532 int ret;
5533
Josef Bacik72ac3c02012-05-23 14:13:11 -04005534 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005535 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005536
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005537 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005538 if (IS_ERR(trans))
5539 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005540
5541 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005542 if (ret && ret == -ENOSPC) {
5543 /* whoops, lets try again with the full transaction */
5544 btrfs_end_transaction(trans, root);
5545 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005546 if (IS_ERR(trans))
5547 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005548
Chris Mason94b60442010-05-26 11:02:00 -04005549 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005550 }
Chris Mason39279cc2007-06-12 06:35:45 -04005551 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005552 if (BTRFS_I(inode)->delayed_node)
5553 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005554
5555 return ret;
5556}
5557
5558/*
5559 * This is a copy of file_update_time. We need this so we can return error on
5560 * ENOSPC for updating the inode in the case of file write and mmap writes.
5561 */
Josef Bacike41f9412012-03-26 09:46:47 -04005562static int btrfs_update_time(struct inode *inode, struct timespec *now,
5563 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005564{
Alexander Block2bc5565282012-06-15 09:49:33 +02005565 struct btrfs_root *root = BTRFS_I(inode)->root;
5566
5567 if (btrfs_root_readonly(root))
5568 return -EROFS;
5569
Josef Bacike41f9412012-03-26 09:46:47 -04005570 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005571 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005572 if (flags & S_CTIME)
5573 inode->i_ctime = *now;
5574 if (flags & S_MTIME)
5575 inode->i_mtime = *now;
5576 if (flags & S_ATIME)
5577 inode->i_atime = *now;
5578 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005579}
5580
Chris Masond352ac62008-09-29 15:18:18 -04005581/*
5582 * find the highest existing sequence number in a directory
5583 * and then set the in-memory index_cnt variable to reflect
5584 * free sequence numbers
5585 */
Josef Bacikaec74772008-07-24 12:12:38 -04005586static int btrfs_set_inode_index_count(struct inode *inode)
5587{
5588 struct btrfs_root *root = BTRFS_I(inode)->root;
5589 struct btrfs_key key, found_key;
5590 struct btrfs_path *path;
5591 struct extent_buffer *leaf;
5592 int ret;
5593
Li Zefan33345d012011-04-20 10:31:50 +08005594 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005595 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005596 key.offset = (u64)-1;
5597
5598 path = btrfs_alloc_path();
5599 if (!path)
5600 return -ENOMEM;
5601
5602 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5603 if (ret < 0)
5604 goto out;
5605 /* FIXME: we should be able to handle this */
5606 if (ret == 0)
5607 goto out;
5608 ret = 0;
5609
5610 /*
5611 * MAGIC NUMBER EXPLANATION:
5612 * since we search a directory based on f_pos we have to start at 2
5613 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5614 * else has to start at 2
5615 */
5616 if (path->slots[0] == 0) {
5617 BTRFS_I(inode)->index_cnt = 2;
5618 goto out;
5619 }
5620
5621 path->slots[0]--;
5622
5623 leaf = path->nodes[0];
5624 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5625
Li Zefan33345d012011-04-20 10:31:50 +08005626 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005627 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005628 BTRFS_I(inode)->index_cnt = 2;
5629 goto out;
5630 }
5631
5632 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5633out:
5634 btrfs_free_path(path);
5635 return ret;
5636}
5637
Chris Masond352ac62008-09-29 15:18:18 -04005638/*
5639 * helper to find a free sequence number in a given directory. This current
5640 * code is very simple, later versions will do smarter things in the btree
5641 */
Chris Mason3de45862008-11-17 21:02:50 -05005642int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005643{
5644 int ret = 0;
5645
5646 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005647 ret = btrfs_inode_delayed_dir_index_count(dir);
5648 if (ret) {
5649 ret = btrfs_set_inode_index_count(dir);
5650 if (ret)
5651 return ret;
5652 }
Josef Bacikaec74772008-07-24 12:12:38 -04005653 }
5654
Chris Mason00e4e6b2008-08-05 11:18:09 -04005655 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005656 BTRFS_I(dir)->index_cnt++;
5657
5658 return ret;
5659}
5660
Chris Masonb0d5d102014-09-08 13:08:51 -07005661static int btrfs_insert_inode_locked(struct inode *inode)
5662{
5663 struct btrfs_iget_args args;
5664 args.location = &BTRFS_I(inode)->location;
5665 args.root = BTRFS_I(inode)->root;
5666
5667 return insert_inode_locked4(inode,
5668 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5669 btrfs_find_actor, &args);
5670}
5671
Chris Mason39279cc2007-06-12 06:35:45 -04005672static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5673 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005674 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005675 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005676 u64 ref_objectid, u64 objectid,
5677 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005678{
5679 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005680 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005681 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005682 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005683 struct btrfs_inode_ref *ref;
5684 struct btrfs_key key[2];
5685 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005686 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005687 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005688 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005689
Chris Mason5f39d392007-10-15 16:14:19 -04005690 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005691 if (!path)
5692 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005693
Chris Mason39279cc2007-06-12 06:35:45 -04005694 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005695 if (!inode) {
5696 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005697 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005698 }
Chris Mason39279cc2007-06-12 06:35:45 -04005699
Li Zefan581bb052011-04-20 10:06:11 +08005700 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005701 * O_TMPFILE, set link count to 0, so that after this point,
5702 * we fill in an inode item with the correct link count.
5703 */
5704 if (!name)
5705 set_nlink(inode, 0);
5706
5707 /*
Li Zefan581bb052011-04-20 10:06:11 +08005708 * we have to initialize this early, so we can reclaim the inode
5709 * number if we fail afterwards in this function.
5710 */
5711 inode->i_ino = objectid;
5712
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005713 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005714 trace_btrfs_inode_request(dir);
5715
Chris Mason3de45862008-11-17 21:02:50 -05005716 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005717 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005718 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005719 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005720 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005721 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005722 } else if (dir) {
5723 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005724 }
5725 /*
5726 * index_cnt is ignored for everything but a dir,
5727 * btrfs_get_inode_index_count has an explanation for the magic
5728 * number
5729 */
5730 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005731 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005732 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005733 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005734 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005735
Josef Bacik5dc562c2012-08-17 13:14:17 -04005736 /*
5737 * We could have gotten an inode number from somebody who was fsynced
5738 * and then removed in this same transaction, so let's just set full
5739 * sync since it will be a full sync anyway and this will blow away the
5740 * old info in the log.
5741 */
5742 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5743
Chris Mason9c583092008-01-29 15:15:18 -05005744 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005745 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005746 key[0].offset = 0;
5747
Chris Mason9c583092008-01-29 15:15:18 -05005748 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005749
5750 if (name) {
5751 /*
5752 * Start new inodes with an inode_ref. This is slightly more
5753 * efficient for small numbers of hard links since they will
5754 * be packed into one item. Extended refs will kick in if we
5755 * add more hard links than can fit in the ref item.
5756 */
5757 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005758 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005759 key[1].offset = ref_objectid;
5760
5761 sizes[1] = name_len + sizeof(*ref);
5762 }
Chris Mason9c583092008-01-29 15:15:18 -05005763
Chris Masonb0d5d102014-09-08 13:08:51 -07005764 location = &BTRFS_I(inode)->location;
5765 location->objectid = objectid;
5766 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005767 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005768
5769 ret = btrfs_insert_inode_locked(inode);
5770 if (ret < 0)
5771 goto fail;
5772
Chris Masonb9473432009-03-13 11:00:37 -04005773 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005774 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005775 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005776 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005777
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005778 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005779 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005780 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005781 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5782 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005783 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5784 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005785 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005786
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005787 if (name) {
5788 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5789 struct btrfs_inode_ref);
5790 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5791 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5792 ptr = (unsigned long)(ref + 1);
5793 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5794 }
Chris Mason9c583092008-01-29 15:15:18 -05005795
Chris Mason5f39d392007-10-15 16:14:19 -04005796 btrfs_mark_buffer_dirty(path->nodes[0]);
5797 btrfs_free_path(path);
5798
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005799 btrfs_inherit_iflags(inode, dir);
5800
Al Viro569254b2011-07-24 17:08:40 -04005801 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005802 if (btrfs_test_opt(root, NODATASUM))
5803 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005804 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005805 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5806 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005807 }
5808
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005809 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005810
5811 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005812 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005813
Alexander Block8ea05e32012-07-25 17:35:53 +02005814 btrfs_update_root_times(trans, root);
5815
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005816 ret = btrfs_inode_inherit_props(trans, inode, dir);
5817 if (ret)
5818 btrfs_err(root->fs_info,
5819 "error inheriting props for ino %llu (root %llu): %d",
5820 btrfs_ino(inode), root->root_key.objectid, ret);
5821
Chris Mason39279cc2007-06-12 06:35:45 -04005822 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005823
5824fail_unlock:
5825 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005826fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005827 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005828 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005829 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005830 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005831 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005832}
5833
5834static inline u8 btrfs_inode_type(struct inode *inode)
5835{
5836 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5837}
5838
Chris Masond352ac62008-09-29 15:18:18 -04005839/*
5840 * utility function to add 'inode' into 'parent_inode' with
5841 * a give name and a given sequence number.
5842 * if 'add_backref' is true, also insert a backref from the
5843 * inode to the parent directory.
5844 */
Chris Masone02119d2008-09-05 16:13:11 -04005845int btrfs_add_link(struct btrfs_trans_handle *trans,
5846 struct inode *parent_inode, struct inode *inode,
5847 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005848{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005849 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005850 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005851 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005852 u64 ino = btrfs_ino(inode);
5853 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005854
Li Zefan33345d012011-04-20 10:31:50 +08005855 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005856 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5857 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005858 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02005859 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005860 key.offset = 0;
5861 }
Chris Mason39279cc2007-06-12 06:35:45 -04005862
Li Zefan33345d012011-04-20 10:31:50 +08005863 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005864 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5865 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005866 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005867 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005868 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5869 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005870 }
5871
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005872 /* Nothing to clean up yet */
5873 if (ret)
5874 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005875
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005876 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5877 parent_inode, &key,
5878 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005879 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005880 goto fail_dir_item;
5881 else if (ret) {
5882 btrfs_abort_transaction(trans, root, ret);
5883 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005884 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005885
5886 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5887 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005888 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005889 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5890 ret = btrfs_update_inode(trans, root, parent_inode);
5891 if (ret)
5892 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005893 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005894
5895fail_dir_item:
5896 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5897 u64 local_index;
5898 int err;
5899 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5900 key.objectid, root->root_key.objectid,
5901 parent_ino, &local_index, name, name_len);
5902
5903 } else if (add_backref) {
5904 u64 local_index;
5905 int err;
5906
5907 err = btrfs_del_inode_ref(trans, root, name, name_len,
5908 ino, parent_ino, &local_index);
5909 }
5910 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005911}
5912
5913static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005914 struct inode *dir, struct dentry *dentry,
5915 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005916{
Josef Bacika1b075d2010-11-19 20:36:11 +00005917 int err = btrfs_add_link(trans, dir, inode,
5918 dentry->d_name.name, dentry->d_name.len,
5919 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005920 if (err > 0)
5921 err = -EEXIST;
5922 return err;
5923}
5924
Josef Bacik618e21d2007-07-11 10:18:17 -04005925static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005926 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005927{
5928 struct btrfs_trans_handle *trans;
5929 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005930 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005931 int err;
5932 int drop_inode = 0;
5933 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005934 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005935
5936 if (!new_valid_dev(rdev))
5937 return -EINVAL;
5938
Josef Bacik9ed74f22009-09-11 16:12:44 -04005939 /*
5940 * 2 for inode item and ref
5941 * 2 for dir items
5942 * 1 for xattr if selinux is on
5943 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005944 trans = btrfs_start_transaction(root, 5);
5945 if (IS_ERR(trans))
5946 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005947
Li Zefan581bb052011-04-20 10:06:11 +08005948 err = btrfs_find_free_ino(root, &objectid);
5949 if (err)
5950 goto out_unlock;
5951
Josef Bacikaec74772008-07-24 12:12:38 -04005952 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005953 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005954 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005955 if (IS_ERR(inode)) {
5956 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005957 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005958 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005959
Casey Schauflerad19db72011-12-15 10:09:07 -05005960 /*
5961 * If the active LSM wants to access the inode during
5962 * d_instantiate it needs these. Smack checks to see
5963 * if the filesystem supports xattrs by looking at the
5964 * ops vector.
5965 */
Casey Schauflerad19db72011-12-15 10:09:07 -05005966 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07005967 init_special_inode(inode, inode->i_mode, rdev);
5968
5969 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04005970 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07005971 goto out_unlock_inode;
5972
5973 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
5974 if (err) {
5975 goto out_unlock_inode;
5976 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04005977 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07005978 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05005979 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005980 }
Chris Masonb0d5d102014-09-08 13:08:51 -07005981
Josef Bacik618e21d2007-07-11 10:18:17 -04005982out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005983 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05005984 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00005985 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005986 if (drop_inode) {
5987 inode_dec_link_count(inode);
5988 iput(inode);
5989 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005990 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07005991
5992out_unlock_inode:
5993 drop_inode = 1;
5994 unlock_new_inode(inode);
5995 goto out_unlock;
5996
Josef Bacik618e21d2007-07-11 10:18:17 -04005997}
5998
Chris Mason39279cc2007-06-12 06:35:45 -04005999static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006000 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006001{
6002 struct btrfs_trans_handle *trans;
6003 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006004 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006005 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006006 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006007 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006008 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006009
Josef Bacik9ed74f22009-09-11 16:12:44 -04006010 /*
6011 * 2 for inode item and ref
6012 * 2 for dir items
6013 * 1 for xattr if selinux is on
6014 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006015 trans = btrfs_start_transaction(root, 5);
6016 if (IS_ERR(trans))
6017 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006018
Li Zefan581bb052011-04-20 10:06:11 +08006019 err = btrfs_find_free_ino(root, &objectid);
6020 if (err)
6021 goto out_unlock;
6022
Josef Bacikaec74772008-07-24 12:12:38 -04006023 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006024 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006025 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006026 if (IS_ERR(inode)) {
6027 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006028 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006029 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006030 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006031 /*
6032 * If the active LSM wants to access the inode during
6033 * d_instantiate it needs these. Smack checks to see
6034 * if the filesystem supports xattrs by looking at the
6035 * ops vector.
6036 */
6037 inode->i_fop = &btrfs_file_operations;
6038 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006039 inode->i_mapping->a_ops = &btrfs_aops;
6040 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
6041
6042 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6043 if (err)
6044 goto out_unlock_inode;
6045
6046 err = btrfs_update_inode(trans, root, inode);
6047 if (err)
6048 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006049
Josef Bacika1b075d2010-11-19 20:36:11 +00006050 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006051 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006052 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006053
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006054 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006055 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006056 d_instantiate(dentry, inode);
6057
Chris Mason39279cc2007-06-12 06:35:45 -04006058out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006059 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006060 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006061 inode_dec_link_count(inode);
6062 iput(inode);
6063 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006064 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006065 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006066 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006067
6068out_unlock_inode:
6069 unlock_new_inode(inode);
6070 goto out_unlock;
6071
Chris Mason39279cc2007-06-12 06:35:45 -04006072}
6073
6074static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6075 struct dentry *dentry)
6076{
6077 struct btrfs_trans_handle *trans;
6078 struct btrfs_root *root = BTRFS_I(dir)->root;
6079 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006080 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006081 int err;
6082 int drop_inode = 0;
6083
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006084 /* do not allow sys_link's with other subvols of the same device */
6085 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006086 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006087
Mark Fashehf1863732012-08-08 11:32:27 -07006088 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006089 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006090
Chris Mason3de45862008-11-17 21:02:50 -05006091 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006092 if (err)
6093 goto fail;
6094
Yan, Zhenga22285a2010-05-16 10:48:46 -04006095 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006096 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006097 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006098 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006099 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006100 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006101 if (IS_ERR(trans)) {
6102 err = PTR_ERR(trans);
6103 goto fail;
6104 }
Chris Mason5f39d392007-10-15 16:14:19 -04006105
Miao Xie67de1172013-12-26 13:07:06 +08006106 /* There are several dir indexes for this inode, clear the cache. */
6107 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006108 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006109 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006110 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006111 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006112 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006113
Josef Bacika1b075d2010-11-19 20:36:11 +00006114 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006115
Yan, Zhenga5719522009-09-24 09:17:31 -04006116 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006117 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006118 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006119 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006120 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006121 if (err)
6122 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006123 if (inode->i_nlink == 1) {
6124 /*
6125 * If new hard link count is 1, it's a file created
6126 * with open(2) O_TMPFILE flag.
6127 */
6128 err = btrfs_orphan_del(trans, inode);
6129 if (err)
6130 goto fail;
6131 }
Al Viro08c422c2011-12-23 07:58:13 -05006132 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006133 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006134 }
Chris Mason39279cc2007-06-12 06:35:45 -04006135
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006136 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006137 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006138fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006139 if (drop_inode) {
6140 inode_dec_link_count(inode);
6141 iput(inode);
6142 }
Liu Bob53d3f52012-11-14 14:34:34 +00006143 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006144 return err;
6145}
6146
Al Viro18bb1db2011-07-26 01:41:39 -04006147static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006148{
Chris Masonb9d86662008-05-02 16:13:49 -04006149 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006150 struct btrfs_trans_handle *trans;
6151 struct btrfs_root *root = BTRFS_I(dir)->root;
6152 int err = 0;
6153 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006154 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006155 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006156
Josef Bacik9ed74f22009-09-11 16:12:44 -04006157 /*
6158 * 2 items for inode and ref
6159 * 2 items for dir items
6160 * 1 for xattr if selinux is on
6161 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006162 trans = btrfs_start_transaction(root, 5);
6163 if (IS_ERR(trans))
6164 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006165
Li Zefan581bb052011-04-20 10:06:11 +08006166 err = btrfs_find_free_ino(root, &objectid);
6167 if (err)
6168 goto out_fail;
6169
Josef Bacikaec74772008-07-24 12:12:38 -04006170 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006171 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006172 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006173 if (IS_ERR(inode)) {
6174 err = PTR_ERR(inode);
6175 goto out_fail;
6176 }
Chris Mason5f39d392007-10-15 16:14:19 -04006177
Chris Mason39279cc2007-06-12 06:35:45 -04006178 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006179 /* these must be set before we unlock the inode */
6180 inode->i_op = &btrfs_dir_inode_operations;
6181 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006182
Eric Paris2a7dba32011-02-01 11:05:39 -05006183 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006184 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006185 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006186
Chris Masondbe674a2008-07-17 12:54:05 -04006187 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006188 err = btrfs_update_inode(trans, root, inode);
6189 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006190 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006191
Josef Bacika1b075d2010-11-19 20:36:11 +00006192 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6193 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006194 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006195 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006196
Chris Mason39279cc2007-06-12 06:35:45 -04006197 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006198 /*
6199 * mkdir is special. We're unlocking after we call d_instantiate
6200 * to avoid a race with nfsd calling d_instantiate.
6201 */
6202 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006203 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006204
6205out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006206 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006207 if (drop_on_err)
6208 iput(inode);
Josef Bacikc581afc2014-02-06 16:06:06 -05006209 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006210 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006211 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006212
6213out_fail_inode:
6214 unlock_new_inode(inode);
6215 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006216}
6217
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006218/* Find next extent map of a given extent map, caller needs to ensure locks */
6219static struct extent_map *next_extent_map(struct extent_map *em)
6220{
6221 struct rb_node *next;
6222
6223 next = rb_next(&em->rb_node);
6224 if (!next)
6225 return NULL;
6226 return container_of(next, struct extent_map, rb_node);
6227}
6228
6229static struct extent_map *prev_extent_map(struct extent_map *em)
6230{
6231 struct rb_node *prev;
6232
6233 prev = rb_prev(&em->rb_node);
6234 if (!prev)
6235 return NULL;
6236 return container_of(prev, struct extent_map, rb_node);
6237}
6238
Chris Masond352ac62008-09-29 15:18:18 -04006239/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006240 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006241 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006242 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006243 */
Chris Mason3b951512008-04-17 11:29:12 -04006244static int merge_extent_mapping(struct extent_map_tree *em_tree,
6245 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006246 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006247 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006248{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006249 struct extent_map *prev;
6250 struct extent_map *next;
6251 u64 start;
6252 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006253 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006254
Chris Masone6dcd2d2008-07-17 12:53:50 -04006255 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006256
6257 if (existing->start > map_start) {
6258 next = existing;
6259 prev = prev_extent_map(next);
6260 } else {
6261 prev = existing;
6262 next = next_extent_map(prev);
6263 }
6264
6265 start = prev ? extent_map_end(prev) : em->start;
6266 start = max_t(u64, start, em->start);
6267 end = next ? next->start : extent_map_end(em);
6268 end = min_t(u64, end, extent_map_end(em));
6269 start_diff = start - em->start;
6270 em->start = start;
6271 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006272 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6273 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006274 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006275 em->block_len -= start_diff;
6276 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006277 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006278}
6279
Chris Masonc8b97812008-10-29 14:49:59 -04006280static noinline int uncompress_inline(struct btrfs_path *path,
6281 struct inode *inode, struct page *page,
6282 size_t pg_offset, u64 extent_offset,
6283 struct btrfs_file_extent_item *item)
6284{
6285 int ret;
6286 struct extent_buffer *leaf = path->nodes[0];
6287 char *tmp;
6288 size_t max_size;
6289 unsigned long inline_size;
6290 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006291 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006292
6293 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006294 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006295 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6296 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006297 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006298 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006299 if (!tmp)
6300 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006301 ptr = btrfs_file_extent_inline_start(item);
6302
6303 read_extent_buffer(leaf, tmp, ptr, inline_size);
6304
Chris Mason5b050f02008-11-11 09:34:41 -05006305 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006306 ret = btrfs_decompress(compress_type, tmp, page,
6307 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006308 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006309 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006310}
6311
Chris Masond352ac62008-09-29 15:18:18 -04006312/*
6313 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006314 * the ugly parts come from merging extents from the disk with the in-ram
6315 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006316 * where the in-ram extents might be locked pending data=ordered completion.
6317 *
6318 * This also copies inline extents directly into the page.
6319 */
Chris Masond3977122009-01-05 21:25:51 -05006320
Chris Masona52d9a82007-08-27 16:49:44 -04006321struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006322 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006323 int create)
6324{
6325 int ret;
6326 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006327 u64 extent_start = 0;
6328 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006329 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006330 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006331 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006332 struct btrfs_root *root = BTRFS_I(inode)->root;
6333 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006334 struct extent_buffer *leaf;
6335 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006336 struct extent_map *em = NULL;
6337 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006338 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006339 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006340 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006341
Chris Masona52d9a82007-08-27 16:49:44 -04006342again:
Chris Mason890871b2009-09-02 16:24:52 -04006343 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006344 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006345 if (em)
6346 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006347 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006348
Chris Masona52d9a82007-08-27 16:49:44 -04006349 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006350 if (em->start > start || em->start + em->len <= start)
6351 free_extent_map(em);
6352 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006353 free_extent_map(em);
6354 else
6355 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006356 }
David Sterba172ddd62011-04-21 00:48:27 +02006357 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006358 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006359 err = -ENOMEM;
6360 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006361 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006362 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006363 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006364 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006365 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006366 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006367
6368 if (!path) {
6369 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006370 if (!path) {
6371 err = -ENOMEM;
6372 goto out;
6373 }
6374 /*
6375 * Chances are we'll be called again, so go ahead and do
6376 * readahead
6377 */
6378 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006379 }
6380
Chris Mason179e29e2007-11-01 11:28:41 -04006381 ret = btrfs_lookup_file_extent(trans, root, path,
6382 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006383 if (ret < 0) {
6384 err = ret;
6385 goto out;
6386 }
6387
6388 if (ret != 0) {
6389 if (path->slots[0] == 0)
6390 goto not_found;
6391 path->slots[0]--;
6392 }
6393
Chris Mason5f39d392007-10-15 16:14:19 -04006394 leaf = path->nodes[0];
6395 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006396 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006397 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006398 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006399 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006400 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006401 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006402 /*
6403 * If we backup past the first extent we want to move forward
6404 * and see if there is an extent in front of us, otherwise we'll
6405 * say there is a hole for our whole search range which can
6406 * cause problems.
6407 */
6408 extent_end = start;
6409 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006410 }
6411
Chris Mason5f39d392007-10-15 16:14:19 -04006412 found_type = btrfs_file_extent_type(leaf, item);
6413 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006414 if (found_type == BTRFS_FILE_EXTENT_REG ||
6415 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006416 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006417 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006418 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6419 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006420 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006421 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006422 }
Josef Bacik25a50342013-10-14 12:08:38 -04006423next:
Yan Zheng9036c102008-10-30 14:19:41 -04006424 if (start >= extent_end) {
6425 path->slots[0]++;
6426 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6427 ret = btrfs_next_leaf(root, path);
6428 if (ret < 0) {
6429 err = ret;
6430 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006431 }
Yan Zheng9036c102008-10-30 14:19:41 -04006432 if (ret > 0)
6433 goto not_found;
6434 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006435 }
Yan Zheng9036c102008-10-30 14:19:41 -04006436 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6437 if (found_key.objectid != objectid ||
6438 found_key.type != BTRFS_EXTENT_DATA_KEY)
6439 goto not_found;
6440 if (start + len <= found_key.offset)
6441 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006442 if (start > found_key.offset)
6443 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006444 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006445 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006446 em->len = found_key.offset - start;
6447 goto not_found_em;
6448 }
6449
Filipe Manana7ffbb592014-06-09 03:48:05 +01006450 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6451
Yan Zhengd899e052008-10-30 14:25:28 -04006452 if (found_type == BTRFS_FILE_EXTENT_REG ||
6453 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006454 goto insert;
6455 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006456 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006457 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006458 size_t size;
6459 size_t extent_offset;
6460 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006461
Filipe Manana7ffbb592014-06-09 03:48:05 +01006462 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006463 goto out;
Yan689f9342007-10-29 11:41:07 -04006464
Chris Mason514ac8a2014-01-03 21:07:00 -08006465 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006466 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006467 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006468 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006469 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006470 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006471 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006472 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006473 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006474 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006475 if (btrfs_file_extent_compression(leaf, item) !=
6476 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006477 ret = uncompress_inline(path, inode, page,
6478 pg_offset,
6479 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006480 if (ret) {
6481 err = ret;
6482 goto out;
6483 }
Chris Masonc8b97812008-10-29 14:49:59 -04006484 } else {
6485 map = kmap(page);
6486 read_extent_buffer(leaf, map + pg_offset, ptr,
6487 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006488 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6489 memset(map + pg_offset + copy_size, 0,
6490 PAGE_CACHE_SIZE - pg_offset -
6491 copy_size);
6492 }
Chris Masonc8b97812008-10-29 14:49:59 -04006493 kunmap(page);
6494 }
Chris Mason179e29e2007-11-01 11:28:41 -04006495 flush_dcache_page(page);
6496 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006497 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006498 if (!trans) {
6499 kunmap(page);
6500 free_extent_map(em);
6501 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006502
David Sterbab3b4aa72011-04-21 01:20:15 +02006503 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006504 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006505
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006506 if (IS_ERR(trans))
6507 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006508 goto again;
6509 }
Chris Masonc8b97812008-10-29 14:49:59 -04006510 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006511 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006512 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006513 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006514 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006515 }
Chris Masond1310b22008-01-24 16:13:08 -05006516 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006517 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006518 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006519 }
6520not_found:
6521 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006522 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006523 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006524not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006525 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006526 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006527insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006528 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006529 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006530 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006531 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006532 err = -EIO;
6533 goto out;
6534 }
Chris Masond1310b22008-01-24 16:13:08 -05006535
6536 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006537 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006538 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006539 /* it is possible that someone inserted the extent into the tree
6540 * while we had the lock dropped. It is also possible that
6541 * an overlapping map exists in the tree
6542 */
Chris Masona52d9a82007-08-27 16:49:44 -04006543 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006544 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006545
6546 ret = 0;
6547
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006548 existing = search_extent_mapping(em_tree, start, len);
6549 /*
6550 * existing will always be non-NULL, since there must be
6551 * extent causing the -EEXIST.
6552 */
6553 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006554 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006555 /*
6556 * The existing extent map is the one nearest to
6557 * the [start, start + len) range which overlaps
6558 */
6559 err = merge_extent_mapping(em_tree, existing,
6560 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006561 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006562 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006563 free_extent_map(em);
6564 em = NULL;
6565 }
6566 } else {
6567 free_extent_map(em);
6568 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006569 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006570 }
Chris Masona52d9a82007-08-27 16:49:44 -04006571 }
Chris Mason890871b2009-09-02 16:24:52 -04006572 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006573out:
liubo1abe9b82011-03-24 11:18:59 +00006574
Steven Rostedt4cd85872013-11-14 22:57:29 -05006575 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006576
Chris Masonf4219502008-07-22 11:18:09 -04006577 if (path)
6578 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006579 if (trans) {
6580 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006581 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006582 err = ret;
6583 }
Chris Masona52d9a82007-08-27 16:49:44 -04006584 if (err) {
6585 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006586 return ERR_PTR(err);
6587 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006588 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006589 return em;
6590}
6591
Chris Masonec29ed52011-02-23 16:23:20 -05006592struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6593 size_t pg_offset, u64 start, u64 len,
6594 int create)
6595{
6596 struct extent_map *em;
6597 struct extent_map *hole_em = NULL;
6598 u64 range_start = start;
6599 u64 end;
6600 u64 found;
6601 u64 found_end;
6602 int err = 0;
6603
6604 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6605 if (IS_ERR(em))
6606 return em;
6607 if (em) {
6608 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006609 * if our em maps to
6610 * - a hole or
6611 * - a pre-alloc extent,
6612 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006613 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006614 if (em->block_start != EXTENT_MAP_HOLE &&
6615 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006616 return em;
6617 else
6618 hole_em = em;
6619 }
6620
6621 /* check to see if we've wrapped (len == -1 or similar) */
6622 end = start + len;
6623 if (end < start)
6624 end = (u64)-1;
6625 else
6626 end -= 1;
6627
6628 em = NULL;
6629
6630 /* ok, we didn't find anything, lets look for delalloc */
6631 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6632 end, len, EXTENT_DELALLOC, 1);
6633 found_end = range_start + found;
6634 if (found_end < range_start)
6635 found_end = (u64)-1;
6636
6637 /*
6638 * we didn't find anything useful, return
6639 * the original results from get_extent()
6640 */
6641 if (range_start > end || found_end <= start) {
6642 em = hole_em;
6643 hole_em = NULL;
6644 goto out;
6645 }
6646
6647 /* adjust the range_start to make sure it doesn't
6648 * go backwards from the start they passed in
6649 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306650 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006651 found = found_end - range_start;
6652
6653 if (found > 0) {
6654 u64 hole_start = start;
6655 u64 hole_len = len;
6656
David Sterba172ddd62011-04-21 00:48:27 +02006657 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006658 if (!em) {
6659 err = -ENOMEM;
6660 goto out;
6661 }
6662 /*
6663 * when btrfs_get_extent can't find anything it
6664 * returns one huge hole
6665 *
6666 * make sure what it found really fits our range, and
6667 * adjust to make sure it is based on the start from
6668 * the caller
6669 */
6670 if (hole_em) {
6671 u64 calc_end = extent_map_end(hole_em);
6672
6673 if (calc_end <= start || (hole_em->start > end)) {
6674 free_extent_map(hole_em);
6675 hole_em = NULL;
6676 } else {
6677 hole_start = max(hole_em->start, start);
6678 hole_len = calc_end - hole_start;
6679 }
6680 }
6681 em->bdev = NULL;
6682 if (hole_em && range_start > hole_start) {
6683 /* our hole starts before our delalloc, so we
6684 * have to return just the parts of the hole
6685 * that go until the delalloc starts
6686 */
6687 em->len = min(hole_len,
6688 range_start - hole_start);
6689 em->start = hole_start;
6690 em->orig_start = hole_start;
6691 /*
6692 * don't adjust block start at all,
6693 * it is fixed at EXTENT_MAP_HOLE
6694 */
6695 em->block_start = hole_em->block_start;
6696 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006697 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6698 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006699 } else {
6700 em->start = range_start;
6701 em->len = found;
6702 em->orig_start = range_start;
6703 em->block_start = EXTENT_MAP_DELALLOC;
6704 em->block_len = found;
6705 }
6706 } else if (hole_em) {
6707 return hole_em;
6708 }
6709out:
6710
6711 free_extent_map(hole_em);
6712 if (err) {
6713 free_extent_map(em);
6714 return ERR_PTR(err);
6715 }
6716 return em;
6717}
6718
Josef Bacik4b46fce2010-05-23 11:00:55 -04006719static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6720 u64 start, u64 len)
6721{
6722 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006723 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006724 struct btrfs_key ins;
6725 u64 alloc_hint;
6726 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006727
Josef Bacik4b46fce2010-05-23 11:00:55 -04006728 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006729 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08006730 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006731 if (ret)
6732 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006733
Josef Bacik70c8a912012-10-11 16:54:30 -04006734 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006735 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006736 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08006737 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006738 return em;
6739 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006740
6741 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6742 ins.offset, ins.offset, 0);
6743 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08006744 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006745 free_extent_map(em);
6746 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006747 }
Josef Bacik00361582013-08-14 14:02:47 -04006748
Josef Bacik4b46fce2010-05-23 11:00:55 -04006749 return em;
6750}
6751
Chris Mason46bfbb52010-05-26 11:04:10 -04006752/*
6753 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6754 * block must be cow'd
6755 */
Josef Bacik00361582013-08-14 14:02:47 -04006756noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006757 u64 *orig_start, u64 *orig_block_len,
6758 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006759{
Josef Bacik00361582013-08-14 14:02:47 -04006760 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006761 struct btrfs_path *path;
6762 int ret;
6763 struct extent_buffer *leaf;
6764 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006765 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006766 struct btrfs_file_extent_item *fi;
6767 struct btrfs_key key;
6768 u64 disk_bytenr;
6769 u64 backref_offset;
6770 u64 extent_end;
6771 u64 num_bytes;
6772 int slot;
6773 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006774 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006775
Chris Mason46bfbb52010-05-26 11:04:10 -04006776 path = btrfs_alloc_path();
6777 if (!path)
6778 return -ENOMEM;
6779
Josef Bacik00361582013-08-14 14:02:47 -04006780 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006781 offset, 0);
6782 if (ret < 0)
6783 goto out;
6784
6785 slot = path->slots[0];
6786 if (ret == 1) {
6787 if (slot == 0) {
6788 /* can't find the item, must cow */
6789 ret = 0;
6790 goto out;
6791 }
6792 slot--;
6793 }
6794 ret = 0;
6795 leaf = path->nodes[0];
6796 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006797 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006798 key.type != BTRFS_EXTENT_DATA_KEY) {
6799 /* not our file or wrong item type, must cow */
6800 goto out;
6801 }
6802
6803 if (key.offset > offset) {
6804 /* Wrong offset, must cow */
6805 goto out;
6806 }
6807
6808 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6809 found_type = btrfs_file_extent_type(leaf, fi);
6810 if (found_type != BTRFS_FILE_EXTENT_REG &&
6811 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6812 /* not a regular extent, must cow */
6813 goto out;
6814 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006815
6816 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6817 goto out;
6818
Miao Xiee77751a2013-12-27 21:11:50 +08006819 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6820 if (extent_end <= offset)
6821 goto out;
6822
Chris Mason46bfbb52010-05-26 11:04:10 -04006823 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006824 if (disk_bytenr == 0)
6825 goto out;
6826
6827 if (btrfs_file_extent_compression(leaf, fi) ||
6828 btrfs_file_extent_encryption(leaf, fi) ||
6829 btrfs_file_extent_other_encoding(leaf, fi))
6830 goto out;
6831
Chris Mason46bfbb52010-05-26 11:04:10 -04006832 backref_offset = btrfs_file_extent_offset(leaf, fi);
6833
Josef Bacik7ee9e442013-06-21 16:37:03 -04006834 if (orig_start) {
6835 *orig_start = key.offset - backref_offset;
6836 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6837 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6838 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006839
Chris Mason46bfbb52010-05-26 11:04:10 -04006840 if (btrfs_extent_readonly(root, disk_bytenr))
6841 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006842
6843 num_bytes = min(offset + *len, extent_end) - offset;
6844 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6845 u64 range_end;
6846
6847 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6848 ret = test_range_bit(io_tree, offset, range_end,
6849 EXTENT_DELALLOC, 0, NULL);
6850 if (ret) {
6851 ret = -EAGAIN;
6852 goto out;
6853 }
6854 }
6855
Josef Bacik1bda19e2013-10-18 12:10:36 -04006856 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006857
6858 /*
6859 * look for other files referencing this extent, if we
6860 * find any we must cow
6861 */
Josef Bacik00361582013-08-14 14:02:47 -04006862 trans = btrfs_join_transaction(root);
6863 if (IS_ERR(trans)) {
6864 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006865 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006866 }
6867
6868 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6869 key.offset - backref_offset, disk_bytenr);
6870 btrfs_end_transaction(trans, root);
6871 if (ret) {
6872 ret = 0;
6873 goto out;
6874 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006875
6876 /*
6877 * adjust disk_bytenr and num_bytes to cover just the bytes
6878 * in this extent we are about to write. If there
6879 * are any csums in that range we have to cow in order
6880 * to keep the csums correct
6881 */
6882 disk_bytenr += backref_offset;
6883 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006884 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6885 goto out;
6886 /*
6887 * all of the above have passed, it is safe to overwrite this extent
6888 * without cow
6889 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006890 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006891 ret = 1;
6892out:
6893 btrfs_free_path(path);
6894 return ret;
6895}
6896
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006897bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
6898{
6899 struct radix_tree_root *root = &inode->i_mapping->page_tree;
6900 int found = false;
6901 void **pagep = NULL;
6902 struct page *page = NULL;
6903 int start_idx;
6904 int end_idx;
6905
6906 start_idx = start >> PAGE_CACHE_SHIFT;
6907
6908 /*
6909 * end is the last byte in the last page. end == start is legal
6910 */
6911 end_idx = end >> PAGE_CACHE_SHIFT;
6912
6913 rcu_read_lock();
6914
6915 /* Most of the code in this while loop is lifted from
6916 * find_get_page. It's been modified to begin searching from a
6917 * page and return just the first page found in that range. If the
6918 * found idx is less than or equal to the end idx then we know that
6919 * a page exists. If no pages are found or if those pages are
6920 * outside of the range then we're fine (yay!) */
6921 while (page == NULL &&
6922 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
6923 page = radix_tree_deref_slot(pagep);
6924 if (unlikely(!page))
6925 break;
6926
6927 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01006928 if (radix_tree_deref_retry(page)) {
6929 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006930 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01006931 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006932 /*
6933 * Otherwise, shmem/tmpfs must be storing a swap entry
6934 * here as an exceptional entry: so return it without
6935 * attempting to raise page count.
6936 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01006937 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006938 break; /* TODO: Is this relevant for this use case? */
6939 }
6940
Filipe Manana91405152014-06-05 13:22:24 +01006941 if (!page_cache_get_speculative(page)) {
6942 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006943 continue;
Filipe Manana91405152014-06-05 13:22:24 +01006944 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006945
6946 /*
6947 * Has the page moved?
6948 * This is part of the lockless pagecache protocol. See
6949 * include/linux/pagemap.h for details.
6950 */
6951 if (unlikely(page != *pagep)) {
6952 page_cache_release(page);
6953 page = NULL;
6954 }
6955 }
6956
6957 if (page) {
6958 if (page->index <= end_idx)
6959 found = true;
6960 page_cache_release(page);
6961 }
6962
6963 rcu_read_unlock();
6964 return found;
6965}
6966
Josef Bacikeb838e72012-07-31 16:28:48 -04006967static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6968 struct extent_state **cached_state, int writing)
6969{
6970 struct btrfs_ordered_extent *ordered;
6971 int ret = 0;
6972
6973 while (1) {
6974 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6975 0, cached_state);
6976 /*
6977 * We're concerned with the entire range that we're going to be
6978 * doing DIO to, so we need to make sure theres no ordered
6979 * extents in this range.
6980 */
6981 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6982 lockend - lockstart + 1);
6983
6984 /*
6985 * We need to make sure there are no buffered pages in this
6986 * range either, we could have raced between the invalidate in
6987 * generic_file_direct_write and locking the extent. The
6988 * invalidate needs to happen so that reads after a write do not
6989 * get stale data.
6990 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006991 if (!ordered &&
6992 (!writing ||
6993 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04006994 break;
6995
6996 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6997 cached_state, GFP_NOFS);
6998
6999 if (ordered) {
7000 btrfs_start_ordered_extent(inode, ordered, 1);
7001 btrfs_put_ordered_extent(ordered);
7002 } else {
7003 /* Screw you mmap */
7004 ret = filemap_write_and_wait_range(inode->i_mapping,
7005 lockstart,
7006 lockend);
7007 if (ret)
7008 break;
7009
7010 /*
7011 * If we found a page that couldn't be invalidated just
7012 * fall back to buffered.
7013 */
7014 ret = invalidate_inode_pages2_range(inode->i_mapping,
7015 lockstart >> PAGE_CACHE_SHIFT,
7016 lockend >> PAGE_CACHE_SHIFT);
7017 if (ret)
7018 break;
7019 }
7020
7021 cond_resched();
7022 }
7023
7024 return ret;
7025}
7026
Josef Bacik69ffb542012-09-11 15:40:07 -04007027static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7028 u64 len, u64 orig_start,
7029 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007030 u64 orig_block_len, u64 ram_bytes,
7031 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007032{
7033 struct extent_map_tree *em_tree;
7034 struct extent_map *em;
7035 struct btrfs_root *root = BTRFS_I(inode)->root;
7036 int ret;
7037
7038 em_tree = &BTRFS_I(inode)->extent_tree;
7039 em = alloc_extent_map();
7040 if (!em)
7041 return ERR_PTR(-ENOMEM);
7042
7043 em->start = start;
7044 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007045 em->mod_start = start;
7046 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007047 em->len = len;
7048 em->block_len = block_len;
7049 em->block_start = block_start;
7050 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007051 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007052 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007053 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007054 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7055 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007056 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007057
7058 do {
7059 btrfs_drop_extent_cache(inode, em->start,
7060 em->start + em->len - 1, 0);
7061 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007062 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007063 write_unlock(&em_tree->lock);
7064 } while (ret == -EEXIST);
7065
7066 if (ret) {
7067 free_extent_map(em);
7068 return ERR_PTR(ret);
7069 }
7070
7071 return em;
7072}
7073
7074
Josef Bacik4b46fce2010-05-23 11:00:55 -04007075static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7076 struct buffer_head *bh_result, int create)
7077{
7078 struct extent_map *em;
7079 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007080 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007081 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007082 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007083 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007084 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007085 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007086
Miao Xie172a5042013-02-21 02:48:22 -07007087 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04007088 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007089 else
Josef Bacikc3298612012-08-03 16:49:19 -04007090 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007091
Josef Bacikc3298612012-08-03 16:49:19 -04007092 lockstart = start;
7093 lockend = start + len - 1;
7094
Josef Bacikeb838e72012-07-31 16:28:48 -04007095 /*
7096 * If this errors out it's because we couldn't invalidate pagecache for
7097 * this range and we need to fallback to buffered.
7098 */
7099 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7100 return -ENOTBLK;
7101
Josef Bacik4b46fce2010-05-23 11:00:55 -04007102 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007103 if (IS_ERR(em)) {
7104 ret = PTR_ERR(em);
7105 goto unlock_err;
7106 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007107
7108 /*
7109 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7110 * io. INLINE is special, and we could probably kludge it in here, but
7111 * it's still buffered so for safety lets just fall back to the generic
7112 * buffered path.
7113 *
7114 * For COMPRESSED we _have_ to read the entire extent in so we can
7115 * decompress it, so there will be buffering required no matter what we
7116 * do, so go ahead and fallback to buffered.
7117 *
7118 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7119 * to buffered IO. Don't blame me, this is the price we pay for using
7120 * the generic code.
7121 */
7122 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7123 em->block_start == EXTENT_MAP_INLINE) {
7124 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007125 ret = -ENOTBLK;
7126 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007127 }
7128
7129 /* Just a good old fashioned hole, return */
7130 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7131 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7132 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007133 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007134 }
7135
7136 /*
7137 * We don't allocate a new extent in the following cases
7138 *
7139 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7140 * existing extent.
7141 * 2) The extent is marked as PREALLOC. We're good to go here and can
7142 * just use the extent.
7143 *
7144 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007145 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007146 len = min(len, em->len - (start - em->start));
7147 lockstart = start + len;
7148 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007149 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007150
7151 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7152 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7153 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007154 int type;
7155 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04007156 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007157
7158 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7159 type = BTRFS_ORDERED_PREALLOC;
7160 else
7161 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007162 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007163 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007164
Josef Bacik00361582013-08-14 14:02:47 -04007165 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007166 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007167 if (type == BTRFS_ORDERED_PREALLOC) {
7168 free_extent_map(em);
7169 em = create_pinned_em(inode, start, len,
7170 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007171 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007172 orig_block_len,
7173 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007174 if (IS_ERR(em)) {
7175 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007176 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007177 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007178 }
7179
Chris Mason46bfbb52010-05-26 11:04:10 -04007180 ret = btrfs_add_ordered_extent_dio(inode, start,
7181 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007182 if (ret) {
7183 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007184 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007185 }
7186 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007187 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007188 }
Josef Bacik00361582013-08-14 14:02:47 -04007189
Chris Mason46bfbb52010-05-26 11:04:10 -04007190 /*
7191 * this will cow the extent, reset the len in case we changed
7192 * it above
7193 */
7194 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007195 free_extent_map(em);
7196 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007197 if (IS_ERR(em)) {
7198 ret = PTR_ERR(em);
7199 goto unlock_err;
7200 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007201 len = min(len, em->len - (start - em->start));
7202unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007203 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7204 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007205 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007206 bh_result->b_bdev = em->bdev;
7207 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007208 if (create) {
7209 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7210 set_buffer_new(bh_result);
7211
7212 /*
7213 * Need to update the i_size under the extent lock so buffered
7214 * readers will get the updated i_size when we unlock.
7215 */
7216 if (start + len > i_size_read(inode))
7217 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007218
Miao Xie172a5042013-02-21 02:48:22 -07007219 spin_lock(&BTRFS_I(inode)->lock);
7220 BTRFS_I(inode)->outstanding_extents++;
7221 spin_unlock(&BTRFS_I(inode)->lock);
7222
Miao Xie09348562013-02-07 10:12:07 +00007223 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7224 lockstart + len - 1, EXTENT_DELALLOC, NULL,
7225 &cached_state, GFP_NOFS);
7226 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04007227 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007228
Josef Bacikeb838e72012-07-31 16:28:48 -04007229 /*
7230 * In the case of write we need to clear and unlock the entire range,
7231 * in the case of read we need to unlock only the end area that we
7232 * aren't using if there is any left over space.
7233 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007234 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007235 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7236 lockend, unlock_bits, 1, 0,
7237 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007238 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007239 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007240 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007241
Josef Bacik4b46fce2010-05-23 11:00:55 -04007242 free_extent_map(em);
7243
7244 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007245
7246unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007247 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7248 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7249 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007250}
7251
Miao Xie8b110e32014-09-12 18:44:03 +08007252static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7253 int rw, int mirror_num)
7254{
7255 struct btrfs_root *root = BTRFS_I(inode)->root;
7256 int ret;
7257
7258 BUG_ON(rw & REQ_WRITE);
7259
7260 bio_get(bio);
7261
7262 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7263 BTRFS_WQ_ENDIO_DIO_REPAIR);
7264 if (ret)
7265 goto err;
7266
7267 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7268err:
7269 bio_put(bio);
7270 return ret;
7271}
7272
7273static int btrfs_check_dio_repairable(struct inode *inode,
7274 struct bio *failed_bio,
7275 struct io_failure_record *failrec,
7276 int failed_mirror)
7277{
7278 int num_copies;
7279
7280 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7281 failrec->logical, failrec->len);
7282 if (num_copies == 1) {
7283 /*
7284 * we only have a single copy of the data, so don't bother with
7285 * all the retry and error correction code that follows. no
7286 * matter what the error is, it is very likely to persist.
7287 */
7288 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7289 num_copies, failrec->this_mirror, failed_mirror);
7290 return 0;
7291 }
7292
7293 failrec->failed_mirror = failed_mirror;
7294 failrec->this_mirror++;
7295 if (failrec->this_mirror == failed_mirror)
7296 failrec->this_mirror++;
7297
7298 if (failrec->this_mirror > num_copies) {
7299 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7300 num_copies, failrec->this_mirror, failed_mirror);
7301 return 0;
7302 }
7303
7304 return 1;
7305}
7306
7307static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7308 struct page *page, u64 start, u64 end,
7309 int failed_mirror, bio_end_io_t *repair_endio,
7310 void *repair_arg)
7311{
7312 struct io_failure_record *failrec;
7313 struct bio *bio;
7314 int isector;
7315 int read_mode;
7316 int ret;
7317
7318 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7319
7320 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7321 if (ret)
7322 return ret;
7323
7324 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7325 failed_mirror);
7326 if (!ret) {
7327 free_io_failure(inode, failrec);
7328 return -EIO;
7329 }
7330
7331 if (failed_bio->bi_vcnt > 1)
7332 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7333 else
7334 read_mode = READ_SYNC;
7335
7336 isector = start - btrfs_io_bio(failed_bio)->logical;
7337 isector >>= inode->i_sb->s_blocksize_bits;
7338 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7339 0, isector, repair_endio, repair_arg);
7340 if (!bio) {
7341 free_io_failure(inode, failrec);
7342 return -EIO;
7343 }
7344
7345 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7346 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7347 read_mode, failrec->this_mirror, failrec->in_validation);
7348
7349 ret = submit_dio_repair_bio(inode, bio, read_mode,
7350 failrec->this_mirror);
7351 if (ret) {
7352 free_io_failure(inode, failrec);
7353 bio_put(bio);
7354 }
7355
7356 return ret;
7357}
7358
7359struct btrfs_retry_complete {
7360 struct completion done;
7361 struct inode *inode;
7362 u64 start;
7363 int uptodate;
7364};
7365
7366static void btrfs_retry_endio_nocsum(struct bio *bio, int err)
7367{
7368 struct btrfs_retry_complete *done = bio->bi_private;
7369 struct bio_vec *bvec;
7370 int i;
7371
7372 if (err)
7373 goto end;
7374
7375 done->uptodate = 1;
7376 bio_for_each_segment_all(bvec, bio, i)
7377 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7378end:
7379 complete(&done->done);
7380 bio_put(bio);
7381}
7382
7383static int __btrfs_correct_data_nocsum(struct inode *inode,
7384 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007385{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007386 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007387 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007388 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007389 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007390 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007391
Miao Xiec1dc0892014-09-12 18:43:56 +08007392 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007393 done.inode = inode;
7394
7395 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7396try_again:
7397 done.uptodate = 0;
7398 done.start = start;
7399 init_completion(&done.done);
7400
7401 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7402 start + bvec->bv_len - 1,
7403 io_bio->mirror_num,
7404 btrfs_retry_endio_nocsum, &done);
7405 if (ret)
7406 return ret;
7407
7408 wait_for_completion(&done.done);
7409
7410 if (!done.uptodate) {
7411 /* We might have another mirror, so try again */
7412 goto try_again;
7413 }
7414
7415 start += bvec->bv_len;
7416 }
7417
7418 return 0;
7419}
7420
7421static void btrfs_retry_endio(struct bio *bio, int err)
7422{
7423 struct btrfs_retry_complete *done = bio->bi_private;
7424 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7425 struct bio_vec *bvec;
7426 int uptodate;
7427 int ret;
7428 int i;
7429
7430 if (err)
7431 goto end;
7432
7433 uptodate = 1;
7434 bio_for_each_segment_all(bvec, bio, i) {
7435 ret = __readpage_endio_check(done->inode, io_bio, i,
7436 bvec->bv_page, 0,
7437 done->start, bvec->bv_len);
7438 if (!ret)
7439 clean_io_failure(done->inode, done->start,
7440 bvec->bv_page, 0);
7441 else
7442 uptodate = 0;
7443 }
7444
7445 done->uptodate = uptodate;
7446end:
7447 complete(&done->done);
7448 bio_put(bio);
7449}
7450
7451static int __btrfs_subio_endio_read(struct inode *inode,
7452 struct btrfs_io_bio *io_bio, int err)
7453{
7454 struct bio_vec *bvec;
7455 struct btrfs_retry_complete done;
7456 u64 start;
7457 u64 offset = 0;
7458 int i;
7459 int ret;
7460
7461 err = 0;
7462 start = io_bio->logical;
7463 done.inode = inode;
7464
Miao Xiec1dc0892014-09-12 18:43:56 +08007465 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007466 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7467 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007468 if (likely(!ret))
7469 goto next;
7470try_again:
7471 done.uptodate = 0;
7472 done.start = start;
7473 init_completion(&done.done);
7474
7475 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7476 start + bvec->bv_len - 1,
7477 io_bio->mirror_num,
7478 btrfs_retry_endio, &done);
7479 if (ret) {
7480 err = ret;
7481 goto next;
7482 }
7483
7484 wait_for_completion(&done.done);
7485
7486 if (!done.uptodate) {
7487 /* We might have another mirror, so try again */
7488 goto try_again;
7489 }
7490next:
7491 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007492 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007493 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007494
7495 return err;
7496}
7497
Miao Xie8b110e32014-09-12 18:44:03 +08007498static int btrfs_subio_endio_read(struct inode *inode,
7499 struct btrfs_io_bio *io_bio, int err)
7500{
7501 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7502
7503 if (skip_csum) {
7504 if (unlikely(err))
7505 return __btrfs_correct_data_nocsum(inode, io_bio);
7506 else
7507 return 0;
7508 } else {
7509 return __btrfs_subio_endio_read(inode, io_bio, err);
7510 }
7511}
7512
Miao Xiec1dc0892014-09-12 18:43:56 +08007513static void btrfs_endio_direct_read(struct bio *bio, int err)
7514{
7515 struct btrfs_dio_private *dip = bio->bi_private;
7516 struct inode *inode = dip->inode;
7517 struct bio *dio_bio;
7518 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7519
Miao Xie8b110e32014-09-12 18:44:03 +08007520 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7521 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007522
Josef Bacik4b46fce2010-05-23 11:00:55 -04007523 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007524 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007525 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007526
Josef Bacik4b46fce2010-05-23 11:00:55 -04007527 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007528
7529 /* If we had a csum failure make sure to clear the uptodate flag */
7530 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007531 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7532 dio_end_io(dio_bio, err);
Miao Xie23ea8e52014-09-12 18:43:54 +08007533
7534 if (io_bio->end_io)
7535 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007536 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007537}
7538
7539static void btrfs_endio_direct_write(struct bio *bio, int err)
7540{
7541 struct btrfs_dio_private *dip = bio->bi_private;
7542 struct inode *inode = dip->inode;
7543 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007544 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007545 u64 ordered_offset = dip->logical_offset;
7546 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007547 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007548 int ret;
7549
7550 if (err)
7551 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007552again:
7553 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7554 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007555 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007556 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007557 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007558
Liu Bo9e0af232014-08-15 23:36:53 +08007559 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7560 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007561 btrfs_queue_work(root->fs_info->endio_write_workers,
7562 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007563out_test:
7564 /*
7565 * our bio might span multiple ordered extents. If we haven't
7566 * completed the accounting for the whole dio, go back and try again
7567 */
7568 if (ordered_offset < dip->logical_offset + dip->bytes) {
7569 ordered_bytes = dip->logical_offset + dip->bytes -
7570 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007571 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007572 goto again;
7573 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007574out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007575 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007576
Josef Bacik4b46fce2010-05-23 11:00:55 -04007577 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007578
7579 /* If we had an error make sure to clear the uptodate flag */
7580 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007581 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7582 dio_end_io(dio_bio, err);
7583 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007584}
7585
Chris Masoneaf25d92010-05-25 09:48:28 -04007586static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7587 struct bio *bio, int mirror_num,
7588 unsigned long bio_flags, u64 offset)
7589{
7590 int ret;
7591 struct btrfs_root *root = BTRFS_I(inode)->root;
7592 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007593 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007594 return 0;
7595}
7596
Miao Xiee65e1532010-11-22 03:04:43 +00007597static void btrfs_end_dio_bio(struct bio *bio, int err)
7598{
7599 struct btrfs_dio_private *dip = bio->bi_private;
7600
Miao Xie8b110e32014-09-12 18:44:03 +08007601 if (err)
7602 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
7603 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7604 btrfs_ino(dip->inode), bio->bi_rw,
7605 (unsigned long long)bio->bi_iter.bi_sector,
7606 bio->bi_iter.bi_size, err);
7607
7608 if (dip->subio_endio)
7609 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007610
7611 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007612 dip->errors = 1;
7613
7614 /*
7615 * before atomic variable goto zero, we must make sure
7616 * dip->errors is perceived to be set.
7617 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007618 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007619 }
7620
7621 /* if there are more bios still pending for this dio, just exit */
7622 if (!atomic_dec_and_test(&dip->pending_bios))
7623 goto out;
7624
Chris Mason9be33952013-05-17 18:30:14 -04007625 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007626 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007627 } else {
7628 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007629 bio_endio(dip->orig_bio, 0);
7630 }
7631out:
7632 bio_put(bio);
7633}
7634
7635static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7636 u64 first_sector, gfp_t gfp_flags)
7637{
7638 int nr_vecs = bio_get_nr_vecs(bdev);
7639 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7640}
7641
Miao Xiec1dc0892014-09-12 18:43:56 +08007642static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
7643 struct inode *inode,
7644 struct btrfs_dio_private *dip,
7645 struct bio *bio,
7646 u64 file_offset)
7647{
7648 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7649 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
7650 int ret;
7651
7652 /*
7653 * We load all the csum data we need when we submit
7654 * the first bio to reduce the csum tree search and
7655 * contention.
7656 */
7657 if (dip->logical_offset == file_offset) {
7658 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
7659 file_offset);
7660 if (ret)
7661 return ret;
7662 }
7663
7664 if (bio == dip->orig_bio)
7665 return 0;
7666
7667 file_offset -= dip->logical_offset;
7668 file_offset >>= inode->i_sb->s_blocksize_bits;
7669 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
7670
7671 return 0;
7672}
7673
Miao Xiee65e1532010-11-22 03:04:43 +00007674static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7675 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007676 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007677{
Miao Xiefacc8a222013-07-25 19:22:34 +08007678 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007679 int write = rw & REQ_WRITE;
7680 struct btrfs_root *root = BTRFS_I(inode)->root;
7681 int ret;
7682
Josef Bacikb812ce22012-11-16 13:56:32 -05007683 if (async_submit)
7684 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7685
Miao Xiee65e1532010-11-22 03:04:43 +00007686 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007687
7688 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02007689 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7690 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007691 if (ret)
7692 goto err;
7693 }
Miao Xiee65e1532010-11-22 03:04:43 +00007694
Josef Bacik1ae39932011-04-06 14:41:34 -04007695 if (skip_sum)
7696 goto map;
7697
7698 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007699 ret = btrfs_wq_submit_bio(root->fs_info,
7700 inode, rw, bio, 0, 0,
7701 file_offset,
7702 __btrfs_submit_bio_start_direct_io,
7703 __btrfs_submit_bio_done);
7704 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007705 } else if (write) {
7706 /*
7707 * If we aren't doing async submit, calculate the csum of the
7708 * bio now.
7709 */
7710 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7711 if (ret)
7712 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007713 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08007714 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
7715 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007716 if (ret)
7717 goto err;
7718 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007719map:
7720 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007721err:
7722 bio_put(bio);
7723 return ret;
7724}
7725
7726static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7727 int skip_sum)
7728{
7729 struct inode *inode = dip->inode;
7730 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007731 struct bio *bio;
7732 struct bio *orig_bio = dip->orig_bio;
7733 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007734 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007735 u64 file_offset = dip->logical_offset;
7736 u64 submit_len = 0;
7737 u64 map_length;
7738 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08007739 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04007740 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007741
Kent Overstreet4f024f32013-10-11 15:44:27 -07007742 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007743 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007744 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007745 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007746 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08007747
Kent Overstreet4f024f32013-10-11 15:44:27 -07007748 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007749 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007750 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04007751 goto submit;
7752 }
7753
David Woodhouse53b381b2013-01-29 18:40:14 -05007754 /* async crcs make it difficult to collect full stripe writes. */
7755 if (btrfs_get_alloc_profile(root, 1) &
7756 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7757 async_submit = 0;
7758 else
7759 async_submit = 1;
7760
Josef Bacik02f57c72011-04-06 14:25:44 -04007761 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7762 if (!bio)
7763 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08007764
Josef Bacik02f57c72011-04-06 14:25:44 -04007765 bio->bi_private = dip;
7766 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007767 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04007768 atomic_inc(&dip->pending_bios);
7769
Miao Xiee65e1532010-11-22 03:04:43 +00007770 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02007771 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00007772 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02007773 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00007774 /*
7775 * inc the count before we submit the bio so
7776 * we know the end IO handler won't happen before
7777 * we inc the count. Otherwise, the dip might get freed
7778 * before we're done setting it up
7779 */
7780 atomic_inc(&dip->pending_bios);
7781 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7782 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007783 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007784 if (ret) {
7785 bio_put(bio);
7786 atomic_dec(&dip->pending_bios);
7787 goto out_err;
7788 }
7789
Miao Xiee65e1532010-11-22 03:04:43 +00007790 start_sector += submit_len >> 9;
7791 file_offset += submit_len;
7792
7793 submit_len = 0;
7794 nr_pages = 0;
7795
7796 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7797 start_sector, GFP_NOFS);
7798 if (!bio)
7799 goto out_err;
7800 bio->bi_private = dip;
7801 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007802 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007803
Kent Overstreet4f024f32013-10-11 15:44:27 -07007804 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007805 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007806 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007807 &map_length, NULL, 0);
7808 if (ret) {
7809 bio_put(bio);
7810 goto out_err;
7811 }
7812 } else {
7813 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307814 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007815 bvec++;
7816 }
7817 }
7818
Josef Bacik02f57c72011-04-06 14:25:44 -04007819submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007820 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007821 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007822 if (!ret)
7823 return 0;
7824
7825 bio_put(bio);
7826out_err:
7827 dip->errors = 1;
7828 /*
7829 * before atomic variable goto zero, we must
7830 * make sure dip->errors is perceived to be set.
7831 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007832 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007833 if (atomic_dec_and_test(&dip->pending_bios))
7834 bio_io_error(dip->orig_bio);
7835
7836 /* bio_end_io() will handle error, so we needn't return it */
7837 return 0;
7838}
7839
Chris Mason9be33952013-05-17 18:30:14 -04007840static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7841 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007842{
7843 struct btrfs_root *root = BTRFS_I(inode)->root;
7844 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007845 struct bio *io_bio;
Miao Xie23ea8e52014-09-12 18:43:54 +08007846 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007847 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007848 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007849 int ret = 0;
7850
7851 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7852
Chris Mason9be33952013-05-17 18:30:14 -04007853 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007854 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007855 ret = -ENOMEM;
7856 goto free_ordered;
7857 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007858
Miao Xiec1dc0892014-09-12 18:43:56 +08007859 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007860 if (!dip) {
7861 ret = -ENOMEM;
7862 goto free_io_bio;
7863 }
7864
7865 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007866 dip->inode = inode;
7867 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007868 dip->bytes = dio_bio->bi_iter.bi_size;
7869 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04007870 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04007871 dip->orig_bio = io_bio;
7872 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007873 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08007874 btrfs_bio = btrfs_io_bio(io_bio);
7875 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007876
Miao Xiec1dc0892014-09-12 18:43:56 +08007877 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04007878 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08007879 } else {
Chris Mason9be33952013-05-17 18:30:14 -04007880 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08007881 dip->subio_endio = btrfs_subio_endio_read;
7882 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007883
Miao Xiee65e1532010-11-22 03:04:43 +00007884 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7885 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007886 return;
Chris Mason9be33952013-05-17 18:30:14 -04007887
Miao Xie23ea8e52014-09-12 18:43:54 +08007888 if (btrfs_bio->end_io)
7889 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04007890free_io_bio:
7891 bio_put(io_bio);
7892
Josef Bacik4b46fce2010-05-23 11:00:55 -04007893free_ordered:
7894 /*
7895 * If this is a write, we need to clean up the reserved space and kill
7896 * the ordered extent.
7897 */
7898 if (write) {
7899 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007900 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007901 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7902 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7903 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08007904 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007905 btrfs_put_ordered_extent(ordered);
7906 btrfs_put_ordered_extent(ordered);
7907 }
Chris Mason9be33952013-05-17 18:30:14 -04007908 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007909}
7910
Chris Mason5a5f79b2010-05-26 21:33:37 -04007911static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04007912 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04007913{
7914 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007915 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007916 unsigned blocksize_mask = root->sectorsize - 1;
7917 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007918
7919 if (offset & blocksize_mask)
7920 goto out;
7921
Al Viro28060d52014-03-22 05:15:17 -04007922 if (iov_iter_alignment(iter) & blocksize_mask)
7923 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007924
Al Viro28060d52014-03-22 05:15:17 -04007925 /* If this is a write we don't need to check anymore */
7926 if (rw & WRITE)
7927 return 0;
7928 /*
7929 * Check to make sure we don't have duplicate iov_base's in this
7930 * iovec, if so return EINVAL, otherwise we'll get csum errors
7931 * when reading back.
7932 */
7933 for (seg = 0; seg < iter->nr_segs; seg++) {
7934 for (i = seg + 1; i < iter->nr_segs; i++) {
7935 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00007936 goto out;
7937 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007938 }
7939 retval = 0;
7940out:
7941 return retval;
7942}
Josef Bacikeb838e72012-07-31 16:28:48 -04007943
Chris Mason16432982008-04-10 10:23:21 -04007944static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05007945 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04007946{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007947 struct file *file = iocb->ki_filp;
7948 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007949 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007950 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007951 bool wakeup = true;
7952 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007953 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007954
Al Viro28060d52014-03-22 05:15:17 -04007955 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007956 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007957
Miao Xie38851cc2013-02-08 07:04:11 +00007958 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007959 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00007960
Josef Bacik0e267c42013-07-02 10:38:02 -04007961 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08007962 * The generic stuff only does filemap_write_and_wait_range, which
7963 * isn't enough if we've written compressed pages to this area, so
7964 * we need to flush the dirty pages again to make absolutely sure
7965 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04007966 */
Al Viroa6cbcd42014-03-04 22:38:00 -05007967 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08007968 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7969 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08007970 filemap_fdatawrite_range(inode->i_mapping, offset,
7971 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04007972
Miao Xie09348562013-02-07 10:12:07 +00007973 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007974 /*
7975 * If the write DIO is beyond the EOF, we need update
7976 * the isize, but it is protected by i_mutex. So we can
7977 * not unlock the i_mutex at this case.
7978 */
7979 if (offset + count <= inode->i_size) {
7980 mutex_unlock(&inode->i_mutex);
7981 relock = true;
7982 }
Miao Xie09348562013-02-07 10:12:07 +00007983 ret = btrfs_delalloc_reserve_space(inode, count);
7984 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007985 goto out;
David Sterbaee39b432014-09-30 01:33:33 +02007986 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7987 &BTRFS_I(inode)->runtime_flags)) {
Miao Xie38851cc2013-02-08 07:04:11 +00007988 inode_dio_done(inode);
7989 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7990 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007991 }
7992
7993 ret = __blockdev_direct_IO(rw, iocb, inode,
7994 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05007995 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007996 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007997 if (rw & WRITE) {
7998 if (ret < 0 && ret != -EIOCBQUEUED)
7999 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07008000 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00008001 btrfs_delalloc_release_space(inode,
8002 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07008003 else
8004 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00008005 }
Miao Xie38851cc2013-02-08 07:04:11 +00008006out:
Miao Xie2e60a512013-02-08 07:01:08 +00008007 if (wakeup)
8008 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008009 if (relock)
8010 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008011
8012 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008013}
8014
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008015#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8016
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008017static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8018 __u64 start, __u64 len)
8019{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008020 int ret;
8021
8022 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8023 if (ret)
8024 return ret;
8025
Chris Masonec29ed52011-02-23 16:23:20 -05008026 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008027}
8028
Chris Mason9ebefb182007-06-15 13:50:00 -04008029int btrfs_readpage(struct file *file, struct page *page)
8030{
Chris Masond1310b22008-01-24 16:13:08 -05008031 struct extent_io_tree *tree;
8032 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008033 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008034}
Chris Mason1832a6d2007-12-21 16:27:21 -05008035
Chris Mason39279cc2007-06-12 06:35:45 -04008036static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8037{
Chris Masond1310b22008-01-24 16:13:08 -05008038 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04008039
8040
8041 if (current->flags & PF_MEMALLOC) {
8042 redirty_page_for_writepage(wbc, page);
8043 unlock_page(page);
8044 return 0;
8045 }
Chris Masond1310b22008-01-24 16:13:08 -05008046 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008047 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8048}
Chris Mason39279cc2007-06-12 06:35:45 -04008049
Eric Sandeen48a3b632013-04-25 20:41:01 +00008050static int btrfs_writepages(struct address_space *mapping,
8051 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008052{
Chris Masond1310b22008-01-24 16:13:08 -05008053 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008054
Chris Masond1310b22008-01-24 16:13:08 -05008055 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008056 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8057}
8058
Chris Mason3ab2fb52007-11-08 10:59:22 -05008059static int
8060btrfs_readpages(struct file *file, struct address_space *mapping,
8061 struct list_head *pages, unsigned nr_pages)
8062{
Chris Masond1310b22008-01-24 16:13:08 -05008063 struct extent_io_tree *tree;
8064 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008065 return extent_readpages(tree, mapping, pages, nr_pages,
8066 btrfs_get_extent);
8067}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008068static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008069{
Chris Masond1310b22008-01-24 16:13:08 -05008070 struct extent_io_tree *tree;
8071 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008072 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008073
Chris Masond1310b22008-01-24 16:13:08 -05008074 tree = &BTRFS_I(page->mapping->host)->io_tree;
8075 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008076 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008077 if (ret == 1) {
8078 ClearPagePrivate(page);
8079 set_page_private(page, 0);
8080 page_cache_release(page);
8081 }
8082 return ret;
8083}
Chris Mason39279cc2007-06-12 06:35:45 -04008084
Chris Masone6dcd2d2008-07-17 12:53:50 -04008085static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8086{
Chris Mason98509cf2008-09-11 15:51:43 -04008087 if (PageWriteback(page) || PageDirty(page))
8088 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008089 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008090}
8091
Lukas Czernerd47992f2013-05-21 23:17:23 -04008092static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8093 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008094{
Josef Bacik5fd02042012-05-02 14:00:54 -04008095 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008096 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008097 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008098 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008099 u64 page_start = page_offset(page);
8100 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008101 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008102
Chris Mason8b62b722009-09-02 16:53:46 -04008103 /*
8104 * we have the page locked, so new writeback can't start,
8105 * and the dirty bit won't be cleared while we are here.
8106 *
8107 * Wait for IO on this page so that we can safely clear
8108 * the PagePrivate2 bit and do ordered accounting
8109 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008110 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008111
Josef Bacik5fd02042012-05-02 14:00:54 -04008112 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008113 if (offset) {
8114 btrfs_releasepage(page, GFP_NOFS);
8115 return;
8116 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008117
8118 if (!inode_evicting)
8119 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8120 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008121 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008122 /*
8123 * IO on this page will never be started, so we need
8124 * to account for any ordered extents now
8125 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008126 if (!inode_evicting)
8127 clear_extent_bit(tree, page_start, page_end,
8128 EXTENT_DIRTY | EXTENT_DELALLOC |
8129 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8130 EXTENT_DEFRAG, 1, 0, &cached_state,
8131 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008132 /*
8133 * whoever cleared the private bit is responsible
8134 * for the finish_ordered_io
8135 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008136 if (TestClearPagePrivate2(page)) {
8137 struct btrfs_ordered_inode_tree *tree;
8138 u64 new_len;
8139
8140 tree = &BTRFS_I(inode)->ordered_tree;
8141
8142 spin_lock_irq(&tree->lock);
8143 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8144 new_len = page_start - ordered->file_offset;
8145 if (new_len < ordered->truncated_len)
8146 ordered->truncated_len = new_len;
8147 spin_unlock_irq(&tree->lock);
8148
8149 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8150 page_start,
8151 PAGE_CACHE_SIZE, 1))
8152 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008153 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008154 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008155 if (!inode_evicting) {
8156 cached_state = NULL;
8157 lock_extent_bits(tree, page_start, page_end, 0,
8158 &cached_state);
8159 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008160 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008161
8162 if (!inode_evicting) {
8163 clear_extent_bit(tree, page_start, page_end,
8164 EXTENT_LOCKED | EXTENT_DIRTY |
8165 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8166 EXTENT_DEFRAG, 1, 1,
8167 &cached_state, GFP_NOFS);
8168
8169 __btrfs_releasepage(page, GFP_NOFS);
8170 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008171
Chris Mason4a096752008-07-21 10:29:44 -04008172 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008173 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008174 ClearPagePrivate(page);
8175 set_page_private(page, 0);
8176 page_cache_release(page);
8177 }
Chris Mason39279cc2007-06-12 06:35:45 -04008178}
8179
Chris Mason9ebefb182007-06-15 13:50:00 -04008180/*
8181 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8182 * called from a page fault handler when a page is first dirtied. Hence we must
8183 * be careful to check for EOF conditions here. We set the page up correctly
8184 * for a written page which means we get ENOSPC checking when writing into
8185 * holes and correct delalloc and unwritten extent mapping on filesystems that
8186 * support these features.
8187 *
8188 * We are not allowed to take the i_mutex here so we have to play games to
8189 * protect against truncate races as the page could now be beyond EOF. Because
8190 * vmtruncate() writes the inode size before removing pages, once we have the
8191 * page lock we can determine safely if the page is beyond EOF. If it is not
8192 * beyond EOF, then the page is guaranteed safe against truncation until we
8193 * unlock the page.
8194 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008195int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008196{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008197 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008198 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008199 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008200 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8201 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008202 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008203 char *kaddr;
8204 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008205 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008206 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008207 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008208 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008209 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008210
Jan Karab2b5ef52012-06-12 16:20:45 +02008211 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04008212 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008213 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008214 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008215 reserved = 1;
8216 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008217 if (ret) {
8218 if (ret == -ENOMEM)
8219 ret = VM_FAULT_OOM;
8220 else /* -ENOSPC, -EIO, etc */
8221 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008222 if (reserved)
8223 goto out;
8224 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008225 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008226
Nick Piggin56a76f82009-03-31 15:23:23 -07008227 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008228again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008229 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008230 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008231 page_start = page_offset(page);
8232 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04008233
Chris Mason9ebefb182007-06-15 13:50:00 -04008234 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008235 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008236 /* page got truncated out from underneath us */
8237 goto out_unlock;
8238 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008239 wait_on_page_writeback(page);
8240
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008241 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008242 set_page_extent_mapped(page);
8243
Chris Masoneb84ae02008-07-17 13:53:27 -04008244 /*
8245 * we can't set the delalloc bits if there are pending ordered
8246 * extents. Drop our locks and wait for them to finish
8247 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008248 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8249 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008250 unlock_extent_cached(io_tree, page_start, page_end,
8251 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008252 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008253 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008254 btrfs_put_ordered_extent(ordered);
8255 goto again;
8256 }
8257
Josef Bacikfbf19082009-10-01 17:10:23 -04008258 /*
8259 * XXX - page_mkwrite gets called every time the page is dirtied, even
8260 * if it was already dirty, so for space accounting reasons we need to
8261 * clear any delalloc bits for the range we are fixing to save. There
8262 * is probably a better way to do this, but for now keep consistent with
8263 * prepare_pages in the normal write path.
8264 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008265 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008266 EXTENT_DIRTY | EXTENT_DELALLOC |
8267 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008268 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008269
Josef Bacik2ac55d42010-02-03 19:33:23 +00008270 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8271 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008272 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008273 unlock_extent_cached(io_tree, page_start, page_end,
8274 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008275 ret = VM_FAULT_SIGBUS;
8276 goto out_unlock;
8277 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008278 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008279
8280 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008281 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008282 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008283 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008284 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008285
Chris Masone6dcd2d2008-07-17 12:53:50 -04008286 if (zero_start != PAGE_CACHE_SIZE) {
8287 kaddr = kmap(page);
8288 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8289 flush_dcache_page(page);
8290 kunmap(page);
8291 }
Chris Mason247e7432008-07-17 12:53:51 -04008292 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008293 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008294 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008295
Chris Mason257c62e2009-10-13 13:21:08 -04008296 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8297 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008298 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008299
Josef Bacik2ac55d42010-02-03 19:33:23 +00008300 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008301
8302out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008303 if (!ret) {
8304 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008305 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008306 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008307 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008308out:
Josef Bacikec39e182012-01-12 19:10:12 -05008309 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008310out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008311 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008312 return ret;
8313}
8314
Josef Bacika41ad392011-01-31 15:30:16 -05008315static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008316{
8317 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008318 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008319 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008320 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008321 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008322 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008323 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008324
Josef Bacik0ef8b722013-10-25 16:13:35 -04008325 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8326 (u64)-1);
8327 if (ret)
8328 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008329
Josef Bacikfcb80c22011-05-03 10:40:22 -04008330 /*
8331 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8332 * 3 things going on here
8333 *
8334 * 1) We need to reserve space for our orphan item and the space to
8335 * delete our orphan item. Lord knows we don't want to have a dangling
8336 * orphan item because we didn't reserve space to remove it.
8337 *
8338 * 2) We need to reserve space to update our inode.
8339 *
8340 * 3) We need to have something to cache all the space that is going to
8341 * be free'd up by the truncate operation, but also have some slack
8342 * space reserved in case it uses space during the truncate (thank you
8343 * very much snapshotting).
8344 *
8345 * And we need these to all be seperate. The fact is we can use alot of
8346 * space doing the truncate, and we have no earthly idea how much space
8347 * we will use, so we need the truncate reservation to be seperate so it
8348 * doesn't end up using space reserved for updating the inode or
8349 * removing the orphan item. We also need to be able to stop the
8350 * transaction and start a new one, which means we need to be able to
8351 * update the inode several times, and we have no idea of knowing how
8352 * many times that will be, so we can't just reserve 1 item for the
8353 * entirety of the opration, so that has to be done seperately as well.
8354 * Then there is the orphan item, which does indeed need to be held on
8355 * to for the whole operation, and we need nobody to touch this reserved
8356 * space except the orphan code.
8357 *
8358 * So that leaves us with
8359 *
8360 * 1) root->orphan_block_rsv - for the orphan deletion.
8361 * 2) rsv - for the truncate reservation, which we will steal from the
8362 * transaction reservation.
8363 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8364 * updating the inode.
8365 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008366 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008367 if (!rsv)
8368 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008369 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008370 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008371
Josef Bacik907cbce2011-08-08 13:46:15 -04008372 /*
Josef Bacik07127182011-08-19 10:29:59 -04008373 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008374 * 1 for updating the inode.
8375 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008376 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008377 if (IS_ERR(trans)) {
8378 err = PTR_ERR(trans);
8379 goto out;
8380 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008381
Josef Bacik907cbce2011-08-08 13:46:15 -04008382 /* Migrate the slack space for the truncate to our reserve */
8383 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8384 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008385 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008386
Chris Mason5a3f23d2009-03-31 13:27:11 -04008387 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008388 * So if we truncate and then write and fsync we normally would just
8389 * write the extents that changed, which is a problem if we need to
8390 * first truncate that entire inode. So set this flag so we write out
8391 * all of the extents in the inode to the sync log so we're completely
8392 * safe.
8393 */
8394 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008395 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008396
Yan, Zheng80825102009-11-12 09:35:36 +00008397 while (1) {
8398 ret = btrfs_truncate_inode_items(trans, root, inode,
8399 inode->i_size,
8400 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008401 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05008402 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008403 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008404 }
Chris Mason39279cc2007-06-12 06:35:45 -04008405
Josef Bacikfcb80c22011-05-03 10:40:22 -04008406 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008407 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008408 if (ret) {
8409 err = ret;
8410 break;
8411 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008412
Yan, Zheng80825102009-11-12 09:35:36 +00008413 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008414 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008415
8416 trans = btrfs_start_transaction(root, 2);
8417 if (IS_ERR(trans)) {
8418 ret = err = PTR_ERR(trans);
8419 trans = NULL;
8420 break;
8421 }
8422
8423 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8424 rsv, min_size);
8425 BUG_ON(ret); /* shouldn't happen */
8426 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008427 }
8428
8429 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008430 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008431 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008432 if (ret)
8433 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008434 }
8435
Chris Mason917c16b2011-11-08 14:49:59 -05008436 if (trans) {
8437 trans->block_rsv = &root->fs_info->trans_block_rsv;
8438 ret = btrfs_update_inode(trans, root, inode);
8439 if (ret && !err)
8440 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008441
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008442 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008443 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008444 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008445
8446out:
8447 btrfs_free_block_rsv(root, rsv);
8448
Josef Bacik3893e332011-01-31 16:03:11 -05008449 if (ret && !err)
8450 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008451
Josef Bacik3893e332011-01-31 16:03:11 -05008452 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008453}
8454
Sven Wegener3b963622008-06-09 21:57:42 -04008455/*
Chris Masond352ac62008-09-29 15:18:18 -04008456 * create a new subvolume directory/inode (helper for the ioctl).
8457 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008458int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008459 struct btrfs_root *new_root,
8460 struct btrfs_root *parent_root,
8461 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008462{
Chris Mason39279cc2007-06-12 06:35:45 -04008463 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008464 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008465 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008466
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008467 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8468 new_dirid, new_dirid,
8469 S_IFDIR | (~current_umask() & S_IRWXUGO),
8470 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008471 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008472 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008473 inode->i_op = &btrfs_dir_inode_operations;
8474 inode->i_fop = &btrfs_dir_file_operations;
8475
Miklos Szeredibfe86842011-10-28 14:13:29 +02008476 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008477 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008478 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008479
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008480 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8481 if (err)
8482 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008483 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008484 new_root->root_key.objectid, err);
8485
Yan, Zheng76dda932009-09-21 16:00:26 -04008486 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008487
Yan, Zheng76dda932009-09-21 16:00:26 -04008488 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008489 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008490}
8491
Chris Mason39279cc2007-06-12 06:35:45 -04008492struct inode *btrfs_alloc_inode(struct super_block *sb)
8493{
8494 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008495 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008496
8497 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8498 if (!ei)
8499 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008500
8501 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008502 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008503 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008504 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008505 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008506 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008507 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008508 ei->disk_i_size = 0;
8509 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008510 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008511 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008512 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008513 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008514 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008515
Josef Bacik9e0baf62011-07-15 15:16:44 +00008516 spin_lock_init(&ei->lock);
8517 ei->outstanding_extents = 0;
8518 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008519
Josef Bacik72ac3c02012-05-23 14:13:11 -04008520 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008521 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008522
Miao Xie16cdcec2011-04-22 18:12:22 +08008523 ei->delayed_node = NULL;
8524
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008525 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008526 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008527 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8528 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008529 ei->io_tree.track_uptodate = 1;
8530 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008531 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008532 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008533 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008534 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008535 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008536 RB_CLEAR_NODE(&ei->rb_node);
8537
8538 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008539}
8540
Josef Bacikaaedb552013-10-11 14:44:09 -04008541#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8542void btrfs_test_destroy_inode(struct inode *inode)
8543{
8544 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8545 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8546}
8547#endif
8548
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008549static void btrfs_i_callback(struct rcu_head *head)
8550{
8551 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008552 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8553}
8554
Chris Mason39279cc2007-06-12 06:35:45 -04008555void btrfs_destroy_inode(struct inode *inode)
8556{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008557 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008558 struct btrfs_root *root = BTRFS_I(inode)->root;
8559
Al Virob3d9b7a2012-06-09 13:51:19 -04008560 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008561 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008562 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8563 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008564 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8565 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008566 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008567
Chris Mason5a3f23d2009-03-31 13:27:11 -04008568 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008569 * This can happen where we create an inode, but somebody else also
8570 * created the same inode and we need to destroy the one we already
8571 * created.
8572 */
8573 if (!root)
8574 goto free;
8575
Josef Bacik8a35d952012-05-23 14:26:42 -04008576 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8577 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008578 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008579 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008580 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008581 }
Josef Bacik7b128762008-07-24 12:17:14 -04008582
Chris Masond3977122009-01-05 21:25:51 -05008583 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008584 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8585 if (!ordered)
8586 break;
8587 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008588 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008589 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008590 btrfs_remove_ordered_extent(inode, ordered);
8591 btrfs_put_ordered_extent(ordered);
8592 btrfs_put_ordered_extent(ordered);
8593 }
8594 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008595 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008596 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008597free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008598 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008599}
8600
Al Viro45321ac2010-06-07 13:43:19 -04008601int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008602{
8603 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008604
Naohiro Aota6379ef92013-06-06 09:56:34 +00008605 if (root == NULL)
8606 return 1;
8607
Liu Bofa6ac872013-02-20 14:10:23 +00008608 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008609 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008610 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008611 else
Al Viro45321ac2010-06-07 13:43:19 -04008612 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008613}
8614
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008615static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008616{
8617 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8618
8619 inode_init_once(&ei->vfs_inode);
8620}
8621
8622void btrfs_destroy_cachep(void)
8623{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008624 /*
8625 * Make sure all delayed rcu free inodes are flushed before we
8626 * destroy cache.
8627 */
8628 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008629 if (btrfs_inode_cachep)
8630 kmem_cache_destroy(btrfs_inode_cachep);
8631 if (btrfs_trans_handle_cachep)
8632 kmem_cache_destroy(btrfs_trans_handle_cachep);
8633 if (btrfs_transaction_cachep)
8634 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008635 if (btrfs_path_cachep)
8636 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008637 if (btrfs_free_space_cachep)
8638 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008639 if (btrfs_delalloc_work_cachep)
8640 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008641}
8642
8643int btrfs_init_cachep(void)
8644{
David Sterba837e1972012-09-07 03:00:48 -06008645 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008646 sizeof(struct btrfs_inode), 0,
8647 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008648 if (!btrfs_inode_cachep)
8649 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008650
David Sterba837e1972012-09-07 03:00:48 -06008651 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008652 sizeof(struct btrfs_trans_handle), 0,
8653 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008654 if (!btrfs_trans_handle_cachep)
8655 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008656
David Sterba837e1972012-09-07 03:00:48 -06008657 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008658 sizeof(struct btrfs_transaction), 0,
8659 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008660 if (!btrfs_transaction_cachep)
8661 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008662
David Sterba837e1972012-09-07 03:00:48 -06008663 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008664 sizeof(struct btrfs_path), 0,
8665 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008666 if (!btrfs_path_cachep)
8667 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008668
David Sterba837e1972012-09-07 03:00:48 -06008669 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008670 sizeof(struct btrfs_free_space), 0,
8671 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8672 if (!btrfs_free_space_cachep)
8673 goto fail;
8674
Miao Xie8ccf6f192012-10-25 09:28:04 +00008675 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8676 sizeof(struct btrfs_delalloc_work), 0,
8677 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8678 NULL);
8679 if (!btrfs_delalloc_work_cachep)
8680 goto fail;
8681
Chris Mason39279cc2007-06-12 06:35:45 -04008682 return 0;
8683fail:
8684 btrfs_destroy_cachep();
8685 return -ENOMEM;
8686}
8687
8688static int btrfs_getattr(struct vfsmount *mnt,
8689 struct dentry *dentry, struct kstat *stat)
8690{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008691 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008692 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008693 u32 blocksize = inode->i_sb->s_blocksize;
8694
Chris Mason39279cc2007-06-12 06:35:45 -04008695 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008696 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008697 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008698
8699 spin_lock(&BTRFS_I(inode)->lock);
8700 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8701 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008702 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008703 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008704 return 0;
8705}
8706
Chris Masond3977122009-01-05 21:25:51 -05008707static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8708 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008709{
8710 struct btrfs_trans_handle *trans;
8711 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008712 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008713 struct inode *new_inode = new_dentry->d_inode;
8714 struct inode *old_inode = old_dentry->d_inode;
8715 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008716 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008717 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008718 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008719 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008720
Li Zefan33345d012011-04-20 10:31:50 +08008721 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008722 return -EPERM;
8723
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008724 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008725 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008726 return -EXDEV;
8727
Li Zefan33345d012011-04-20 10:31:50 +08008728 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8729 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008730 return -ENOTEMPTY;
8731
Chris Mason39279cc2007-06-12 06:35:45 -04008732 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008733 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008734 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008735
8736
8737 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008738 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008739 new_dentry->d_name.name,
8740 new_dentry->d_name.len);
8741
8742 if (ret) {
8743 if (ret == -EEXIST) {
8744 /* we shouldn't get
8745 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308746 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008747 return ret;
8748 }
8749 } else {
8750 /* maybe -EOVERFLOW */
8751 return ret;
8752 }
8753 }
8754 ret = 0;
8755
Chris Mason5a3f23d2009-03-31 13:27:11 -04008756 /*
Chris Mason8d875f92014-08-12 10:47:42 -07008757 * we're using rename to replace one file with another. Start IO on it
8758 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04008759 */
Chris Mason8d875f92014-08-12 10:47:42 -07008760 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04008761 filemap_flush(old_inode->i_mapping);
8762
Yan, Zheng76dda932009-09-21 16:00:26 -04008763 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008764 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008765 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008766 /*
8767 * We want to reserve the absolute worst case amount of items. So if
8768 * both inodes are subvols and we need to unlink them then that would
8769 * require 4 item modifications, but if they are both normal inodes it
8770 * would require 5 item modifications, so we'll assume their normal
8771 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8772 * should cover the worst case number of items we'll modify.
8773 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008774 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008775 if (IS_ERR(trans)) {
8776 ret = PTR_ERR(trans);
8777 goto out_notrans;
8778 }
Chris Mason5f39d392007-10-15 16:14:19 -04008779
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008780 if (dest != root)
8781 btrfs_record_root_in_trans(trans, dest);
8782
Yan, Zhenga5719522009-09-24 09:17:31 -04008783 ret = btrfs_set_inode_index(new_dir, &index);
8784 if (ret)
8785 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008786
Miao Xie67de1172013-12-26 13:07:06 +08008787 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008788 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008789 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08008790 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008791 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008792 ret = btrfs_insert_inode_ref(trans, dest,
8793 new_dentry->d_name.name,
8794 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008795 old_ino,
8796 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008797 if (ret)
8798 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008799 /*
8800 * this is an ugly little race, but the rename is required
8801 * to make sure that if we crash, the inode is either at the
8802 * old name or the new one. pinning the log transaction lets
8803 * us make sure we don't allow a log commit to come in after
8804 * we unlink the name but before we add the new name back in.
8805 */
8806 btrfs_pin_log_trans(root);
8807 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04008808
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008809 inode_inc_iversion(old_dir);
8810 inode_inc_iversion(new_dir);
8811 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008812 old_dir->i_ctime = old_dir->i_mtime = ctime;
8813 new_dir->i_ctime = new_dir->i_mtime = ctime;
8814 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008815
Chris Mason12fcfd22009-03-24 10:24:20 -04008816 if (old_dentry->d_parent != new_dentry->d_parent)
8817 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8818
Li Zefan33345d012011-04-20 10:31:50 +08008819 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008820 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8821 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8822 old_dentry->d_name.name,
8823 old_dentry->d_name.len);
8824 } else {
Al Viro92986792011-03-04 17:14:37 +00008825 ret = __btrfs_unlink_inode(trans, root, old_dir,
8826 old_dentry->d_inode,
8827 old_dentry->d_name.name,
8828 old_dentry->d_name.len);
8829 if (!ret)
8830 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008831 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008832 if (ret) {
8833 btrfs_abort_transaction(trans, root, ret);
8834 goto out_fail;
8835 }
Chris Mason39279cc2007-06-12 06:35:45 -04008836
8837 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008838 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008839 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008840 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008841 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8842 root_objectid = BTRFS_I(new_inode)->location.objectid;
8843 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8844 root_objectid,
8845 new_dentry->d_name.name,
8846 new_dentry->d_name.len);
8847 BUG_ON(new_inode->i_nlink == 0);
8848 } else {
8849 ret = btrfs_unlink_inode(trans, dest, new_dir,
8850 new_dentry->d_inode,
8851 new_dentry->d_name.name,
8852 new_dentry->d_name.len);
8853 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008854 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008855 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008856 if (ret) {
8857 btrfs_abort_transaction(trans, root, ret);
8858 goto out_fail;
8859 }
Chris Mason39279cc2007-06-12 06:35:45 -04008860 }
Josef Bacikaec74772008-07-24 12:12:38 -04008861
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008862 ret = btrfs_add_link(trans, new_dir, old_inode,
8863 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008864 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008865 if (ret) {
8866 btrfs_abort_transaction(trans, root, ret);
8867 goto out_fail;
8868 }
Chris Mason39279cc2007-06-12 06:35:45 -04008869
Miao Xie67de1172013-12-26 13:07:06 +08008870 if (old_inode->i_nlink == 1)
8871 BTRFS_I(old_inode)->dir_index = index;
8872
Li Zefan33345d012011-04-20 10:31:50 +08008873 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008874 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008875 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008876 btrfs_end_log_trans(root);
8877 }
Chris Mason39279cc2007-06-12 06:35:45 -04008878out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008879 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008880out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008881 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008882 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008883
Chris Mason39279cc2007-06-12 06:35:45 -04008884 return ret;
8885}
8886
Miklos Szeredi80ace852014-07-23 15:15:32 +02008887static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
8888 struct inode *new_dir, struct dentry *new_dentry,
8889 unsigned int flags)
8890{
8891 if (flags & ~RENAME_NOREPLACE)
8892 return -EINVAL;
8893
8894 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
8895}
8896
Miao Xie8ccf6f192012-10-25 09:28:04 +00008897static void btrfs_run_delalloc_work(struct btrfs_work *work)
8898{
8899 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008900 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008901
8902 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8903 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008904 inode = delalloc_work->inode;
8905 if (delalloc_work->wait) {
8906 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8907 } else {
8908 filemap_flush(inode->i_mapping);
8909 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8910 &BTRFS_I(inode)->runtime_flags))
8911 filemap_flush(inode->i_mapping);
8912 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008913
8914 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008915 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008916 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008917 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008918 complete(&delalloc_work->completion);
8919}
8920
8921struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8922 int wait, int delay_iput)
8923{
8924 struct btrfs_delalloc_work *work;
8925
8926 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8927 if (!work)
8928 return NULL;
8929
8930 init_completion(&work->completion);
8931 INIT_LIST_HEAD(&work->list);
8932 work->inode = inode;
8933 work->wait = wait;
8934 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08008935 WARN_ON_ONCE(!inode);
8936 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
8937 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008938
8939 return work;
8940}
8941
8942void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8943{
8944 wait_for_completion(&work->completion);
8945 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8946}
8947
Chris Masond352ac62008-09-29 15:18:18 -04008948/*
8949 * some fairly slow code that needs optimization. This walks the list
8950 * of all the inodes with pending delalloc and forces them to disk.
8951 */
Miao Xie6c255e62014-03-06 13:55:01 +08008952static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
8953 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04008954{
Chris Masonea8c2812008-08-04 23:17:27 -04008955 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008956 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008957 struct btrfs_delalloc_work *work, *next;
8958 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008959 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008960 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008961
Miao Xie8ccf6f192012-10-25 09:28:04 +00008962 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008963 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008964
Miao Xie573bfb72014-03-06 13:55:03 +08008965 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008966 spin_lock(&root->delalloc_lock);
8967 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008968 while (!list_empty(&splice)) {
8969 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008970 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008971
Miao Xieeb73c1b2013-05-15 07:48:22 +00008972 list_move_tail(&binode->delalloc_inodes,
8973 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008974 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008975 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008976 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008977 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008978 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008979 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008980
8981 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02008982 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008983 if (delay_iput)
8984 btrfs_add_delayed_iput(inode);
8985 else
8986 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008987 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008988 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008989 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008990 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08008991 btrfs_queue_work(root->fs_info->flush_workers,
8992 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08008993 ret++;
8994 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008995 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008996 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008997 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008998 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008999 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009000
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009001out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009002 list_for_each_entry_safe(work, next, &works, list) {
9003 list_del_init(&work->list);
9004 btrfs_wait_and_free_delalloc_work(work);
9005 }
9006
Miao Xieeb73c1b2013-05-15 07:48:22 +00009007 if (!list_empty_careful(&splice)) {
9008 spin_lock(&root->delalloc_lock);
9009 list_splice_tail(&splice, &root->delalloc_inodes);
9010 spin_unlock(&root->delalloc_lock);
9011 }
Miao Xie573bfb72014-03-06 13:55:03 +08009012 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009013 return ret;
9014}
9015
9016int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9017{
9018 int ret;
9019
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009020 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009021 return -EROFS;
9022
Miao Xie6c255e62014-03-06 13:55:01 +08009023 ret = __start_delalloc_inodes(root, delay_iput, -1);
9024 if (ret > 0)
9025 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009026 /*
9027 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009028 * we have to make sure the IO is actually started and that
9029 * ordered extents get created before we return
9030 */
9031 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009032 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009033 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009034 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009035 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9036 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009037 }
9038 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009039 return ret;
9040}
9041
Miao Xie6c255e62014-03-06 13:55:01 +08009042int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9043 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009044{
9045 struct btrfs_root *root;
9046 struct list_head splice;
9047 int ret;
9048
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009049 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009050 return -EROFS;
9051
9052 INIT_LIST_HEAD(&splice);
9053
Miao Xie573bfb72014-03-06 13:55:03 +08009054 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009055 spin_lock(&fs_info->delalloc_root_lock);
9056 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009057 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009058 root = list_first_entry(&splice, struct btrfs_root,
9059 delalloc_root);
9060 root = btrfs_grab_fs_root(root);
9061 BUG_ON(!root);
9062 list_move_tail(&root->delalloc_root,
9063 &fs_info->delalloc_roots);
9064 spin_unlock(&fs_info->delalloc_root_lock);
9065
Miao Xie6c255e62014-03-06 13:55:01 +08009066 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009067 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009068 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009069 goto out;
9070
Miao Xie6c255e62014-03-06 13:55:01 +08009071 if (nr != -1) {
9072 nr -= ret;
9073 WARN_ON(nr < 0);
9074 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009075 spin_lock(&fs_info->delalloc_root_lock);
9076 }
9077 spin_unlock(&fs_info->delalloc_root_lock);
9078
Miao Xie6c255e62014-03-06 13:55:01 +08009079 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009080 atomic_inc(&fs_info->async_submit_draining);
9081 while (atomic_read(&fs_info->nr_async_submits) ||
9082 atomic_read(&fs_info->async_delalloc_pages)) {
9083 wait_event(fs_info->async_submit_wait,
9084 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9085 atomic_read(&fs_info->async_delalloc_pages) == 0));
9086 }
9087 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009088out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009089 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009090 spin_lock(&fs_info->delalloc_root_lock);
9091 list_splice_tail(&splice, &fs_info->delalloc_roots);
9092 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009093 }
Miao Xie573bfb72014-03-06 13:55:03 +08009094 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009095 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009096}
9097
Chris Mason39279cc2007-06-12 06:35:45 -04009098static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9099 const char *symname)
9100{
9101 struct btrfs_trans_handle *trans;
9102 struct btrfs_root *root = BTRFS_I(dir)->root;
9103 struct btrfs_path *path;
9104 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009105 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009106 int err;
9107 int drop_inode = 0;
9108 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309109 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009110 int name_len;
9111 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009112 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009113 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009114 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009115
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009116 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009117 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9118 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009119
Josef Bacik9ed74f22009-09-11 16:12:44 -04009120 /*
9121 * 2 items for inode item and ref
9122 * 2 items for dir items
9123 * 1 item for xattr if selinux is on
9124 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009125 trans = btrfs_start_transaction(root, 5);
9126 if (IS_ERR(trans))
9127 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009128
Li Zefan581bb052011-04-20 10:06:11 +08009129 err = btrfs_find_free_ino(root, &objectid);
9130 if (err)
9131 goto out_unlock;
9132
Josef Bacikaec74772008-07-24 12:12:38 -04009133 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009134 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04009135 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009136 if (IS_ERR(inode)) {
9137 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009138 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009139 }
Chris Mason39279cc2007-06-12 06:35:45 -04009140
Casey Schauflerad19db72011-12-15 10:09:07 -05009141 /*
9142 * If the active LSM wants to access the inode during
9143 * d_instantiate it needs these. Smack checks to see
9144 * if the filesystem supports xattrs by looking at the
9145 * ops vector.
9146 */
9147 inode->i_fop = &btrfs_file_operations;
9148 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009149 inode->i_mapping->a_ops = &btrfs_aops;
9150 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9151 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9152
9153 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9154 if (err)
9155 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009156
Josef Bacika1b075d2010-11-19 20:36:11 +00009157 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009158 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009159 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009160
9161 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009162 if (!path) {
9163 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009164 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009165 }
Li Zefan33345d012011-04-20 10:31:50 +08009166 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009167 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009168 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009169 datasize = btrfs_file_extent_calc_inline_size(name_len);
9170 err = btrfs_insert_empty_item(trans, root, path, &key,
9171 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009172 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009173 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009174 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009175 }
Chris Mason5f39d392007-10-15 16:14:19 -04009176 leaf = path->nodes[0];
9177 ei = btrfs_item_ptr(leaf, path->slots[0],
9178 struct btrfs_file_extent_item);
9179 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9180 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009181 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009182 btrfs_set_file_extent_encryption(leaf, ei, 0);
9183 btrfs_set_file_extent_compression(leaf, ei, 0);
9184 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9185 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9186
Chris Mason39279cc2007-06-12 06:35:45 -04009187 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009188 write_extent_buffer(leaf, symname, ptr, name_len);
9189 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009190 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009191
Chris Mason39279cc2007-06-12 06:35:45 -04009192 inode->i_op = &btrfs_symlink_inode_operations;
9193 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04009194 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04009195 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009196 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009197 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009198 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009199 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009200 goto out_unlock_inode;
9201 }
9202
9203 unlock_new_inode(inode);
9204 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009205
9206out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009207 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009208 if (drop_inode) {
9209 inode_dec_link_count(inode);
9210 iput(inode);
9211 }
Liu Bob53d3f52012-11-14 14:34:34 +00009212 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009213 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009214
9215out_unlock_inode:
9216 drop_inode = 1;
9217 unlock_new_inode(inode);
9218 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009219}
Chris Mason16432982008-04-10 10:23:21 -04009220
Josef Bacik0af3d002010-06-21 14:48:16 -04009221static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9222 u64 start, u64 num_bytes, u64 min_size,
9223 loff_t actual_len, u64 *alloc_hint,
9224 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009225{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009226 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9227 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009228 struct btrfs_root *root = BTRFS_I(inode)->root;
9229 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009230 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009231 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009232 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009233 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009234 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009235
Josef Bacik0af3d002010-06-21 14:48:16 -04009236 if (trans)
9237 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009238 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009239 if (own_trans) {
9240 trans = btrfs_start_transaction(root, 3);
9241 if (IS_ERR(trans)) {
9242 ret = PTR_ERR(trans);
9243 break;
9244 }
Yan Zhengd899e052008-10-30 14:25:28 -04009245 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009246
Chris Mason154ea282013-03-05 11:11:26 -05009247 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9248 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009249 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009250 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009251 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009252 if (own_trans)
9253 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009254 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009255 }
9256
Yan Zhengd899e052008-10-30 14:25:28 -04009257 ret = insert_reserved_file_extent(trans, inode,
9258 cur_offset, ins.objectid,
9259 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009260 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009261 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009262 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009263 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009264 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009265 btrfs_abort_transaction(trans, root, ret);
9266 if (own_trans)
9267 btrfs_end_transaction(trans, root);
9268 break;
9269 }
Chris Masona1ed8352009-09-11 12:27:37 -04009270 btrfs_drop_extent_cache(inode, cur_offset,
9271 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009272
Josef Bacik5dc562c2012-08-17 13:14:17 -04009273 em = alloc_extent_map();
9274 if (!em) {
9275 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9276 &BTRFS_I(inode)->runtime_flags);
9277 goto next;
9278 }
9279
9280 em->start = cur_offset;
9281 em->orig_start = cur_offset;
9282 em->len = ins.offset;
9283 em->block_start = ins.objectid;
9284 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009285 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009286 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009287 em->bdev = root->fs_info->fs_devices->latest_bdev;
9288 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9289 em->generation = trans->transid;
9290
9291 while (1) {
9292 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009293 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009294 write_unlock(&em_tree->lock);
9295 if (ret != -EEXIST)
9296 break;
9297 btrfs_drop_extent_cache(inode, cur_offset,
9298 cur_offset + ins.offset - 1,
9299 0);
9300 }
9301 free_extent_map(em);
9302next:
Yan Zhengd899e052008-10-30 14:25:28 -04009303 num_bytes -= ins.offset;
9304 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009305 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009306
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009307 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009308 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009309 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009310 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009311 (actual_len > inode->i_size) &&
9312 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009313 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009314 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009315 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009316 i_size = cur_offset;
9317 i_size_write(inode, i_size);
9318 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009319 }
9320
Yan Zhengd899e052008-10-30 14:25:28 -04009321 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009322
9323 if (ret) {
9324 btrfs_abort_transaction(trans, root, ret);
9325 if (own_trans)
9326 btrfs_end_transaction(trans, root);
9327 break;
9328 }
Yan Zhengd899e052008-10-30 14:25:28 -04009329
Josef Bacik0af3d002010-06-21 14:48:16 -04009330 if (own_trans)
9331 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009332 }
Yan Zhengd899e052008-10-30 14:25:28 -04009333 return ret;
9334}
9335
Josef Bacik0af3d002010-06-21 14:48:16 -04009336int btrfs_prealloc_file_range(struct inode *inode, int mode,
9337 u64 start, u64 num_bytes, u64 min_size,
9338 loff_t actual_len, u64 *alloc_hint)
9339{
9340 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9341 min_size, actual_len, alloc_hint,
9342 NULL);
9343}
9344
9345int btrfs_prealloc_file_range_trans(struct inode *inode,
9346 struct btrfs_trans_handle *trans, int mode,
9347 u64 start, u64 num_bytes, u64 min_size,
9348 loff_t actual_len, u64 *alloc_hint)
9349{
9350 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9351 min_size, actual_len, alloc_hint, trans);
9352}
9353
Chris Masone6dcd2d2008-07-17 12:53:50 -04009354static int btrfs_set_page_dirty(struct page *page)
9355{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009356 return __set_page_dirty_nobuffers(page);
9357}
9358
Al Viro10556cb2011-06-20 19:28:19 -04009359static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009360{
Li Zefanb83cc962010-12-20 16:04:08 +08009361 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009362 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009363
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009364 if (mask & MAY_WRITE &&
9365 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9366 if (btrfs_root_readonly(root))
9367 return -EROFS;
9368 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9369 return -EACCES;
9370 }
Al Viro2830ba72011-06-20 19:16:29 -04009371 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009372}
Chris Mason39279cc2007-06-12 06:35:45 -04009373
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009374static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9375{
9376 struct btrfs_trans_handle *trans;
9377 struct btrfs_root *root = BTRFS_I(dir)->root;
9378 struct inode *inode = NULL;
9379 u64 objectid;
9380 u64 index;
9381 int ret = 0;
9382
9383 /*
9384 * 5 units required for adding orphan entry
9385 */
9386 trans = btrfs_start_transaction(root, 5);
9387 if (IS_ERR(trans))
9388 return PTR_ERR(trans);
9389
9390 ret = btrfs_find_free_ino(root, &objectid);
9391 if (ret)
9392 goto out;
9393
9394 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9395 btrfs_ino(dir), objectid, mode, &index);
9396 if (IS_ERR(inode)) {
9397 ret = PTR_ERR(inode);
9398 inode = NULL;
9399 goto out;
9400 }
9401
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009402 inode->i_fop = &btrfs_file_operations;
9403 inode->i_op = &btrfs_file_inode_operations;
9404
9405 inode->i_mapping->a_ops = &btrfs_aops;
9406 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9407 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9408
Chris Masonb0d5d102014-09-08 13:08:51 -07009409 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9410 if (ret)
9411 goto out_inode;
9412
9413 ret = btrfs_update_inode(trans, root, inode);
9414 if (ret)
9415 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009416 ret = btrfs_orphan_add(trans, inode);
9417 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009418 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009419
Filipe Manana5762b5c2014-08-01 00:10:32 +01009420 /*
9421 * We set number of links to 0 in btrfs_new_inode(), and here we set
9422 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9423 * through:
9424 *
9425 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9426 */
9427 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009428 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009429 d_tmpfile(dentry, inode);
9430 mark_inode_dirty(inode);
9431
9432out:
9433 btrfs_end_transaction(trans, root);
9434 if (ret)
9435 iput(inode);
9436 btrfs_balance_delayed_items(root);
9437 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009438 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009439
9440out_inode:
9441 unlock_new_inode(inode);
9442 goto out;
9443
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009444}
9445
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009446static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009447 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009448 .lookup = btrfs_lookup,
9449 .create = btrfs_create,
9450 .unlink = btrfs_unlink,
9451 .link = btrfs_link,
9452 .mkdir = btrfs_mkdir,
9453 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009454 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009455 .symlink = btrfs_symlink,
9456 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009457 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009458 .setxattr = btrfs_setxattr,
9459 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009460 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009461 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009462 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009463 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009464 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009465 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009466 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009467};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009468static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009469 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009470 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009471 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009472 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009473 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009474};
Yan, Zheng76dda932009-09-21 16:00:26 -04009475
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009476static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009477 .llseek = generic_file_llseek,
9478 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009479 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009480 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009481#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009482 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009483#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009484 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009485 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009486};
9487
Chris Masond1310b22008-01-24 16:13:08 -05009488static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009489 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009490 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009491 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009492 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009493 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009494 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009495 .set_bit_hook = btrfs_set_bit_hook,
9496 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009497 .merge_extent_hook = btrfs_merge_extent_hook,
9498 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009499};
9500
Chris Mason35054392009-01-21 13:11:13 -05009501/*
9502 * btrfs doesn't support the bmap operation because swapfiles
9503 * use bmap to make a mapping of extents in the file. They assume
9504 * these extents won't change over the life of the file and they
9505 * use the bmap result to do IO directly to the drive.
9506 *
9507 * the btrfs bmap call would return logical addresses that aren't
9508 * suitable for IO and they also will change frequently as COW
9509 * operations happen. So, swapfile + btrfs == corruption.
9510 *
9511 * For now we're avoiding this by dropping bmap.
9512 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009513static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009514 .readpage = btrfs_readpage,
9515 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04009516 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009517 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009518 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009519 .invalidatepage = btrfs_invalidatepage,
9520 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009521 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009522 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009523};
9524
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009525static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009526 .readpage = btrfs_readpage,
9527 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009528 .invalidatepage = btrfs_invalidatepage,
9529 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009530};
9531
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009532static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009533 .getattr = btrfs_getattr,
9534 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009535 .setxattr = btrfs_setxattr,
9536 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009537 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009538 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009539 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009540 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009541 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009542 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009543 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009544};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009545static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009546 .getattr = btrfs_getattr,
9547 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009548 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009549 .setxattr = btrfs_setxattr,
9550 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009551 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009552 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009553 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009554 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009555 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009556};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009557static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009558 .readlink = generic_readlink,
9559 .follow_link = page_follow_link_light,
9560 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009561 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009562 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009563 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009564 .setxattr = btrfs_setxattr,
9565 .getxattr = btrfs_getxattr,
9566 .listxattr = btrfs_listxattr,
9567 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009568 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009569};
Yan, Zheng76dda932009-09-21 16:00:26 -04009570
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009571const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009572 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009573 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009574};