blob: 6fed30c3f9ced9b00ed4e0665e12d3d89cfa7c08 [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 Masonb888db22007-08-27 16:49:44 -0400390{
391 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400392 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400393 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400394 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500395 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400396 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400397 struct page **pages = NULL;
398 unsigned long nr_pages;
399 unsigned long nr_pages_ret = 0;
400 unsigned long total_compressed = 0;
401 unsigned long total_in = 0;
402 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500403 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400404 int i;
405 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800406 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400407 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400408
Liu Bo4cb13e52012-03-29 09:57:45 -0400409 /* if this is a small write inside eof, kick off a defrag */
410 if ((end - start + 1) < 16 * 1024 &&
411 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400412 btrfs_add_inode_defrag(NULL, inode);
413
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 Masonf03d9302009-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 Masonf03d9302009-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
Filipe Manana40ae8372014-10-06 22:14:24 +0100636static void free_async_extent_pages(struct async_extent *async_extent)
637{
638 int i;
639
640 if (!async_extent->pages)
641 return;
642
643 for (i = 0; i < async_extent->nr_pages; i++) {
644 WARN_ON(async_extent->pages[i]->mapping);
645 page_cache_release(async_extent->pages[i]);
646 }
647 kfree(async_extent->pages);
648 async_extent->nr_pages = 0;
649 async_extent->pages = NULL;
650}
651
Chris Mason771ed682008-11-06 22:02:51 -0500652/*
653 * phase two of compressed writeback. This is the ordered portion
654 * of the code, which only gets called in the order the work was
655 * queued. We walk all the async extents created by compress_file_range
656 * and send them down to the disk.
657 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100658static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500659 struct async_cow *async_cow)
660{
661 struct async_extent *async_extent;
662 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500663 struct btrfs_key ins;
664 struct extent_map *em;
665 struct btrfs_root *root = BTRFS_I(inode)->root;
666 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
667 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500668 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500669
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500670again:
Chris Masond3977122009-01-05 21:25:51 -0500671 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500672 async_extent = list_entry(async_cow->extents.next,
673 struct async_extent, list);
674 list_del(&async_extent->list);
675
676 io_tree = &BTRFS_I(inode)->io_tree;
677
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500678retry:
Chris Mason771ed682008-11-06 22:02:51 -0500679 /* did the compression code fall back to uncompressed IO? */
680 if (!async_extent->pages) {
681 int page_started = 0;
682 unsigned long nr_written = 0;
683
684 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000685 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100686 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500687
688 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500689 ret = cow_file_range(inode, async_cow->locked_page,
690 async_extent->start,
691 async_extent->start +
692 async_extent->ram_size - 1,
693 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500694
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100695 /* JDM XXX */
696
Chris Mason771ed682008-11-06 22:02:51 -0500697 /*
698 * if page_started, cow_file_range inserted an
699 * inline extent and took care of all the unlocking
700 * and IO for us. Otherwise, we need to submit
701 * all those pages down to the drive.
702 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500703 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500704 extent_write_locked_range(io_tree,
705 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500706 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500707 async_extent->ram_size - 1,
708 btrfs_get_extent,
709 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500710 else if (ret)
711 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500712 kfree(async_extent);
713 cond_resched();
714 continue;
715 }
716
717 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100718 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500719
Josef Bacik00361582013-08-14 14:02:47 -0400720 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500721 async_extent->compressed_size,
722 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800723 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500724 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100725 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500726
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400727 if (ret == -ENOSPC) {
728 unlock_extent(io_tree, async_extent->start,
729 async_extent->start +
730 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800731
732 /*
733 * we need to redirty the pages if we decide to
734 * fallback to uncompressed IO, otherwise we
735 * will not submit these pages down to lower
736 * layers.
737 */
738 extent_range_redirty_for_io(inode,
739 async_extent->start,
740 async_extent->start +
741 async_extent->ram_size - 1);
742
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100743 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400744 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500745 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500746 }
747
Yan, Zhengc2167752009-11-12 09:34:21 +0000748 /*
749 * here we're doing allocation and writeback of the
750 * compressed pages
751 */
752 btrfs_drop_extent_cache(inode, async_extent->start,
753 async_extent->start +
754 async_extent->ram_size - 1, 0);
755
David Sterba172ddd62011-04-21 00:48:27 +0200756 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000757 if (!em) {
758 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500759 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000760 }
Chris Mason771ed682008-11-06 22:02:51 -0500761 em->start = async_extent->start;
762 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500763 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400764 em->mod_start = em->start;
765 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500766
767 em->block_start = ins.objectid;
768 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500769 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400770 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500771 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800772 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500773 set_bit(EXTENT_FLAG_PINNED, &em->flags);
774 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400775 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500776
Chris Masond3977122009-01-05 21:25:51 -0500777 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400778 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400779 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400780 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500781 if (ret != -EEXIST) {
782 free_extent_map(em);
783 break;
784 }
785 btrfs_drop_extent_cache(inode, async_extent->start,
786 async_extent->start +
787 async_extent->ram_size - 1, 0);
788 }
789
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500790 if (ret)
791 goto out_free_reserve;
792
Li Zefan261507a02010-12-17 14:21:50 +0800793 ret = btrfs_add_ordered_extent_compress(inode,
794 async_extent->start,
795 ins.objectid,
796 async_extent->ram_size,
797 ins.offset,
798 BTRFS_ORDERED_COMPRESSED,
799 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100800 if (ret) {
801 btrfs_drop_extent_cache(inode, async_extent->start,
802 async_extent->start +
803 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500804 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100805 }
Chris Mason771ed682008-11-06 22:02:51 -0500806
Chris Mason771ed682008-11-06 22:02:51 -0500807 /*
808 * clear dirty, set writeback and unlock the pages.
809 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400810 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400811 async_extent->start +
812 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400813 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
814 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400815 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500816 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500817 async_extent->start,
818 async_extent->ram_size,
819 ins.objectid,
820 ins.offset, async_extent->pages,
821 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100822 if (ret) {
823 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
824 struct page *p = async_extent->pages[0];
825 const u64 start = async_extent->start;
826 const u64 end = start + async_extent->ram_size - 1;
827
828 p->mapping = inode->i_mapping;
829 tree->ops->writepage_end_io_hook(p, start, end,
830 NULL, 0);
831 p->mapping = NULL;
832 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
833 PAGE_END_WRITEBACK |
834 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100835 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100836 }
Chris Mason771ed682008-11-06 22:02:51 -0500837 alloc_hint = ins.objectid + ins.offset;
838 kfree(async_extent);
839 cond_resched();
840 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100841 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500842out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800843 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100844out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400845 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500846 async_extent->start +
847 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400848 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400849 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
850 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100851 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
852 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100853 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100854 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500855 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500856}
857
Josef Bacik4b46fce2010-05-23 11:00:55 -0400858static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
859 u64 num_bytes)
860{
861 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
862 struct extent_map *em;
863 u64 alloc_hint = 0;
864
865 read_lock(&em_tree->lock);
866 em = search_extent_mapping(em_tree, start, num_bytes);
867 if (em) {
868 /*
869 * if block start isn't an actual block number then find the
870 * first block in this inode and use that as a hint. If that
871 * block is also bogus then just don't worry about it.
872 */
873 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
874 free_extent_map(em);
875 em = search_extent_mapping(em_tree, 0, 0);
876 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
877 alloc_hint = em->block_start;
878 if (em)
879 free_extent_map(em);
880 } else {
881 alloc_hint = em->block_start;
882 free_extent_map(em);
883 }
884 }
885 read_unlock(&em_tree->lock);
886
887 return alloc_hint;
888}
889
Chris Mason771ed682008-11-06 22:02:51 -0500890/*
891 * when extent_io.c finds a delayed allocation range in the file,
892 * the call backs end up in this code. The basic idea is to
893 * allocate extents on disk for the range, and create ordered data structs
894 * in ram to track those extents.
895 *
896 * locked_page is the page that writepage had locked already. We use
897 * it to make sure we don't do extra locks or unlocks.
898 *
899 * *page_started is set to one if we unlock locked_page and do everything
900 * required to start IO on it. It may be clean and already done with
901 * IO when we return.
902 */
Josef Bacik00361582013-08-14 14:02:47 -0400903static noinline int cow_file_range(struct inode *inode,
904 struct page *locked_page,
905 u64 start, u64 end, int *page_started,
906 unsigned long *nr_written,
907 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500908{
Josef Bacik00361582013-08-14 14:02:47 -0400909 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500910 u64 alloc_hint = 0;
911 u64 num_bytes;
912 unsigned long ram_size;
913 u64 disk_num_bytes;
914 u64 cur_alloc_size;
915 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500916 struct btrfs_key ins;
917 struct extent_map *em;
918 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
919 int ret = 0;
920
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400921 if (btrfs_is_free_space_inode(inode)) {
922 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500923 ret = -EINVAL;
924 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400925 }
Chris Mason771ed682008-11-06 22:02:51 -0500926
Qu Wenruofda28322013-02-26 08:10:22 +0000927 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500928 num_bytes = max(blocksize, num_bytes);
929 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500930
Chris Mason4cb53002011-05-24 15:35:30 -0400931 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400932 if (num_bytes < 64 * 1024 &&
933 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400934 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400935
Chris Mason771ed682008-11-06 22:02:51 -0500936 if (start == 0) {
937 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400938 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
939 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500940 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400941 extent_clear_unlock_delalloc(inode, start, end, NULL,
942 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400943 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400944 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
945 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000946
Chris Mason771ed682008-11-06 22:02:51 -0500947 *nr_written = *nr_written +
948 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
949 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500950 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100951 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100952 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500953 }
954 }
Chris Masonc8b97812008-10-29 14:49:59 -0400955
956 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200957 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400958
Josef Bacik4b46fce2010-05-23 11:00:55 -0400959 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400960 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400961
Chris Masond3977122009-01-05 21:25:51 -0500962 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400963 unsigned long op;
964
Josef Bacik287a0ab2010-03-19 18:07:23 +0000965 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400966 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500967 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800968 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400969 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100970 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500971
David Sterba172ddd62011-04-21 00:48:27 +0200972 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000973 if (!em) {
974 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000975 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000976 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400977 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500978 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500979 ram_size = ins.offset;
980 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400981 em->mod_start = em->start;
982 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400983
Chris Masone6dcd2d2008-07-17 12:53:50 -0400984 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400985 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500986 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400987 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400988 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400989 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400990 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400991
Chris Masond3977122009-01-05 21:25:51 -0500992 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400993 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400994 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400995 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400996 if (ret != -EEXIST) {
997 free_extent_map(em);
998 break;
999 }
1000 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001001 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001002 }
Liu Boace68ba2013-04-22 10:53:47 +00001003 if (ret)
1004 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001005
Chris Mason98d20f62008-04-14 09:46:10 -04001006 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001007 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001008 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001009 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001010 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001011
Yan Zheng17d217f2008-12-12 10:03:38 -05001012 if (root->root_key.objectid ==
1013 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1014 ret = btrfs_reloc_clone_csums(inode, start,
1015 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001016 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001017 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001018 }
1019
Chris Masond3977122009-01-05 21:25:51 -05001020 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001021 break;
Chris Masond3977122009-01-05 21:25:51 -05001022
Chris Masonc8b97812008-10-29 14:49:59 -04001023 /* we're not doing compressed IO, don't unlock the first
1024 * page (which the caller expects to stay locked), don't
1025 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001026 *
1027 * Do set the Private2 bit so we know this page was properly
1028 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001029 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001030 op = unlock ? PAGE_UNLOCK : 0;
1031 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001032
Josef Bacikc2790a22013-07-29 11:20:47 -04001033 extent_clear_unlock_delalloc(inode, start,
1034 start + ram_size - 1, locked_page,
1035 EXTENT_LOCKED | EXTENT_DELALLOC,
1036 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001037 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001038 num_bytes -= cur_alloc_size;
1039 alloc_hint = ins.objectid + ins.offset;
1040 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001041 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001042out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001043 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001044
Filipe Mananad9f85962014-08-25 10:43:00 +01001045out_drop_extent_cache:
1046 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001047out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001048 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001049out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001050 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001051 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1052 EXTENT_DELALLOC | EXTENT_DEFRAG,
1053 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1054 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001055 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001056}
Chris Masonc8b97812008-10-29 14:49:59 -04001057
Chris Mason771ed682008-11-06 22:02:51 -05001058/*
1059 * work queue call back to started compression on a file and pages
1060 */
1061static noinline void async_cow_start(struct btrfs_work *work)
1062{
1063 struct async_cow *async_cow;
1064 int num_added = 0;
1065 async_cow = container_of(work, struct async_cow, work);
1066
1067 compress_file_range(async_cow->inode, async_cow->locked_page,
1068 async_cow->start, async_cow->end, async_cow,
1069 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001070 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001071 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001072 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001073 }
Chris Mason771ed682008-11-06 22:02:51 -05001074}
1075
1076/*
1077 * work queue call back to submit previously compressed pages
1078 */
1079static noinline void async_cow_submit(struct btrfs_work *work)
1080{
1081 struct async_cow *async_cow;
1082 struct btrfs_root *root;
1083 unsigned long nr_pages;
1084
1085 async_cow = container_of(work, struct async_cow, work);
1086
1087 root = async_cow->root;
1088 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1089 PAGE_CACHE_SHIFT;
1090
Josef Bacik66657b32012-08-01 15:36:24 -04001091 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001092 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001093 waitqueue_active(&root->fs_info->async_submit_wait))
1094 wake_up(&root->fs_info->async_submit_wait);
1095
Chris Masond3977122009-01-05 21:25:51 -05001096 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001097 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001098}
Chris Masonc8b97812008-10-29 14:49:59 -04001099
Chris Mason771ed682008-11-06 22:02:51 -05001100static noinline void async_cow_free(struct btrfs_work *work)
1101{
1102 struct async_cow *async_cow;
1103 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001104 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001105 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001106 kfree(async_cow);
1107}
1108
1109static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1110 u64 start, u64 end, int *page_started,
1111 unsigned long *nr_written)
1112{
1113 struct async_cow *async_cow;
1114 struct btrfs_root *root = BTRFS_I(inode)->root;
1115 unsigned long nr_pages;
1116 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001117 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001118
Chris Masona3429ab2009-10-08 12:30:20 -04001119 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1120 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001121 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001122 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001123 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001124 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001125 async_cow->root = root;
1126 async_cow->locked_page = locked_page;
1127 async_cow->start = start;
1128
Wang Shilongf79707b2014-07-17 11:44:09 +08001129 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1130 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001131 cur_end = end;
1132 else
1133 cur_end = min(end, start + 512 * 1024 - 1);
1134
1135 async_cow->end = cur_end;
1136 INIT_LIST_HEAD(&async_cow->extents);
1137
Liu Bo9e0af232014-08-15 23:36:53 +08001138 btrfs_init_work(&async_cow->work,
1139 btrfs_delalloc_helper,
1140 async_cow_start, async_cow_submit,
1141 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001142
Chris Mason771ed682008-11-06 22:02:51 -05001143 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1144 PAGE_CACHE_SHIFT;
1145 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1146
Qu Wenruoafe3d242014-02-28 10:46:07 +08001147 btrfs_queue_work(root->fs_info->delalloc_workers,
1148 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001149
1150 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1151 wait_event(root->fs_info->async_submit_wait,
1152 (atomic_read(&root->fs_info->async_delalloc_pages) <
1153 limit));
1154 }
1155
Chris Masond3977122009-01-05 21:25:51 -05001156 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001157 atomic_read(&root->fs_info->async_delalloc_pages)) {
1158 wait_event(root->fs_info->async_submit_wait,
1159 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1160 0));
1161 }
1162
1163 *nr_written += nr_pages;
1164 start = cur_end + 1;
1165 }
1166 *page_started = 1;
1167 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001168}
1169
Chris Masond3977122009-01-05 21:25:51 -05001170static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001171 u64 bytenr, u64 num_bytes)
1172{
1173 int ret;
1174 struct btrfs_ordered_sum *sums;
1175 LIST_HEAD(list);
1176
Yan Zheng07d400a2009-01-06 11:42:00 -05001177 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001178 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001179 if (ret == 0 && list_empty(&list))
1180 return 0;
1181
1182 while (!list_empty(&list)) {
1183 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1184 list_del(&sums->list);
1185 kfree(sums);
1186 }
1187 return 1;
1188}
1189
Chris Masond352ac62008-09-29 15:18:18 -04001190/*
1191 * when nowcow writeback call back. This checks for snapshots or COW copies
1192 * of the extents that exist in the file, and COWs the file as required.
1193 *
1194 * If no cow copies or snapshots exist, we write directly to the existing
1195 * blocks on disk
1196 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001197static noinline int run_delalloc_nocow(struct inode *inode,
1198 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001199 u64 start, u64 end, int *page_started, int force,
1200 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001201{
Chris Masonbe20aa92007-12-17 20:14:01 -05001202 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001203 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001204 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001205 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001206 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001207 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001208 u64 cow_start;
1209 u64 cur_offset;
1210 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001211 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001212 u64 disk_bytenr;
1213 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001214 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001215 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001216 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001217 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001218 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001219 int nocow;
1220 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001221 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001222 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001223
1224 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001225 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001226 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1227 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001228 EXTENT_DO_ACCOUNTING |
1229 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001230 PAGE_CLEAR_DIRTY |
1231 PAGE_SET_WRITEBACK |
1232 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001233 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001234 }
Li Zefan82d59022011-04-20 10:33:24 +08001235
Liu Bo83eea1f2012-07-10 05:28:39 -06001236 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001237
1238 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001239 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001240 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001241 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001242
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001243 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001244 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1245 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001246 EXTENT_DO_ACCOUNTING |
1247 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001248 PAGE_CLEAR_DIRTY |
1249 PAGE_SET_WRITEBACK |
1250 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001251 btrfs_free_path(path);
1252 return PTR_ERR(trans);
1253 }
1254
Josef Bacik74b21072011-04-13 12:02:53 -04001255 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001256
Yan Zheng80ff3852008-10-30 14:20:02 -04001257 cow_start = (u64)-1;
1258 cur_offset = start;
1259 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001260 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001262 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001263 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001264 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1265 leaf = path->nodes[0];
1266 btrfs_item_key_to_cpu(leaf, &found_key,
1267 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001268 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 found_key.type == BTRFS_EXTENT_DATA_KEY)
1270 path->slots[0]--;
1271 }
1272 check_prev = 0;
1273next_slot:
1274 leaf = path->nodes[0];
1275 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1276 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001277 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001278 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 if (ret > 0)
1280 break;
1281 leaf = path->nodes[0];
1282 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001283
Yan Zheng80ff3852008-10-30 14:20:02 -04001284 nocow = 0;
1285 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001286 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001288
Li Zefan33345d012011-04-20 10:31:50 +08001289 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1291 found_key.offset > end)
1292 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001293
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 if (found_key.offset > cur_offset) {
1295 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001296 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 goto out_check;
1298 }
Chris Masonc31f8832008-01-08 15:46:31 -05001299
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 fi = btrfs_item_ptr(leaf, path->slots[0],
1301 struct btrfs_file_extent_item);
1302 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001303
Josef Bacikcc95bef2013-04-04 14:31:27 -04001304 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001305 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1306 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001307 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001308 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 extent_end = found_key.offset +
1310 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001311 disk_num_bytes =
1312 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 if (extent_end <= start) {
1314 path->slots[0]++;
1315 goto next_slot;
1316 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001317 if (disk_bytenr == 0)
1318 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001319 if (btrfs_file_extent_compression(leaf, fi) ||
1320 btrfs_file_extent_encryption(leaf, fi) ||
1321 btrfs_file_extent_other_encoding(leaf, fi))
1322 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001323 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1324 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001325 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001326 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001327 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001328 found_key.offset -
1329 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001330 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001331 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001332 disk_bytenr += cur_offset - found_key.offset;
1333 num_bytes = min(end + 1, extent_end) - cur_offset;
1334 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001335 * if there are pending snapshots for this root,
1336 * we fall into common COW way.
1337 */
1338 if (!nolock) {
1339 err = btrfs_start_nocow_write(root);
1340 if (!err)
1341 goto out_check;
1342 }
1343 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001344 * force cow if csum exists in the range.
1345 * this ensure that csum for a given extent are
1346 * either valid or do not exist.
1347 */
1348 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1349 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001350 nocow = 1;
1351 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1352 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001353 btrfs_file_extent_inline_len(leaf,
1354 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001355 extent_end = ALIGN(extent_end, root->sectorsize);
1356 } else {
1357 BUG_ON(1);
1358 }
1359out_check:
1360 if (extent_end <= start) {
1361 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001362 if (!nolock && nocow)
1363 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 goto next_slot;
1365 }
1366 if (!nocow) {
1367 if (cow_start == (u64)-1)
1368 cow_start = cur_offset;
1369 cur_offset = extent_end;
1370 if (cur_offset > end)
1371 break;
1372 path->slots[0]++;
1373 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001374 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001375
David Sterbab3b4aa72011-04-21 01:20:15 +02001376 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001377 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001378 ret = cow_file_range(inode, locked_page,
1379 cow_start, found_key.offset - 1,
1380 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001381 if (ret) {
1382 if (!nolock && nocow)
1383 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001384 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001385 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001386 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001387 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001388
Yan Zhengd899e052008-10-30 14:25:28 -04001389 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1390 struct extent_map *em;
1391 struct extent_map_tree *em_tree;
1392 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001393 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001394 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001395 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001396 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001397 em->len = num_bytes;
1398 em->block_len = num_bytes;
1399 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001400 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001401 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001402 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001403 em->mod_start = em->start;
1404 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001405 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001406 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001407 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001408 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001409 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001410 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001411 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001412 if (ret != -EEXIST) {
1413 free_extent_map(em);
1414 break;
1415 }
1416 btrfs_drop_extent_cache(inode, em->start,
1417 em->start + em->len - 1, 0);
1418 }
1419 type = BTRFS_ORDERED_PREALLOC;
1420 } else {
1421 type = BTRFS_ORDERED_NOCOW;
1422 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001423
1424 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001425 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001426 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001427
Yan, Zhengefa56462010-05-16 10:49:59 -04001428 if (root->root_key.objectid ==
1429 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1430 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1431 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001432 if (ret) {
1433 if (!nolock && nocow)
1434 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001435 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001436 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001437 }
1438
Josef Bacikc2790a22013-07-29 11:20:47 -04001439 extent_clear_unlock_delalloc(inode, cur_offset,
1440 cur_offset + num_bytes - 1,
1441 locked_page, EXTENT_LOCKED |
1442 EXTENT_DELALLOC, PAGE_UNLOCK |
1443 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001444 if (!nolock && nocow)
1445 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001446 cur_offset = extent_end;
1447 if (cur_offset > end)
1448 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001449 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001450 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001451
Josef Bacik17ca04a2012-05-31 15:58:55 -04001452 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001453 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001454 cur_offset = end;
1455 }
1456
Yan Zheng80ff3852008-10-30 14:20:02 -04001457 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001458 ret = cow_file_range(inode, locked_page, cow_start, end,
1459 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001460 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001461 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001462 }
1463
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001464error:
Miao Xiea698d0752012-09-20 01:51:59 -06001465 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001466 if (!ret)
1467 ret = err;
1468
Josef Bacik17ca04a2012-05-31 15:58:55 -04001469 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001470 extent_clear_unlock_delalloc(inode, cur_offset, end,
1471 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001472 EXTENT_DELALLOC | EXTENT_DEFRAG |
1473 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1474 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001475 PAGE_SET_WRITEBACK |
1476 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001477 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001478 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001479}
1480
Wang Shilong47059d92014-07-03 18:22:07 +08001481static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1482{
1483
1484 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1485 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1486 return 0;
1487
1488 /*
1489 * @defrag_bytes is a hint value, no spinlock held here,
1490 * if is not zero, it means the file is defragging.
1491 * Force cow if given extent needs to be defragged.
1492 */
1493 if (BTRFS_I(inode)->defrag_bytes &&
1494 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1495 EXTENT_DEFRAG, 0, NULL))
1496 return 1;
1497
1498 return 0;
1499}
1500
Chris Masond352ac62008-09-29 15:18:18 -04001501/*
1502 * extent_io.c call back to do delayed allocation processing
1503 */
Chris Masonc8b97812008-10-29 14:49:59 -04001504static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001505 u64 start, u64 end, int *page_started,
1506 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001507{
Chris Masonbe20aa92007-12-17 20:14:01 -05001508 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001509 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001510
Wang Shilong47059d92014-07-03 18:22:07 +08001511 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001512 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001513 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001514 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001515 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001516 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001517 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001518 ret = cow_file_range(inode, locked_page, start, end,
1519 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001520 } else {
1521 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1522 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001523 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001524 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001525 }
Chris Masonb888db22007-08-27 16:49:44 -04001526 return ret;
1527}
1528
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001529static void btrfs_split_extent_hook(struct inode *inode,
1530 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001531{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001532 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001533 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001534 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001535
Josef Bacik9e0baf62011-07-15 15:16:44 +00001536 spin_lock(&BTRFS_I(inode)->lock);
1537 BTRFS_I(inode)->outstanding_extents++;
1538 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001539}
1540
1541/*
1542 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1543 * extents so we can keep track of new extents that are just merged onto old
1544 * extents, such as when we are doing sequential writes, so we can properly
1545 * account for the metadata space we'll need.
1546 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001547static void btrfs_merge_extent_hook(struct inode *inode,
1548 struct extent_state *new,
1549 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001550{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001551 /* not delalloc, ignore it */
1552 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001553 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001554
Josef Bacik9e0baf62011-07-15 15:16:44 +00001555 spin_lock(&BTRFS_I(inode)->lock);
1556 BTRFS_I(inode)->outstanding_extents--;
1557 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001558}
1559
Miao Xieeb73c1b2013-05-15 07:48:22 +00001560static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1561 struct inode *inode)
1562{
1563 spin_lock(&root->delalloc_lock);
1564 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1565 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1566 &root->delalloc_inodes);
1567 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1568 &BTRFS_I(inode)->runtime_flags);
1569 root->nr_delalloc_inodes++;
1570 if (root->nr_delalloc_inodes == 1) {
1571 spin_lock(&root->fs_info->delalloc_root_lock);
1572 BUG_ON(!list_empty(&root->delalloc_root));
1573 list_add_tail(&root->delalloc_root,
1574 &root->fs_info->delalloc_roots);
1575 spin_unlock(&root->fs_info->delalloc_root_lock);
1576 }
1577 }
1578 spin_unlock(&root->delalloc_lock);
1579}
1580
1581static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1582 struct inode *inode)
1583{
1584 spin_lock(&root->delalloc_lock);
1585 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1586 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1587 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1588 &BTRFS_I(inode)->runtime_flags);
1589 root->nr_delalloc_inodes--;
1590 if (!root->nr_delalloc_inodes) {
1591 spin_lock(&root->fs_info->delalloc_root_lock);
1592 BUG_ON(list_empty(&root->delalloc_root));
1593 list_del_init(&root->delalloc_root);
1594 spin_unlock(&root->fs_info->delalloc_root_lock);
1595 }
1596 }
1597 spin_unlock(&root->delalloc_lock);
1598}
1599
Chris Masond352ac62008-09-29 15:18:18 -04001600/*
1601 * extent_io.c set_bit_hook, used to track delayed allocation
1602 * bytes in this file, and to maintain the list of inodes that
1603 * have pending delalloc work to be done.
1604 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001605static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001606 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001607{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001608
Wang Shilong47059d92014-07-03 18:22:07 +08001609 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1610 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001611 /*
1612 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001613 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001614 * bit, which is only set or cleared with irqs on
1615 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001616 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001617 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001618 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001619 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001620
Josef Bacik9e0baf62011-07-15 15:16:44 +00001621 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001622 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001623 } else {
1624 spin_lock(&BTRFS_I(inode)->lock);
1625 BTRFS_I(inode)->outstanding_extents++;
1626 spin_unlock(&BTRFS_I(inode)->lock);
1627 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001628
Miao Xie963d6782013-01-29 10:10:51 +00001629 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1630 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001631 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001632 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001633 if (*bits & EXTENT_DEFRAG)
1634 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001635 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001636 &BTRFS_I(inode)->runtime_flags))
1637 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001638 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001639 }
Chris Mason291d6732008-01-29 15:55:23 -05001640}
1641
Chris Masond352ac62008-09-29 15:18:18 -04001642/*
1643 * extent_io.c clear_bit_hook, see set_bit_hook for why
1644 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001645static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001646 struct extent_state *state,
1647 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001648{
Wang Shilong47059d92014-07-03 18:22:07 +08001649 u64 len = state->end + 1 - state->start;
1650
1651 spin_lock(&BTRFS_I(inode)->lock);
1652 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1653 BTRFS_I(inode)->defrag_bytes -= len;
1654 spin_unlock(&BTRFS_I(inode)->lock);
1655
Chris Mason75eff682008-12-15 15:54:40 -05001656 /*
1657 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001658 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001659 * bit, which is only set or cleared with irqs on
1660 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001661 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001662 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001663 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001664
Josef Bacik9e0baf62011-07-15 15:16:44 +00001665 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001666 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001667 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1668 spin_lock(&BTRFS_I(inode)->lock);
1669 BTRFS_I(inode)->outstanding_extents--;
1670 spin_unlock(&BTRFS_I(inode)->lock);
1671 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001672
Josef Bacikb6d08f02013-09-27 14:57:43 -04001673 /*
1674 * We don't reserve metadata space for space cache inodes so we
1675 * don't need to call dellalloc_release_metadata if there is an
1676 * error.
1677 */
1678 if (*bits & EXTENT_DO_ACCOUNTING &&
1679 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001680 btrfs_delalloc_release_metadata(inode, len);
1681
Josef Bacik0cb59c92010-07-02 12:14:14 -04001682 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001683 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001684 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001685
Miao Xie963d6782013-01-29 10:10:51 +00001686 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1687 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001688 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001689 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001690 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001691 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001692 &BTRFS_I(inode)->runtime_flags))
1693 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001694 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001695 }
Chris Mason291d6732008-01-29 15:55:23 -05001696}
1697
Chris Masond352ac62008-09-29 15:18:18 -04001698/*
1699 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1700 * we don't create bios that span stripes or chunks
1701 */
David Woodhouse64a16702009-07-15 23:29:37 +01001702int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001703 size_t size, struct bio *bio,
1704 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001705{
1706 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001707 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001708 u64 length = 0;
1709 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001710 int ret;
1711
Chris Mason771ed682008-11-06 22:02:51 -05001712 if (bio_flags & EXTENT_BIO_COMPRESSED)
1713 return 0;
1714
Kent Overstreet4f024f32013-10-11 15:44:27 -07001715 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001716 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001717 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001718 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001719 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001720 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001721 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001722 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001723 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001724}
1725
Chris Masond352ac62008-09-29 15:18:18 -04001726/*
1727 * in order to insert checksums into the metadata in large chunks,
1728 * we wait until bio submission time. All the pages in the bio are
1729 * checksummed and sums are attached onto the ordered extent record.
1730 *
1731 * At IO completion time the cums attached on the ordered extent record
1732 * are inserted into the btree
1733 */
Chris Masond3977122009-01-05 21:25:51 -05001734static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1735 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001736 unsigned long bio_flags,
1737 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001738{
Chris Mason065631f2008-02-20 12:07:25 -05001739 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001740 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001741
Chris Masond20f7042008-12-08 16:58:54 -05001742 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001743 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001744 return 0;
1745}
Chris Masone0156402008-04-16 11:15:20 -04001746
Chris Mason4a69a412008-11-06 22:03:00 -05001747/*
1748 * in order to insert checksums into the metadata in large chunks,
1749 * we wait until bio submission time. All the pages in the bio are
1750 * checksummed and sums are attached onto the ordered extent record.
1751 *
1752 * At IO completion time the cums attached on the ordered extent record
1753 * are inserted into the btree
1754 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001755static int __btrfs_submit_bio_done(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 Mason4a69a412008-11-06 22:03:00 -05001758{
1759 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001760 int ret;
1761
1762 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1763 if (ret)
1764 bio_endio(bio, ret);
1765 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001766}
1767
Chris Masond352ac62008-09-29 15:18:18 -04001768/*
Chris Masoncad321a2008-12-17 14:51:42 -05001769 * extent_io.c submission hook. This does the right thing for csum calculation
1770 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001771 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001772static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001773 int mirror_num, unsigned long bio_flags,
1774 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001775{
1776 struct btrfs_root *root = BTRFS_I(inode)->root;
1777 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001778 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001779 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001780 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001781
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001782 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001783
Liu Bo83eea1f2012-07-10 05:28:39 -06001784 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001785 metadata = 2;
1786
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001787 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001788 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1789 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001790 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001791
Chris Masond20f7042008-12-08 16:58:54 -05001792 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001793 ret = btrfs_submit_compressed_read(inode, bio,
1794 mirror_num,
1795 bio_flags);
1796 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001797 } else if (!skip_sum) {
1798 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1799 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001800 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001801 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001802 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001803 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001804 /* csum items have already been cloned */
1805 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1806 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001807 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001808 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001809 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001810 bio_flags, bio_offset,
1811 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001812 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001813 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001814 } else if (!skip_sum) {
1815 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1816 if (ret)
1817 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001818 }
1819
Chris Mason0b86a832008-03-24 15:01:56 -04001820mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001821 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1822
1823out:
1824 if (ret < 0)
1825 bio_endio(bio, ret);
1826 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001827}
Chris Mason6885f302008-02-20 16:11:05 -05001828
Chris Masond352ac62008-09-29 15:18:18 -04001829/*
1830 * given a list of ordered sums record them in the inode. This happens
1831 * at IO completion time based on sums calculated at bio submission time.
1832 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001833static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001834 struct inode *inode, u64 file_offset,
1835 struct list_head *list)
1836{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001837 struct btrfs_ordered_sum *sum;
1838
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001839 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001840 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001841 btrfs_csum_file_blocks(trans,
1842 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001843 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001844 }
1845 return 0;
1846}
1847
Josef Bacik2ac55d42010-02-03 19:33:23 +00001848int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1849 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001850{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001851 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001852 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001853 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001854}
1855
Chris Masond352ac62008-09-29 15:18:18 -04001856/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001857struct btrfs_writepage_fixup {
1858 struct page *page;
1859 struct btrfs_work work;
1860};
1861
Christoph Hellwigb2950862008-12-02 09:54:17 -05001862static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001863{
1864 struct btrfs_writepage_fixup *fixup;
1865 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001866 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001867 struct page *page;
1868 struct inode *inode;
1869 u64 page_start;
1870 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001871 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001872
1873 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1874 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001875again:
Chris Mason247e7432008-07-17 12:53:51 -04001876 lock_page(page);
1877 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1878 ClearPageChecked(page);
1879 goto out_page;
1880 }
1881
1882 inode = page->mapping->host;
1883 page_start = page_offset(page);
1884 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1885
Josef Bacik2ac55d42010-02-03 19:33:23 +00001886 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001887 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001888
1889 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001890 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001891 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001892
1893 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1894 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001895 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1896 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001897 unlock_page(page);
1898 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001899 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001900 goto again;
1901 }
Chris Mason247e7432008-07-17 12:53:51 -04001902
Jeff Mahoney87826df2012-02-15 16:23:57 +01001903 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1904 if (ret) {
1905 mapping_set_error(page->mapping, ret);
1906 end_extent_writepage(page, ret, page_start, page_end);
1907 ClearPageChecked(page);
1908 goto out;
1909 }
1910
Josef Bacik2ac55d42010-02-03 19:33:23 +00001911 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001912 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001913 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001914out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001915 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1916 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001917out_page:
1918 unlock_page(page);
1919 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001920 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001921}
1922
1923/*
1924 * There are a few paths in the higher layers of the kernel that directly
1925 * set the page dirty bit without asking the filesystem if it is a
1926 * good idea. This causes problems because we want to make sure COW
1927 * properly happens and the data=ordered rules are followed.
1928 *
Chris Masonc8b97812008-10-29 14:49:59 -04001929 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001930 * hasn't been properly setup for IO. We kick off an async process
1931 * to fix it up. The async helper will wait for ordered extents, set
1932 * the delalloc bit and make it safe to write the page.
1933 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001934static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001935{
1936 struct inode *inode = page->mapping->host;
1937 struct btrfs_writepage_fixup *fixup;
1938 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001939
Chris Mason8b62b722009-09-02 16:53:46 -04001940 /* this page is properly in the ordered list */
1941 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001942 return 0;
1943
1944 if (PageChecked(page))
1945 return -EAGAIN;
1946
1947 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1948 if (!fixup)
1949 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001950
Chris Mason247e7432008-07-17 12:53:51 -04001951 SetPageChecked(page);
1952 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08001953 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
1954 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04001955 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08001956 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001957 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001958}
1959
Yan Zhengd899e052008-10-30 14:25:28 -04001960static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1961 struct inode *inode, u64 file_pos,
1962 u64 disk_bytenr, u64 disk_num_bytes,
1963 u64 num_bytes, u64 ram_bytes,
1964 u8 compression, u8 encryption,
1965 u16 other_encoding, int extent_type)
1966{
1967 struct btrfs_root *root = BTRFS_I(inode)->root;
1968 struct btrfs_file_extent_item *fi;
1969 struct btrfs_path *path;
1970 struct extent_buffer *leaf;
1971 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001972 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001973 int ret;
1974
1975 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001976 if (!path)
1977 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001978
Chris Masona1ed8352009-09-11 12:27:37 -04001979 /*
1980 * we may be replacing one extent in the tree with another.
1981 * The new extent is pinned in the extent map, and we don't want
1982 * to drop it from the cache until it is completely in the btree.
1983 *
1984 * So, tell btrfs_drop_extents to leave this extent in the cache.
1985 * the caller is expected to unpin it and allow it to be merged
1986 * with the others.
1987 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001988 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1989 file_pos + num_bytes, NULL, 0,
1990 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001991 if (ret)
1992 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001993
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001994 if (!extent_inserted) {
1995 ins.objectid = btrfs_ino(inode);
1996 ins.offset = file_pos;
1997 ins.type = BTRFS_EXTENT_DATA_KEY;
1998
1999 path->leave_spinning = 1;
2000 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2001 sizeof(*fi));
2002 if (ret)
2003 goto out;
2004 }
Yan Zhengd899e052008-10-30 14:25:28 -04002005 leaf = path->nodes[0];
2006 fi = btrfs_item_ptr(leaf, path->slots[0],
2007 struct btrfs_file_extent_item);
2008 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2009 btrfs_set_file_extent_type(leaf, fi, extent_type);
2010 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2011 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2012 btrfs_set_file_extent_offset(leaf, fi, 0);
2013 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2014 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2015 btrfs_set_file_extent_compression(leaf, fi, compression);
2016 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2017 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002018
Yan Zhengd899e052008-10-30 14:25:28 -04002019 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002020 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002021
2022 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002023
2024 ins.objectid = disk_bytenr;
2025 ins.offset = disk_num_bytes;
2026 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002027 ret = btrfs_alloc_reserved_file_extent(trans, root,
2028 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002029 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002030out:
Yan Zhengd899e052008-10-30 14:25:28 -04002031 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002032
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002033 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002034}
2035
Liu Bo38c227d2013-01-29 03:18:40 +00002036/* snapshot-aware defrag */
2037struct sa_defrag_extent_backref {
2038 struct rb_node node;
2039 struct old_sa_defrag_extent *old;
2040 u64 root_id;
2041 u64 inum;
2042 u64 file_pos;
2043 u64 extent_offset;
2044 u64 num_bytes;
2045 u64 generation;
2046};
2047
2048struct old_sa_defrag_extent {
2049 struct list_head list;
2050 struct new_sa_defrag_extent *new;
2051
2052 u64 extent_offset;
2053 u64 bytenr;
2054 u64 offset;
2055 u64 len;
2056 int count;
2057};
2058
2059struct new_sa_defrag_extent {
2060 struct rb_root root;
2061 struct list_head head;
2062 struct btrfs_path *path;
2063 struct inode *inode;
2064 u64 file_pos;
2065 u64 len;
2066 u64 bytenr;
2067 u64 disk_len;
2068 u8 compress_type;
2069};
2070
2071static int backref_comp(struct sa_defrag_extent_backref *b1,
2072 struct sa_defrag_extent_backref *b2)
2073{
2074 if (b1->root_id < b2->root_id)
2075 return -1;
2076 else if (b1->root_id > b2->root_id)
2077 return 1;
2078
2079 if (b1->inum < b2->inum)
2080 return -1;
2081 else if (b1->inum > b2->inum)
2082 return 1;
2083
2084 if (b1->file_pos < b2->file_pos)
2085 return -1;
2086 else if (b1->file_pos > b2->file_pos)
2087 return 1;
2088
2089 /*
2090 * [------------------------------] ===> (a range of space)
2091 * |<--->| |<---->| =============> (fs/file tree A)
2092 * |<---------------------------->| ===> (fs/file tree B)
2093 *
2094 * A range of space can refer to two file extents in one tree while
2095 * refer to only one file extent in another tree.
2096 *
2097 * So we may process a disk offset more than one time(two extents in A)
2098 * and locate at the same extent(one extent in B), then insert two same
2099 * backrefs(both refer to the extent in B).
2100 */
2101 return 0;
2102}
2103
2104static void backref_insert(struct rb_root *root,
2105 struct sa_defrag_extent_backref *backref)
2106{
2107 struct rb_node **p = &root->rb_node;
2108 struct rb_node *parent = NULL;
2109 struct sa_defrag_extent_backref *entry;
2110 int ret;
2111
2112 while (*p) {
2113 parent = *p;
2114 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2115
2116 ret = backref_comp(backref, entry);
2117 if (ret < 0)
2118 p = &(*p)->rb_left;
2119 else
2120 p = &(*p)->rb_right;
2121 }
2122
2123 rb_link_node(&backref->node, parent, p);
2124 rb_insert_color(&backref->node, root);
2125}
2126
2127/*
2128 * Note the backref might has changed, and in this case we just return 0.
2129 */
2130static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2131 void *ctx)
2132{
2133 struct btrfs_file_extent_item *extent;
2134 struct btrfs_fs_info *fs_info;
2135 struct old_sa_defrag_extent *old = ctx;
2136 struct new_sa_defrag_extent *new = old->new;
2137 struct btrfs_path *path = new->path;
2138 struct btrfs_key key;
2139 struct btrfs_root *root;
2140 struct sa_defrag_extent_backref *backref;
2141 struct extent_buffer *leaf;
2142 struct inode *inode = new->inode;
2143 int slot;
2144 int ret;
2145 u64 extent_offset;
2146 u64 num_bytes;
2147
2148 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2149 inum == btrfs_ino(inode))
2150 return 0;
2151
2152 key.objectid = root_id;
2153 key.type = BTRFS_ROOT_ITEM_KEY;
2154 key.offset = (u64)-1;
2155
2156 fs_info = BTRFS_I(inode)->root->fs_info;
2157 root = btrfs_read_fs_root_no_name(fs_info, &key);
2158 if (IS_ERR(root)) {
2159 if (PTR_ERR(root) == -ENOENT)
2160 return 0;
2161 WARN_ON(1);
2162 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2163 inum, offset, root_id);
2164 return PTR_ERR(root);
2165 }
2166
2167 key.objectid = inum;
2168 key.type = BTRFS_EXTENT_DATA_KEY;
2169 if (offset > (u64)-1 << 32)
2170 key.offset = 0;
2171 else
2172 key.offset = offset;
2173
2174 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302175 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002176 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002177 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002178
2179 while (1) {
2180 cond_resched();
2181
2182 leaf = path->nodes[0];
2183 slot = path->slots[0];
2184
2185 if (slot >= btrfs_header_nritems(leaf)) {
2186 ret = btrfs_next_leaf(root, path);
2187 if (ret < 0) {
2188 goto out;
2189 } else if (ret > 0) {
2190 ret = 0;
2191 goto out;
2192 }
2193 continue;
2194 }
2195
2196 path->slots[0]++;
2197
2198 btrfs_item_key_to_cpu(leaf, &key, slot);
2199
2200 if (key.objectid > inum)
2201 goto out;
2202
2203 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2204 continue;
2205
2206 extent = btrfs_item_ptr(leaf, slot,
2207 struct btrfs_file_extent_item);
2208
2209 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2210 continue;
2211
Liu Boe68afa42013-07-01 22:13:26 +08002212 /*
2213 * 'offset' refers to the exact key.offset,
2214 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2215 * (key.offset - extent_offset).
2216 */
2217 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002218 continue;
2219
Liu Boe68afa42013-07-01 22:13:26 +08002220 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002221 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002222
Liu Bo38c227d2013-01-29 03:18:40 +00002223 if (extent_offset >= old->extent_offset + old->offset +
2224 old->len || extent_offset + num_bytes <=
2225 old->extent_offset + old->offset)
2226 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002227 break;
2228 }
2229
2230 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2231 if (!backref) {
2232 ret = -ENOENT;
2233 goto out;
2234 }
2235
2236 backref->root_id = root_id;
2237 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002238 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002239 backref->num_bytes = num_bytes;
2240 backref->extent_offset = extent_offset;
2241 backref->generation = btrfs_file_extent_generation(leaf, extent);
2242 backref->old = old;
2243 backref_insert(&new->root, backref);
2244 old->count++;
2245out:
2246 btrfs_release_path(path);
2247 WARN_ON(ret);
2248 return ret;
2249}
2250
2251static noinline bool record_extent_backrefs(struct btrfs_path *path,
2252 struct new_sa_defrag_extent *new)
2253{
2254 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2255 struct old_sa_defrag_extent *old, *tmp;
2256 int ret;
2257
2258 new->path = path;
2259
2260 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002261 ret = iterate_inodes_from_logical(old->bytenr +
2262 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002263 path, record_one_backref,
2264 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002265 if (ret < 0 && ret != -ENOENT)
2266 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002267
2268 /* no backref to be processed for this extent */
2269 if (!old->count) {
2270 list_del(&old->list);
2271 kfree(old);
2272 }
2273 }
2274
2275 if (list_empty(&new->head))
2276 return false;
2277
2278 return true;
2279}
2280
2281static int relink_is_mergable(struct extent_buffer *leaf,
2282 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002283 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002284{
Liu Bo116e0022013-08-02 16:30:40 +08002285 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002286 return 0;
2287
2288 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2289 return 0;
2290
Liu Bo116e0022013-08-02 16:30:40 +08002291 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2292 return 0;
2293
2294 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002295 btrfs_file_extent_other_encoding(leaf, fi))
2296 return 0;
2297
2298 return 1;
2299}
2300
2301/*
2302 * Note the backref might has changed, and in this case we just return 0.
2303 */
2304static noinline int relink_extent_backref(struct btrfs_path *path,
2305 struct sa_defrag_extent_backref *prev,
2306 struct sa_defrag_extent_backref *backref)
2307{
2308 struct btrfs_file_extent_item *extent;
2309 struct btrfs_file_extent_item *item;
2310 struct btrfs_ordered_extent *ordered;
2311 struct btrfs_trans_handle *trans;
2312 struct btrfs_fs_info *fs_info;
2313 struct btrfs_root *root;
2314 struct btrfs_key key;
2315 struct extent_buffer *leaf;
2316 struct old_sa_defrag_extent *old = backref->old;
2317 struct new_sa_defrag_extent *new = old->new;
2318 struct inode *src_inode = new->inode;
2319 struct inode *inode;
2320 struct extent_state *cached = NULL;
2321 int ret = 0;
2322 u64 start;
2323 u64 len;
2324 u64 lock_start;
2325 u64 lock_end;
2326 bool merge = false;
2327 int index;
2328
2329 if (prev && prev->root_id == backref->root_id &&
2330 prev->inum == backref->inum &&
2331 prev->file_pos + prev->num_bytes == backref->file_pos)
2332 merge = true;
2333
2334 /* step 1: get root */
2335 key.objectid = backref->root_id;
2336 key.type = BTRFS_ROOT_ITEM_KEY;
2337 key.offset = (u64)-1;
2338
2339 fs_info = BTRFS_I(src_inode)->root->fs_info;
2340 index = srcu_read_lock(&fs_info->subvol_srcu);
2341
2342 root = btrfs_read_fs_root_no_name(fs_info, &key);
2343 if (IS_ERR(root)) {
2344 srcu_read_unlock(&fs_info->subvol_srcu, index);
2345 if (PTR_ERR(root) == -ENOENT)
2346 return 0;
2347 return PTR_ERR(root);
2348 }
Liu Bo38c227d2013-01-29 03:18:40 +00002349
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002350 if (btrfs_root_readonly(root)) {
2351 srcu_read_unlock(&fs_info->subvol_srcu, index);
2352 return 0;
2353 }
2354
Liu Bo38c227d2013-01-29 03:18:40 +00002355 /* step 2: get inode */
2356 key.objectid = backref->inum;
2357 key.type = BTRFS_INODE_ITEM_KEY;
2358 key.offset = 0;
2359
2360 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2361 if (IS_ERR(inode)) {
2362 srcu_read_unlock(&fs_info->subvol_srcu, index);
2363 return 0;
2364 }
2365
2366 srcu_read_unlock(&fs_info->subvol_srcu, index);
2367
2368 /* step 3: relink backref */
2369 lock_start = backref->file_pos;
2370 lock_end = backref->file_pos + backref->num_bytes - 1;
2371 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2372 0, &cached);
2373
2374 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2375 if (ordered) {
2376 btrfs_put_ordered_extent(ordered);
2377 goto out_unlock;
2378 }
2379
2380 trans = btrfs_join_transaction(root);
2381 if (IS_ERR(trans)) {
2382 ret = PTR_ERR(trans);
2383 goto out_unlock;
2384 }
2385
2386 key.objectid = backref->inum;
2387 key.type = BTRFS_EXTENT_DATA_KEY;
2388 key.offset = backref->file_pos;
2389
2390 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2391 if (ret < 0) {
2392 goto out_free_path;
2393 } else if (ret > 0) {
2394 ret = 0;
2395 goto out_free_path;
2396 }
2397
2398 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2399 struct btrfs_file_extent_item);
2400
2401 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2402 backref->generation)
2403 goto out_free_path;
2404
2405 btrfs_release_path(path);
2406
2407 start = backref->file_pos;
2408 if (backref->extent_offset < old->extent_offset + old->offset)
2409 start += old->extent_offset + old->offset -
2410 backref->extent_offset;
2411
2412 len = min(backref->extent_offset + backref->num_bytes,
2413 old->extent_offset + old->offset + old->len);
2414 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2415
2416 ret = btrfs_drop_extents(trans, root, inode, start,
2417 start + len, 1);
2418 if (ret)
2419 goto out_free_path;
2420again:
2421 key.objectid = btrfs_ino(inode);
2422 key.type = BTRFS_EXTENT_DATA_KEY;
2423 key.offset = start;
2424
Liu Boa09a0a72013-03-11 09:20:58 +00002425 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002426 if (merge) {
2427 struct btrfs_file_extent_item *fi;
2428 u64 extent_len;
2429 struct btrfs_key found_key;
2430
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002431 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002432 if (ret < 0)
2433 goto out_free_path;
2434
2435 path->slots[0]--;
2436 leaf = path->nodes[0];
2437 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2438
2439 fi = btrfs_item_ptr(leaf, path->slots[0],
2440 struct btrfs_file_extent_item);
2441 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2442
Liu Bo116e0022013-08-02 16:30:40 +08002443 if (extent_len + found_key.offset == start &&
2444 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002445 btrfs_set_file_extent_num_bytes(leaf, fi,
2446 extent_len + len);
2447 btrfs_mark_buffer_dirty(leaf);
2448 inode_add_bytes(inode, len);
2449
2450 ret = 1;
2451 goto out_free_path;
2452 } else {
2453 merge = false;
2454 btrfs_release_path(path);
2455 goto again;
2456 }
2457 }
2458
2459 ret = btrfs_insert_empty_item(trans, root, path, &key,
2460 sizeof(*extent));
2461 if (ret) {
2462 btrfs_abort_transaction(trans, root, ret);
2463 goto out_free_path;
2464 }
2465
2466 leaf = path->nodes[0];
2467 item = btrfs_item_ptr(leaf, path->slots[0],
2468 struct btrfs_file_extent_item);
2469 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2470 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2471 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2472 btrfs_set_file_extent_num_bytes(leaf, item, len);
2473 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2474 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2475 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2476 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2477 btrfs_set_file_extent_encryption(leaf, item, 0);
2478 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2479
2480 btrfs_mark_buffer_dirty(leaf);
2481 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002482 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002483
2484 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2485 new->disk_len, 0,
2486 backref->root_id, backref->inum,
2487 new->file_pos, 0); /* start - extent_offset */
2488 if (ret) {
2489 btrfs_abort_transaction(trans, root, ret);
2490 goto out_free_path;
2491 }
2492
2493 ret = 1;
2494out_free_path:
2495 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002496 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002497 btrfs_end_transaction(trans, root);
2498out_unlock:
2499 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2500 &cached, GFP_NOFS);
2501 iput(inode);
2502 return ret;
2503}
2504
Liu Bo6f519562013-10-29 10:45:05 +08002505static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2506{
2507 struct old_sa_defrag_extent *old, *tmp;
2508
2509 if (!new)
2510 return;
2511
2512 list_for_each_entry_safe(old, tmp, &new->head, list) {
2513 list_del(&old->list);
2514 kfree(old);
2515 }
2516 kfree(new);
2517}
2518
Liu Bo38c227d2013-01-29 03:18:40 +00002519static void relink_file_extents(struct new_sa_defrag_extent *new)
2520{
2521 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002522 struct sa_defrag_extent_backref *backref;
2523 struct sa_defrag_extent_backref *prev = NULL;
2524 struct inode *inode;
2525 struct btrfs_root *root;
2526 struct rb_node *node;
2527 int ret;
2528
2529 inode = new->inode;
2530 root = BTRFS_I(inode)->root;
2531
2532 path = btrfs_alloc_path();
2533 if (!path)
2534 return;
2535
2536 if (!record_extent_backrefs(path, new)) {
2537 btrfs_free_path(path);
2538 goto out;
2539 }
2540 btrfs_release_path(path);
2541
2542 while (1) {
2543 node = rb_first(&new->root);
2544 if (!node)
2545 break;
2546 rb_erase(node, &new->root);
2547
2548 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2549
2550 ret = relink_extent_backref(path, prev, backref);
2551 WARN_ON(ret < 0);
2552
2553 kfree(prev);
2554
2555 if (ret == 1)
2556 prev = backref;
2557 else
2558 prev = NULL;
2559 cond_resched();
2560 }
2561 kfree(prev);
2562
2563 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002564out:
Liu Bo6f519562013-10-29 10:45:05 +08002565 free_sa_defrag_extent(new);
2566
Liu Bo38c227d2013-01-29 03:18:40 +00002567 atomic_dec(&root->fs_info->defrag_running);
2568 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002569}
2570
2571static struct new_sa_defrag_extent *
2572record_old_file_extents(struct inode *inode,
2573 struct btrfs_ordered_extent *ordered)
2574{
2575 struct btrfs_root *root = BTRFS_I(inode)->root;
2576 struct btrfs_path *path;
2577 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002578 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002579 struct new_sa_defrag_extent *new;
2580 int ret;
2581
2582 new = kmalloc(sizeof(*new), GFP_NOFS);
2583 if (!new)
2584 return NULL;
2585
2586 new->inode = inode;
2587 new->file_pos = ordered->file_offset;
2588 new->len = ordered->len;
2589 new->bytenr = ordered->start;
2590 new->disk_len = ordered->disk_len;
2591 new->compress_type = ordered->compress_type;
2592 new->root = RB_ROOT;
2593 INIT_LIST_HEAD(&new->head);
2594
2595 path = btrfs_alloc_path();
2596 if (!path)
2597 goto out_kfree;
2598
2599 key.objectid = btrfs_ino(inode);
2600 key.type = BTRFS_EXTENT_DATA_KEY;
2601 key.offset = new->file_pos;
2602
2603 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2604 if (ret < 0)
2605 goto out_free_path;
2606 if (ret > 0 && path->slots[0] > 0)
2607 path->slots[0]--;
2608
2609 /* find out all the old extents for the file range */
2610 while (1) {
2611 struct btrfs_file_extent_item *extent;
2612 struct extent_buffer *l;
2613 int slot;
2614 u64 num_bytes;
2615 u64 offset;
2616 u64 end;
2617 u64 disk_bytenr;
2618 u64 extent_offset;
2619
2620 l = path->nodes[0];
2621 slot = path->slots[0];
2622
2623 if (slot >= btrfs_header_nritems(l)) {
2624 ret = btrfs_next_leaf(root, path);
2625 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002626 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002627 else if (ret > 0)
2628 break;
2629 continue;
2630 }
2631
2632 btrfs_item_key_to_cpu(l, &key, slot);
2633
2634 if (key.objectid != btrfs_ino(inode))
2635 break;
2636 if (key.type != BTRFS_EXTENT_DATA_KEY)
2637 break;
2638 if (key.offset >= new->file_pos + new->len)
2639 break;
2640
2641 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2642
2643 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2644 if (key.offset + num_bytes < new->file_pos)
2645 goto next;
2646
2647 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2648 if (!disk_bytenr)
2649 goto next;
2650
2651 extent_offset = btrfs_file_extent_offset(l, extent);
2652
2653 old = kmalloc(sizeof(*old), GFP_NOFS);
2654 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002655 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002656
2657 offset = max(new->file_pos, key.offset);
2658 end = min(new->file_pos + new->len, key.offset + num_bytes);
2659
2660 old->bytenr = disk_bytenr;
2661 old->extent_offset = extent_offset;
2662 old->offset = offset - key.offset;
2663 old->len = end - offset;
2664 old->new = new;
2665 old->count = 0;
2666 list_add_tail(&old->list, &new->head);
2667next:
2668 path->slots[0]++;
2669 cond_resched();
2670 }
2671
2672 btrfs_free_path(path);
2673 atomic_inc(&root->fs_info->defrag_running);
2674
2675 return new;
2676
Liu Bo38c227d2013-01-29 03:18:40 +00002677out_free_path:
2678 btrfs_free_path(path);
2679out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002680 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002681 return NULL;
2682}
2683
Miao Xiee570fd22014-06-19 10:42:50 +08002684static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2685 u64 start, u64 len)
2686{
2687 struct btrfs_block_group_cache *cache;
2688
2689 cache = btrfs_lookup_block_group(root->fs_info, start);
2690 ASSERT(cache);
2691
2692 spin_lock(&cache->lock);
2693 cache->delalloc_bytes -= len;
2694 spin_unlock(&cache->lock);
2695
2696 btrfs_put_block_group(cache);
2697}
2698
Chris Masond352ac62008-09-29 15:18:18 -04002699/* as ordered data IO finishes, this gets called so we can finish
2700 * an ordered extent if the range of bytes in the file it covers are
2701 * fully written.
2702 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002703static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002704{
Josef Bacik5fd02042012-05-02 14:00:54 -04002705 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002706 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002707 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002708 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002709 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002710 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002711 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002712 int ret = 0;
2713 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002714 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002715 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002716
Liu Bo83eea1f2012-07-10 05:28:39 -06002717 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002718
Josef Bacik5fd02042012-05-02 14:00:54 -04002719 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2720 ret = -EIO;
2721 goto out;
2722 }
2723
Miao Xief6124962014-09-12 18:44:04 +08002724 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2725 ordered_extent->file_offset +
2726 ordered_extent->len - 1);
2727
Josef Bacik77cef2e2013-08-29 13:57:21 -04002728 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2729 truncated = true;
2730 logical_len = ordered_extent->truncated_len;
2731 /* Truncated the entire extent, don't bother adding */
2732 if (!logical_len)
2733 goto out;
2734 }
2735
Yan, Zhengc2167752009-11-12 09:34:21 +00002736 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002737 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002738 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2739 if (nolock)
2740 trans = btrfs_join_transaction_nolock(root);
2741 else
2742 trans = btrfs_join_transaction(root);
2743 if (IS_ERR(trans)) {
2744 ret = PTR_ERR(trans);
2745 trans = NULL;
2746 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002747 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002748 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2749 ret = btrfs_update_inode_fallback(trans, root, inode);
2750 if (ret) /* -ENOMEM or corruption */
2751 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002752 goto out;
2753 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002754
Josef Bacik2ac55d42010-02-03 19:33:23 +00002755 lock_extent_bits(io_tree, ordered_extent->file_offset,
2756 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002757 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002758
Liu Bo38c227d2013-01-29 03:18:40 +00002759 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2760 ordered_extent->file_offset + ordered_extent->len - 1,
2761 EXTENT_DEFRAG, 1, cached_state);
2762 if (ret) {
2763 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002764 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002765 /* the inode is shared */
2766 new = record_old_file_extents(inode, ordered_extent);
2767
2768 clear_extent_bit(io_tree, ordered_extent->file_offset,
2769 ordered_extent->file_offset + ordered_extent->len - 1,
2770 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2771 }
2772
Josef Bacik0cb59c92010-07-02 12:14:14 -04002773 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002774 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002775 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002776 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002777 if (IS_ERR(trans)) {
2778 ret = PTR_ERR(trans);
2779 trans = NULL;
2780 goto out_unlock;
2781 }
Chris Masona79b7d42014-05-22 16:18:52 -07002782
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002783 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002784
Chris Masonc8b97812008-10-29 14:49:59 -04002785 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002786 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002787 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002788 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002789 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002790 ordered_extent->file_offset,
2791 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002792 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002793 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002794 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002795 ret = insert_reserved_file_extent(trans, inode,
2796 ordered_extent->file_offset,
2797 ordered_extent->start,
2798 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002799 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002800 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002801 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002802 if (!ret)
2803 btrfs_release_delalloc_bytes(root,
2804 ordered_extent->start,
2805 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002806 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002807 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2808 ordered_extent->file_offset, ordered_extent->len,
2809 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002810 if (ret < 0) {
2811 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002812 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002813 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002814
Chris Masone6dcd2d2008-07-17 12:53:50 -04002815 add_pending_csums(trans, inode, ordered_extent->file_offset,
2816 &ordered_extent->list);
2817
Josef Bacik6c760c02012-11-09 10:53:21 -05002818 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2819 ret = btrfs_update_inode_fallback(trans, root, inode);
2820 if (ret) { /* -ENOMEM or corruption */
2821 btrfs_abort_transaction(trans, root, ret);
2822 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002823 }
2824 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002825out_unlock:
2826 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2827 ordered_extent->file_offset +
2828 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002829out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002830 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002831 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002832 if (trans)
2833 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002834
Josef Bacik77cef2e2013-08-29 13:57:21 -04002835 if (ret || truncated) {
2836 u64 start, end;
2837
2838 if (truncated)
2839 start = ordered_extent->file_offset + logical_len;
2840 else
2841 start = ordered_extent->file_offset;
2842 end = ordered_extent->file_offset + ordered_extent->len - 1;
2843 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2844
2845 /* Drop the cache for the part of the extent we didn't write. */
2846 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002847
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002848 /*
2849 * If the ordered extent had an IOERR or something else went
2850 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002851 * back to the allocator. We only free the extent in the
2852 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002853 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002854 if ((ret || !logical_len) &&
2855 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002856 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2857 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002858 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002859 }
2860
2861
Josef Bacik5fd02042012-05-02 14:00:54 -04002862 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002863 * This needs to be done to make sure anybody waiting knows we are done
2864 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002865 */
2866 btrfs_remove_ordered_extent(inode, ordered_extent);
2867
Liu Bo38c227d2013-01-29 03:18:40 +00002868 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002869 if (new) {
2870 if (ret) {
2871 free_sa_defrag_extent(new);
2872 atomic_dec(&root->fs_info->defrag_running);
2873 } else {
2874 relink_file_extents(new);
2875 }
2876 }
Liu Bo38c227d2013-01-29 03:18:40 +00002877
Chris Masone6dcd2d2008-07-17 12:53:50 -04002878 /* once for us */
2879 btrfs_put_ordered_extent(ordered_extent);
2880 /* once for the tree */
2881 btrfs_put_ordered_extent(ordered_extent);
2882
Josef Bacik5fd02042012-05-02 14:00:54 -04002883 return ret;
2884}
2885
2886static void finish_ordered_fn(struct btrfs_work *work)
2887{
2888 struct btrfs_ordered_extent *ordered_extent;
2889 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2890 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002891}
2892
Christoph Hellwigb2950862008-12-02 09:54:17 -05002893static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002894 struct extent_state *state, int uptodate)
2895{
Josef Bacik5fd02042012-05-02 14:00:54 -04002896 struct inode *inode = page->mapping->host;
2897 struct btrfs_root *root = BTRFS_I(inode)->root;
2898 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002899 struct btrfs_workqueue *wq;
2900 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002901
liubo1abe9b82011-03-24 11:18:59 +00002902 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2903
Chris Mason8b62b722009-09-02 16:53:46 -04002904 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002905 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2906 end - start + 1, uptodate))
2907 return 0;
2908
Liu Bo9e0af232014-08-15 23:36:53 +08002909 if (btrfs_is_free_space_inode(inode)) {
2910 wq = root->fs_info->endio_freespace_worker;
2911 func = btrfs_freespace_write_helper;
2912 } else {
2913 wq = root->fs_info->endio_write_workers;
2914 func = btrfs_endio_write_helper;
2915 }
Josef Bacik5fd02042012-05-02 14:00:54 -04002916
Liu Bo9e0af232014-08-15 23:36:53 +08002917 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
2918 NULL);
2919 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002920
2921 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002922}
2923
Miao Xiedc380ae2014-09-12 18:43:55 +08002924static int __readpage_endio_check(struct inode *inode,
2925 struct btrfs_io_bio *io_bio,
2926 int icsum, struct page *page,
2927 int pgoff, u64 start, size_t len)
2928{
2929 char *kaddr;
2930 u32 csum_expected;
2931 u32 csum = ~(u32)0;
2932 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2933 DEFAULT_RATELIMIT_BURST);
2934
2935 csum_expected = *(((u32 *)io_bio->csum) + icsum);
2936
2937 kaddr = kmap_atomic(page);
2938 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
2939 btrfs_csum_final(csum, (char *)&csum);
2940 if (csum != csum_expected)
2941 goto zeroit;
2942
2943 kunmap_atomic(kaddr);
2944 return 0;
2945zeroit:
2946 if (__ratelimit(&_rs))
2947 btrfs_info(BTRFS_I(inode)->root->fs_info,
2948 "csum failed ino %llu off %llu csum %u expected csum %u",
2949 btrfs_ino(inode), start, csum, csum_expected);
2950 memset(kaddr + pgoff, 1, len);
2951 flush_dcache_page(page);
2952 kunmap_atomic(kaddr);
2953 if (csum_expected == 0)
2954 return 0;
2955 return -EIO;
2956}
2957
Chris Masond352ac62008-09-29 15:18:18 -04002958/*
Chris Masond352ac62008-09-29 15:18:18 -04002959 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002960 * if there's a match, we allow the bio to finish. If not, the code in
2961 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002962 */
Miao Xiefacc8a22013-07-25 19:22:34 +08002963static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2964 u64 phy_offset, struct page *page,
2965 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002966{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002967 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002968 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002969 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002970 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002971
Chris Masond20f7042008-12-08 16:58:54 -05002972 if (PageChecked(page)) {
2973 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002974 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002975 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002976
2977 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002978 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002979
Yan Zheng17d217f2008-12-12 10:03:38 -05002980 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002981 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002982 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2983 GFP_NOFS);
2984 return 0;
2985 }
2986
Miao Xiefacc8a22013-07-25 19:22:34 +08002987 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08002988 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
2989 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002990}
Chris Masonb888db22007-08-27 16:49:44 -04002991
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002992struct delayed_iput {
2993 struct list_head list;
2994 struct inode *inode;
2995};
2996
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002997/* JDM: If this is fs-wide, why can't we add a pointer to
2998 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002999void btrfs_add_delayed_iput(struct inode *inode)
3000{
3001 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3002 struct delayed_iput *delayed;
3003
3004 if (atomic_add_unless(&inode->i_count, -1, 1))
3005 return;
3006
3007 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3008 delayed->inode = inode;
3009
3010 spin_lock(&fs_info->delayed_iput_lock);
3011 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3012 spin_unlock(&fs_info->delayed_iput_lock);
3013}
3014
3015void btrfs_run_delayed_iputs(struct btrfs_root *root)
3016{
3017 LIST_HEAD(list);
3018 struct btrfs_fs_info *fs_info = root->fs_info;
3019 struct delayed_iput *delayed;
3020 int empty;
3021
3022 spin_lock(&fs_info->delayed_iput_lock);
3023 empty = list_empty(&fs_info->delayed_iputs);
3024 spin_unlock(&fs_info->delayed_iput_lock);
3025 if (empty)
3026 return;
3027
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003028 spin_lock(&fs_info->delayed_iput_lock);
3029 list_splice_init(&fs_info->delayed_iputs, &list);
3030 spin_unlock(&fs_info->delayed_iput_lock);
3031
3032 while (!list_empty(&list)) {
3033 delayed = list_entry(list.next, struct delayed_iput, list);
3034 list_del(&delayed->list);
3035 iput(delayed->inode);
3036 kfree(delayed);
3037 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003038}
3039
Yan, Zhengd68fc572010-05-16 10:49:58 -04003040/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003041 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003042 * files in the subvolume, it removes orphan item and frees block_rsv
3043 * structure.
3044 */
3045void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3046 struct btrfs_root *root)
3047{
Josef Bacik90290e12011-12-02 15:44:12 -05003048 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003049 int ret;
3050
Josef Bacik8a35d952012-05-23 14:26:42 -04003051 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003052 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3053 return;
3054
Josef Bacik90290e12011-12-02 15:44:12 -05003055 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003056 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003057 spin_unlock(&root->orphan_lock);
3058 return;
3059 }
3060
3061 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3062 spin_unlock(&root->orphan_lock);
3063 return;
3064 }
3065
3066 block_rsv = root->orphan_block_rsv;
3067 root->orphan_block_rsv = NULL;
3068 spin_unlock(&root->orphan_lock);
3069
Miao Xie27cdeb72014-04-02 19:51:05 +08003070 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003071 btrfs_root_refs(&root->root_item) > 0) {
3072 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3073 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003074 if (ret)
3075 btrfs_abort_transaction(trans, root, ret);
3076 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003077 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3078 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003079 }
3080
Josef Bacik90290e12011-12-02 15:44:12 -05003081 if (block_rsv) {
3082 WARN_ON(block_rsv->size > 0);
3083 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003084 }
3085}
3086
3087/*
Josef Bacik7b128762008-07-24 12:17:14 -04003088 * This creates an orphan entry for the given inode in case something goes
3089 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003090 *
3091 * NOTE: caller of this function should reserve 5 units of metadata for
3092 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003093 */
3094int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3095{
3096 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003097 struct btrfs_block_rsv *block_rsv = NULL;
3098 int reserve = 0;
3099 int insert = 0;
3100 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003101
Yan, Zhengd68fc572010-05-16 10:49:58 -04003102 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003103 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003104 if (!block_rsv)
3105 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003106 }
3107
Yan, Zhengd68fc572010-05-16 10:49:58 -04003108 spin_lock(&root->orphan_lock);
3109 if (!root->orphan_block_rsv) {
3110 root->orphan_block_rsv = block_rsv;
3111 } else if (block_rsv) {
3112 btrfs_free_block_rsv(root, block_rsv);
3113 block_rsv = NULL;
3114 }
Josef Bacik7b128762008-07-24 12:17:14 -04003115
Josef Bacik8a35d952012-05-23 14:26:42 -04003116 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3117 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003118#if 0
3119 /*
3120 * For proper ENOSPC handling, we should do orphan
3121 * cleanup when mounting. But this introduces backward
3122 * compatibility issue.
3123 */
3124 if (!xchg(&root->orphan_item_inserted, 1))
3125 insert = 2;
3126 else
3127 insert = 1;
3128#endif
3129 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003130 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003131 }
Josef Bacik7b128762008-07-24 12:17:14 -04003132
Josef Bacik72ac3c02012-05-23 14:13:11 -04003133 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3134 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003135 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003136 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003137
Yan, Zhengd68fc572010-05-16 10:49:58 -04003138 /* grab metadata reservation from transaction handle */
3139 if (reserve) {
3140 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003141 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003142 }
3143
3144 /* insert an orphan item to track this unlinked/truncated file */
3145 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003146 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003147 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003148 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003149 if (reserve) {
3150 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3151 &BTRFS_I(inode)->runtime_flags);
3152 btrfs_orphan_release_metadata(inode);
3153 }
3154 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003155 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3156 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003157 btrfs_abort_transaction(trans, root, ret);
3158 return ret;
3159 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003160 }
3161 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003162 }
3163
3164 /* insert an orphan item to track subvolume contains orphan files */
3165 if (insert >= 2) {
3166 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3167 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003168 if (ret && ret != -EEXIST) {
3169 btrfs_abort_transaction(trans, root, ret);
3170 return ret;
3171 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003172 }
3173 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003174}
3175
3176/*
3177 * We have done the truncate/delete so we can go ahead and remove the orphan
3178 * item for this particular inode.
3179 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003180static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3181 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003182{
3183 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003184 int delete_item = 0;
3185 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003186 int ret = 0;
3187
Yan, Zhengd68fc572010-05-16 10:49:58 -04003188 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003189 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3190 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003191 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003192
Josef Bacik72ac3c02012-05-23 14:13:11 -04003193 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3194 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003195 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003196 spin_unlock(&root->orphan_lock);
3197
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003198 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003199 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003200 if (trans)
3201 ret = btrfs_del_orphan_item(trans, root,
3202 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003203 }
Josef Bacik7b128762008-07-24 12:17:14 -04003204
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003205 if (release_rsv)
3206 btrfs_orphan_release_metadata(inode);
3207
Josef Bacik4ef31a42013-08-13 14:10:08 -04003208 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003209}
3210
3211/*
3212 * this cleans up any orphans that may be left on the list from the last use
3213 * of this root.
3214 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003215int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003216{
3217 struct btrfs_path *path;
3218 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003219 struct btrfs_key key, found_key;
3220 struct btrfs_trans_handle *trans;
3221 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003222 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003223 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3224
Yan, Zhengd68fc572010-05-16 10:49:58 -04003225 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003226 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003227
3228 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003229 if (!path) {
3230 ret = -ENOMEM;
3231 goto out;
3232 }
Josef Bacik7b128762008-07-24 12:17:14 -04003233 path->reada = -1;
3234
3235 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003236 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003237 key.offset = (u64)-1;
3238
Josef Bacik7b128762008-07-24 12:17:14 -04003239 while (1) {
3240 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003241 if (ret < 0)
3242 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003243
3244 /*
3245 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003246 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003247 * find the key and see if we have stuff that matches
3248 */
3249 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003250 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003251 if (path->slots[0] == 0)
3252 break;
3253 path->slots[0]--;
3254 }
3255
3256 /* pull out the item */
3257 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003258 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3259
3260 /* make sure the item matches what we want */
3261 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3262 break;
David Sterba962a2982014-06-04 18:41:45 +02003263 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003264 break;
3265
3266 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003267 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003268
3269 /*
3270 * this is where we are basically btrfs_lookup, without the
3271 * crossing root thing. we store the inode number in the
3272 * offset of the orphan item.
3273 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003274
3275 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003276 btrfs_err(root->fs_info,
3277 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003278 ret = -EINVAL;
3279 goto out;
3280 }
3281
3282 last_objectid = found_key.offset;
3283
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003284 found_key.objectid = found_key.offset;
3285 found_key.type = BTRFS_INODE_ITEM_KEY;
3286 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003287 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303288 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003289 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003290 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003291
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003292 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3293 struct btrfs_root *dead_root;
3294 struct btrfs_fs_info *fs_info = root->fs_info;
3295 int is_dead_root = 0;
3296
3297 /*
3298 * this is an orphan in the tree root. Currently these
3299 * could come from 2 sources:
3300 * a) a snapshot deletion in progress
3301 * b) a free space cache inode
3302 * We need to distinguish those two, as the snapshot
3303 * orphan must not get deleted.
3304 * find_dead_roots already ran before us, so if this
3305 * is a snapshot deletion, we should find the root
3306 * in the dead_roots list
3307 */
3308 spin_lock(&fs_info->trans_lock);
3309 list_for_each_entry(dead_root, &fs_info->dead_roots,
3310 root_list) {
3311 if (dead_root->root_key.objectid ==
3312 found_key.objectid) {
3313 is_dead_root = 1;
3314 break;
3315 }
3316 }
3317 spin_unlock(&fs_info->trans_lock);
3318 if (is_dead_root) {
3319 /* prevent this orphan from being found again */
3320 key.offset = found_key.objectid - 1;
3321 continue;
3322 }
3323 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003324 /*
3325 * Inode is already gone but the orphan item is still there,
3326 * kill the orphan item.
3327 */
3328 if (ret == -ESTALE) {
3329 trans = btrfs_start_transaction(root, 1);
3330 if (IS_ERR(trans)) {
3331 ret = PTR_ERR(trans);
3332 goto out;
3333 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003334 btrfs_debug(root->fs_info, "auto deleting %Lu",
3335 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003336 ret = btrfs_del_orphan_item(trans, root,
3337 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003338 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003339 if (ret)
3340 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003341 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003342 }
Josef Bacik7b128762008-07-24 12:17:14 -04003343
Josef Bacik7b128762008-07-24 12:17:14 -04003344 /*
3345 * add this inode to the orphan list so btrfs_orphan_del does
3346 * the proper thing when we hit it
3347 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003348 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3349 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003350 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003351
Josef Bacik7b128762008-07-24 12:17:14 -04003352 /* if we have links, this was a truncate, lets do that */
3353 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303354 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003355 iput(inode);
3356 continue;
3357 }
Josef Bacik7b128762008-07-24 12:17:14 -04003358 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003359
3360 /* 1 for the orphan item deletion. */
3361 trans = btrfs_start_transaction(root, 1);
3362 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003363 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003364 ret = PTR_ERR(trans);
3365 goto out;
3366 }
3367 ret = btrfs_orphan_add(trans, inode);
3368 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003369 if (ret) {
3370 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003371 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003372 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003373
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003374 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003375 if (ret)
3376 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003377 } else {
3378 nr_unlink++;
3379 }
3380
3381 /* this will do delete_inode and everything for us */
3382 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003383 if (ret)
3384 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003385 }
Miao Xie3254c872011-11-10 20:45:05 -05003386 /* release the path since we're done with it */
3387 btrfs_release_path(path);
3388
Yan, Zhengd68fc572010-05-16 10:49:58 -04003389 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3390
3391 if (root->orphan_block_rsv)
3392 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3393 (u64)-1);
3394
Miao Xie27cdeb72014-04-02 19:51:05 +08003395 if (root->orphan_block_rsv ||
3396 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003397 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003398 if (!IS_ERR(trans))
3399 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003400 }
Josef Bacik7b128762008-07-24 12:17:14 -04003401
3402 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003403 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003404 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003405 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003406
3407out:
3408 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003409 btrfs_crit(root->fs_info,
3410 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003411 btrfs_free_path(path);
3412 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003413}
3414
Chris Masond352ac62008-09-29 15:18:18 -04003415/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003416 * very simple check to peek ahead in the leaf looking for xattrs. If we
3417 * don't find any xattrs, we know there can't be any acls.
3418 *
3419 * slot is the slot the inode is in, objectid is the objectid of the inode
3420 */
3421static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003422 int slot, u64 objectid,
3423 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003424{
3425 u32 nritems = btrfs_header_nritems(leaf);
3426 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003427 static u64 xattr_access = 0;
3428 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003429 int scanned = 0;
3430
Josef Bacikf23b5a52013-06-19 10:16:26 -04003431 if (!xattr_access) {
3432 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3433 strlen(POSIX_ACL_XATTR_ACCESS));
3434 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3435 strlen(POSIX_ACL_XATTR_DEFAULT));
3436 }
3437
Chris Mason46a53cc2009-04-27 11:47:50 -04003438 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003439 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003440 while (slot < nritems) {
3441 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3442
3443 /* we found a different objectid, there must not be acls */
3444 if (found_key.objectid != objectid)
3445 return 0;
3446
3447 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003448 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003449 if (*first_xattr_slot == -1)
3450 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003451 if (found_key.offset == xattr_access ||
3452 found_key.offset == xattr_default)
3453 return 1;
3454 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003455
3456 /*
3457 * we found a key greater than an xattr key, there can't
3458 * be any acls later on
3459 */
3460 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3461 return 0;
3462
3463 slot++;
3464 scanned++;
3465
3466 /*
3467 * it goes inode, inode backrefs, xattrs, extents,
3468 * so if there are a ton of hard links to an inode there can
3469 * be a lot of backrefs. Don't waste time searching too hard,
3470 * this is just an optimization
3471 */
3472 if (scanned >= 8)
3473 break;
3474 }
3475 /* we hit the end of the leaf before we found an xattr or
3476 * something larger than an xattr. We have to assume the inode
3477 * has acls
3478 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003479 if (*first_xattr_slot == -1)
3480 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003481 return 1;
3482}
3483
3484/*
Chris Masond352ac62008-09-29 15:18:18 -04003485 * read an inode from the btree into the in-memory inode
3486 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003487static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003488{
3489 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003490 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003491 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003492 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003493 struct btrfs_root *root = BTRFS_I(inode)->root;
3494 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003495 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003496 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003497 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003498 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003499 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003500 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003501
3502 ret = btrfs_fill_inode(inode, &rdev);
3503 if (!ret)
3504 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003505
3506 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003507 if (!path)
3508 goto make_bad;
3509
Chris Mason39279cc2007-06-12 06:35:45 -04003510 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003511
Chris Mason39279cc2007-06-12 06:35:45 -04003512 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003513 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003514 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003515
Chris Mason5f39d392007-10-15 16:14:19 -04003516 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003517
3518 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003519 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003520
Chris Mason5f39d392007-10-15 16:14:19 -04003521 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3522 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003523 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003524 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003525 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3526 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003527 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003528
3529 tspec = btrfs_inode_atime(inode_item);
3530 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3531 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3532
3533 tspec = btrfs_inode_mtime(inode_item);
3534 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3535 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3536
3537 tspec = btrfs_inode_ctime(inode_item);
3538 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3539 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3540
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003541 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003542 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003543 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3544
3545 /*
3546 * If we were modified in the current generation and evicted from memory
3547 * and then re-read we need to do a full sync since we don't have any
3548 * idea about which extents were modified before we were evicted from
3549 * cache.
3550 */
3551 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3552 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3553 &BTRFS_I(inode)->runtime_flags);
3554
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003555 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003556 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003557 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003558 rdev = btrfs_inode_rdev(leaf, inode_item);
3559
Josef Bacikaec74772008-07-24 12:12:38 -04003560 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003561 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003562
3563cache_index:
3564 path->slots[0]++;
3565 if (inode->i_nlink != 1 ||
3566 path->slots[0] >= btrfs_header_nritems(leaf))
3567 goto cache_acl;
3568
3569 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3570 if (location.objectid != btrfs_ino(inode))
3571 goto cache_acl;
3572
3573 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3574 if (location.type == BTRFS_INODE_REF_KEY) {
3575 struct btrfs_inode_ref *ref;
3576
3577 ref = (struct btrfs_inode_ref *)ptr;
3578 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3579 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3580 struct btrfs_inode_extref *extref;
3581
3582 extref = (struct btrfs_inode_extref *)ptr;
3583 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3584 extref);
3585 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003586cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003587 /*
3588 * try to precache a NULL acl entry for files that don't have
3589 * any xattrs or acls
3590 */
Li Zefan33345d012011-04-20 10:31:50 +08003591 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003592 btrfs_ino(inode), &first_xattr_slot);
3593 if (first_xattr_slot != -1) {
3594 path->slots[0] = first_xattr_slot;
3595 ret = btrfs_load_inode_props(inode, path);
3596 if (ret)
3597 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003598 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003599 btrfs_ino(inode),
3600 root->root_key.objectid, ret);
3601 }
3602 btrfs_free_path(path);
3603
Al Viro72c04902009-06-24 16:58:48 -04003604 if (!maybe_acls)
3605 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003606
Chris Mason39279cc2007-06-12 06:35:45 -04003607 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003608 case S_IFREG:
3609 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003610 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003611 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003612 inode->i_fop = &btrfs_file_operations;
3613 inode->i_op = &btrfs_file_inode_operations;
3614 break;
3615 case S_IFDIR:
3616 inode->i_fop = &btrfs_dir_file_operations;
3617 if (root == root->fs_info->tree_root)
3618 inode->i_op = &btrfs_dir_ro_inode_operations;
3619 else
3620 inode->i_op = &btrfs_dir_inode_operations;
3621 break;
3622 case S_IFLNK:
3623 inode->i_op = &btrfs_symlink_inode_operations;
3624 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003625 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003626 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003627 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003628 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003629 init_special_inode(inode, inode->i_mode, rdev);
3630 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003631 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003632
3633 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003634 return;
3635
3636make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003637 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003638 make_bad_inode(inode);
3639}
3640
Chris Masond352ac62008-09-29 15:18:18 -04003641/*
3642 * given a leaf and an inode, copy the inode fields into the leaf
3643 */
Chris Masone02119d2008-09-05 16:13:11 -04003644static void fill_inode_item(struct btrfs_trans_handle *trans,
3645 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003646 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003647 struct inode *inode)
3648{
Liu Bo51fab692012-12-27 09:01:21 +00003649 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003650
Liu Bo51fab692012-12-27 09:01:21 +00003651 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003652
Liu Bo51fab692012-12-27 09:01:21 +00003653 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3654 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3655 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3656 &token);
3657 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3658 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003659
Liu Bo51fab692012-12-27 09:01:21 +00003660 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3661 inode->i_atime.tv_sec, &token);
3662 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3663 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003664
Liu Bo51fab692012-12-27 09:01:21 +00003665 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3666 inode->i_mtime.tv_sec, &token);
3667 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3668 inode->i_mtime.tv_nsec, &token);
3669
3670 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3671 inode->i_ctime.tv_sec, &token);
3672 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3673 inode->i_ctime.tv_nsec, &token);
3674
3675 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3676 &token);
3677 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3678 &token);
3679 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3680 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3681 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3682 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3683 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003684}
3685
Chris Masond352ac62008-09-29 15:18:18 -04003686/*
3687 * copy everything in the in-memory inode into the btree.
3688 */
Chris Mason21151332011-11-10 20:39:08 -05003689static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003690 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003691{
3692 struct btrfs_inode_item *inode_item;
3693 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003694 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003695 int ret;
3696
3697 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003698 if (!path)
3699 return -ENOMEM;
3700
Chris Masonb9473432009-03-13 11:00:37 -04003701 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003702 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3703 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003704 if (ret) {
3705 if (ret > 0)
3706 ret = -ENOENT;
3707 goto failed;
3708 }
3709
Chris Mason5f39d392007-10-15 16:14:19 -04003710 leaf = path->nodes[0];
3711 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003712 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003713
Chris Masone02119d2008-09-05 16:13:11 -04003714 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003715 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003716 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003717 ret = 0;
3718failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003719 btrfs_free_path(path);
3720 return ret;
3721}
3722
Chris Masond352ac62008-09-29 15:18:18 -04003723/*
Chris Mason21151332011-11-10 20:39:08 -05003724 * copy everything in the in-memory inode into the btree.
3725 */
3726noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3727 struct btrfs_root *root, struct inode *inode)
3728{
3729 int ret;
3730
3731 /*
3732 * If the inode is a free space inode, we can deadlock during commit
3733 * if we put it into the delayed code.
3734 *
3735 * The data relocation inode should also be directly updated
3736 * without delay
3737 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003738 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003739 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3740 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003741 btrfs_update_root_times(trans, root);
3742
Chris Mason21151332011-11-10 20:39:08 -05003743 ret = btrfs_delayed_update_inode(trans, root, inode);
3744 if (!ret)
3745 btrfs_set_inode_last_trans(trans, inode);
3746 return ret;
3747 }
3748
3749 return btrfs_update_inode_item(trans, root, inode);
3750}
3751
Josef Bacikbe6aef62012-10-22 15:43:12 -04003752noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3753 struct btrfs_root *root,
3754 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003755{
3756 int ret;
3757
3758 ret = btrfs_update_inode(trans, root, inode);
3759 if (ret == -ENOSPC)
3760 return btrfs_update_inode_item(trans, root, inode);
3761 return ret;
3762}
3763
3764/*
Chris Masond352ac62008-09-29 15:18:18 -04003765 * unlink helper that gets used here in inode.c and in the tree logging
3766 * recovery code. It remove a link in a directory with a given name, and
3767 * also drops the back refs in the inode to the directory
3768 */
Al Viro92986792011-03-04 17:14:37 +00003769static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3770 struct btrfs_root *root,
3771 struct inode *dir, struct inode *inode,
3772 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003773{
3774 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003775 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003776 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003777 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003778 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003779 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003780 u64 ino = btrfs_ino(inode);
3781 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003782
3783 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003784 if (!path) {
3785 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003786 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003787 }
3788
Chris Masonb9473432009-03-13 11:00:37 -04003789 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003790 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003791 name, name_len, -1);
3792 if (IS_ERR(di)) {
3793 ret = PTR_ERR(di);
3794 goto err;
3795 }
3796 if (!di) {
3797 ret = -ENOENT;
3798 goto err;
3799 }
Chris Mason5f39d392007-10-15 16:14:19 -04003800 leaf = path->nodes[0];
3801 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003802 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003803 if (ret)
3804 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003805 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003806
Miao Xie67de1172013-12-26 13:07:06 +08003807 /*
3808 * If we don't have dir index, we have to get it by looking up
3809 * the inode ref, since we get the inode ref, remove it directly,
3810 * it is unnecessary to do delayed deletion.
3811 *
3812 * But if we have dir index, needn't search inode ref to get it.
3813 * Since the inode ref is close to the inode item, it is better
3814 * that we delay to delete it, and just do this deletion when
3815 * we update the inode item.
3816 */
3817 if (BTRFS_I(inode)->dir_index) {
3818 ret = btrfs_delayed_delete_inode_ref(inode);
3819 if (!ret) {
3820 index = BTRFS_I(inode)->dir_index;
3821 goto skip_backref;
3822 }
3823 }
3824
Li Zefan33345d012011-04-20 10:31:50 +08003825 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3826 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003827 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003828 btrfs_info(root->fs_info,
3829 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003830 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003831 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003832 goto err;
3833 }
Miao Xie67de1172013-12-26 13:07:06 +08003834skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003835 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003836 if (ret) {
3837 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003838 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003839 }
Chris Mason39279cc2007-06-12 06:35:45 -04003840
Chris Masone02119d2008-09-05 16:13:11 -04003841 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003842 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003843 if (ret != 0 && ret != -ENOENT) {
3844 btrfs_abort_transaction(trans, root, ret);
3845 goto err;
3846 }
Chris Masone02119d2008-09-05 16:13:11 -04003847
3848 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3849 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003850 if (ret == -ENOENT)
3851 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003852 else if (ret)
3853 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003854err:
3855 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003856 if (ret)
3857 goto out;
3858
3859 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003860 inode_inc_iversion(inode);
3861 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003862 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003863 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003864out:
Chris Mason39279cc2007-06-12 06:35:45 -04003865 return ret;
3866}
3867
Al Viro92986792011-03-04 17:14:37 +00003868int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3869 struct btrfs_root *root,
3870 struct inode *dir, struct inode *inode,
3871 const char *name, int name_len)
3872{
3873 int ret;
3874 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3875 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003876 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003877 ret = btrfs_update_inode(trans, root, inode);
3878 }
3879 return ret;
3880}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003881
3882/*
3883 * helper to start transaction for unlink and rmdir.
3884 *
Josef Bacikd52be812013-05-29 14:54:47 -04003885 * unlink and rmdir are special in btrfs, they do not always free space, so
3886 * if we cannot make our reservations the normal way try and see if there is
3887 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3888 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003889 */
Josef Bacikd52be812013-05-29 14:54:47 -04003890static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003891{
3892 struct btrfs_trans_handle *trans;
3893 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003894 int ret;
3895
Josef Bacike70bea52011-10-11 14:18:24 -04003896 /*
3897 * 1 for the possible orphan item
3898 * 1 for the dir item
3899 * 1 for the dir index
3900 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003901 * 1 for the inode
3902 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003903 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003904 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3905 return trans;
3906
Josef Bacikd52be812013-05-29 14:54:47 -04003907 if (PTR_ERR(trans) == -ENOSPC) {
3908 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003909
Josef Bacikd52be812013-05-29 14:54:47 -04003910 trans = btrfs_start_transaction(root, 0);
3911 if (IS_ERR(trans))
3912 return trans;
3913 ret = btrfs_cond_migrate_bytes(root->fs_info,
3914 &root->fs_info->trans_block_rsv,
3915 num_bytes, 5);
3916 if (ret) {
3917 btrfs_end_transaction(trans, root);
3918 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003919 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003920 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003921 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003922 }
Josef Bacikd52be812013-05-29 14:54:47 -04003923 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003924}
3925
Chris Mason39279cc2007-06-12 06:35:45 -04003926static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3927{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003928 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003929 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003930 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003931 int ret;
3932
Josef Bacikd52be812013-05-29 14:54:47 -04003933 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003934 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003935 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003936
Chris Mason12fcfd22009-03-24 10:24:20 -04003937 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3938
Chris Masone02119d2008-09-05 16:13:11 -04003939 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3940 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003941 if (ret)
3942 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003943
Yan, Zhenga22285a2010-05-16 10:48:46 -04003944 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003945 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003946 if (ret)
3947 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003948 }
Josef Bacik7b128762008-07-24 12:17:14 -04003949
Tsutomu Itohb5324022011-07-19 07:27:20 +00003950out:
Josef Bacikd52be812013-05-29 14:54:47 -04003951 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003952 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003953 return ret;
3954}
3955
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003956int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3957 struct btrfs_root *root,
3958 struct inode *dir, u64 objectid,
3959 const char *name, int name_len)
3960{
3961 struct btrfs_path *path;
3962 struct extent_buffer *leaf;
3963 struct btrfs_dir_item *di;
3964 struct btrfs_key key;
3965 u64 index;
3966 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003967 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003968
3969 path = btrfs_alloc_path();
3970 if (!path)
3971 return -ENOMEM;
3972
Li Zefan33345d012011-04-20 10:31:50 +08003973 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003974 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003975 if (IS_ERR_OR_NULL(di)) {
3976 if (!di)
3977 ret = -ENOENT;
3978 else
3979 ret = PTR_ERR(di);
3980 goto out;
3981 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003982
3983 leaf = path->nodes[0];
3984 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3985 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3986 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003987 if (ret) {
3988 btrfs_abort_transaction(trans, root, ret);
3989 goto out;
3990 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003991 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003992
3993 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3994 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003995 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003996 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003997 if (ret != -ENOENT) {
3998 btrfs_abort_transaction(trans, root, ret);
3999 goto out;
4000 }
Li Zefan33345d012011-04-20 10:31:50 +08004001 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004002 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004003 if (IS_ERR_OR_NULL(di)) {
4004 if (!di)
4005 ret = -ENOENT;
4006 else
4007 ret = PTR_ERR(di);
4008 btrfs_abort_transaction(trans, root, ret);
4009 goto out;
4010 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004011
4012 leaf = path->nodes[0];
4013 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004014 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004015 index = key.offset;
4016 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004017 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004018
Miao Xie16cdcec2011-04-22 18:12:22 +08004019 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004020 if (ret) {
4021 btrfs_abort_transaction(trans, root, ret);
4022 goto out;
4023 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004024
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004025 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004026 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004027 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004028 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004029 if (ret)
4030 btrfs_abort_transaction(trans, root, ret);
4031out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004032 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004033 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004034}
4035
Chris Mason39279cc2007-06-12 06:35:45 -04004036static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4037{
4038 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004039 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004040 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004041 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004042
David Sterbab3ae2442012-09-13 16:04:34 -06004043 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004044 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004045 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4046 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004047
Josef Bacikd52be812013-05-29 14:54:47 -04004048 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004049 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004050 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004051
Li Zefan33345d012011-04-20 10:31:50 +08004052 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004053 err = btrfs_unlink_subvol(trans, root, dir,
4054 BTRFS_I(inode)->location.objectid,
4055 dentry->d_name.name,
4056 dentry->d_name.len);
4057 goto out;
4058 }
4059
Josef Bacik7b128762008-07-24 12:17:14 -04004060 err = btrfs_orphan_add(trans, inode);
4061 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004062 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004063
Chris Mason39279cc2007-06-12 06:35:45 -04004064 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004065 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4066 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004067 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004068 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004069out:
Josef Bacikd52be812013-05-29 14:54:47 -04004070 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004071 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004072
Chris Mason39279cc2007-06-12 06:35:45 -04004073 return err;
4074}
4075
Chris Mason323ac952008-10-01 19:05:46 -04004076/*
Chris Mason39279cc2007-06-12 06:35:45 -04004077 * this can truncate away extent items, csum items and directory items.
4078 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004079 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004080 *
4081 * csum items that cross the new i_size are truncated to the new size
4082 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004083 *
4084 * min_type is the minimum key type to truncate down to. If set to 0, this
4085 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004086 */
Yan, Zheng80825102009-11-12 09:35:36 +00004087int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4088 struct btrfs_root *root,
4089 struct inode *inode,
4090 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004091{
Chris Mason39279cc2007-06-12 06:35:45 -04004092 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004093 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004094 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004095 struct btrfs_key key;
4096 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004097 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004098 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004099 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004100 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004101 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004102 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004103 int found_extent;
4104 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004105 int pending_del_nr = 0;
4106 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004107 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004108 int ret;
4109 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004110 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004111
4112 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004113
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004114 path = btrfs_alloc_path();
4115 if (!path)
4116 return -ENOMEM;
4117 path->reada = -1;
4118
Josef Bacik5dc562c2012-08-17 13:14:17 -04004119 /*
4120 * We want to drop from the next block forward in case this new size is
4121 * not block aligned since we will be keeping the last block of the
4122 * extent just the way it is.
4123 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004124 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4125 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004126 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4127 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004128
Miao Xie16cdcec2011-04-22 18:12:22 +08004129 /*
4130 * This function is also used to drop the items in the log tree before
4131 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4132 * it is used to drop the loged items. So we shouldn't kill the delayed
4133 * items.
4134 */
4135 if (min_type == 0 && root == BTRFS_I(inode)->root)
4136 btrfs_kill_delayed_inode_items(inode);
4137
Li Zefan33345d012011-04-20 10:31:50 +08004138 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004139 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004140 key.type = (u8)-1;
4141
Chris Mason85e21ba2008-01-29 15:11:36 -05004142search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004143 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004144 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004145 if (ret < 0) {
4146 err = ret;
4147 goto out;
4148 }
Chris Masond3977122009-01-05 21:25:51 -05004149
Chris Mason85e21ba2008-01-29 15:11:36 -05004150 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004151 /* there are no items in the tree for us to truncate, we're
4152 * done
4153 */
Yan, Zheng80825102009-11-12 09:35:36 +00004154 if (path->slots[0] == 0)
4155 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004156 path->slots[0]--;
4157 }
4158
Chris Masond3977122009-01-05 21:25:51 -05004159 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004160 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004161 leaf = path->nodes[0];
4162 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004163 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004164
Li Zefan33345d012011-04-20 10:31:50 +08004165 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004166 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004167
Chris Mason85e21ba2008-01-29 15:11:36 -05004168 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004169 break;
4170
Chris Mason5f39d392007-10-15 16:14:19 -04004171 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004172 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004173 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004174 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004175 extent_type = btrfs_file_extent_type(leaf, fi);
4176 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004177 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004178 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004179 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004180 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004181 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004182 }
Yan008630c2007-11-07 13:31:09 -05004183 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004184 }
Yan, Zheng80825102009-11-12 09:35:36 +00004185 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004186 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004187 } else {
4188 if (item_end < new_size)
4189 break;
4190 if (found_key.offset >= new_size)
4191 del_item = 1;
4192 else
4193 del_item = 0;
4194 }
Chris Mason39279cc2007-06-12 06:35:45 -04004195 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004196 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004197 if (found_type != BTRFS_EXTENT_DATA_KEY)
4198 goto delete;
4199
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004200 if (del_item)
4201 last_size = found_key.offset;
4202 else
4203 last_size = new_size;
4204
Chris Mason179e29e2007-11-01 11:28:41 -04004205 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004206 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004207 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004208 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004209 u64 orig_num_bytes =
4210 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004211 extent_num_bytes = ALIGN(new_size -
4212 found_key.offset,
4213 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004214 btrfs_set_file_extent_num_bytes(leaf, fi,
4215 extent_num_bytes);
4216 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004217 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004218 if (test_bit(BTRFS_ROOT_REF_COWS,
4219 &root->state) &&
4220 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004221 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004222 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004223 } else {
Chris Masondb945352007-10-15 16:15:53 -04004224 extent_num_bytes =
4225 btrfs_file_extent_disk_num_bytes(leaf,
4226 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004227 extent_offset = found_key.offset -
4228 btrfs_file_extent_offset(leaf, fi);
4229
Chris Mason39279cc2007-06-12 06:35:45 -04004230 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004231 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004232 if (extent_start != 0) {
4233 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004234 if (test_bit(BTRFS_ROOT_REF_COWS,
4235 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004236 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004237 }
4238 }
Chris Mason90692182008-02-08 13:49:28 -05004239 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004240 /*
4241 * we can't truncate inline items that have had
4242 * special encodings
4243 */
4244 if (!del_item &&
4245 btrfs_file_extent_compression(leaf, fi) == 0 &&
4246 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4247 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004248 u32 size = new_size - found_key.offset;
4249
Miao Xie27cdeb72014-04-02 19:51:05 +08004250 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004251 inode_sub_bytes(inode, item_end + 1 -
4252 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004253
4254 /*
4255 * update the ram bytes to properly reflect
4256 * the new size of our item
4257 */
4258 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004259 size =
4260 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004261 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004262 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4263 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004264 inode_sub_bytes(inode, item_end + 1 -
4265 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004266 }
Chris Mason39279cc2007-06-12 06:35:45 -04004267 }
Chris Mason179e29e2007-11-01 11:28:41 -04004268delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004269 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004270 if (!pending_del_nr) {
4271 /* no pending yet, add ourselves */
4272 pending_del_slot = path->slots[0];
4273 pending_del_nr = 1;
4274 } else if (pending_del_nr &&
4275 path->slots[0] + 1 == pending_del_slot) {
4276 /* hop on the pending chunk */
4277 pending_del_nr++;
4278 pending_del_slot = path->slots[0];
4279 } else {
Chris Masond3977122009-01-05 21:25:51 -05004280 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004281 }
Chris Mason39279cc2007-06-12 06:35:45 -04004282 } else {
4283 break;
4284 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004285 if (found_extent &&
4286 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4287 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004288 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004289 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004290 extent_num_bytes, 0,
4291 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004292 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004293 BUG_ON(ret);
4294 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004295
Yan, Zheng80825102009-11-12 09:35:36 +00004296 if (found_type == BTRFS_INODE_ITEM_KEY)
4297 break;
4298
4299 if (path->slots[0] == 0 ||
4300 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004301 if (pending_del_nr) {
4302 ret = btrfs_del_items(trans, root, path,
4303 pending_del_slot,
4304 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004305 if (ret) {
4306 btrfs_abort_transaction(trans,
4307 root, ret);
4308 goto error;
4309 }
Yan, Zheng80825102009-11-12 09:35:36 +00004310 pending_del_nr = 0;
4311 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004312 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004313 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004314 } else {
4315 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004316 }
Chris Mason39279cc2007-06-12 06:35:45 -04004317 }
Yan, Zheng80825102009-11-12 09:35:36 +00004318out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004319 if (pending_del_nr) {
4320 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4321 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004322 if (ret)
4323 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004324 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004325error:
Filipe Mananadac57052014-08-29 20:54:26 +01004326 if (last_size != (u64)-1 &&
4327 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004328 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004329 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004330 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004331}
4332
Chris Masona52d9a82007-08-27 16:49:44 -04004333/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004334 * btrfs_truncate_page - read, zero a chunk and write a page
4335 * @inode - inode that we're zeroing
4336 * @from - the offset to start zeroing
4337 * @len - the length to zero, 0 to zero the entire range respective to the
4338 * offset
4339 * @front - zero up to the offset instead of from the offset on
4340 *
4341 * This will find the page for the "from" offset and cow the page and zero the
4342 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004343 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004344int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4345 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004346{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004347 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004348 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004349 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4350 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004351 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004352 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004353 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004354 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4355 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4356 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004357 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004358 int ret = 0;
4359 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004360 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004361
Josef Bacik2aaa6652012-08-29 14:27:18 -04004362 if ((offset & (blocksize - 1)) == 0 &&
4363 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004364 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004365 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004366 if (ret)
4367 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004368
Chris Mason211c17f2008-05-15 09:13:45 -04004369again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004370 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004371 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004372 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004373 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004374 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004375 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004376
4377 page_start = page_offset(page);
4378 page_end = page_start + PAGE_CACHE_SIZE - 1;
4379
Chris Masona52d9a82007-08-27 16:49:44 -04004380 if (!PageUptodate(page)) {
4381 ret = btrfs_readpage(NULL, page);
4382 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004383 if (page->mapping != mapping) {
4384 unlock_page(page);
4385 page_cache_release(page);
4386 goto again;
4387 }
Chris Masona52d9a82007-08-27 16:49:44 -04004388 if (!PageUptodate(page)) {
4389 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004390 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004391 }
4392 }
Chris Mason211c17f2008-05-15 09:13:45 -04004393 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004394
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004395 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004396 set_page_extent_mapped(page);
4397
4398 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4399 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004400 unlock_extent_cached(io_tree, page_start, page_end,
4401 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004402 unlock_page(page);
4403 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004404 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004405 btrfs_put_ordered_extent(ordered);
4406 goto again;
4407 }
4408
Josef Bacik2ac55d42010-02-03 19:33:23 +00004409 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004410 EXTENT_DIRTY | EXTENT_DELALLOC |
4411 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004412 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004413
Josef Bacik2ac55d42010-02-03 19:33:23 +00004414 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4415 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004416 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004417 unlock_extent_cached(io_tree, page_start, page_end,
4418 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004419 goto out_unlock;
4420 }
4421
Chris Masone6dcd2d2008-07-17 12:53:50 -04004422 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004423 if (!len)
4424 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004425 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004426 if (front)
4427 memset(kaddr, 0, offset);
4428 else
4429 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004430 flush_dcache_page(page);
4431 kunmap(page);
4432 }
Chris Mason247e7432008-07-17 12:53:51 -04004433 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004434 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004435 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4436 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004437
Chris Mason89642222008-07-24 09:41:53 -04004438out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004439 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004440 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004441 unlock_page(page);
4442 page_cache_release(page);
4443out:
4444 return ret;
4445}
4446
Josef Bacik16e75492013-10-22 12:18:51 -04004447static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4448 u64 offset, u64 len)
4449{
4450 struct btrfs_trans_handle *trans;
4451 int ret;
4452
4453 /*
4454 * Still need to make sure the inode looks like it's been updated so
4455 * that any holes get logged if we fsync.
4456 */
4457 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4458 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4459 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4460 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4461 return 0;
4462 }
4463
4464 /*
4465 * 1 - for the one we're dropping
4466 * 1 - for the one we're adding
4467 * 1 - for updating the inode.
4468 */
4469 trans = btrfs_start_transaction(root, 3);
4470 if (IS_ERR(trans))
4471 return PTR_ERR(trans);
4472
4473 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4474 if (ret) {
4475 btrfs_abort_transaction(trans, root, ret);
4476 btrfs_end_transaction(trans, root);
4477 return ret;
4478 }
4479
4480 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4481 0, 0, len, 0, len, 0, 0, 0);
4482 if (ret)
4483 btrfs_abort_transaction(trans, root, ret);
4484 else
4485 btrfs_update_inode(trans, root, inode);
4486 btrfs_end_transaction(trans, root);
4487 return ret;
4488}
4489
Josef Bacik695a0d02011-03-04 15:46:53 -05004490/*
4491 * This function puts in dummy file extents for the area we're creating a hole
4492 * for. So if we are truncating this file to a larger size we need to insert
4493 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4494 * the range between oldsize and size
4495 */
Josef Bacika41ad392011-01-31 15:30:16 -05004496int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004497{
Yan Zheng9036c102008-10-30 14:19:41 -04004498 struct btrfs_root *root = BTRFS_I(inode)->root;
4499 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004500 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004501 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004502 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004503 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4504 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004505 u64 last_byte;
4506 u64 cur_offset;
4507 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004508 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004509
Josef Bacika71754f2013-06-17 17:14:39 -04004510 /*
4511 * If our size started in the middle of a page we need to zero out the
4512 * rest of the page before we expand the i_size, otherwise we could
4513 * expose stale data.
4514 */
4515 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4516 if (err)
4517 return err;
4518
Yan Zheng9036c102008-10-30 14:19:41 -04004519 if (size <= hole_start)
4520 return 0;
4521
Yan Zheng9036c102008-10-30 14:19:41 -04004522 while (1) {
4523 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004524
Josef Bacik2ac55d42010-02-03 19:33:23 +00004525 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004526 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004527 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4528 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004529 if (!ordered)
4530 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004531 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4532 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004533 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004534 btrfs_put_ordered_extent(ordered);
4535 }
4536
Yan Zheng9036c102008-10-30 14:19:41 -04004537 cur_offset = hole_start;
4538 while (1) {
4539 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4540 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004541 if (IS_ERR(em)) {
4542 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004543 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004544 break;
4545 }
Yan Zheng9036c102008-10-30 14:19:41 -04004546 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004547 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004548 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004549 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004550 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004551
Josef Bacik16e75492013-10-22 12:18:51 -04004552 err = maybe_insert_hole(root, inode, cur_offset,
4553 hole_size);
4554 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004555 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004556 btrfs_drop_extent_cache(inode, cur_offset,
4557 cur_offset + hole_size - 1, 0);
4558 hole_em = alloc_extent_map();
4559 if (!hole_em) {
4560 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4561 &BTRFS_I(inode)->runtime_flags);
4562 goto next;
4563 }
4564 hole_em->start = cur_offset;
4565 hole_em->len = hole_size;
4566 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004567
Josef Bacik5dc562c2012-08-17 13:14:17 -04004568 hole_em->block_start = EXTENT_MAP_HOLE;
4569 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004570 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004571 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004572 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4573 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004574 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004575
4576 while (1) {
4577 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004578 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004579 write_unlock(&em_tree->lock);
4580 if (err != -EEXIST)
4581 break;
4582 btrfs_drop_extent_cache(inode, cur_offset,
4583 cur_offset +
4584 hole_size - 1, 0);
4585 }
4586 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004587 }
Josef Bacik16e75492013-10-22 12:18:51 -04004588next:
Yan Zheng9036c102008-10-30 14:19:41 -04004589 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004590 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004591 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004592 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004593 break;
4594 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004595 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004596 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4597 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004598 return err;
4599}
4600
Eric Sandeen3972f262013-01-12 02:57:22 +00004601static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004602{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004603 struct btrfs_root *root = BTRFS_I(inode)->root;
4604 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004605 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004606 loff_t newsize = attr->ia_size;
4607 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004608 int ret;
4609
Eric Sandeen3972f262013-01-12 02:57:22 +00004610 /*
4611 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4612 * special case where we need to update the times despite not having
4613 * these flags set. For all other operations the VFS set these flags
4614 * explicitly if it wants a timestamp update.
4615 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004616 if (newsize != oldsize) {
4617 inode_inc_iversion(inode);
4618 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4619 inode->i_ctime = inode->i_mtime =
4620 current_fs_time(inode->i_sb);
4621 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004622
Josef Bacika41ad392011-01-31 15:30:16 -05004623 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004624 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004625 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004626 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004627 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004628
Miao Xief4a2f4c2011-12-14 20:12:01 -05004629 trans = btrfs_start_transaction(root, 1);
4630 if (IS_ERR(trans))
4631 return PTR_ERR(trans);
4632
4633 i_size_write(inode, newsize);
4634 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4635 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004636 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004637 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004638
Josef Bacika41ad392011-01-31 15:30:16 -05004639 /*
4640 * We're truncating a file that used to have good data down to
4641 * zero. Make sure it gets into the ordered flush list so that
4642 * any new writes get down to disk quickly.
4643 */
4644 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004645 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4646 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004647
Josef Bacikf3fe8202013-01-07 17:03:21 -05004648 /*
4649 * 1 for the orphan item we're going to add
4650 * 1 for the orphan item deletion.
4651 */
4652 trans = btrfs_start_transaction(root, 2);
4653 if (IS_ERR(trans))
4654 return PTR_ERR(trans);
4655
4656 /*
4657 * We need to do this in case we fail at _any_ point during the
4658 * actual truncate. Once we do the truncate_setsize we could
4659 * invalidate pages which forces any outstanding ordered io to
4660 * be instantly completed which will give us extents that need
4661 * to be truncated. If we fail to get an orphan inode down we
4662 * could have left over extents that were never meant to live,
4663 * so we need to garuntee from this point on that everything
4664 * will be consistent.
4665 */
4666 ret = btrfs_orphan_add(trans, inode);
4667 btrfs_end_transaction(trans, root);
4668 if (ret)
4669 return ret;
4670
Josef Bacika41ad392011-01-31 15:30:16 -05004671 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4672 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004673
4674 /* Disable nonlocked read DIO to avoid the end less truncate */
4675 btrfs_inode_block_unlocked_dio(inode);
4676 inode_dio_wait(inode);
4677 btrfs_inode_resume_unlocked_dio(inode);
4678
Josef Bacika41ad392011-01-31 15:30:16 -05004679 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004680 if (ret && inode->i_nlink) {
4681 int err;
4682
4683 /*
4684 * failed to truncate, disk_i_size is only adjusted down
4685 * as we remove extents, so it should represent the true
4686 * size of the inode, so reset the in memory size and
4687 * delete our orphan entry.
4688 */
4689 trans = btrfs_join_transaction(root);
4690 if (IS_ERR(trans)) {
4691 btrfs_orphan_del(NULL, inode);
4692 return ret;
4693 }
4694 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4695 err = btrfs_orphan_del(trans, inode);
4696 if (err)
4697 btrfs_abort_transaction(trans, root, err);
4698 btrfs_end_transaction(trans, root);
4699 }
Yan, Zheng80825102009-11-12 09:35:36 +00004700 }
4701
Josef Bacika41ad392011-01-31 15:30:16 -05004702 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004703}
4704
Chris Mason39279cc2007-06-12 06:35:45 -04004705static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4706{
4707 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004708 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004709 int err;
4710
Li Zefanb83cc962010-12-20 16:04:08 +08004711 if (btrfs_root_readonly(root))
4712 return -EROFS;
4713
Chris Mason39279cc2007-06-12 06:35:45 -04004714 err = inode_change_ok(inode, attr);
4715 if (err)
4716 return err;
4717
Chris Mason5a3f23d2009-03-31 13:27:11 -04004718 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004719 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004720 if (err)
4721 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004722 }
Yan Zheng9036c102008-10-30 14:19:41 -04004723
Christoph Hellwig10257742010-06-04 11:30:02 +02004724 if (attr->ia_valid) {
4725 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004726 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004727 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004728
Josef Bacik22c44fe2011-11-30 10:45:38 -05004729 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004730 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004731 }
4732
Chris Mason39279cc2007-06-12 06:35:45 -04004733 return err;
4734}
Chris Mason61295eb2008-01-14 16:24:38 -05004735
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004736/*
4737 * While truncating the inode pages during eviction, we get the VFS calling
4738 * btrfs_invalidatepage() against each page of the inode. This is slow because
4739 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4740 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4741 * extent_state structures over and over, wasting lots of time.
4742 *
4743 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4744 * those expensive operations on a per page basis and do only the ordered io
4745 * finishing, while we release here the extent_map and extent_state structures,
4746 * without the excessive merging and splitting.
4747 */
4748static void evict_inode_truncate_pages(struct inode *inode)
4749{
4750 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4751 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4752 struct rb_node *node;
4753
4754 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004755 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004756
4757 write_lock(&map_tree->lock);
4758 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4759 struct extent_map *em;
4760
4761 node = rb_first(&map_tree->map);
4762 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004763 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4764 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004765 remove_extent_mapping(map_tree, em);
4766 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004767 if (need_resched()) {
4768 write_unlock(&map_tree->lock);
4769 cond_resched();
4770 write_lock(&map_tree->lock);
4771 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004772 }
4773 write_unlock(&map_tree->lock);
4774
4775 spin_lock(&io_tree->lock);
4776 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4777 struct extent_state *state;
4778 struct extent_state *cached_state = NULL;
4779
4780 node = rb_first(&io_tree->state);
4781 state = rb_entry(node, struct extent_state, rb_node);
4782 atomic_inc(&state->refs);
4783 spin_unlock(&io_tree->lock);
4784
4785 lock_extent_bits(io_tree, state->start, state->end,
4786 0, &cached_state);
4787 clear_extent_bit(io_tree, state->start, state->end,
4788 EXTENT_LOCKED | EXTENT_DIRTY |
4789 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4790 EXTENT_DEFRAG, 1, 1,
4791 &cached_state, GFP_NOFS);
4792 free_extent_state(state);
4793
Filipe Manana7064dd52014-08-08 02:47:05 +01004794 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004795 spin_lock(&io_tree->lock);
4796 }
4797 spin_unlock(&io_tree->lock);
4798}
4799
Al Virobd555972010-06-07 11:35:40 -04004800void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004801{
4802 struct btrfs_trans_handle *trans;
4803 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004804 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004805 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004806 int ret;
4807
liubo1abe9b82011-03-24 11:18:59 +00004808 trace_btrfs_inode_evict(inode);
4809
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004810 evict_inode_truncate_pages(inode);
4811
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004812 if (inode->i_nlink &&
4813 ((btrfs_root_refs(&root->root_item) != 0 &&
4814 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4815 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004816 goto no_delete;
4817
Chris Mason39279cc2007-06-12 06:35:45 -04004818 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004819 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004820 goto no_delete;
4821 }
Al Virobd555972010-06-07 11:35:40 -04004822 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004823 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004824
Miao Xief6124962014-09-12 18:44:04 +08004825 btrfs_free_io_failure_record(inode, 0, (u64)-1);
4826
Yan, Zhengc71bf092009-11-12 09:34:40 +00004827 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004828 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004829 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004830 goto no_delete;
4831 }
4832
Yan, Zheng76dda932009-09-21 16:00:26 -04004833 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004834 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4835 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004836 goto no_delete;
4837 }
4838
Miao Xie0e8c36a2012-12-19 06:59:51 +00004839 ret = btrfs_commit_inode_delayed_inode(inode);
4840 if (ret) {
4841 btrfs_orphan_del(NULL, inode);
4842 goto no_delete;
4843 }
4844
Miao Xie66d8f3d2012-09-06 04:02:28 -06004845 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004846 if (!rsv) {
4847 btrfs_orphan_del(NULL, inode);
4848 goto no_delete;
4849 }
Josef Bacik4a338542011-08-29 11:01:31 -04004850 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004851 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004852 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004853
Chris Masondbe674a2008-07-17 12:54:05 -04004854 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004855
Josef Bacik4289a662011-08-05 13:22:24 -04004856 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004857 * This is a bit simpler than btrfs_truncate since we've already
4858 * reserved our space for our orphan item in the unlink, so we just
4859 * need to reserve some slack space in case we add bytes and update
4860 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004861 */
Yan, Zheng80825102009-11-12 09:35:36 +00004862 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004863 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4864 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004865
Josef Bacik726c35f2011-09-26 15:46:06 -04004866 /*
4867 * Try and steal from the global reserve since we will
4868 * likely not use this space anyway, we want to try as
4869 * hard as possible to get this to work.
4870 */
4871 if (ret)
4872 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4873
Yan, Zhengd68fc572010-05-16 10:49:58 -04004874 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004875 btrfs_warn(root->fs_info,
4876 "Could not get space for a delete, will truncate on mount %d",
4877 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004878 btrfs_orphan_del(NULL, inode);
4879 btrfs_free_block_rsv(root, rsv);
4880 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004881 }
4882
Miao Xie0e8c36a2012-12-19 06:59:51 +00004883 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004884 if (IS_ERR(trans)) {
4885 btrfs_orphan_del(NULL, inode);
4886 btrfs_free_block_rsv(root, rsv);
4887 goto no_delete;
4888 }
4889
4890 trans->block_rsv = rsv;
4891
Yan, Zhengd68fc572010-05-16 10:49:58 -04004892 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004893 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004894 break;
4895
Miao Xie8407aa42012-09-07 01:43:32 -06004896 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004897 btrfs_end_transaction(trans, root);
4898 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004899 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004900 }
4901
Josef Bacik4289a662011-08-05 13:22:24 -04004902 btrfs_free_block_rsv(root, rsv);
4903
Josef Bacik4ef31a42013-08-13 14:10:08 -04004904 /*
4905 * Errors here aren't a big deal, it just means we leave orphan items
4906 * in the tree. They will be cleaned up on the next mount.
4907 */
Yan, Zheng80825102009-11-12 09:35:36 +00004908 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004909 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004910 btrfs_orphan_del(trans, inode);
4911 } else {
4912 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004913 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004914
Josef Bacik4289a662011-08-05 13:22:24 -04004915 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004916 if (!(root == root->fs_info->tree_root ||
4917 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004918 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004919
Chris Mason54aa1f42007-06-22 14:16:25 -04004920 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004921 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004922no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004923 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004924 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004925 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004926}
4927
4928/*
4929 * this returns the key found in the dir entry in the location pointer.
4930 * If no dir entries were found, location->objectid is 0.
4931 */
4932static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4933 struct btrfs_key *location)
4934{
4935 const char *name = dentry->d_name.name;
4936 int namelen = dentry->d_name.len;
4937 struct btrfs_dir_item *di;
4938 struct btrfs_path *path;
4939 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004940 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004941
4942 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004943 if (!path)
4944 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004945
Li Zefan33345d012011-04-20 10:31:50 +08004946 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004947 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004948 if (IS_ERR(di))
4949 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004950
David Sterbac7040052011-04-19 18:00:01 +02004951 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004952 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004953
Chris Mason5f39d392007-10-15 16:14:19 -04004954 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004955out:
Chris Mason39279cc2007-06-12 06:35:45 -04004956 btrfs_free_path(path);
4957 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004958out_err:
4959 location->objectid = 0;
4960 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004961}
4962
4963/*
4964 * when we hit a tree root in a directory, the btrfs part of the inode
4965 * needs to be changed to reflect the root directory of the tree root. This
4966 * is kind of like crossing a mount point.
4967 */
4968static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004969 struct inode *dir,
4970 struct dentry *dentry,
4971 struct btrfs_key *location,
4972 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004973{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004974 struct btrfs_path *path;
4975 struct btrfs_root *new_root;
4976 struct btrfs_root_ref *ref;
4977 struct extent_buffer *leaf;
4978 int ret;
4979 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004980
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004981 path = btrfs_alloc_path();
4982 if (!path) {
4983 err = -ENOMEM;
4984 goto out;
4985 }
Chris Mason39279cc2007-06-12 06:35:45 -04004986
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004987 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004988 ret = btrfs_find_item(root->fs_info->tree_root, path,
4989 BTRFS_I(dir)->root->root_key.objectid,
4990 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004991 if (ret) {
4992 if (ret < 0)
4993 err = ret;
4994 goto out;
4995 }
Chris Mason39279cc2007-06-12 06:35:45 -04004996
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004997 leaf = path->nodes[0];
4998 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004999 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005000 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5001 goto out;
5002
5003 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5004 (unsigned long)(ref + 1),
5005 dentry->d_name.len);
5006 if (ret)
5007 goto out;
5008
David Sterbab3b4aa72011-04-21 01:20:15 +02005009 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005010
5011 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5012 if (IS_ERR(new_root)) {
5013 err = PTR_ERR(new_root);
5014 goto out;
5015 }
5016
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005017 *sub_root = new_root;
5018 location->objectid = btrfs_root_dirid(&new_root->root_item);
5019 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005020 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005021 err = 0;
5022out:
5023 btrfs_free_path(path);
5024 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005025}
5026
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005027static void inode_tree_add(struct inode *inode)
5028{
5029 struct btrfs_root *root = BTRFS_I(inode)->root;
5030 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005031 struct rb_node **p;
5032 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005033 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005034 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005035
Al Viro1d3382cb2010-10-23 15:19:20 -04005036 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005037 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005038 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005039 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005040 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005041 while (*p) {
5042 parent = *p;
5043 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5044
Li Zefan33345d012011-04-20 10:31:50 +08005045 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005046 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005047 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005048 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005049 else {
5050 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005051 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005052 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005053 RB_CLEAR_NODE(parent);
5054 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005055 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005056 }
5057 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005058 rb_link_node(new, parent, p);
5059 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005060 spin_unlock(&root->inode_lock);
5061}
5062
5063static void inode_tree_del(struct inode *inode)
5064{
5065 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005066 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005067
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005068 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005069 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005070 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005071 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005072 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005073 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005074 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005075
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005076 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005077 synchronize_srcu(&root->fs_info->subvol_srcu);
5078 spin_lock(&root->inode_lock);
5079 empty = RB_EMPTY_ROOT(&root->inode_tree);
5080 spin_unlock(&root->inode_lock);
5081 if (empty)
5082 btrfs_add_dead_root(root);
5083 }
5084}
5085
Jeff Mahoney143bede2012-03-01 14:56:26 +01005086void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005087{
5088 struct rb_node *node;
5089 struct rb_node *prev;
5090 struct btrfs_inode *entry;
5091 struct inode *inode;
5092 u64 objectid = 0;
5093
Liu Bo7813b3d2014-02-10 17:37:25 +08005094 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5095 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005096
5097 spin_lock(&root->inode_lock);
5098again:
5099 node = root->inode_tree.rb_node;
5100 prev = NULL;
5101 while (node) {
5102 prev = node;
5103 entry = rb_entry(node, struct btrfs_inode, rb_node);
5104
Li Zefan33345d012011-04-20 10:31:50 +08005105 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005106 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005107 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005108 node = node->rb_right;
5109 else
5110 break;
5111 }
5112 if (!node) {
5113 while (prev) {
5114 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005115 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005116 node = prev;
5117 break;
5118 }
5119 prev = rb_next(prev);
5120 }
5121 }
5122 while (node) {
5123 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005124 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005125 inode = igrab(&entry->vfs_inode);
5126 if (inode) {
5127 spin_unlock(&root->inode_lock);
5128 if (atomic_read(&inode->i_count) > 1)
5129 d_prune_aliases(inode);
5130 /*
Al Viro45321ac2010-06-07 13:43:19 -04005131 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005132 * the inode cache when its usage count
5133 * hits zero.
5134 */
5135 iput(inode);
5136 cond_resched();
5137 spin_lock(&root->inode_lock);
5138 goto again;
5139 }
5140
5141 if (cond_resched_lock(&root->inode_lock))
5142 goto again;
5143
5144 node = rb_next(node);
5145 }
5146 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005147}
5148
Chris Masone02119d2008-09-05 16:13:11 -04005149static int btrfs_init_locked_inode(struct inode *inode, void *p)
5150{
5151 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005152 inode->i_ino = args->location->objectid;
5153 memcpy(&BTRFS_I(inode)->location, args->location,
5154 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005155 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005156 return 0;
5157}
5158
5159static int btrfs_find_actor(struct inode *inode, void *opaque)
5160{
5161 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005162 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005163 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005164}
5165
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005166static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005167 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005168 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005169{
5170 struct inode *inode;
5171 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005172 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005173
Chris Mason90d3e592014-01-09 17:28:00 -08005174 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005175 args.root = root;
5176
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005177 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005178 btrfs_init_locked_inode,
5179 (void *)&args);
5180 return inode;
5181}
5182
Balaji Rao1a54ef82008-07-21 02:01:04 +05305183/* Get an inode object given its location and corresponding root.
5184 * Returns in *is_new if the inode was read from disk
5185 */
5186struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005187 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305188{
5189 struct inode *inode;
5190
Chris Mason90d3e592014-01-09 17:28:00 -08005191 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305192 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005193 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305194
5195 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305196 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005197 if (!is_bad_inode(inode)) {
5198 inode_tree_add(inode);
5199 unlock_new_inode(inode);
5200 if (new)
5201 *new = 1;
5202 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005203 unlock_new_inode(inode);
5204 iput(inode);
5205 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005206 }
5207 }
5208
Balaji Rao1a54ef82008-07-21 02:01:04 +05305209 return inode;
5210}
5211
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005212static struct inode *new_simple_dir(struct super_block *s,
5213 struct btrfs_key *key,
5214 struct btrfs_root *root)
5215{
5216 struct inode *inode = new_inode(s);
5217
5218 if (!inode)
5219 return ERR_PTR(-ENOMEM);
5220
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005221 BTRFS_I(inode)->root = root;
5222 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005223 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005224
5225 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005226 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005227 inode->i_fop = &simple_dir_operations;
5228 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5229 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5230
5231 return inode;
5232}
5233
Chris Mason3de45862008-11-17 21:02:50 -05005234struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005235{
Chris Masond3977122009-01-05 21:25:51 -05005236 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005237 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005238 struct btrfs_root *sub_root = root;
5239 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005240 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005241 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005242
5243 if (dentry->d_name.len > BTRFS_NAME_LEN)
5244 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005245
Jeff Layton39e3c952012-11-28 11:30:53 -05005246 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005247 if (ret < 0)
5248 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005249
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005250 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005251 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005252
5253 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005254 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005255 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005256 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005257
5258 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5259
Yan, Zheng76dda932009-09-21 16:00:26 -04005260 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005261 ret = fixup_tree_root_location(root, dir, dentry,
5262 &location, &sub_root);
5263 if (ret < 0) {
5264 if (ret != -ENOENT)
5265 inode = ERR_PTR(ret);
5266 else
5267 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5268 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005269 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005270 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005271 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5272
Julia Lawall34d19ba2011-01-24 19:55:19 +00005273 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005274 down_read(&root->fs_info->cleanup_work_sem);
5275 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005276 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005277 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005278 if (ret) {
5279 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005280 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005281 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005282 }
5283
Chris Mason3de45862008-11-17 21:02:50 -05005284 return inode;
5285}
5286
Nick Pigginfe15ce42011-01-07 17:49:23 +11005287static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005288{
5289 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005290 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005291
Li Zefan848cce02012-02-21 17:04:28 +08005292 if (!inode && !IS_ROOT(dentry))
5293 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005294
Li Zefan848cce02012-02-21 17:04:28 +08005295 if (inode) {
5296 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005297 if (btrfs_root_refs(&root->root_item) == 0)
5298 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005299
5300 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5301 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005302 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005303 return 0;
5304}
5305
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005306static void btrfs_dentry_release(struct dentry *dentry)
5307{
Daeseok Youn944a4512014-04-14 15:37:02 +09005308 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005309}
5310
Chris Mason3de45862008-11-17 21:02:50 -05005311static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005312 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005313{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005314 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005315
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005316 inode = btrfs_lookup_dentry(dir, dentry);
5317 if (IS_ERR(inode)) {
5318 if (PTR_ERR(inode) == -ENOENT)
5319 inode = NULL;
5320 else
5321 return ERR_CAST(inode);
5322 }
5323
Josef Bacik3a0dfa62014-02-14 13:43:48 -05005324 return d_materialise_unique(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005325}
5326
Miao Xie16cdcec2011-04-22 18:12:22 +08005327unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005328 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5329};
5330
Al Viro9cdda8d2013-05-22 16:48:09 -04005331static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005332{
Al Viro9cdda8d2013-05-22 16:48:09 -04005333 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005334 struct btrfs_root *root = BTRFS_I(inode)->root;
5335 struct btrfs_item *item;
5336 struct btrfs_dir_item *di;
5337 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005338 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005339 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005340 struct list_head ins_list;
5341 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005342 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005343 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005344 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005345 unsigned char d_type;
5346 int over = 0;
5347 u32 di_cur;
5348 u32 di_total;
5349 u32 di_len;
5350 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005351 char tmp_name[32];
5352 char *name_ptr;
5353 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005354 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005355
5356 /* FIXME, use a real flag for deciding about the key type */
5357 if (root->fs_info->tree_root == root)
5358 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005359
Al Viro9cdda8d2013-05-22 16:48:09 -04005360 if (!dir_emit_dots(file, ctx))
5361 return 0;
5362
David Woodhouse49593bf2008-08-17 17:08:36 +01005363 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005364 if (!path)
5365 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005366
Josef Bacik026fd312011-05-13 10:32:11 -04005367 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005368
Miao Xie16cdcec2011-04-22 18:12:22 +08005369 if (key_type == BTRFS_DIR_INDEX_KEY) {
5370 INIT_LIST_HEAD(&ins_list);
5371 INIT_LIST_HEAD(&del_list);
5372 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5373 }
5374
David Sterba962a2982014-06-04 18:41:45 +02005375 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005376 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005377 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005378
Chris Mason39279cc2007-06-12 06:35:45 -04005379 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5380 if (ret < 0)
5381 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005382
5383 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005384 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005385 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005386 if (slot >= btrfs_header_nritems(leaf)) {
5387 ret = btrfs_next_leaf(root, path);
5388 if (ret < 0)
5389 goto err;
5390 else if (ret > 0)
5391 break;
5392 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005393 }
Chris Mason3de45862008-11-17 21:02:50 -05005394
Ross Kirkdd3cc162013-09-16 15:58:09 +01005395 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005396 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5397
5398 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005399 break;
David Sterba962a2982014-06-04 18:41:45 +02005400 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005401 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005402 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005403 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005404 if (key_type == BTRFS_DIR_INDEX_KEY &&
5405 btrfs_should_delete_dir_index(&del_list,
5406 found_key.offset))
5407 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005408
Al Viro9cdda8d2013-05-22 16:48:09 -04005409 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005410 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005411
Chris Mason39279cc2007-06-12 06:35:45 -04005412 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5413 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005414 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005415
5416 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005417 struct btrfs_key location;
5418
Josef Bacik22a94d42011-03-16 16:47:17 -04005419 if (verify_dir_item(root, leaf, di))
5420 break;
5421
Chris Mason5f39d392007-10-15 16:14:19 -04005422 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005423 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005424 name_ptr = tmp_name;
5425 } else {
5426 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005427 if (!name_ptr) {
5428 ret = -ENOMEM;
5429 goto err;
5430 }
Chris Mason5f39d392007-10-15 16:14:19 -04005431 }
5432 read_extent_buffer(leaf, name_ptr,
5433 (unsigned long)(di + 1), name_len);
5434
5435 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5436 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005437
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005438
Chris Mason3de45862008-11-17 21:02:50 -05005439 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005440 * skip it.
5441 *
5442 * In contrast to old kernels, we insert the snapshot's
5443 * dir item and dir index after it has been created, so
5444 * we won't find a reference to our own snapshot. We
5445 * still keep the following code for backward
5446 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005447 */
5448 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5449 location.objectid == root->root_key.objectid) {
5450 over = 0;
5451 goto skip;
5452 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005453 over = !dir_emit(ctx, name_ptr, name_len,
5454 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005455
Chris Mason3de45862008-11-17 21:02:50 -05005456skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005457 if (name_ptr != tmp_name)
5458 kfree(name_ptr);
5459
Chris Mason39279cc2007-06-12 06:35:45 -04005460 if (over)
5461 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005462 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005463 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005464 di_cur += di_len;
5465 di = (struct btrfs_dir_item *)((char *)di + di_len);
5466 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005467next:
5468 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005469 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005470
Miao Xie16cdcec2011-04-22 18:12:22 +08005471 if (key_type == BTRFS_DIR_INDEX_KEY) {
5472 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005473 ctx->pos++;
5474 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005475 if (ret)
5476 goto nopos;
5477 }
5478
David Woodhouse49593bf2008-08-17 17:08:36 +01005479 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005480 ctx->pos++;
5481
5482 /*
5483 * Stop new entries from being returned after we return the last
5484 * entry.
5485 *
5486 * New directory entries are assigned a strictly increasing
5487 * offset. This means that new entries created during readdir
5488 * are *guaranteed* to be seen in the future by that readdir.
5489 * This has broken buggy programs which operate on names as
5490 * they're returned by readdir. Until we re-use freed offsets
5491 * we have this hack to stop new entries from being returned
5492 * under the assumption that they'll never reach this huge
5493 * offset.
5494 *
5495 * This is being careful not to overflow 32bit loff_t unless the
5496 * last entry requires it because doing so has broken 32bit apps
5497 * in the past.
5498 */
5499 if (key_type == BTRFS_DIR_INDEX_KEY) {
5500 if (ctx->pos >= INT_MAX)
5501 ctx->pos = LLONG_MAX;
5502 else
5503 ctx->pos = INT_MAX;
5504 }
Chris Mason39279cc2007-06-12 06:35:45 -04005505nopos:
5506 ret = 0;
5507err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005508 if (key_type == BTRFS_DIR_INDEX_KEY)
5509 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005510 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005511 return ret;
5512}
5513
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005514int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005515{
5516 struct btrfs_root *root = BTRFS_I(inode)->root;
5517 struct btrfs_trans_handle *trans;
5518 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005519 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005520
Josef Bacik72ac3c02012-05-23 14:13:11 -04005521 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005522 return 0;
5523
Liu Bo83eea1f2012-07-10 05:28:39 -06005524 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005525 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005526
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005527 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005528 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005529 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005530 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005531 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005532 if (IS_ERR(trans))
5533 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005534 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005535 }
5536 return ret;
5537}
5538
5539/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005540 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005541 * inode changes. But, it is most likely to find the inode in cache.
5542 * FIXME, needs more benchmarking...there are no reasons other than performance
5543 * to keep or drop this code.
5544 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005545static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005546{
5547 struct btrfs_root *root = BTRFS_I(inode)->root;
5548 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005549 int ret;
5550
Josef Bacik72ac3c02012-05-23 14:13:11 -04005551 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005552 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005553
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005554 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005555 if (IS_ERR(trans))
5556 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005557
5558 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005559 if (ret && ret == -ENOSPC) {
5560 /* whoops, lets try again with the full transaction */
5561 btrfs_end_transaction(trans, root);
5562 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005563 if (IS_ERR(trans))
5564 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005565
Chris Mason94b60442010-05-26 11:02:00 -04005566 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005567 }
Chris Mason39279cc2007-06-12 06:35:45 -04005568 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005569 if (BTRFS_I(inode)->delayed_node)
5570 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005571
5572 return ret;
5573}
5574
5575/*
5576 * This is a copy of file_update_time. We need this so we can return error on
5577 * ENOSPC for updating the inode in the case of file write and mmap writes.
5578 */
Josef Bacike41f9412012-03-26 09:46:47 -04005579static int btrfs_update_time(struct inode *inode, struct timespec *now,
5580 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005581{
Alexander Block2bc5565282012-06-15 09:49:33 +02005582 struct btrfs_root *root = BTRFS_I(inode)->root;
5583
5584 if (btrfs_root_readonly(root))
5585 return -EROFS;
5586
Josef Bacike41f9412012-03-26 09:46:47 -04005587 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005588 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005589 if (flags & S_CTIME)
5590 inode->i_ctime = *now;
5591 if (flags & S_MTIME)
5592 inode->i_mtime = *now;
5593 if (flags & S_ATIME)
5594 inode->i_atime = *now;
5595 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005596}
5597
Chris Masond352ac62008-09-29 15:18:18 -04005598/*
5599 * find the highest existing sequence number in a directory
5600 * and then set the in-memory index_cnt variable to reflect
5601 * free sequence numbers
5602 */
Josef Bacikaec74772008-07-24 12:12:38 -04005603static int btrfs_set_inode_index_count(struct inode *inode)
5604{
5605 struct btrfs_root *root = BTRFS_I(inode)->root;
5606 struct btrfs_key key, found_key;
5607 struct btrfs_path *path;
5608 struct extent_buffer *leaf;
5609 int ret;
5610
Li Zefan33345d012011-04-20 10:31:50 +08005611 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005612 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005613 key.offset = (u64)-1;
5614
5615 path = btrfs_alloc_path();
5616 if (!path)
5617 return -ENOMEM;
5618
5619 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5620 if (ret < 0)
5621 goto out;
5622 /* FIXME: we should be able to handle this */
5623 if (ret == 0)
5624 goto out;
5625 ret = 0;
5626
5627 /*
5628 * MAGIC NUMBER EXPLANATION:
5629 * since we search a directory based on f_pos we have to start at 2
5630 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5631 * else has to start at 2
5632 */
5633 if (path->slots[0] == 0) {
5634 BTRFS_I(inode)->index_cnt = 2;
5635 goto out;
5636 }
5637
5638 path->slots[0]--;
5639
5640 leaf = path->nodes[0];
5641 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5642
Li Zefan33345d012011-04-20 10:31:50 +08005643 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005644 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005645 BTRFS_I(inode)->index_cnt = 2;
5646 goto out;
5647 }
5648
5649 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5650out:
5651 btrfs_free_path(path);
5652 return ret;
5653}
5654
Chris Masond352ac62008-09-29 15:18:18 -04005655/*
5656 * helper to find a free sequence number in a given directory. This current
5657 * code is very simple, later versions will do smarter things in the btree
5658 */
Chris Mason3de45862008-11-17 21:02:50 -05005659int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005660{
5661 int ret = 0;
5662
5663 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005664 ret = btrfs_inode_delayed_dir_index_count(dir);
5665 if (ret) {
5666 ret = btrfs_set_inode_index_count(dir);
5667 if (ret)
5668 return ret;
5669 }
Josef Bacikaec74772008-07-24 12:12:38 -04005670 }
5671
Chris Mason00e4e6b2008-08-05 11:18:09 -04005672 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005673 BTRFS_I(dir)->index_cnt++;
5674
5675 return ret;
5676}
5677
Chris Masonb0d5d102014-09-08 13:08:51 -07005678static int btrfs_insert_inode_locked(struct inode *inode)
5679{
5680 struct btrfs_iget_args args;
5681 args.location = &BTRFS_I(inode)->location;
5682 args.root = BTRFS_I(inode)->root;
5683
5684 return insert_inode_locked4(inode,
5685 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5686 btrfs_find_actor, &args);
5687}
5688
Chris Mason39279cc2007-06-12 06:35:45 -04005689static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5690 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005691 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005692 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005693 u64 ref_objectid, u64 objectid,
5694 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005695{
5696 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005697 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005698 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005699 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005700 struct btrfs_inode_ref *ref;
5701 struct btrfs_key key[2];
5702 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005703 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005704 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005705 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005706
Chris Mason5f39d392007-10-15 16:14:19 -04005707 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005708 if (!path)
5709 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005710
Chris Mason39279cc2007-06-12 06:35:45 -04005711 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005712 if (!inode) {
5713 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005714 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005715 }
Chris Mason39279cc2007-06-12 06:35:45 -04005716
Li Zefan581bb052011-04-20 10:06:11 +08005717 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005718 * O_TMPFILE, set link count to 0, so that after this point,
5719 * we fill in an inode item with the correct link count.
5720 */
5721 if (!name)
5722 set_nlink(inode, 0);
5723
5724 /*
Li Zefan581bb052011-04-20 10:06:11 +08005725 * we have to initialize this early, so we can reclaim the inode
5726 * number if we fail afterwards in this function.
5727 */
5728 inode->i_ino = objectid;
5729
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005730 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005731 trace_btrfs_inode_request(dir);
5732
Chris Mason3de45862008-11-17 21:02:50 -05005733 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005734 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005735 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005736 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005737 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005738 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005739 } else if (dir) {
5740 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005741 }
5742 /*
5743 * index_cnt is ignored for everything but a dir,
5744 * btrfs_get_inode_index_count has an explanation for the magic
5745 * number
5746 */
5747 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005748 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005749 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005750 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005751 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005752
Josef Bacik5dc562c2012-08-17 13:14:17 -04005753 /*
5754 * We could have gotten an inode number from somebody who was fsynced
5755 * and then removed in this same transaction, so let's just set full
5756 * sync since it will be a full sync anyway and this will blow away the
5757 * old info in the log.
5758 */
5759 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5760
Chris Mason9c583092008-01-29 15:15:18 -05005761 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005762 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005763 key[0].offset = 0;
5764
Chris Mason9c583092008-01-29 15:15:18 -05005765 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005766
5767 if (name) {
5768 /*
5769 * Start new inodes with an inode_ref. This is slightly more
5770 * efficient for small numbers of hard links since they will
5771 * be packed into one item. Extended refs will kick in if we
5772 * add more hard links than can fit in the ref item.
5773 */
5774 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005775 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005776 key[1].offset = ref_objectid;
5777
5778 sizes[1] = name_len + sizeof(*ref);
5779 }
Chris Mason9c583092008-01-29 15:15:18 -05005780
Chris Masonb0d5d102014-09-08 13:08:51 -07005781 location = &BTRFS_I(inode)->location;
5782 location->objectid = objectid;
5783 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005784 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005785
5786 ret = btrfs_insert_inode_locked(inode);
5787 if (ret < 0)
5788 goto fail;
5789
Chris Masonb9473432009-03-13 11:00:37 -04005790 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005791 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005792 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005793 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005794
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005795 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005796 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005797 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005798 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5799 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005800 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5801 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005802 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005803
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005804 if (name) {
5805 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5806 struct btrfs_inode_ref);
5807 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5808 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5809 ptr = (unsigned long)(ref + 1);
5810 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5811 }
Chris Mason9c583092008-01-29 15:15:18 -05005812
Chris Mason5f39d392007-10-15 16:14:19 -04005813 btrfs_mark_buffer_dirty(path->nodes[0]);
5814 btrfs_free_path(path);
5815
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005816 btrfs_inherit_iflags(inode, dir);
5817
Al Viro569254b2011-07-24 17:08:40 -04005818 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005819 if (btrfs_test_opt(root, NODATASUM))
5820 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005821 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005822 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5823 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005824 }
5825
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005826 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005827
5828 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005829 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005830
Alexander Block8ea05e32012-07-25 17:35:53 +02005831 btrfs_update_root_times(trans, root);
5832
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005833 ret = btrfs_inode_inherit_props(trans, inode, dir);
5834 if (ret)
5835 btrfs_err(root->fs_info,
5836 "error inheriting props for ino %llu (root %llu): %d",
5837 btrfs_ino(inode), root->root_key.objectid, ret);
5838
Chris Mason39279cc2007-06-12 06:35:45 -04005839 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005840
5841fail_unlock:
5842 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005843fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005844 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005845 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005846 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005847 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005848 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005849}
5850
5851static inline u8 btrfs_inode_type(struct inode *inode)
5852{
5853 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5854}
5855
Chris Masond352ac62008-09-29 15:18:18 -04005856/*
5857 * utility function to add 'inode' into 'parent_inode' with
5858 * a give name and a given sequence number.
5859 * if 'add_backref' is true, also insert a backref from the
5860 * inode to the parent directory.
5861 */
Chris Masone02119d2008-09-05 16:13:11 -04005862int btrfs_add_link(struct btrfs_trans_handle *trans,
5863 struct inode *parent_inode, struct inode *inode,
5864 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005865{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005866 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005867 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005868 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005869 u64 ino = btrfs_ino(inode);
5870 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005871
Li Zefan33345d012011-04-20 10:31:50 +08005872 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005873 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5874 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005875 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02005876 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005877 key.offset = 0;
5878 }
Chris Mason39279cc2007-06-12 06:35:45 -04005879
Li Zefan33345d012011-04-20 10:31:50 +08005880 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005881 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5882 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005883 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005884 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005885 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5886 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005887 }
5888
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005889 /* Nothing to clean up yet */
5890 if (ret)
5891 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005892
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005893 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5894 parent_inode, &key,
5895 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005896 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005897 goto fail_dir_item;
5898 else if (ret) {
5899 btrfs_abort_transaction(trans, root, ret);
5900 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005901 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005902
5903 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5904 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005905 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005906 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5907 ret = btrfs_update_inode(trans, root, parent_inode);
5908 if (ret)
5909 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005910 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005911
5912fail_dir_item:
5913 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5914 u64 local_index;
5915 int err;
5916 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5917 key.objectid, root->root_key.objectid,
5918 parent_ino, &local_index, name, name_len);
5919
5920 } else if (add_backref) {
5921 u64 local_index;
5922 int err;
5923
5924 err = btrfs_del_inode_ref(trans, root, name, name_len,
5925 ino, parent_ino, &local_index);
5926 }
5927 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005928}
5929
5930static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005931 struct inode *dir, struct dentry *dentry,
5932 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005933{
Josef Bacika1b075d2010-11-19 20:36:11 +00005934 int err = btrfs_add_link(trans, dir, inode,
5935 dentry->d_name.name, dentry->d_name.len,
5936 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005937 if (err > 0)
5938 err = -EEXIST;
5939 return err;
5940}
5941
Josef Bacik618e21d2007-07-11 10:18:17 -04005942static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005943 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005944{
5945 struct btrfs_trans_handle *trans;
5946 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005947 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005948 int err;
5949 int drop_inode = 0;
5950 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005951 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005952
5953 if (!new_valid_dev(rdev))
5954 return -EINVAL;
5955
Josef Bacik9ed74f22009-09-11 16:12:44 -04005956 /*
5957 * 2 for inode item and ref
5958 * 2 for dir items
5959 * 1 for xattr if selinux is on
5960 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005961 trans = btrfs_start_transaction(root, 5);
5962 if (IS_ERR(trans))
5963 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005964
Li Zefan581bb052011-04-20 10:06:11 +08005965 err = btrfs_find_free_ino(root, &objectid);
5966 if (err)
5967 goto out_unlock;
5968
Josef Bacikaec74772008-07-24 12:12:38 -04005969 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005970 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005971 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005972 if (IS_ERR(inode)) {
5973 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005974 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005975 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005976
Casey Schauflerad19db72011-12-15 10:09:07 -05005977 /*
5978 * If the active LSM wants to access the inode during
5979 * d_instantiate it needs these. Smack checks to see
5980 * if the filesystem supports xattrs by looking at the
5981 * ops vector.
5982 */
Casey Schauflerad19db72011-12-15 10:09:07 -05005983 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07005984 init_special_inode(inode, inode->i_mode, rdev);
5985
5986 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04005987 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07005988 goto out_unlock_inode;
5989
5990 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
5991 if (err) {
5992 goto out_unlock_inode;
5993 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04005994 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07005995 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05005996 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005997 }
Chris Masonb0d5d102014-09-08 13:08:51 -07005998
Josef Bacik618e21d2007-07-11 10:18:17 -04005999out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006000 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006001 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006002 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006003 if (drop_inode) {
6004 inode_dec_link_count(inode);
6005 iput(inode);
6006 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006007 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006008
6009out_unlock_inode:
6010 drop_inode = 1;
6011 unlock_new_inode(inode);
6012 goto out_unlock;
6013
Josef Bacik618e21d2007-07-11 10:18:17 -04006014}
6015
Chris Mason39279cc2007-06-12 06:35:45 -04006016static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006017 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006018{
6019 struct btrfs_trans_handle *trans;
6020 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006021 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006022 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006023 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006024 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006025 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006026
Josef Bacik9ed74f22009-09-11 16:12:44 -04006027 /*
6028 * 2 for inode item and ref
6029 * 2 for dir items
6030 * 1 for xattr if selinux is on
6031 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006032 trans = btrfs_start_transaction(root, 5);
6033 if (IS_ERR(trans))
6034 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006035
Li Zefan581bb052011-04-20 10:06:11 +08006036 err = btrfs_find_free_ino(root, &objectid);
6037 if (err)
6038 goto out_unlock;
6039
Josef Bacikaec74772008-07-24 12:12:38 -04006040 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006041 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006042 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006043 if (IS_ERR(inode)) {
6044 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006045 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006046 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006047 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006048 /*
6049 * If the active LSM wants to access the inode during
6050 * d_instantiate it needs these. Smack checks to see
6051 * if the filesystem supports xattrs by looking at the
6052 * ops vector.
6053 */
6054 inode->i_fop = &btrfs_file_operations;
6055 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006056 inode->i_mapping->a_ops = &btrfs_aops;
6057 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
6058
6059 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6060 if (err)
6061 goto out_unlock_inode;
6062
6063 err = btrfs_update_inode(trans, root, inode);
6064 if (err)
6065 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006066
Josef Bacika1b075d2010-11-19 20:36:11 +00006067 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006068 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006069 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006070
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006071 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006072 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006073 d_instantiate(dentry, inode);
6074
Chris Mason39279cc2007-06-12 06:35:45 -04006075out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006076 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006077 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006078 inode_dec_link_count(inode);
6079 iput(inode);
6080 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006081 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006082 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006083 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006084
6085out_unlock_inode:
6086 unlock_new_inode(inode);
6087 goto out_unlock;
6088
Chris Mason39279cc2007-06-12 06:35:45 -04006089}
6090
6091static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6092 struct dentry *dentry)
6093{
6094 struct btrfs_trans_handle *trans;
6095 struct btrfs_root *root = BTRFS_I(dir)->root;
6096 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006097 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006098 int err;
6099 int drop_inode = 0;
6100
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006101 /* do not allow sys_link's with other subvols of the same device */
6102 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006103 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006104
Mark Fashehf1863732012-08-08 11:32:27 -07006105 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006106 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006107
Chris Mason3de45862008-11-17 21:02:50 -05006108 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006109 if (err)
6110 goto fail;
6111
Yan, Zhenga22285a2010-05-16 10:48:46 -04006112 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006113 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006114 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006115 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006116 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006117 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006118 if (IS_ERR(trans)) {
6119 err = PTR_ERR(trans);
6120 goto fail;
6121 }
Chris Mason5f39d392007-10-15 16:14:19 -04006122
Miao Xie67de1172013-12-26 13:07:06 +08006123 /* There are several dir indexes for this inode, clear the cache. */
6124 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006125 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006126 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006127 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006128 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006129 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006130
Josef Bacika1b075d2010-11-19 20:36:11 +00006131 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006132
Yan, Zhenga5719522009-09-24 09:17:31 -04006133 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006134 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006135 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006136 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006137 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006138 if (err)
6139 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006140 if (inode->i_nlink == 1) {
6141 /*
6142 * If new hard link count is 1, it's a file created
6143 * with open(2) O_TMPFILE flag.
6144 */
6145 err = btrfs_orphan_del(trans, inode);
6146 if (err)
6147 goto fail;
6148 }
Al Viro08c422c2011-12-23 07:58:13 -05006149 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006150 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006151 }
Chris Mason39279cc2007-06-12 06:35:45 -04006152
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006153 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006154 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006155fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006156 if (drop_inode) {
6157 inode_dec_link_count(inode);
6158 iput(inode);
6159 }
Liu Bob53d3f52012-11-14 14:34:34 +00006160 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006161 return err;
6162}
6163
Al Viro18bb1db2011-07-26 01:41:39 -04006164static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006165{
Chris Masonb9d86662008-05-02 16:13:49 -04006166 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006167 struct btrfs_trans_handle *trans;
6168 struct btrfs_root *root = BTRFS_I(dir)->root;
6169 int err = 0;
6170 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006171 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006172 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006173
Josef Bacik9ed74f22009-09-11 16:12:44 -04006174 /*
6175 * 2 items for inode and ref
6176 * 2 items for dir items
6177 * 1 for xattr if selinux is on
6178 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006179 trans = btrfs_start_transaction(root, 5);
6180 if (IS_ERR(trans))
6181 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006182
Li Zefan581bb052011-04-20 10:06:11 +08006183 err = btrfs_find_free_ino(root, &objectid);
6184 if (err)
6185 goto out_fail;
6186
Josef Bacikaec74772008-07-24 12:12:38 -04006187 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006188 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006189 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006190 if (IS_ERR(inode)) {
6191 err = PTR_ERR(inode);
6192 goto out_fail;
6193 }
Chris Mason5f39d392007-10-15 16:14:19 -04006194
Chris Mason39279cc2007-06-12 06:35:45 -04006195 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006196 /* these must be set before we unlock the inode */
6197 inode->i_op = &btrfs_dir_inode_operations;
6198 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006199
Eric Paris2a7dba32011-02-01 11:05:39 -05006200 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006201 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006202 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006203
Chris Masondbe674a2008-07-17 12:54:05 -04006204 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006205 err = btrfs_update_inode(trans, root, inode);
6206 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006207 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006208
Josef Bacika1b075d2010-11-19 20:36:11 +00006209 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6210 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006211 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006212 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006213
Chris Mason39279cc2007-06-12 06:35:45 -04006214 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006215 /*
6216 * mkdir is special. We're unlocking after we call d_instantiate
6217 * to avoid a race with nfsd calling d_instantiate.
6218 */
6219 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006220 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006221
6222out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006223 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006224 if (drop_on_err)
6225 iput(inode);
Josef Bacikc581afc2014-02-06 16:06:06 -05006226 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006227 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006228 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006229
6230out_fail_inode:
6231 unlock_new_inode(inode);
6232 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006233}
6234
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006235/* Find next extent map of a given extent map, caller needs to ensure locks */
6236static struct extent_map *next_extent_map(struct extent_map *em)
6237{
6238 struct rb_node *next;
6239
6240 next = rb_next(&em->rb_node);
6241 if (!next)
6242 return NULL;
6243 return container_of(next, struct extent_map, rb_node);
6244}
6245
6246static struct extent_map *prev_extent_map(struct extent_map *em)
6247{
6248 struct rb_node *prev;
6249
6250 prev = rb_prev(&em->rb_node);
6251 if (!prev)
6252 return NULL;
6253 return container_of(prev, struct extent_map, rb_node);
6254}
6255
Chris Masond352ac62008-09-29 15:18:18 -04006256/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006257 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006258 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006259 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006260 */
Chris Mason3b951512008-04-17 11:29:12 -04006261static int merge_extent_mapping(struct extent_map_tree *em_tree,
6262 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006263 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006264 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006265{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006266 struct extent_map *prev;
6267 struct extent_map *next;
6268 u64 start;
6269 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006270 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006271
Chris Masone6dcd2d2008-07-17 12:53:50 -04006272 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006273
6274 if (existing->start > map_start) {
6275 next = existing;
6276 prev = prev_extent_map(next);
6277 } else {
6278 prev = existing;
6279 next = next_extent_map(prev);
6280 }
6281
6282 start = prev ? extent_map_end(prev) : em->start;
6283 start = max_t(u64, start, em->start);
6284 end = next ? next->start : extent_map_end(em);
6285 end = min_t(u64, end, extent_map_end(em));
6286 start_diff = start - em->start;
6287 em->start = start;
6288 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006289 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6290 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006291 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006292 em->block_len -= start_diff;
6293 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006294 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006295}
6296
Chris Masonc8b97812008-10-29 14:49:59 -04006297static noinline int uncompress_inline(struct btrfs_path *path,
6298 struct inode *inode, struct page *page,
6299 size_t pg_offset, u64 extent_offset,
6300 struct btrfs_file_extent_item *item)
6301{
6302 int ret;
6303 struct extent_buffer *leaf = path->nodes[0];
6304 char *tmp;
6305 size_t max_size;
6306 unsigned long inline_size;
6307 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006308 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006309
6310 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006311 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006312 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6313 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006314 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006315 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006316 if (!tmp)
6317 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006318 ptr = btrfs_file_extent_inline_start(item);
6319
6320 read_extent_buffer(leaf, tmp, ptr, inline_size);
6321
Chris Mason5b050f02008-11-11 09:34:41 -05006322 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006323 ret = btrfs_decompress(compress_type, tmp, page,
6324 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006325 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006326 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006327}
6328
Chris Masond352ac62008-09-29 15:18:18 -04006329/*
6330 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006331 * the ugly parts come from merging extents from the disk with the in-ram
6332 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006333 * where the in-ram extents might be locked pending data=ordered completion.
6334 *
6335 * This also copies inline extents directly into the page.
6336 */
Chris Masond3977122009-01-05 21:25:51 -05006337
Chris Masona52d9a82007-08-27 16:49:44 -04006338struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006339 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006340 int create)
6341{
6342 int ret;
6343 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006344 u64 extent_start = 0;
6345 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006346 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006347 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006348 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006349 struct btrfs_root *root = BTRFS_I(inode)->root;
6350 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006351 struct extent_buffer *leaf;
6352 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006353 struct extent_map *em = NULL;
6354 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006355 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006356 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006357 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006358
Chris Masona52d9a82007-08-27 16:49:44 -04006359again:
Chris Mason890871b2009-09-02 16:24:52 -04006360 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006361 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006362 if (em)
6363 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006364 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006365
Chris Masona52d9a82007-08-27 16:49:44 -04006366 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006367 if (em->start > start || em->start + em->len <= start)
6368 free_extent_map(em);
6369 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006370 free_extent_map(em);
6371 else
6372 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006373 }
David Sterba172ddd62011-04-21 00:48:27 +02006374 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006375 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006376 err = -ENOMEM;
6377 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006378 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006379 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006380 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006381 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006382 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006383 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006384
6385 if (!path) {
6386 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006387 if (!path) {
6388 err = -ENOMEM;
6389 goto out;
6390 }
6391 /*
6392 * Chances are we'll be called again, so go ahead and do
6393 * readahead
6394 */
6395 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006396 }
6397
Chris Mason179e29e2007-11-01 11:28:41 -04006398 ret = btrfs_lookup_file_extent(trans, root, path,
6399 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006400 if (ret < 0) {
6401 err = ret;
6402 goto out;
6403 }
6404
6405 if (ret != 0) {
6406 if (path->slots[0] == 0)
6407 goto not_found;
6408 path->slots[0]--;
6409 }
6410
Chris Mason5f39d392007-10-15 16:14:19 -04006411 leaf = path->nodes[0];
6412 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006413 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006414 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006415 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006416 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006417 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006418 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006419 /*
6420 * If we backup past the first extent we want to move forward
6421 * and see if there is an extent in front of us, otherwise we'll
6422 * say there is a hole for our whole search range which can
6423 * cause problems.
6424 */
6425 extent_end = start;
6426 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006427 }
6428
Chris Mason5f39d392007-10-15 16:14:19 -04006429 found_type = btrfs_file_extent_type(leaf, item);
6430 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006431 if (found_type == BTRFS_FILE_EXTENT_REG ||
6432 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006433 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006434 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006435 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6436 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006437 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006438 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006439 }
Josef Bacik25a50342013-10-14 12:08:38 -04006440next:
Yan Zheng9036c102008-10-30 14:19:41 -04006441 if (start >= extent_end) {
6442 path->slots[0]++;
6443 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6444 ret = btrfs_next_leaf(root, path);
6445 if (ret < 0) {
6446 err = ret;
6447 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006448 }
Yan Zheng9036c102008-10-30 14:19:41 -04006449 if (ret > 0)
6450 goto not_found;
6451 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006452 }
Yan Zheng9036c102008-10-30 14:19:41 -04006453 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6454 if (found_key.objectid != objectid ||
6455 found_key.type != BTRFS_EXTENT_DATA_KEY)
6456 goto not_found;
6457 if (start + len <= found_key.offset)
6458 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006459 if (start > found_key.offset)
6460 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006461 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006462 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006463 em->len = found_key.offset - start;
6464 goto not_found_em;
6465 }
6466
Filipe Manana7ffbb592014-06-09 03:48:05 +01006467 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6468
Yan Zhengd899e052008-10-30 14:25:28 -04006469 if (found_type == BTRFS_FILE_EXTENT_REG ||
6470 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006471 goto insert;
6472 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006473 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006474 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006475 size_t size;
6476 size_t extent_offset;
6477 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006478
Filipe Manana7ffbb592014-06-09 03:48:05 +01006479 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006480 goto out;
Yan689f9342007-10-29 11:41:07 -04006481
Chris Mason514ac8a2014-01-03 21:07:00 -08006482 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006483 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006484 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006485 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006486 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006487 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006488 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006489 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006490 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006491 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006492 if (btrfs_file_extent_compression(leaf, item) !=
6493 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006494 ret = uncompress_inline(path, inode, page,
6495 pg_offset,
6496 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006497 if (ret) {
6498 err = ret;
6499 goto out;
6500 }
Chris Masonc8b97812008-10-29 14:49:59 -04006501 } else {
6502 map = kmap(page);
6503 read_extent_buffer(leaf, map + pg_offset, ptr,
6504 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006505 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6506 memset(map + pg_offset + copy_size, 0,
6507 PAGE_CACHE_SIZE - pg_offset -
6508 copy_size);
6509 }
Chris Masonc8b97812008-10-29 14:49:59 -04006510 kunmap(page);
6511 }
Chris Mason179e29e2007-11-01 11:28:41 -04006512 flush_dcache_page(page);
6513 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006514 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006515 if (!trans) {
6516 kunmap(page);
6517 free_extent_map(em);
6518 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006519
David Sterbab3b4aa72011-04-21 01:20:15 +02006520 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006521 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006522
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006523 if (IS_ERR(trans))
6524 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006525 goto again;
6526 }
Chris Masonc8b97812008-10-29 14:49:59 -04006527 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006528 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006529 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006530 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006531 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006532 }
Chris Masond1310b22008-01-24 16:13:08 -05006533 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006534 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006535 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006536 }
6537not_found:
6538 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006539 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006540 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006541not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006542 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006543 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006544insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006545 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006546 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006547 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006548 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006549 err = -EIO;
6550 goto out;
6551 }
Chris Masond1310b22008-01-24 16:13:08 -05006552
6553 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006554 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006555 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006556 /* it is possible that someone inserted the extent into the tree
6557 * while we had the lock dropped. It is also possible that
6558 * an overlapping map exists in the tree
6559 */
Chris Masona52d9a82007-08-27 16:49:44 -04006560 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006561 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006562
6563 ret = 0;
6564
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006565 existing = search_extent_mapping(em_tree, start, len);
6566 /*
6567 * existing will always be non-NULL, since there must be
6568 * extent causing the -EEXIST.
6569 */
6570 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006571 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006572 /*
6573 * The existing extent map is the one nearest to
6574 * the [start, start + len) range which overlaps
6575 */
6576 err = merge_extent_mapping(em_tree, existing,
6577 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006578 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006579 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006580 free_extent_map(em);
6581 em = NULL;
6582 }
6583 } else {
6584 free_extent_map(em);
6585 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006586 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006587 }
Chris Masona52d9a82007-08-27 16:49:44 -04006588 }
Chris Mason890871b2009-09-02 16:24:52 -04006589 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006590out:
liubo1abe9b82011-03-24 11:18:59 +00006591
Steven Rostedt4cd85872013-11-14 22:57:29 -05006592 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006593
Chris Masonf4219502008-07-22 11:18:09 -04006594 if (path)
6595 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006596 if (trans) {
6597 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006598 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006599 err = ret;
6600 }
Chris Masona52d9a82007-08-27 16:49:44 -04006601 if (err) {
6602 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006603 return ERR_PTR(err);
6604 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006605 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006606 return em;
6607}
6608
Chris Masonec29ed52011-02-23 16:23:20 -05006609struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6610 size_t pg_offset, u64 start, u64 len,
6611 int create)
6612{
6613 struct extent_map *em;
6614 struct extent_map *hole_em = NULL;
6615 u64 range_start = start;
6616 u64 end;
6617 u64 found;
6618 u64 found_end;
6619 int err = 0;
6620
6621 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6622 if (IS_ERR(em))
6623 return em;
6624 if (em) {
6625 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006626 * if our em maps to
6627 * - a hole or
6628 * - a pre-alloc extent,
6629 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006630 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006631 if (em->block_start != EXTENT_MAP_HOLE &&
6632 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006633 return em;
6634 else
6635 hole_em = em;
6636 }
6637
6638 /* check to see if we've wrapped (len == -1 or similar) */
6639 end = start + len;
6640 if (end < start)
6641 end = (u64)-1;
6642 else
6643 end -= 1;
6644
6645 em = NULL;
6646
6647 /* ok, we didn't find anything, lets look for delalloc */
6648 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6649 end, len, EXTENT_DELALLOC, 1);
6650 found_end = range_start + found;
6651 if (found_end < range_start)
6652 found_end = (u64)-1;
6653
6654 /*
6655 * we didn't find anything useful, return
6656 * the original results from get_extent()
6657 */
6658 if (range_start > end || found_end <= start) {
6659 em = hole_em;
6660 hole_em = NULL;
6661 goto out;
6662 }
6663
6664 /* adjust the range_start to make sure it doesn't
6665 * go backwards from the start they passed in
6666 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306667 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006668 found = found_end - range_start;
6669
6670 if (found > 0) {
6671 u64 hole_start = start;
6672 u64 hole_len = len;
6673
David Sterba172ddd62011-04-21 00:48:27 +02006674 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006675 if (!em) {
6676 err = -ENOMEM;
6677 goto out;
6678 }
6679 /*
6680 * when btrfs_get_extent can't find anything it
6681 * returns one huge hole
6682 *
6683 * make sure what it found really fits our range, and
6684 * adjust to make sure it is based on the start from
6685 * the caller
6686 */
6687 if (hole_em) {
6688 u64 calc_end = extent_map_end(hole_em);
6689
6690 if (calc_end <= start || (hole_em->start > end)) {
6691 free_extent_map(hole_em);
6692 hole_em = NULL;
6693 } else {
6694 hole_start = max(hole_em->start, start);
6695 hole_len = calc_end - hole_start;
6696 }
6697 }
6698 em->bdev = NULL;
6699 if (hole_em && range_start > hole_start) {
6700 /* our hole starts before our delalloc, so we
6701 * have to return just the parts of the hole
6702 * that go until the delalloc starts
6703 */
6704 em->len = min(hole_len,
6705 range_start - hole_start);
6706 em->start = hole_start;
6707 em->orig_start = hole_start;
6708 /*
6709 * don't adjust block start at all,
6710 * it is fixed at EXTENT_MAP_HOLE
6711 */
6712 em->block_start = hole_em->block_start;
6713 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006714 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6715 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006716 } else {
6717 em->start = range_start;
6718 em->len = found;
6719 em->orig_start = range_start;
6720 em->block_start = EXTENT_MAP_DELALLOC;
6721 em->block_len = found;
6722 }
6723 } else if (hole_em) {
6724 return hole_em;
6725 }
6726out:
6727
6728 free_extent_map(hole_em);
6729 if (err) {
6730 free_extent_map(em);
6731 return ERR_PTR(err);
6732 }
6733 return em;
6734}
6735
Josef Bacik4b46fce2010-05-23 11:00:55 -04006736static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6737 u64 start, u64 len)
6738{
6739 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006740 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006741 struct btrfs_key ins;
6742 u64 alloc_hint;
6743 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006744
Josef Bacik4b46fce2010-05-23 11:00:55 -04006745 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006746 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08006747 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006748 if (ret)
6749 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006750
Josef Bacik70c8a912012-10-11 16:54:30 -04006751 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006752 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006753 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08006754 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006755 return em;
6756 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006757
6758 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6759 ins.offset, ins.offset, 0);
6760 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08006761 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006762 free_extent_map(em);
6763 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006764 }
Josef Bacik00361582013-08-14 14:02:47 -04006765
Josef Bacik4b46fce2010-05-23 11:00:55 -04006766 return em;
6767}
6768
Chris Mason46bfbb52010-05-26 11:04:10 -04006769/*
6770 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6771 * block must be cow'd
6772 */
Josef Bacik00361582013-08-14 14:02:47 -04006773noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006774 u64 *orig_start, u64 *orig_block_len,
6775 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006776{
Josef Bacik00361582013-08-14 14:02:47 -04006777 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006778 struct btrfs_path *path;
6779 int ret;
6780 struct extent_buffer *leaf;
6781 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006782 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006783 struct btrfs_file_extent_item *fi;
6784 struct btrfs_key key;
6785 u64 disk_bytenr;
6786 u64 backref_offset;
6787 u64 extent_end;
6788 u64 num_bytes;
6789 int slot;
6790 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006791 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006792
Chris Mason46bfbb52010-05-26 11:04:10 -04006793 path = btrfs_alloc_path();
6794 if (!path)
6795 return -ENOMEM;
6796
Josef Bacik00361582013-08-14 14:02:47 -04006797 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006798 offset, 0);
6799 if (ret < 0)
6800 goto out;
6801
6802 slot = path->slots[0];
6803 if (ret == 1) {
6804 if (slot == 0) {
6805 /* can't find the item, must cow */
6806 ret = 0;
6807 goto out;
6808 }
6809 slot--;
6810 }
6811 ret = 0;
6812 leaf = path->nodes[0];
6813 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006814 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006815 key.type != BTRFS_EXTENT_DATA_KEY) {
6816 /* not our file or wrong item type, must cow */
6817 goto out;
6818 }
6819
6820 if (key.offset > offset) {
6821 /* Wrong offset, must cow */
6822 goto out;
6823 }
6824
6825 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6826 found_type = btrfs_file_extent_type(leaf, fi);
6827 if (found_type != BTRFS_FILE_EXTENT_REG &&
6828 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6829 /* not a regular extent, must cow */
6830 goto out;
6831 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006832
6833 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6834 goto out;
6835
Miao Xiee77751a2013-12-27 21:11:50 +08006836 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6837 if (extent_end <= offset)
6838 goto out;
6839
Chris Mason46bfbb52010-05-26 11:04:10 -04006840 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006841 if (disk_bytenr == 0)
6842 goto out;
6843
6844 if (btrfs_file_extent_compression(leaf, fi) ||
6845 btrfs_file_extent_encryption(leaf, fi) ||
6846 btrfs_file_extent_other_encoding(leaf, fi))
6847 goto out;
6848
Chris Mason46bfbb52010-05-26 11:04:10 -04006849 backref_offset = btrfs_file_extent_offset(leaf, fi);
6850
Josef Bacik7ee9e442013-06-21 16:37:03 -04006851 if (orig_start) {
6852 *orig_start = key.offset - backref_offset;
6853 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6854 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6855 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006856
Chris Mason46bfbb52010-05-26 11:04:10 -04006857 if (btrfs_extent_readonly(root, disk_bytenr))
6858 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006859
6860 num_bytes = min(offset + *len, extent_end) - offset;
6861 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6862 u64 range_end;
6863
6864 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6865 ret = test_range_bit(io_tree, offset, range_end,
6866 EXTENT_DELALLOC, 0, NULL);
6867 if (ret) {
6868 ret = -EAGAIN;
6869 goto out;
6870 }
6871 }
6872
Josef Bacik1bda19e2013-10-18 12:10:36 -04006873 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006874
6875 /*
6876 * look for other files referencing this extent, if we
6877 * find any we must cow
6878 */
Josef Bacik00361582013-08-14 14:02:47 -04006879 trans = btrfs_join_transaction(root);
6880 if (IS_ERR(trans)) {
6881 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006882 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006883 }
6884
6885 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6886 key.offset - backref_offset, disk_bytenr);
6887 btrfs_end_transaction(trans, root);
6888 if (ret) {
6889 ret = 0;
6890 goto out;
6891 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006892
6893 /*
6894 * adjust disk_bytenr and num_bytes to cover just the bytes
6895 * in this extent we are about to write. If there
6896 * are any csums in that range we have to cow in order
6897 * to keep the csums correct
6898 */
6899 disk_bytenr += backref_offset;
6900 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006901 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6902 goto out;
6903 /*
6904 * all of the above have passed, it is safe to overwrite this extent
6905 * without cow
6906 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006907 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006908 ret = 1;
6909out:
6910 btrfs_free_path(path);
6911 return ret;
6912}
6913
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006914bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
6915{
6916 struct radix_tree_root *root = &inode->i_mapping->page_tree;
6917 int found = false;
6918 void **pagep = NULL;
6919 struct page *page = NULL;
6920 int start_idx;
6921 int end_idx;
6922
6923 start_idx = start >> PAGE_CACHE_SHIFT;
6924
6925 /*
6926 * end is the last byte in the last page. end == start is legal
6927 */
6928 end_idx = end >> PAGE_CACHE_SHIFT;
6929
6930 rcu_read_lock();
6931
6932 /* Most of the code in this while loop is lifted from
6933 * find_get_page. It's been modified to begin searching from a
6934 * page and return just the first page found in that range. If the
6935 * found idx is less than or equal to the end idx then we know that
6936 * a page exists. If no pages are found or if those pages are
6937 * outside of the range then we're fine (yay!) */
6938 while (page == NULL &&
6939 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
6940 page = radix_tree_deref_slot(pagep);
6941 if (unlikely(!page))
6942 break;
6943
6944 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01006945 if (radix_tree_deref_retry(page)) {
6946 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006947 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01006948 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006949 /*
6950 * Otherwise, shmem/tmpfs must be storing a swap entry
6951 * here as an exceptional entry: so return it without
6952 * attempting to raise page count.
6953 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01006954 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006955 break; /* TODO: Is this relevant for this use case? */
6956 }
6957
Filipe Manana91405152014-06-05 13:22:24 +01006958 if (!page_cache_get_speculative(page)) {
6959 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006960 continue;
Filipe Manana91405152014-06-05 13:22:24 +01006961 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006962
6963 /*
6964 * Has the page moved?
6965 * This is part of the lockless pagecache protocol. See
6966 * include/linux/pagemap.h for details.
6967 */
6968 if (unlikely(page != *pagep)) {
6969 page_cache_release(page);
6970 page = NULL;
6971 }
6972 }
6973
6974 if (page) {
6975 if (page->index <= end_idx)
6976 found = true;
6977 page_cache_release(page);
6978 }
6979
6980 rcu_read_unlock();
6981 return found;
6982}
6983
Josef Bacikeb838e72012-07-31 16:28:48 -04006984static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6985 struct extent_state **cached_state, int writing)
6986{
6987 struct btrfs_ordered_extent *ordered;
6988 int ret = 0;
6989
6990 while (1) {
6991 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6992 0, cached_state);
6993 /*
6994 * We're concerned with the entire range that we're going to be
6995 * doing DIO to, so we need to make sure theres no ordered
6996 * extents in this range.
6997 */
6998 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6999 lockend - lockstart + 1);
7000
7001 /*
7002 * We need to make sure there are no buffered pages in this
7003 * range either, we could have raced between the invalidate in
7004 * generic_file_direct_write and locking the extent. The
7005 * invalidate needs to happen so that reads after a write do not
7006 * get stale data.
7007 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007008 if (!ordered &&
7009 (!writing ||
7010 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007011 break;
7012
7013 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7014 cached_state, GFP_NOFS);
7015
7016 if (ordered) {
7017 btrfs_start_ordered_extent(inode, ordered, 1);
7018 btrfs_put_ordered_extent(ordered);
7019 } else {
7020 /* Screw you mmap */
7021 ret = filemap_write_and_wait_range(inode->i_mapping,
7022 lockstart,
7023 lockend);
7024 if (ret)
7025 break;
7026
7027 /*
7028 * If we found a page that couldn't be invalidated just
7029 * fall back to buffered.
7030 */
7031 ret = invalidate_inode_pages2_range(inode->i_mapping,
7032 lockstart >> PAGE_CACHE_SHIFT,
7033 lockend >> PAGE_CACHE_SHIFT);
7034 if (ret)
7035 break;
7036 }
7037
7038 cond_resched();
7039 }
7040
7041 return ret;
7042}
7043
Josef Bacik69ffb542012-09-11 15:40:07 -04007044static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7045 u64 len, u64 orig_start,
7046 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007047 u64 orig_block_len, u64 ram_bytes,
7048 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007049{
7050 struct extent_map_tree *em_tree;
7051 struct extent_map *em;
7052 struct btrfs_root *root = BTRFS_I(inode)->root;
7053 int ret;
7054
7055 em_tree = &BTRFS_I(inode)->extent_tree;
7056 em = alloc_extent_map();
7057 if (!em)
7058 return ERR_PTR(-ENOMEM);
7059
7060 em->start = start;
7061 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007062 em->mod_start = start;
7063 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007064 em->len = len;
7065 em->block_len = block_len;
7066 em->block_start = block_start;
7067 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007068 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007069 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007070 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007071 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7072 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007073 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007074
7075 do {
7076 btrfs_drop_extent_cache(inode, em->start,
7077 em->start + em->len - 1, 0);
7078 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007079 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007080 write_unlock(&em_tree->lock);
7081 } while (ret == -EEXIST);
7082
7083 if (ret) {
7084 free_extent_map(em);
7085 return ERR_PTR(ret);
7086 }
7087
7088 return em;
7089}
7090
7091
Josef Bacik4b46fce2010-05-23 11:00:55 -04007092static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7093 struct buffer_head *bh_result, int create)
7094{
7095 struct extent_map *em;
7096 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007097 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007098 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007099 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007100 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007101 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007102 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007103
Miao Xie172a5042013-02-21 02:48:22 -07007104 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04007105 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007106 else
Josef Bacikc3298612012-08-03 16:49:19 -04007107 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007108
Josef Bacikc3298612012-08-03 16:49:19 -04007109 lockstart = start;
7110 lockend = start + len - 1;
7111
Josef Bacikeb838e72012-07-31 16:28:48 -04007112 /*
7113 * If this errors out it's because we couldn't invalidate pagecache for
7114 * this range and we need to fallback to buffered.
7115 */
7116 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7117 return -ENOTBLK;
7118
Josef Bacik4b46fce2010-05-23 11:00:55 -04007119 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007120 if (IS_ERR(em)) {
7121 ret = PTR_ERR(em);
7122 goto unlock_err;
7123 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007124
7125 /*
7126 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7127 * io. INLINE is special, and we could probably kludge it in here, but
7128 * it's still buffered so for safety lets just fall back to the generic
7129 * buffered path.
7130 *
7131 * For COMPRESSED we _have_ to read the entire extent in so we can
7132 * decompress it, so there will be buffering required no matter what we
7133 * do, so go ahead and fallback to buffered.
7134 *
7135 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7136 * to buffered IO. Don't blame me, this is the price we pay for using
7137 * the generic code.
7138 */
7139 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7140 em->block_start == EXTENT_MAP_INLINE) {
7141 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007142 ret = -ENOTBLK;
7143 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007144 }
7145
7146 /* Just a good old fashioned hole, return */
7147 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7148 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7149 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007150 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007151 }
7152
7153 /*
7154 * We don't allocate a new extent in the following cases
7155 *
7156 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7157 * existing extent.
7158 * 2) The extent is marked as PREALLOC. We're good to go here and can
7159 * just use the extent.
7160 *
7161 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007162 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007163 len = min(len, em->len - (start - em->start));
7164 lockstart = start + len;
7165 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007166 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007167
7168 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7169 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7170 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007171 int type;
7172 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04007173 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007174
7175 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7176 type = BTRFS_ORDERED_PREALLOC;
7177 else
7178 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007179 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007180 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007181
Josef Bacik00361582013-08-14 14:02:47 -04007182 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007183 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007184 if (type == BTRFS_ORDERED_PREALLOC) {
7185 free_extent_map(em);
7186 em = create_pinned_em(inode, start, len,
7187 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007188 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007189 orig_block_len,
7190 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007191 if (IS_ERR(em)) {
7192 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007193 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007194 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007195 }
7196
Chris Mason46bfbb52010-05-26 11:04:10 -04007197 ret = btrfs_add_ordered_extent_dio(inode, start,
7198 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007199 if (ret) {
7200 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007201 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007202 }
7203 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007204 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007205 }
Josef Bacik00361582013-08-14 14:02:47 -04007206
Chris Mason46bfbb52010-05-26 11:04:10 -04007207 /*
7208 * this will cow the extent, reset the len in case we changed
7209 * it above
7210 */
7211 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007212 free_extent_map(em);
7213 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007214 if (IS_ERR(em)) {
7215 ret = PTR_ERR(em);
7216 goto unlock_err;
7217 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007218 len = min(len, em->len - (start - em->start));
7219unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007220 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7221 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007222 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007223 bh_result->b_bdev = em->bdev;
7224 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007225 if (create) {
7226 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7227 set_buffer_new(bh_result);
7228
7229 /*
7230 * Need to update the i_size under the extent lock so buffered
7231 * readers will get the updated i_size when we unlock.
7232 */
7233 if (start + len > i_size_read(inode))
7234 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007235
Miao Xie172a5042013-02-21 02:48:22 -07007236 spin_lock(&BTRFS_I(inode)->lock);
7237 BTRFS_I(inode)->outstanding_extents++;
7238 spin_unlock(&BTRFS_I(inode)->lock);
7239
Miao Xie09348562013-02-07 10:12:07 +00007240 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7241 lockstart + len - 1, EXTENT_DELALLOC, NULL,
7242 &cached_state, GFP_NOFS);
7243 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04007244 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007245
Josef Bacikeb838e72012-07-31 16:28:48 -04007246 /*
7247 * In the case of write we need to clear and unlock the entire range,
7248 * in the case of read we need to unlock only the end area that we
7249 * aren't using if there is any left over space.
7250 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007251 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007252 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7253 lockend, unlock_bits, 1, 0,
7254 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007255 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007256 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007257 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007258
Josef Bacik4b46fce2010-05-23 11:00:55 -04007259 free_extent_map(em);
7260
7261 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007262
7263unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007264 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7265 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7266 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007267}
7268
Miao Xie8b110e32014-09-12 18:44:03 +08007269static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7270 int rw, int mirror_num)
7271{
7272 struct btrfs_root *root = BTRFS_I(inode)->root;
7273 int ret;
7274
7275 BUG_ON(rw & REQ_WRITE);
7276
7277 bio_get(bio);
7278
7279 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7280 BTRFS_WQ_ENDIO_DIO_REPAIR);
7281 if (ret)
7282 goto err;
7283
7284 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7285err:
7286 bio_put(bio);
7287 return ret;
7288}
7289
7290static int btrfs_check_dio_repairable(struct inode *inode,
7291 struct bio *failed_bio,
7292 struct io_failure_record *failrec,
7293 int failed_mirror)
7294{
7295 int num_copies;
7296
7297 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7298 failrec->logical, failrec->len);
7299 if (num_copies == 1) {
7300 /*
7301 * we only have a single copy of the data, so don't bother with
7302 * all the retry and error correction code that follows. no
7303 * matter what the error is, it is very likely to persist.
7304 */
7305 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7306 num_copies, failrec->this_mirror, failed_mirror);
7307 return 0;
7308 }
7309
7310 failrec->failed_mirror = failed_mirror;
7311 failrec->this_mirror++;
7312 if (failrec->this_mirror == failed_mirror)
7313 failrec->this_mirror++;
7314
7315 if (failrec->this_mirror > num_copies) {
7316 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7317 num_copies, failrec->this_mirror, failed_mirror);
7318 return 0;
7319 }
7320
7321 return 1;
7322}
7323
7324static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7325 struct page *page, u64 start, u64 end,
7326 int failed_mirror, bio_end_io_t *repair_endio,
7327 void *repair_arg)
7328{
7329 struct io_failure_record *failrec;
7330 struct bio *bio;
7331 int isector;
7332 int read_mode;
7333 int ret;
7334
7335 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7336
7337 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7338 if (ret)
7339 return ret;
7340
7341 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7342 failed_mirror);
7343 if (!ret) {
7344 free_io_failure(inode, failrec);
7345 return -EIO;
7346 }
7347
7348 if (failed_bio->bi_vcnt > 1)
7349 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7350 else
7351 read_mode = READ_SYNC;
7352
7353 isector = start - btrfs_io_bio(failed_bio)->logical;
7354 isector >>= inode->i_sb->s_blocksize_bits;
7355 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7356 0, isector, repair_endio, repair_arg);
7357 if (!bio) {
7358 free_io_failure(inode, failrec);
7359 return -EIO;
7360 }
7361
7362 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7363 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7364 read_mode, failrec->this_mirror, failrec->in_validation);
7365
7366 ret = submit_dio_repair_bio(inode, bio, read_mode,
7367 failrec->this_mirror);
7368 if (ret) {
7369 free_io_failure(inode, failrec);
7370 bio_put(bio);
7371 }
7372
7373 return ret;
7374}
7375
7376struct btrfs_retry_complete {
7377 struct completion done;
7378 struct inode *inode;
7379 u64 start;
7380 int uptodate;
7381};
7382
7383static void btrfs_retry_endio_nocsum(struct bio *bio, int err)
7384{
7385 struct btrfs_retry_complete *done = bio->bi_private;
7386 struct bio_vec *bvec;
7387 int i;
7388
7389 if (err)
7390 goto end;
7391
7392 done->uptodate = 1;
7393 bio_for_each_segment_all(bvec, bio, i)
7394 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7395end:
7396 complete(&done->done);
7397 bio_put(bio);
7398}
7399
7400static int __btrfs_correct_data_nocsum(struct inode *inode,
7401 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007402{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007403 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007404 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007405 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007406 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007407 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007408
Miao Xiec1dc0892014-09-12 18:43:56 +08007409 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007410 done.inode = inode;
7411
7412 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7413try_again:
7414 done.uptodate = 0;
7415 done.start = start;
7416 init_completion(&done.done);
7417
7418 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7419 start + bvec->bv_len - 1,
7420 io_bio->mirror_num,
7421 btrfs_retry_endio_nocsum, &done);
7422 if (ret)
7423 return ret;
7424
7425 wait_for_completion(&done.done);
7426
7427 if (!done.uptodate) {
7428 /* We might have another mirror, so try again */
7429 goto try_again;
7430 }
7431
7432 start += bvec->bv_len;
7433 }
7434
7435 return 0;
7436}
7437
7438static void btrfs_retry_endio(struct bio *bio, int err)
7439{
7440 struct btrfs_retry_complete *done = bio->bi_private;
7441 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7442 struct bio_vec *bvec;
7443 int uptodate;
7444 int ret;
7445 int i;
7446
7447 if (err)
7448 goto end;
7449
7450 uptodate = 1;
7451 bio_for_each_segment_all(bvec, bio, i) {
7452 ret = __readpage_endio_check(done->inode, io_bio, i,
7453 bvec->bv_page, 0,
7454 done->start, bvec->bv_len);
7455 if (!ret)
7456 clean_io_failure(done->inode, done->start,
7457 bvec->bv_page, 0);
7458 else
7459 uptodate = 0;
7460 }
7461
7462 done->uptodate = uptodate;
7463end:
7464 complete(&done->done);
7465 bio_put(bio);
7466}
7467
7468static int __btrfs_subio_endio_read(struct inode *inode,
7469 struct btrfs_io_bio *io_bio, int err)
7470{
7471 struct bio_vec *bvec;
7472 struct btrfs_retry_complete done;
7473 u64 start;
7474 u64 offset = 0;
7475 int i;
7476 int ret;
7477
7478 err = 0;
7479 start = io_bio->logical;
7480 done.inode = inode;
7481
Miao Xiec1dc0892014-09-12 18:43:56 +08007482 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007483 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7484 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007485 if (likely(!ret))
7486 goto next;
7487try_again:
7488 done.uptodate = 0;
7489 done.start = start;
7490 init_completion(&done.done);
7491
7492 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7493 start + bvec->bv_len - 1,
7494 io_bio->mirror_num,
7495 btrfs_retry_endio, &done);
7496 if (ret) {
7497 err = ret;
7498 goto next;
7499 }
7500
7501 wait_for_completion(&done.done);
7502
7503 if (!done.uptodate) {
7504 /* We might have another mirror, so try again */
7505 goto try_again;
7506 }
7507next:
7508 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007509 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007510 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007511
7512 return err;
7513}
7514
Miao Xie8b110e32014-09-12 18:44:03 +08007515static int btrfs_subio_endio_read(struct inode *inode,
7516 struct btrfs_io_bio *io_bio, int err)
7517{
7518 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7519
7520 if (skip_csum) {
7521 if (unlikely(err))
7522 return __btrfs_correct_data_nocsum(inode, io_bio);
7523 else
7524 return 0;
7525 } else {
7526 return __btrfs_subio_endio_read(inode, io_bio, err);
7527 }
7528}
7529
Miao Xiec1dc0892014-09-12 18:43:56 +08007530static void btrfs_endio_direct_read(struct bio *bio, int err)
7531{
7532 struct btrfs_dio_private *dip = bio->bi_private;
7533 struct inode *inode = dip->inode;
7534 struct bio *dio_bio;
7535 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7536
Miao Xie8b110e32014-09-12 18:44:03 +08007537 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7538 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007539
Josef Bacik4b46fce2010-05-23 11:00:55 -04007540 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007541 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007542 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007543
Josef Bacik4b46fce2010-05-23 11:00:55 -04007544 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007545
7546 /* If we had a csum failure make sure to clear the uptodate flag */
7547 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007548 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7549 dio_end_io(dio_bio, err);
Miao Xie23ea8e52014-09-12 18:43:54 +08007550
7551 if (io_bio->end_io)
7552 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007553 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007554}
7555
7556static void btrfs_endio_direct_write(struct bio *bio, int err)
7557{
7558 struct btrfs_dio_private *dip = bio->bi_private;
7559 struct inode *inode = dip->inode;
7560 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007561 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007562 u64 ordered_offset = dip->logical_offset;
7563 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007564 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007565 int ret;
7566
7567 if (err)
7568 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007569again:
7570 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7571 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007572 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007573 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007574 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007575
Liu Bo9e0af232014-08-15 23:36:53 +08007576 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7577 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007578 btrfs_queue_work(root->fs_info->endio_write_workers,
7579 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007580out_test:
7581 /*
7582 * our bio might span multiple ordered extents. If we haven't
7583 * completed the accounting for the whole dio, go back and try again
7584 */
7585 if (ordered_offset < dip->logical_offset + dip->bytes) {
7586 ordered_bytes = dip->logical_offset + dip->bytes -
7587 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007588 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007589 goto again;
7590 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007591out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007592 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007593
Josef Bacik4b46fce2010-05-23 11:00:55 -04007594 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007595
7596 /* If we had an error make sure to clear the uptodate flag */
7597 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007598 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7599 dio_end_io(dio_bio, err);
7600 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007601}
7602
Chris Masoneaf25d92010-05-25 09:48:28 -04007603static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7604 struct bio *bio, int mirror_num,
7605 unsigned long bio_flags, u64 offset)
7606{
7607 int ret;
7608 struct btrfs_root *root = BTRFS_I(inode)->root;
7609 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007610 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007611 return 0;
7612}
7613
Miao Xiee65e1532010-11-22 03:04:43 +00007614static void btrfs_end_dio_bio(struct bio *bio, int err)
7615{
7616 struct btrfs_dio_private *dip = bio->bi_private;
7617
Miao Xie8b110e32014-09-12 18:44:03 +08007618 if (err)
7619 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
7620 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7621 btrfs_ino(dip->inode), bio->bi_rw,
7622 (unsigned long long)bio->bi_iter.bi_sector,
7623 bio->bi_iter.bi_size, err);
7624
7625 if (dip->subio_endio)
7626 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007627
7628 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007629 dip->errors = 1;
7630
7631 /*
7632 * before atomic variable goto zero, we must make sure
7633 * dip->errors is perceived to be set.
7634 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007635 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007636 }
7637
7638 /* if there are more bios still pending for this dio, just exit */
7639 if (!atomic_dec_and_test(&dip->pending_bios))
7640 goto out;
7641
Chris Mason9be33952013-05-17 18:30:14 -04007642 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007643 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007644 } else {
7645 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007646 bio_endio(dip->orig_bio, 0);
7647 }
7648out:
7649 bio_put(bio);
7650}
7651
7652static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7653 u64 first_sector, gfp_t gfp_flags)
7654{
7655 int nr_vecs = bio_get_nr_vecs(bdev);
7656 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7657}
7658
Miao Xiec1dc0892014-09-12 18:43:56 +08007659static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
7660 struct inode *inode,
7661 struct btrfs_dio_private *dip,
7662 struct bio *bio,
7663 u64 file_offset)
7664{
7665 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7666 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
7667 int ret;
7668
7669 /*
7670 * We load all the csum data we need when we submit
7671 * the first bio to reduce the csum tree search and
7672 * contention.
7673 */
7674 if (dip->logical_offset == file_offset) {
7675 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
7676 file_offset);
7677 if (ret)
7678 return ret;
7679 }
7680
7681 if (bio == dip->orig_bio)
7682 return 0;
7683
7684 file_offset -= dip->logical_offset;
7685 file_offset >>= inode->i_sb->s_blocksize_bits;
7686 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
7687
7688 return 0;
7689}
7690
Miao Xiee65e1532010-11-22 03:04:43 +00007691static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7692 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007693 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007694{
Miao Xiefacc8a22013-07-25 19:22:34 +08007695 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007696 int write = rw & REQ_WRITE;
7697 struct btrfs_root *root = BTRFS_I(inode)->root;
7698 int ret;
7699
Josef Bacikb812ce22012-11-16 13:56:32 -05007700 if (async_submit)
7701 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7702
Miao Xiee65e1532010-11-22 03:04:43 +00007703 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007704
7705 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02007706 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7707 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007708 if (ret)
7709 goto err;
7710 }
Miao Xiee65e1532010-11-22 03:04:43 +00007711
Josef Bacik1ae39932011-04-06 14:41:34 -04007712 if (skip_sum)
7713 goto map;
7714
7715 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007716 ret = btrfs_wq_submit_bio(root->fs_info,
7717 inode, rw, bio, 0, 0,
7718 file_offset,
7719 __btrfs_submit_bio_start_direct_io,
7720 __btrfs_submit_bio_done);
7721 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007722 } else if (write) {
7723 /*
7724 * If we aren't doing async submit, calculate the csum of the
7725 * bio now.
7726 */
7727 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7728 if (ret)
7729 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007730 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08007731 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
7732 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007733 if (ret)
7734 goto err;
7735 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007736map:
7737 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007738err:
7739 bio_put(bio);
7740 return ret;
7741}
7742
7743static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7744 int skip_sum)
7745{
7746 struct inode *inode = dip->inode;
7747 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007748 struct bio *bio;
7749 struct bio *orig_bio = dip->orig_bio;
7750 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007751 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007752 u64 file_offset = dip->logical_offset;
7753 u64 submit_len = 0;
7754 u64 map_length;
7755 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08007756 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04007757 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007758
Kent Overstreet4f024f32013-10-11 15:44:27 -07007759 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007760 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007761 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007762 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007763 return -EIO;
Miao Xiefacc8a22013-07-25 19:22:34 +08007764
Kent Overstreet4f024f32013-10-11 15:44:27 -07007765 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007766 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007767 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04007768 goto submit;
7769 }
7770
David Woodhouse53b381b2013-01-29 18:40:14 -05007771 /* async crcs make it difficult to collect full stripe writes. */
7772 if (btrfs_get_alloc_profile(root, 1) &
7773 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7774 async_submit = 0;
7775 else
7776 async_submit = 1;
7777
Josef Bacik02f57c72011-04-06 14:25:44 -04007778 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7779 if (!bio)
7780 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08007781
Josef Bacik02f57c72011-04-06 14:25:44 -04007782 bio->bi_private = dip;
7783 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007784 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04007785 atomic_inc(&dip->pending_bios);
7786
Miao Xiee65e1532010-11-22 03:04:43 +00007787 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02007788 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00007789 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02007790 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00007791 /*
7792 * inc the count before we submit the bio so
7793 * we know the end IO handler won't happen before
7794 * we inc the count. Otherwise, the dip might get freed
7795 * before we're done setting it up
7796 */
7797 atomic_inc(&dip->pending_bios);
7798 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7799 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007800 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007801 if (ret) {
7802 bio_put(bio);
7803 atomic_dec(&dip->pending_bios);
7804 goto out_err;
7805 }
7806
Miao Xiee65e1532010-11-22 03:04:43 +00007807 start_sector += submit_len >> 9;
7808 file_offset += submit_len;
7809
7810 submit_len = 0;
7811 nr_pages = 0;
7812
7813 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7814 start_sector, GFP_NOFS);
7815 if (!bio)
7816 goto out_err;
7817 bio->bi_private = dip;
7818 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007819 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007820
Kent Overstreet4f024f32013-10-11 15:44:27 -07007821 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007822 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007823 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007824 &map_length, NULL, 0);
7825 if (ret) {
7826 bio_put(bio);
7827 goto out_err;
7828 }
7829 } else {
7830 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307831 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007832 bvec++;
7833 }
7834 }
7835
Josef Bacik02f57c72011-04-06 14:25:44 -04007836submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007837 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007838 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007839 if (!ret)
7840 return 0;
7841
7842 bio_put(bio);
7843out_err:
7844 dip->errors = 1;
7845 /*
7846 * before atomic variable goto zero, we must
7847 * make sure dip->errors is perceived to be set.
7848 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007849 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007850 if (atomic_dec_and_test(&dip->pending_bios))
7851 bio_io_error(dip->orig_bio);
7852
7853 /* bio_end_io() will handle error, so we needn't return it */
7854 return 0;
7855}
7856
Chris Mason9be33952013-05-17 18:30:14 -04007857static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7858 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007859{
7860 struct btrfs_root *root = BTRFS_I(inode)->root;
7861 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007862 struct bio *io_bio;
Miao Xie23ea8e52014-09-12 18:43:54 +08007863 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007864 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007865 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007866 int ret = 0;
7867
7868 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7869
Chris Mason9be33952013-05-17 18:30:14 -04007870 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007871 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007872 ret = -ENOMEM;
7873 goto free_ordered;
7874 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007875
Miao Xiec1dc0892014-09-12 18:43:56 +08007876 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007877 if (!dip) {
7878 ret = -ENOMEM;
7879 goto free_io_bio;
7880 }
7881
7882 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007883 dip->inode = inode;
7884 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007885 dip->bytes = dio_bio->bi_iter.bi_size;
7886 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04007887 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04007888 dip->orig_bio = io_bio;
7889 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007890 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08007891 btrfs_bio = btrfs_io_bio(io_bio);
7892 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007893
Miao Xiec1dc0892014-09-12 18:43:56 +08007894 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04007895 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08007896 } else {
Chris Mason9be33952013-05-17 18:30:14 -04007897 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08007898 dip->subio_endio = btrfs_subio_endio_read;
7899 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007900
Miao Xiee65e1532010-11-22 03:04:43 +00007901 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7902 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007903 return;
Chris Mason9be33952013-05-17 18:30:14 -04007904
Miao Xie23ea8e52014-09-12 18:43:54 +08007905 if (btrfs_bio->end_io)
7906 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04007907free_io_bio:
7908 bio_put(io_bio);
7909
Josef Bacik4b46fce2010-05-23 11:00:55 -04007910free_ordered:
7911 /*
7912 * If this is a write, we need to clean up the reserved space and kill
7913 * the ordered extent.
7914 */
7915 if (write) {
7916 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007917 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007918 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7919 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7920 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08007921 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007922 btrfs_put_ordered_extent(ordered);
7923 btrfs_put_ordered_extent(ordered);
7924 }
Chris Mason9be33952013-05-17 18:30:14 -04007925 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007926}
7927
Chris Mason5a5f79b2010-05-26 21:33:37 -04007928static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04007929 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04007930{
7931 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007932 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007933 unsigned blocksize_mask = root->sectorsize - 1;
7934 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007935
7936 if (offset & blocksize_mask)
7937 goto out;
7938
Al Viro28060d52014-03-22 05:15:17 -04007939 if (iov_iter_alignment(iter) & blocksize_mask)
7940 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007941
Al Viro28060d52014-03-22 05:15:17 -04007942 /* If this is a write we don't need to check anymore */
7943 if (rw & WRITE)
7944 return 0;
7945 /*
7946 * Check to make sure we don't have duplicate iov_base's in this
7947 * iovec, if so return EINVAL, otherwise we'll get csum errors
7948 * when reading back.
7949 */
7950 for (seg = 0; seg < iter->nr_segs; seg++) {
7951 for (i = seg + 1; i < iter->nr_segs; i++) {
7952 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00007953 goto out;
7954 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007955 }
7956 retval = 0;
7957out:
7958 return retval;
7959}
Josef Bacikeb838e72012-07-31 16:28:48 -04007960
Chris Mason16432982008-04-10 10:23:21 -04007961static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05007962 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04007963{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007964 struct file *file = iocb->ki_filp;
7965 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007966 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007967 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007968 bool wakeup = true;
7969 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007970 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007971
Al Viro28060d52014-03-22 05:15:17 -04007972 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007973 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007974
Miao Xie38851cc2013-02-08 07:04:11 +00007975 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007976 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00007977
Josef Bacik0e267c42013-07-02 10:38:02 -04007978 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08007979 * The generic stuff only does filemap_write_and_wait_range, which
7980 * isn't enough if we've written compressed pages to this area, so
7981 * we need to flush the dirty pages again to make absolutely sure
7982 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04007983 */
Al Viroa6cbcd42014-03-04 22:38:00 -05007984 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08007985 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7986 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08007987 filemap_fdatawrite_range(inode->i_mapping, offset,
7988 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04007989
Miao Xie09348562013-02-07 10:12:07 +00007990 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007991 /*
7992 * If the write DIO is beyond the EOF, we need update
7993 * the isize, but it is protected by i_mutex. So we can
7994 * not unlock the i_mutex at this case.
7995 */
7996 if (offset + count <= inode->i_size) {
7997 mutex_unlock(&inode->i_mutex);
7998 relock = true;
7999 }
Miao Xie09348562013-02-07 10:12:07 +00008000 ret = btrfs_delalloc_reserve_space(inode, count);
8001 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008002 goto out;
David Sterbaee39b432014-09-30 01:33:33 +02008003 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8004 &BTRFS_I(inode)->runtime_flags)) {
Miao Xie38851cc2013-02-08 07:04:11 +00008005 inode_dio_done(inode);
8006 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8007 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008008 }
8009
8010 ret = __blockdev_direct_IO(rw, iocb, inode,
8011 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05008012 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00008013 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00008014 if (rw & WRITE) {
8015 if (ret < 0 && ret != -EIOCBQUEUED)
8016 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07008017 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00008018 btrfs_delalloc_release_space(inode,
8019 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07008020 else
8021 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00008022 }
Miao Xie38851cc2013-02-08 07:04:11 +00008023out:
Miao Xie2e60a512013-02-08 07:01:08 +00008024 if (wakeup)
8025 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008026 if (relock)
8027 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008028
8029 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008030}
8031
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008032#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8033
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008034static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8035 __u64 start, __u64 len)
8036{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008037 int ret;
8038
8039 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8040 if (ret)
8041 return ret;
8042
Chris Masonec29ed52011-02-23 16:23:20 -05008043 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008044}
8045
Chris Mason9ebefb182007-06-15 13:50:00 -04008046int btrfs_readpage(struct file *file, struct page *page)
8047{
Chris Masond1310b22008-01-24 16:13:08 -05008048 struct extent_io_tree *tree;
8049 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008050 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008051}
Chris Mason1832a6d2007-12-21 16:27:21 -05008052
Chris Mason39279cc2007-06-12 06:35:45 -04008053static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8054{
Chris Masond1310b22008-01-24 16:13:08 -05008055 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04008056
8057
8058 if (current->flags & PF_MEMALLOC) {
8059 redirty_page_for_writepage(wbc, page);
8060 unlock_page(page);
8061 return 0;
8062 }
Chris Masond1310b22008-01-24 16:13:08 -05008063 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008064 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8065}
Chris Mason39279cc2007-06-12 06:35:45 -04008066
Eric Sandeen48a3b632013-04-25 20:41:01 +00008067static int btrfs_writepages(struct address_space *mapping,
8068 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008069{
Chris Masond1310b22008-01-24 16:13:08 -05008070 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008071
Chris Masond1310b22008-01-24 16:13:08 -05008072 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008073 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8074}
8075
Chris Mason3ab2fb52007-11-08 10:59:22 -05008076static int
8077btrfs_readpages(struct file *file, struct address_space *mapping,
8078 struct list_head *pages, unsigned nr_pages)
8079{
Chris Masond1310b22008-01-24 16:13:08 -05008080 struct extent_io_tree *tree;
8081 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008082 return extent_readpages(tree, mapping, pages, nr_pages,
8083 btrfs_get_extent);
8084}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008085static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008086{
Chris Masond1310b22008-01-24 16:13:08 -05008087 struct extent_io_tree *tree;
8088 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008089 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008090
Chris Masond1310b22008-01-24 16:13:08 -05008091 tree = &BTRFS_I(page->mapping->host)->io_tree;
8092 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008093 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008094 if (ret == 1) {
8095 ClearPagePrivate(page);
8096 set_page_private(page, 0);
8097 page_cache_release(page);
8098 }
8099 return ret;
8100}
Chris Mason39279cc2007-06-12 06:35:45 -04008101
Chris Masone6dcd2d2008-07-17 12:53:50 -04008102static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8103{
Chris Mason98509cf2008-09-11 15:51:43 -04008104 if (PageWriteback(page) || PageDirty(page))
8105 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008106 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008107}
8108
Lukas Czernerd47992f2013-05-21 23:17:23 -04008109static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8110 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008111{
Josef Bacik5fd02042012-05-02 14:00:54 -04008112 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008113 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008114 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008115 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008116 u64 page_start = page_offset(page);
8117 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008118 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008119
Chris Mason8b62b722009-09-02 16:53:46 -04008120 /*
8121 * we have the page locked, so new writeback can't start,
8122 * and the dirty bit won't be cleared while we are here.
8123 *
8124 * Wait for IO on this page so that we can safely clear
8125 * the PagePrivate2 bit and do ordered accounting
8126 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008127 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008128
Josef Bacik5fd02042012-05-02 14:00:54 -04008129 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008130 if (offset) {
8131 btrfs_releasepage(page, GFP_NOFS);
8132 return;
8133 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008134
8135 if (!inode_evicting)
8136 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8137 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008138 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008139 /*
8140 * IO on this page will never be started, so we need
8141 * to account for any ordered extents now
8142 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008143 if (!inode_evicting)
8144 clear_extent_bit(tree, page_start, page_end,
8145 EXTENT_DIRTY | EXTENT_DELALLOC |
8146 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8147 EXTENT_DEFRAG, 1, 0, &cached_state,
8148 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008149 /*
8150 * whoever cleared the private bit is responsible
8151 * for the finish_ordered_io
8152 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008153 if (TestClearPagePrivate2(page)) {
8154 struct btrfs_ordered_inode_tree *tree;
8155 u64 new_len;
8156
8157 tree = &BTRFS_I(inode)->ordered_tree;
8158
8159 spin_lock_irq(&tree->lock);
8160 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8161 new_len = page_start - ordered->file_offset;
8162 if (new_len < ordered->truncated_len)
8163 ordered->truncated_len = new_len;
8164 spin_unlock_irq(&tree->lock);
8165
8166 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8167 page_start,
8168 PAGE_CACHE_SIZE, 1))
8169 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008170 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008171 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008172 if (!inode_evicting) {
8173 cached_state = NULL;
8174 lock_extent_bits(tree, page_start, page_end, 0,
8175 &cached_state);
8176 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008177 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008178
8179 if (!inode_evicting) {
8180 clear_extent_bit(tree, page_start, page_end,
8181 EXTENT_LOCKED | EXTENT_DIRTY |
8182 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8183 EXTENT_DEFRAG, 1, 1,
8184 &cached_state, GFP_NOFS);
8185
8186 __btrfs_releasepage(page, GFP_NOFS);
8187 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008188
Chris Mason4a096752008-07-21 10:29:44 -04008189 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008190 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008191 ClearPagePrivate(page);
8192 set_page_private(page, 0);
8193 page_cache_release(page);
8194 }
Chris Mason39279cc2007-06-12 06:35:45 -04008195}
8196
Chris Mason9ebefb182007-06-15 13:50:00 -04008197/*
8198 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8199 * called from a page fault handler when a page is first dirtied. Hence we must
8200 * be careful to check for EOF conditions here. We set the page up correctly
8201 * for a written page which means we get ENOSPC checking when writing into
8202 * holes and correct delalloc and unwritten extent mapping on filesystems that
8203 * support these features.
8204 *
8205 * We are not allowed to take the i_mutex here so we have to play games to
8206 * protect against truncate races as the page could now be beyond EOF. Because
8207 * vmtruncate() writes the inode size before removing pages, once we have the
8208 * page lock we can determine safely if the page is beyond EOF. If it is not
8209 * beyond EOF, then the page is guaranteed safe against truncation until we
8210 * unlock the page.
8211 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008212int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008213{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008214 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008215 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008216 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008217 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8218 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008219 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008220 char *kaddr;
8221 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008222 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008223 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008224 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008225 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008226 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008227
Jan Karab2b5ef52012-06-12 16:20:45 +02008228 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04008229 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008230 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008231 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008232 reserved = 1;
8233 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008234 if (ret) {
8235 if (ret == -ENOMEM)
8236 ret = VM_FAULT_OOM;
8237 else /* -ENOSPC, -EIO, etc */
8238 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008239 if (reserved)
8240 goto out;
8241 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008242 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008243
Nick Piggin56a76f82009-03-31 15:23:23 -07008244 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008245again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008246 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008247 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008248 page_start = page_offset(page);
8249 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04008250
Chris Mason9ebefb182007-06-15 13:50:00 -04008251 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008252 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008253 /* page got truncated out from underneath us */
8254 goto out_unlock;
8255 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008256 wait_on_page_writeback(page);
8257
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008258 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008259 set_page_extent_mapped(page);
8260
Chris Masoneb84ae02008-07-17 13:53:27 -04008261 /*
8262 * we can't set the delalloc bits if there are pending ordered
8263 * extents. Drop our locks and wait for them to finish
8264 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008265 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8266 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008267 unlock_extent_cached(io_tree, page_start, page_end,
8268 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008269 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008270 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008271 btrfs_put_ordered_extent(ordered);
8272 goto again;
8273 }
8274
Josef Bacikfbf19082009-10-01 17:10:23 -04008275 /*
8276 * XXX - page_mkwrite gets called every time the page is dirtied, even
8277 * if it was already dirty, so for space accounting reasons we need to
8278 * clear any delalloc bits for the range we are fixing to save. There
8279 * is probably a better way to do this, but for now keep consistent with
8280 * prepare_pages in the normal write path.
8281 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008282 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008283 EXTENT_DIRTY | EXTENT_DELALLOC |
8284 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008285 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008286
Josef Bacik2ac55d42010-02-03 19:33:23 +00008287 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8288 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008289 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008290 unlock_extent_cached(io_tree, page_start, page_end,
8291 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008292 ret = VM_FAULT_SIGBUS;
8293 goto out_unlock;
8294 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008295 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008296
8297 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008298 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008299 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008300 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008301 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008302
Chris Masone6dcd2d2008-07-17 12:53:50 -04008303 if (zero_start != PAGE_CACHE_SIZE) {
8304 kaddr = kmap(page);
8305 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8306 flush_dcache_page(page);
8307 kunmap(page);
8308 }
Chris Mason247e7432008-07-17 12:53:51 -04008309 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008310 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008311 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008312
Chris Mason257c62e2009-10-13 13:21:08 -04008313 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8314 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008315 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008316
Josef Bacik2ac55d42010-02-03 19:33:23 +00008317 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008318
8319out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008320 if (!ret) {
8321 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008322 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008323 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008324 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008325out:
Josef Bacikec39e182012-01-12 19:10:12 -05008326 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008327out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008328 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008329 return ret;
8330}
8331
Josef Bacika41ad392011-01-31 15:30:16 -05008332static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008333{
8334 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008335 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008336 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008337 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008338 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008339 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008340 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008341
Josef Bacik0ef8b722013-10-25 16:13:35 -04008342 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8343 (u64)-1);
8344 if (ret)
8345 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008346
Josef Bacikfcb80c22011-05-03 10:40:22 -04008347 /*
8348 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8349 * 3 things going on here
8350 *
8351 * 1) We need to reserve space for our orphan item and the space to
8352 * delete our orphan item. Lord knows we don't want to have a dangling
8353 * orphan item because we didn't reserve space to remove it.
8354 *
8355 * 2) We need to reserve space to update our inode.
8356 *
8357 * 3) We need to have something to cache all the space that is going to
8358 * be free'd up by the truncate operation, but also have some slack
8359 * space reserved in case it uses space during the truncate (thank you
8360 * very much snapshotting).
8361 *
8362 * And we need these to all be seperate. The fact is we can use alot of
8363 * space doing the truncate, and we have no earthly idea how much space
8364 * we will use, so we need the truncate reservation to be seperate so it
8365 * doesn't end up using space reserved for updating the inode or
8366 * removing the orphan item. We also need to be able to stop the
8367 * transaction and start a new one, which means we need to be able to
8368 * update the inode several times, and we have no idea of knowing how
8369 * many times that will be, so we can't just reserve 1 item for the
8370 * entirety of the opration, so that has to be done seperately as well.
8371 * Then there is the orphan item, which does indeed need to be held on
8372 * to for the whole operation, and we need nobody to touch this reserved
8373 * space except the orphan code.
8374 *
8375 * So that leaves us with
8376 *
8377 * 1) root->orphan_block_rsv - for the orphan deletion.
8378 * 2) rsv - for the truncate reservation, which we will steal from the
8379 * transaction reservation.
8380 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8381 * updating the inode.
8382 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008383 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008384 if (!rsv)
8385 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008386 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008387 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008388
Josef Bacik907cbce2011-08-08 13:46:15 -04008389 /*
Josef Bacik07127182011-08-19 10:29:59 -04008390 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008391 * 1 for updating the inode.
8392 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008393 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008394 if (IS_ERR(trans)) {
8395 err = PTR_ERR(trans);
8396 goto out;
8397 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008398
Josef Bacik907cbce2011-08-08 13:46:15 -04008399 /* Migrate the slack space for the truncate to our reserve */
8400 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8401 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008402 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008403
Chris Mason5a3f23d2009-03-31 13:27:11 -04008404 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008405 * So if we truncate and then write and fsync we normally would just
8406 * write the extents that changed, which is a problem if we need to
8407 * first truncate that entire inode. So set this flag so we write out
8408 * all of the extents in the inode to the sync log so we're completely
8409 * safe.
8410 */
8411 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008412 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008413
Yan, Zheng80825102009-11-12 09:35:36 +00008414 while (1) {
8415 ret = btrfs_truncate_inode_items(trans, root, inode,
8416 inode->i_size,
8417 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008418 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05008419 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008420 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008421 }
Chris Mason39279cc2007-06-12 06:35:45 -04008422
Josef Bacikfcb80c22011-05-03 10:40:22 -04008423 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008424 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008425 if (ret) {
8426 err = ret;
8427 break;
8428 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008429
Yan, Zheng80825102009-11-12 09:35:36 +00008430 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008431 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008432
8433 trans = btrfs_start_transaction(root, 2);
8434 if (IS_ERR(trans)) {
8435 ret = err = PTR_ERR(trans);
8436 trans = NULL;
8437 break;
8438 }
8439
8440 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8441 rsv, min_size);
8442 BUG_ON(ret); /* shouldn't happen */
8443 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008444 }
8445
8446 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008447 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008448 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008449 if (ret)
8450 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008451 }
8452
Chris Mason917c16b2011-11-08 14:49:59 -05008453 if (trans) {
8454 trans->block_rsv = &root->fs_info->trans_block_rsv;
8455 ret = btrfs_update_inode(trans, root, inode);
8456 if (ret && !err)
8457 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008458
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008459 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008460 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008461 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008462
8463out:
8464 btrfs_free_block_rsv(root, rsv);
8465
Josef Bacik3893e332011-01-31 16:03:11 -05008466 if (ret && !err)
8467 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008468
Josef Bacik3893e332011-01-31 16:03:11 -05008469 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008470}
8471
Sven Wegener3b963622008-06-09 21:57:42 -04008472/*
Chris Masond352ac62008-09-29 15:18:18 -04008473 * create a new subvolume directory/inode (helper for the ioctl).
8474 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008475int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008476 struct btrfs_root *new_root,
8477 struct btrfs_root *parent_root,
8478 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008479{
Chris Mason39279cc2007-06-12 06:35:45 -04008480 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008481 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008482 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008483
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008484 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8485 new_dirid, new_dirid,
8486 S_IFDIR | (~current_umask() & S_IRWXUGO),
8487 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008488 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008489 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008490 inode->i_op = &btrfs_dir_inode_operations;
8491 inode->i_fop = &btrfs_dir_file_operations;
8492
Miklos Szeredibfe86842011-10-28 14:13:29 +02008493 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008494 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008495 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008496
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008497 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8498 if (err)
8499 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008500 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008501 new_root->root_key.objectid, err);
8502
Yan, Zheng76dda932009-09-21 16:00:26 -04008503 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008504
Yan, Zheng76dda932009-09-21 16:00:26 -04008505 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008506 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008507}
8508
Chris Mason39279cc2007-06-12 06:35:45 -04008509struct inode *btrfs_alloc_inode(struct super_block *sb)
8510{
8511 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008512 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008513
8514 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8515 if (!ei)
8516 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008517
8518 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008519 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008520 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008521 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008522 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008523 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008524 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008525 ei->disk_i_size = 0;
8526 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008527 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008528 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008529 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008530 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008531 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008532
Josef Bacik9e0baf62011-07-15 15:16:44 +00008533 spin_lock_init(&ei->lock);
8534 ei->outstanding_extents = 0;
8535 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008536
Josef Bacik72ac3c02012-05-23 14:13:11 -04008537 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008538 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008539
Miao Xie16cdcec2011-04-22 18:12:22 +08008540 ei->delayed_node = NULL;
8541
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008542 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008543 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008544 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8545 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008546 ei->io_tree.track_uptodate = 1;
8547 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008548 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008549 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008550 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008551 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008552 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008553 RB_CLEAR_NODE(&ei->rb_node);
8554
8555 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008556}
8557
Josef Bacikaaedb552013-10-11 14:44:09 -04008558#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8559void btrfs_test_destroy_inode(struct inode *inode)
8560{
8561 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8562 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8563}
8564#endif
8565
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008566static void btrfs_i_callback(struct rcu_head *head)
8567{
8568 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008569 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8570}
8571
Chris Mason39279cc2007-06-12 06:35:45 -04008572void btrfs_destroy_inode(struct inode *inode)
8573{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008574 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008575 struct btrfs_root *root = BTRFS_I(inode)->root;
8576
Al Virob3d9b7a2012-06-09 13:51:19 -04008577 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008578 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008579 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8580 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008581 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8582 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008583 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008584
Chris Mason5a3f23d2009-03-31 13:27:11 -04008585 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008586 * This can happen where we create an inode, but somebody else also
8587 * created the same inode and we need to destroy the one we already
8588 * created.
8589 */
8590 if (!root)
8591 goto free;
8592
Josef Bacik8a35d952012-05-23 14:26:42 -04008593 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8594 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008595 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008596 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008597 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008598 }
Josef Bacik7b128762008-07-24 12:17:14 -04008599
Chris Masond3977122009-01-05 21:25:51 -05008600 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008601 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8602 if (!ordered)
8603 break;
8604 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008605 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008606 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008607 btrfs_remove_ordered_extent(inode, ordered);
8608 btrfs_put_ordered_extent(ordered);
8609 btrfs_put_ordered_extent(ordered);
8610 }
8611 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008612 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008613 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008614free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008615 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008616}
8617
Al Viro45321ac2010-06-07 13:43:19 -04008618int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008619{
8620 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008621
Naohiro Aota6379ef92013-06-06 09:56:34 +00008622 if (root == NULL)
8623 return 1;
8624
Liu Bofa6ac872013-02-20 14:10:23 +00008625 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008626 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008627 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008628 else
Al Viro45321ac2010-06-07 13:43:19 -04008629 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008630}
8631
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008632static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008633{
8634 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8635
8636 inode_init_once(&ei->vfs_inode);
8637}
8638
8639void btrfs_destroy_cachep(void)
8640{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008641 /*
8642 * Make sure all delayed rcu free inodes are flushed before we
8643 * destroy cache.
8644 */
8645 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008646 if (btrfs_inode_cachep)
8647 kmem_cache_destroy(btrfs_inode_cachep);
8648 if (btrfs_trans_handle_cachep)
8649 kmem_cache_destroy(btrfs_trans_handle_cachep);
8650 if (btrfs_transaction_cachep)
8651 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008652 if (btrfs_path_cachep)
8653 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008654 if (btrfs_free_space_cachep)
8655 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008656 if (btrfs_delalloc_work_cachep)
8657 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008658}
8659
8660int btrfs_init_cachep(void)
8661{
David Sterba837e1972012-09-07 03:00:48 -06008662 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008663 sizeof(struct btrfs_inode), 0,
8664 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008665 if (!btrfs_inode_cachep)
8666 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008667
David Sterba837e1972012-09-07 03:00:48 -06008668 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008669 sizeof(struct btrfs_trans_handle), 0,
8670 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008671 if (!btrfs_trans_handle_cachep)
8672 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008673
David Sterba837e1972012-09-07 03:00:48 -06008674 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008675 sizeof(struct btrfs_transaction), 0,
8676 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008677 if (!btrfs_transaction_cachep)
8678 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008679
David Sterba837e1972012-09-07 03:00:48 -06008680 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008681 sizeof(struct btrfs_path), 0,
8682 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008683 if (!btrfs_path_cachep)
8684 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008685
David Sterba837e1972012-09-07 03:00:48 -06008686 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008687 sizeof(struct btrfs_free_space), 0,
8688 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8689 if (!btrfs_free_space_cachep)
8690 goto fail;
8691
Miao Xie8ccf6f192012-10-25 09:28:04 +00008692 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8693 sizeof(struct btrfs_delalloc_work), 0,
8694 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8695 NULL);
8696 if (!btrfs_delalloc_work_cachep)
8697 goto fail;
8698
Chris Mason39279cc2007-06-12 06:35:45 -04008699 return 0;
8700fail:
8701 btrfs_destroy_cachep();
8702 return -ENOMEM;
8703}
8704
8705static int btrfs_getattr(struct vfsmount *mnt,
8706 struct dentry *dentry, struct kstat *stat)
8707{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008708 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008709 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008710 u32 blocksize = inode->i_sb->s_blocksize;
8711
Chris Mason39279cc2007-06-12 06:35:45 -04008712 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008713 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008714 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008715
8716 spin_lock(&BTRFS_I(inode)->lock);
8717 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8718 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008719 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008720 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008721 return 0;
8722}
8723
Chris Masond3977122009-01-05 21:25:51 -05008724static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8725 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008726{
8727 struct btrfs_trans_handle *trans;
8728 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008729 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008730 struct inode *new_inode = new_dentry->d_inode;
8731 struct inode *old_inode = old_dentry->d_inode;
8732 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008733 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008734 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008735 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008736 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008737
Li Zefan33345d012011-04-20 10:31:50 +08008738 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008739 return -EPERM;
8740
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008741 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008742 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008743 return -EXDEV;
8744
Li Zefan33345d012011-04-20 10:31:50 +08008745 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8746 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008747 return -ENOTEMPTY;
8748
Chris Mason39279cc2007-06-12 06:35:45 -04008749 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008750 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008751 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008752
8753
8754 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008755 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008756 new_dentry->d_name.name,
8757 new_dentry->d_name.len);
8758
8759 if (ret) {
8760 if (ret == -EEXIST) {
8761 /* we shouldn't get
8762 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308763 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008764 return ret;
8765 }
8766 } else {
8767 /* maybe -EOVERFLOW */
8768 return ret;
8769 }
8770 }
8771 ret = 0;
8772
Chris Mason5a3f23d2009-03-31 13:27:11 -04008773 /*
Chris Mason8d875f92014-08-12 10:47:42 -07008774 * we're using rename to replace one file with another. Start IO on it
8775 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04008776 */
Chris Mason8d875f92014-08-12 10:47:42 -07008777 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04008778 filemap_flush(old_inode->i_mapping);
8779
Yan, Zheng76dda932009-09-21 16:00:26 -04008780 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008781 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008782 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008783 /*
8784 * We want to reserve the absolute worst case amount of items. So if
8785 * both inodes are subvols and we need to unlink them then that would
8786 * require 4 item modifications, but if they are both normal inodes it
8787 * would require 5 item modifications, so we'll assume their normal
8788 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8789 * should cover the worst case number of items we'll modify.
8790 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008791 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008792 if (IS_ERR(trans)) {
8793 ret = PTR_ERR(trans);
8794 goto out_notrans;
8795 }
Chris Mason5f39d392007-10-15 16:14:19 -04008796
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008797 if (dest != root)
8798 btrfs_record_root_in_trans(trans, dest);
8799
Yan, Zhenga5719522009-09-24 09:17:31 -04008800 ret = btrfs_set_inode_index(new_dir, &index);
8801 if (ret)
8802 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008803
Miao Xie67de1172013-12-26 13:07:06 +08008804 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008805 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008806 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08008807 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008808 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008809 ret = btrfs_insert_inode_ref(trans, dest,
8810 new_dentry->d_name.name,
8811 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008812 old_ino,
8813 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008814 if (ret)
8815 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008816 /*
8817 * this is an ugly little race, but the rename is required
8818 * to make sure that if we crash, the inode is either at the
8819 * old name or the new one. pinning the log transaction lets
8820 * us make sure we don't allow a log commit to come in after
8821 * we unlink the name but before we add the new name back in.
8822 */
8823 btrfs_pin_log_trans(root);
8824 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04008825
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008826 inode_inc_iversion(old_dir);
8827 inode_inc_iversion(new_dir);
8828 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008829 old_dir->i_ctime = old_dir->i_mtime = ctime;
8830 new_dir->i_ctime = new_dir->i_mtime = ctime;
8831 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008832
Chris Mason12fcfd22009-03-24 10:24:20 -04008833 if (old_dentry->d_parent != new_dentry->d_parent)
8834 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8835
Li Zefan33345d012011-04-20 10:31:50 +08008836 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008837 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8838 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8839 old_dentry->d_name.name,
8840 old_dentry->d_name.len);
8841 } else {
Al Viro92986792011-03-04 17:14:37 +00008842 ret = __btrfs_unlink_inode(trans, root, old_dir,
8843 old_dentry->d_inode,
8844 old_dentry->d_name.name,
8845 old_dentry->d_name.len);
8846 if (!ret)
8847 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008848 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008849 if (ret) {
8850 btrfs_abort_transaction(trans, root, ret);
8851 goto out_fail;
8852 }
Chris Mason39279cc2007-06-12 06:35:45 -04008853
8854 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008855 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008856 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008857 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008858 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8859 root_objectid = BTRFS_I(new_inode)->location.objectid;
8860 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8861 root_objectid,
8862 new_dentry->d_name.name,
8863 new_dentry->d_name.len);
8864 BUG_ON(new_inode->i_nlink == 0);
8865 } else {
8866 ret = btrfs_unlink_inode(trans, dest, new_dir,
8867 new_dentry->d_inode,
8868 new_dentry->d_name.name,
8869 new_dentry->d_name.len);
8870 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008871 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008872 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008873 if (ret) {
8874 btrfs_abort_transaction(trans, root, ret);
8875 goto out_fail;
8876 }
Chris Mason39279cc2007-06-12 06:35:45 -04008877 }
Josef Bacikaec74772008-07-24 12:12:38 -04008878
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008879 ret = btrfs_add_link(trans, new_dir, old_inode,
8880 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008881 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008882 if (ret) {
8883 btrfs_abort_transaction(trans, root, ret);
8884 goto out_fail;
8885 }
Chris Mason39279cc2007-06-12 06:35:45 -04008886
Miao Xie67de1172013-12-26 13:07:06 +08008887 if (old_inode->i_nlink == 1)
8888 BTRFS_I(old_inode)->dir_index = index;
8889
Li Zefan33345d012011-04-20 10:31:50 +08008890 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008891 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008892 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008893 btrfs_end_log_trans(root);
8894 }
Chris Mason39279cc2007-06-12 06:35:45 -04008895out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008896 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008897out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008898 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008899 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008900
Chris Mason39279cc2007-06-12 06:35:45 -04008901 return ret;
8902}
8903
Miklos Szeredi80ace852014-07-23 15:15:32 +02008904static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
8905 struct inode *new_dir, struct dentry *new_dentry,
8906 unsigned int flags)
8907{
8908 if (flags & ~RENAME_NOREPLACE)
8909 return -EINVAL;
8910
8911 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
8912}
8913
Miao Xie8ccf6f192012-10-25 09:28:04 +00008914static void btrfs_run_delalloc_work(struct btrfs_work *work)
8915{
8916 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008917 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008918
8919 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8920 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008921 inode = delalloc_work->inode;
8922 if (delalloc_work->wait) {
8923 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8924 } else {
8925 filemap_flush(inode->i_mapping);
8926 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8927 &BTRFS_I(inode)->runtime_flags))
8928 filemap_flush(inode->i_mapping);
8929 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008930
8931 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008932 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008933 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008934 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008935 complete(&delalloc_work->completion);
8936}
8937
8938struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8939 int wait, int delay_iput)
8940{
8941 struct btrfs_delalloc_work *work;
8942
8943 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8944 if (!work)
8945 return NULL;
8946
8947 init_completion(&work->completion);
8948 INIT_LIST_HEAD(&work->list);
8949 work->inode = inode;
8950 work->wait = wait;
8951 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08008952 WARN_ON_ONCE(!inode);
8953 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
8954 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008955
8956 return work;
8957}
8958
8959void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8960{
8961 wait_for_completion(&work->completion);
8962 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8963}
8964
Chris Masond352ac62008-09-29 15:18:18 -04008965/*
8966 * some fairly slow code that needs optimization. This walks the list
8967 * of all the inodes with pending delalloc and forces them to disk.
8968 */
Miao Xie6c255e62014-03-06 13:55:01 +08008969static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
8970 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04008971{
Chris Masonea8c2812008-08-04 23:17:27 -04008972 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008973 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008974 struct btrfs_delalloc_work *work, *next;
8975 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008976 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008977 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008978
Miao Xie8ccf6f192012-10-25 09:28:04 +00008979 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008980 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008981
Miao Xie573bfb72014-03-06 13:55:03 +08008982 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008983 spin_lock(&root->delalloc_lock);
8984 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008985 while (!list_empty(&splice)) {
8986 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008987 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008988
Miao Xieeb73c1b2013-05-15 07:48:22 +00008989 list_move_tail(&binode->delalloc_inodes,
8990 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008991 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008992 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008993 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008994 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008995 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008996 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008997
8998 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02008999 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009000 if (delay_iput)
9001 btrfs_add_delayed_iput(inode);
9002 else
9003 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009004 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009005 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009006 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009007 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009008 btrfs_queue_work(root->fs_info->flush_workers,
9009 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009010 ret++;
9011 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009012 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009013 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009014 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009015 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009016 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee1d2008-09-29 11:19:10 -04009017
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009018out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009019 list_for_each_entry_safe(work, next, &works, list) {
9020 list_del_init(&work->list);
9021 btrfs_wait_and_free_delalloc_work(work);
9022 }
9023
Miao Xieeb73c1b2013-05-15 07:48:22 +00009024 if (!list_empty_careful(&splice)) {
9025 spin_lock(&root->delalloc_lock);
9026 list_splice_tail(&splice, &root->delalloc_inodes);
9027 spin_unlock(&root->delalloc_lock);
9028 }
Miao Xie573bfb72014-03-06 13:55:03 +08009029 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009030 return ret;
9031}
9032
9033int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9034{
9035 int ret;
9036
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009037 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009038 return -EROFS;
9039
Miao Xie6c255e62014-03-06 13:55:01 +08009040 ret = __start_delalloc_inodes(root, delay_iput, -1);
9041 if (ret > 0)
9042 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009043 /*
9044 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee1d2008-09-29 11:19:10 -04009045 * we have to make sure the IO is actually started and that
9046 * ordered extents get created before we return
9047 */
9048 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009049 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009050 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee1d2008-09-29 11:19:10 -04009051 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009052 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9053 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee1d2008-09-29 11:19:10 -04009054 }
9055 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009056 return ret;
9057}
9058
Miao Xie6c255e62014-03-06 13:55:01 +08009059int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9060 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009061{
9062 struct btrfs_root *root;
9063 struct list_head splice;
9064 int ret;
9065
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009066 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009067 return -EROFS;
9068
9069 INIT_LIST_HEAD(&splice);
9070
Miao Xie573bfb72014-03-06 13:55:03 +08009071 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009072 spin_lock(&fs_info->delalloc_root_lock);
9073 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009074 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009075 root = list_first_entry(&splice, struct btrfs_root,
9076 delalloc_root);
9077 root = btrfs_grab_fs_root(root);
9078 BUG_ON(!root);
9079 list_move_tail(&root->delalloc_root,
9080 &fs_info->delalloc_roots);
9081 spin_unlock(&fs_info->delalloc_root_lock);
9082
Miao Xie6c255e62014-03-06 13:55:01 +08009083 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009084 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009085 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009086 goto out;
9087
Miao Xie6c255e62014-03-06 13:55:01 +08009088 if (nr != -1) {
9089 nr -= ret;
9090 WARN_ON(nr < 0);
9091 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009092 spin_lock(&fs_info->delalloc_root_lock);
9093 }
9094 spin_unlock(&fs_info->delalloc_root_lock);
9095
Miao Xie6c255e62014-03-06 13:55:01 +08009096 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009097 atomic_inc(&fs_info->async_submit_draining);
9098 while (atomic_read(&fs_info->nr_async_submits) ||
9099 atomic_read(&fs_info->async_delalloc_pages)) {
9100 wait_event(fs_info->async_submit_wait,
9101 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9102 atomic_read(&fs_info->async_delalloc_pages) == 0));
9103 }
9104 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009105out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009106 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009107 spin_lock(&fs_info->delalloc_root_lock);
9108 list_splice_tail(&splice, &fs_info->delalloc_roots);
9109 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009110 }
Miao Xie573bfb72014-03-06 13:55:03 +08009111 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009112 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009113}
9114
Chris Mason39279cc2007-06-12 06:35:45 -04009115static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9116 const char *symname)
9117{
9118 struct btrfs_trans_handle *trans;
9119 struct btrfs_root *root = BTRFS_I(dir)->root;
9120 struct btrfs_path *path;
9121 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009122 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009123 int err;
9124 int drop_inode = 0;
9125 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309126 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009127 int name_len;
9128 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009129 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009130 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009131 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009132
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009133 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009134 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9135 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009136
Josef Bacik9ed74f22009-09-11 16:12:44 -04009137 /*
9138 * 2 items for inode item and ref
9139 * 2 items for dir items
9140 * 1 item for xattr if selinux is on
9141 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009142 trans = btrfs_start_transaction(root, 5);
9143 if (IS_ERR(trans))
9144 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009145
Li Zefan581bb052011-04-20 10:06:11 +08009146 err = btrfs_find_free_ino(root, &objectid);
9147 if (err)
9148 goto out_unlock;
9149
Josef Bacikaec74772008-07-24 12:12:38 -04009150 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009151 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04009152 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009153 if (IS_ERR(inode)) {
9154 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009155 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009156 }
Chris Mason39279cc2007-06-12 06:35:45 -04009157
Casey Schauflerad19db72011-12-15 10:09:07 -05009158 /*
9159 * If the active LSM wants to access the inode during
9160 * d_instantiate it needs these. Smack checks to see
9161 * if the filesystem supports xattrs by looking at the
9162 * ops vector.
9163 */
9164 inode->i_fop = &btrfs_file_operations;
9165 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009166 inode->i_mapping->a_ops = &btrfs_aops;
9167 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9168 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9169
9170 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9171 if (err)
9172 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009173
Josef Bacika1b075d2010-11-19 20:36:11 +00009174 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009175 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009176 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009177
9178 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009179 if (!path) {
9180 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009181 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009182 }
Li Zefan33345d012011-04-20 10:31:50 +08009183 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009184 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009185 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009186 datasize = btrfs_file_extent_calc_inline_size(name_len);
9187 err = btrfs_insert_empty_item(trans, root, path, &key,
9188 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009189 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009190 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009191 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009192 }
Chris Mason5f39d392007-10-15 16:14:19 -04009193 leaf = path->nodes[0];
9194 ei = btrfs_item_ptr(leaf, path->slots[0],
9195 struct btrfs_file_extent_item);
9196 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9197 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009198 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009199 btrfs_set_file_extent_encryption(leaf, ei, 0);
9200 btrfs_set_file_extent_compression(leaf, ei, 0);
9201 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9202 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9203
Chris Mason39279cc2007-06-12 06:35:45 -04009204 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009205 write_extent_buffer(leaf, symname, ptr, name_len);
9206 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009207 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009208
Chris Mason39279cc2007-06-12 06:35:45 -04009209 inode->i_op = &btrfs_symlink_inode_operations;
9210 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04009211 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04009212 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009213 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009214 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009215 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009216 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009217 goto out_unlock_inode;
9218 }
9219
9220 unlock_new_inode(inode);
9221 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009222
9223out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009224 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009225 if (drop_inode) {
9226 inode_dec_link_count(inode);
9227 iput(inode);
9228 }
Liu Bob53d3f52012-11-14 14:34:34 +00009229 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009230 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009231
9232out_unlock_inode:
9233 drop_inode = 1;
9234 unlock_new_inode(inode);
9235 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009236}
Chris Mason16432982008-04-10 10:23:21 -04009237
Josef Bacik0af3d002010-06-21 14:48:16 -04009238static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9239 u64 start, u64 num_bytes, u64 min_size,
9240 loff_t actual_len, u64 *alloc_hint,
9241 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009242{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009243 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9244 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009245 struct btrfs_root *root = BTRFS_I(inode)->root;
9246 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009247 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009248 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009249 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009250 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009251 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009252
Josef Bacik0af3d002010-06-21 14:48:16 -04009253 if (trans)
9254 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009255 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009256 if (own_trans) {
9257 trans = btrfs_start_transaction(root, 3);
9258 if (IS_ERR(trans)) {
9259 ret = PTR_ERR(trans);
9260 break;
9261 }
Yan Zhengd899e052008-10-30 14:25:28 -04009262 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009263
Chris Mason154ea282013-03-05 11:11:26 -05009264 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9265 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009266 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009267 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009268 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009269 if (own_trans)
9270 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009271 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009272 }
9273
Yan Zhengd899e052008-10-30 14:25:28 -04009274 ret = insert_reserved_file_extent(trans, inode,
9275 cur_offset, ins.objectid,
9276 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009277 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009278 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009279 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009280 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009281 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009282 btrfs_abort_transaction(trans, root, ret);
9283 if (own_trans)
9284 btrfs_end_transaction(trans, root);
9285 break;
9286 }
Chris Masona1ed8352009-09-11 12:27:37 -04009287 btrfs_drop_extent_cache(inode, cur_offset,
9288 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009289
Josef Bacik5dc562c2012-08-17 13:14:17 -04009290 em = alloc_extent_map();
9291 if (!em) {
9292 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9293 &BTRFS_I(inode)->runtime_flags);
9294 goto next;
9295 }
9296
9297 em->start = cur_offset;
9298 em->orig_start = cur_offset;
9299 em->len = ins.offset;
9300 em->block_start = ins.objectid;
9301 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009302 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009303 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009304 em->bdev = root->fs_info->fs_devices->latest_bdev;
9305 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9306 em->generation = trans->transid;
9307
9308 while (1) {
9309 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009310 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009311 write_unlock(&em_tree->lock);
9312 if (ret != -EEXIST)
9313 break;
9314 btrfs_drop_extent_cache(inode, cur_offset,
9315 cur_offset + ins.offset - 1,
9316 0);
9317 }
9318 free_extent_map(em);
9319next:
Yan Zhengd899e052008-10-30 14:25:28 -04009320 num_bytes -= ins.offset;
9321 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009322 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009323
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009324 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009325 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009326 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009327 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009328 (actual_len > inode->i_size) &&
9329 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009330 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009331 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009332 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009333 i_size = cur_offset;
9334 i_size_write(inode, i_size);
9335 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009336 }
9337
Yan Zhengd899e052008-10-30 14:25:28 -04009338 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009339
9340 if (ret) {
9341 btrfs_abort_transaction(trans, root, ret);
9342 if (own_trans)
9343 btrfs_end_transaction(trans, root);
9344 break;
9345 }
Yan Zhengd899e052008-10-30 14:25:28 -04009346
Josef Bacik0af3d002010-06-21 14:48:16 -04009347 if (own_trans)
9348 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009349 }
Yan Zhengd899e052008-10-30 14:25:28 -04009350 return ret;
9351}
9352
Josef Bacik0af3d002010-06-21 14:48:16 -04009353int btrfs_prealloc_file_range(struct inode *inode, int mode,
9354 u64 start, u64 num_bytes, u64 min_size,
9355 loff_t actual_len, u64 *alloc_hint)
9356{
9357 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9358 min_size, actual_len, alloc_hint,
9359 NULL);
9360}
9361
9362int btrfs_prealloc_file_range_trans(struct inode *inode,
9363 struct btrfs_trans_handle *trans, int mode,
9364 u64 start, u64 num_bytes, u64 min_size,
9365 loff_t actual_len, u64 *alloc_hint)
9366{
9367 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9368 min_size, actual_len, alloc_hint, trans);
9369}
9370
Chris Masone6dcd2d2008-07-17 12:53:50 -04009371static int btrfs_set_page_dirty(struct page *page)
9372{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009373 return __set_page_dirty_nobuffers(page);
9374}
9375
Al Viro10556cb2011-06-20 19:28:19 -04009376static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009377{
Li Zefanb83cc962010-12-20 16:04:08 +08009378 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009379 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009380
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009381 if (mask & MAY_WRITE &&
9382 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9383 if (btrfs_root_readonly(root))
9384 return -EROFS;
9385 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9386 return -EACCES;
9387 }
Al Viro2830ba72011-06-20 19:16:29 -04009388 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009389}
Chris Mason39279cc2007-06-12 06:35:45 -04009390
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009391static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9392{
9393 struct btrfs_trans_handle *trans;
9394 struct btrfs_root *root = BTRFS_I(dir)->root;
9395 struct inode *inode = NULL;
9396 u64 objectid;
9397 u64 index;
9398 int ret = 0;
9399
9400 /*
9401 * 5 units required for adding orphan entry
9402 */
9403 trans = btrfs_start_transaction(root, 5);
9404 if (IS_ERR(trans))
9405 return PTR_ERR(trans);
9406
9407 ret = btrfs_find_free_ino(root, &objectid);
9408 if (ret)
9409 goto out;
9410
9411 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9412 btrfs_ino(dir), objectid, mode, &index);
9413 if (IS_ERR(inode)) {
9414 ret = PTR_ERR(inode);
9415 inode = NULL;
9416 goto out;
9417 }
9418
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009419 inode->i_fop = &btrfs_file_operations;
9420 inode->i_op = &btrfs_file_inode_operations;
9421
9422 inode->i_mapping->a_ops = &btrfs_aops;
9423 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9424 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9425
Chris Masonb0d5d102014-09-08 13:08:51 -07009426 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9427 if (ret)
9428 goto out_inode;
9429
9430 ret = btrfs_update_inode(trans, root, inode);
9431 if (ret)
9432 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009433 ret = btrfs_orphan_add(trans, inode);
9434 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009435 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009436
Filipe Manana5762b5c2014-08-01 00:10:32 +01009437 /*
9438 * We set number of links to 0 in btrfs_new_inode(), and here we set
9439 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9440 * through:
9441 *
9442 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9443 */
9444 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009445 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009446 d_tmpfile(dentry, inode);
9447 mark_inode_dirty(inode);
9448
9449out:
9450 btrfs_end_transaction(trans, root);
9451 if (ret)
9452 iput(inode);
9453 btrfs_balance_delayed_items(root);
9454 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009455 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009456
9457out_inode:
9458 unlock_new_inode(inode);
9459 goto out;
9460
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009461}
9462
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009463static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009464 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009465 .lookup = btrfs_lookup,
9466 .create = btrfs_create,
9467 .unlink = btrfs_unlink,
9468 .link = btrfs_link,
9469 .mkdir = btrfs_mkdir,
9470 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009471 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009472 .symlink = btrfs_symlink,
9473 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009474 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009475 .setxattr = btrfs_setxattr,
9476 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009477 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009478 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009479 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009480 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009481 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009482 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009483 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009484};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009485static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009486 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009487 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009488 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009489 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009490 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009491};
Yan, Zheng76dda932009-09-21 16:00:26 -04009492
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009493static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009494 .llseek = generic_file_llseek,
9495 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009496 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009497 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009498#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009499 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009500#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009501 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009502 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009503};
9504
Chris Masond1310b22008-01-24 16:13:08 -05009505static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009506 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009507 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009508 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009509 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009510 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009511 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009512 .set_bit_hook = btrfs_set_bit_hook,
9513 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009514 .merge_extent_hook = btrfs_merge_extent_hook,
9515 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009516};
9517
Chris Mason35054392009-01-21 13:11:13 -05009518/*
9519 * btrfs doesn't support the bmap operation because swapfiles
9520 * use bmap to make a mapping of extents in the file. They assume
9521 * these extents won't change over the life of the file and they
9522 * use the bmap result to do IO directly to the drive.
9523 *
9524 * the btrfs bmap call would return logical addresses that aren't
9525 * suitable for IO and they also will change frequently as COW
9526 * operations happen. So, swapfile + btrfs == corruption.
9527 *
9528 * For now we're avoiding this by dropping bmap.
9529 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009530static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009531 .readpage = btrfs_readpage,
9532 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04009533 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009534 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009535 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009536 .invalidatepage = btrfs_invalidatepage,
9537 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009538 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009539 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009540};
9541
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009542static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009543 .readpage = btrfs_readpage,
9544 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009545 .invalidatepage = btrfs_invalidatepage,
9546 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009547};
9548
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009549static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009550 .getattr = btrfs_getattr,
9551 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009552 .setxattr = btrfs_setxattr,
9553 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009554 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009555 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009556 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009557 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009558 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009559 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009560 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009561};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009562static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009563 .getattr = btrfs_getattr,
9564 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009565 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009566 .setxattr = btrfs_setxattr,
9567 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009568 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009569 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009570 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009571 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009572 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009573};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009574static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009575 .readlink = generic_readlink,
9576 .follow_link = page_follow_link_light,
9577 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009578 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009579 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009580 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009581 .setxattr = btrfs_setxattr,
9582 .getxattr = btrfs_getxattr,
9583 .listxattr = btrfs_listxattr,
9584 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009585 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009586};
Yan, Zheng76dda932009-09-21 16:00:26 -04009587
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009588const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009589 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009590 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009591};