blob: 998e67fdf2f6b6ce49db5b2eb3106e735ce36714 [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 Masonf03d9301f2009-02-04 09:31:06 -0500428 /*
429 * we don't want to send crud past the end of i_size through
430 * compression, that's just a waste of CPU time. So, if the
431 * end of the file is before the start of our current
432 * requested range of bytes, we bail out to the uncompressed
433 * cleanup code that can deal with all of this.
434 *
435 * It isn't really the fastest way to fix things, but this is a
436 * very uncommon corner.
437 */
438 if (actual_end <= start)
439 goto cleanup_and_bail_uncompressed;
440
Chris Masonc8b97812008-10-29 14:49:59 -0400441 total_compressed = actual_end - start;
442
443 /* we want to make sure that amount of ram required to uncompress
444 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500445 * of a compressed extent to 128k. This is a crucial number
446 * because it also controls how easily we can spread reads across
447 * cpus for decompression.
448 *
449 * We also want to make sure the amount of IO required to do
450 * a random read is reasonably small, so we limit the size of
451 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400452 */
453 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000454 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500455 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400456 total_in = 0;
457 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400458
Chris Mason771ed682008-11-06 22:02:51 -0500459 /*
460 * we do compression for mount -o compress and when the
461 * inode has not been flagged as nocompress. This flag can
462 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400463 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800464 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400465 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400466 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800467 if (!pages) {
468 /* just bail out to the uncompressed code */
469 goto cont;
470 }
Chris Mason179e29e2007-11-01 11:28:41 -0400471
Li Zefan261507a02010-12-17 14:21:50 +0800472 if (BTRFS_I(inode)->force_compress)
473 compress_type = BTRFS_I(inode)->force_compress;
474
Chris Mason4adaa612013-03-26 13:07:00 -0400475 /*
476 * we need to call clear_page_dirty_for_io on each
477 * page in the range. Otherwise applications with the file
478 * mmap'd can wander in and change the page contents while
479 * we are compressing them.
480 *
481 * If the compression fails for any reason, we set the pages
482 * dirty again later on.
483 */
484 extent_range_clear_dirty_for_io(inode, start, end);
485 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800486 ret = btrfs_compress_pages(compress_type,
487 inode->i_mapping, start,
488 total_compressed, pages,
489 nr_pages, &nr_pages_ret,
490 &total_in,
491 &total_compressed,
492 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400493
494 if (!ret) {
495 unsigned long offset = total_compressed &
496 (PAGE_CACHE_SIZE - 1);
497 struct page *page = pages[nr_pages_ret - 1];
498 char *kaddr;
499
500 /* zero the tail end of the last page, we might be
501 * sending it down to disk
502 */
503 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800504 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400505 memset(kaddr + offset, 0,
506 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800507 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400508 }
509 will_compress = 1;
510 }
511 }
Li Zefan560f7d72011-09-08 10:22:01 +0800512cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400513 if (start == 0) {
514 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500515 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400516 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500517 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400518 */
Josef Bacik00361582013-08-14 14:02:47 -0400519 ret = cow_file_range_inline(root, inode, start, end,
520 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400521 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500522 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400523 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000524 total_compressed,
525 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400526 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100527 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400528 unsigned long clear_flags = EXTENT_DELALLOC |
529 EXTENT_DEFRAG;
530 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
531
Chris Mason771ed682008-11-06 22:02:51 -0500532 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100533 * inline extent creation worked or returned error,
534 * we don't need to create any more async work items.
535 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500536 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400537 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400538 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400539 PAGE_CLEAR_DIRTY |
540 PAGE_SET_WRITEBACK |
541 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400542 goto free_pages_out;
543 }
544 }
545
546 if (will_compress) {
547 /*
548 * we aren't doing an inline extent round the compressed size
549 * up to a block size boundary so the allocator does sane
550 * things
551 */
Qu Wenruofda28322013-02-26 08:10:22 +0000552 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400553
554 /*
555 * one last check to make sure the compression is really a
556 * win, compare the page count read with the blocks on disk
557 */
Qu Wenruofda28322013-02-26 08:10:22 +0000558 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400559 if (total_compressed >= total_in) {
560 will_compress = 0;
561 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400562 num_bytes = total_in;
563 }
564 }
565 if (!will_compress && pages) {
566 /*
567 * the compression code ran but failed to make things smaller,
568 * free any pages it allocated and our page pointer array
569 */
570 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400571 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400572 page_cache_release(pages[i]);
573 }
574 kfree(pages);
575 pages = NULL;
576 total_compressed = 0;
577 nr_pages_ret = 0;
578
579 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500580 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
581 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500582 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500583 }
Chris Masonc8b97812008-10-29 14:49:59 -0400584 }
Chris Mason771ed682008-11-06 22:02:51 -0500585 if (will_compress) {
586 *num_added += 1;
587
588 /* the async work queues will take care of doing actual
589 * allocation on disk for these compressed pages,
590 * and will submit them to the elevator.
591 */
592 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800593 total_compressed, pages, nr_pages_ret,
594 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500595
Yan, Zheng24ae6362010-12-06 07:02:36 +0000596 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500597 start += num_bytes;
598 pages = NULL;
599 cond_resched();
600 goto again;
601 }
602 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500603cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500604 /*
605 * No compression, but we still need to write the pages in
606 * the file we've been given so far. redirty the locked
607 * page if it corresponds to our extent and set things up
608 * for the async work queue to run cow_file_range to do
609 * the normal delalloc dance
610 */
611 if (page_offset(locked_page) >= start &&
612 page_offset(locked_page) <= end) {
613 __set_page_dirty_nobuffers(locked_page);
614 /* unlocked later on in the async handlers */
615 }
Chris Mason4adaa612013-03-26 13:07:00 -0400616 if (redirty)
617 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800618 add_async_extent(async_cow, start, end - start + 1,
619 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500620 *num_added += 1;
621 }
622
623out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100624 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500625
626free_pages_out:
627 for (i = 0; i < nr_pages_ret; i++) {
628 WARN_ON(pages[i]->mapping);
629 page_cache_release(pages[i]);
630 }
Chris Masond3977122009-01-05 21:25:51 -0500631 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500632
633 goto out;
634}
635
636/*
637 * phase two of compressed writeback. This is the ordered portion
638 * of the code, which only gets called in the order the work was
639 * queued. We walk all the async extents created by compress_file_range
640 * and send them down to the disk.
641 */
642static noinline int submit_compressed_extents(struct inode *inode,
643 struct async_cow *async_cow)
644{
645 struct async_extent *async_extent;
646 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500647 struct btrfs_key ins;
648 struct extent_map *em;
649 struct btrfs_root *root = BTRFS_I(inode)->root;
650 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
651 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500652 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500653
654 if (list_empty(&async_cow->extents))
655 return 0;
656
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500657again:
Chris Masond3977122009-01-05 21:25:51 -0500658 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500659 async_extent = list_entry(async_cow->extents.next,
660 struct async_extent, list);
661 list_del(&async_extent->list);
662
663 io_tree = &BTRFS_I(inode)->io_tree;
664
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500665retry:
Chris Mason771ed682008-11-06 22:02:51 -0500666 /* did the compression code fall back to uncompressed IO? */
667 if (!async_extent->pages) {
668 int page_started = 0;
669 unsigned long nr_written = 0;
670
671 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000672 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100673 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500674
675 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500676 ret = cow_file_range(inode, async_cow->locked_page,
677 async_extent->start,
678 async_extent->start +
679 async_extent->ram_size - 1,
680 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500681
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100682 /* JDM XXX */
683
Chris Mason771ed682008-11-06 22:02:51 -0500684 /*
685 * if page_started, cow_file_range inserted an
686 * inline extent and took care of all the unlocking
687 * and IO for us. Otherwise, we need to submit
688 * all those pages down to the drive.
689 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500690 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500691 extent_write_locked_range(io_tree,
692 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500693 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500694 async_extent->ram_size - 1,
695 btrfs_get_extent,
696 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500697 else if (ret)
698 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500699 kfree(async_extent);
700 cond_resched();
701 continue;
702 }
703
704 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100705 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500706
Josef Bacik00361582013-08-14 14:02:47 -0400707 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500708 async_extent->compressed_size,
709 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800710 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500711 if (ret) {
712 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500713
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500714 for (i = 0; i < async_extent->nr_pages; i++) {
715 WARN_ON(async_extent->pages[i]->mapping);
716 page_cache_release(async_extent->pages[i]);
717 }
718 kfree(async_extent->pages);
719 async_extent->nr_pages = 0;
720 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500721
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400722 if (ret == -ENOSPC) {
723 unlock_extent(io_tree, async_extent->start,
724 async_extent->start +
725 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800726
727 /*
728 * we need to redirty the pages if we decide to
729 * fallback to uncompressed IO, otherwise we
730 * will not submit these pages down to lower
731 * layers.
732 */
733 extent_range_redirty_for_io(inode,
734 async_extent->start,
735 async_extent->start +
736 async_extent->ram_size - 1);
737
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100738 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400739 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500740 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500741 }
742
Yan, Zhengc2167752009-11-12 09:34:21 +0000743 /*
744 * here we're doing allocation and writeback of the
745 * compressed pages
746 */
747 btrfs_drop_extent_cache(inode, async_extent->start,
748 async_extent->start +
749 async_extent->ram_size - 1, 0);
750
David Sterba172ddd62011-04-21 00:48:27 +0200751 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000752 if (!em) {
753 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500754 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000755 }
Chris Mason771ed682008-11-06 22:02:51 -0500756 em->start = async_extent->start;
757 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500758 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400759 em->mod_start = em->start;
760 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500761
762 em->block_start = ins.objectid;
763 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500764 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400765 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500766 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800767 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500768 set_bit(EXTENT_FLAG_PINNED, &em->flags);
769 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400770 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500771
Chris Masond3977122009-01-05 21:25:51 -0500772 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400773 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400774 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400775 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500776 if (ret != -EEXIST) {
777 free_extent_map(em);
778 break;
779 }
780 btrfs_drop_extent_cache(inode, async_extent->start,
781 async_extent->start +
782 async_extent->ram_size - 1, 0);
783 }
784
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500785 if (ret)
786 goto out_free_reserve;
787
Li Zefan261507a02010-12-17 14:21:50 +0800788 ret = btrfs_add_ordered_extent_compress(inode,
789 async_extent->start,
790 ins.objectid,
791 async_extent->ram_size,
792 ins.offset,
793 BTRFS_ORDERED_COMPRESSED,
794 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100795 if (ret) {
796 btrfs_drop_extent_cache(inode, async_extent->start,
797 async_extent->start +
798 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500799 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100800 }
Chris Mason771ed682008-11-06 22:02:51 -0500801
Chris Mason771ed682008-11-06 22:02:51 -0500802 /*
803 * clear dirty, set writeback and unlock the pages.
804 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400805 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400806 async_extent->start +
807 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400808 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
809 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400810 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500811 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500812 async_extent->start,
813 async_extent->ram_size,
814 ins.objectid,
815 ins.offset, async_extent->pages,
816 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500817 alloc_hint = ins.objectid + ins.offset;
818 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500819 if (ret)
820 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500821 cond_resched();
822 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100823 ret = 0;
824out:
825 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500826out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800827 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100828out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400829 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500830 async_extent->start +
831 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400832 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400833 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
834 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
835 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100836 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500837 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500838}
839
Josef Bacik4b46fce2010-05-23 11:00:55 -0400840static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
841 u64 num_bytes)
842{
843 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
844 struct extent_map *em;
845 u64 alloc_hint = 0;
846
847 read_lock(&em_tree->lock);
848 em = search_extent_mapping(em_tree, start, num_bytes);
849 if (em) {
850 /*
851 * if block start isn't an actual block number then find the
852 * first block in this inode and use that as a hint. If that
853 * block is also bogus then just don't worry about it.
854 */
855 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
856 free_extent_map(em);
857 em = search_extent_mapping(em_tree, 0, 0);
858 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
859 alloc_hint = em->block_start;
860 if (em)
861 free_extent_map(em);
862 } else {
863 alloc_hint = em->block_start;
864 free_extent_map(em);
865 }
866 }
867 read_unlock(&em_tree->lock);
868
869 return alloc_hint;
870}
871
Chris Mason771ed682008-11-06 22:02:51 -0500872/*
873 * when extent_io.c finds a delayed allocation range in the file,
874 * the call backs end up in this code. The basic idea is to
875 * allocate extents on disk for the range, and create ordered data structs
876 * in ram to track those extents.
877 *
878 * locked_page is the page that writepage had locked already. We use
879 * it to make sure we don't do extra locks or unlocks.
880 *
881 * *page_started is set to one if we unlock locked_page and do everything
882 * required to start IO on it. It may be clean and already done with
883 * IO when we return.
884 */
Josef Bacik00361582013-08-14 14:02:47 -0400885static noinline int cow_file_range(struct inode *inode,
886 struct page *locked_page,
887 u64 start, u64 end, int *page_started,
888 unsigned long *nr_written,
889 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500890{
Josef Bacik00361582013-08-14 14:02:47 -0400891 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500892 u64 alloc_hint = 0;
893 u64 num_bytes;
894 unsigned long ram_size;
895 u64 disk_num_bytes;
896 u64 cur_alloc_size;
897 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500898 struct btrfs_key ins;
899 struct extent_map *em;
900 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
901 int ret = 0;
902
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400903 if (btrfs_is_free_space_inode(inode)) {
904 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500905 ret = -EINVAL;
906 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400907 }
Chris Mason771ed682008-11-06 22:02:51 -0500908
Qu Wenruofda28322013-02-26 08:10:22 +0000909 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500910 num_bytes = max(blocksize, num_bytes);
911 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500912
Chris Mason4cb53002011-05-24 15:35:30 -0400913 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400914 if (num_bytes < 64 * 1024 &&
915 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400916 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400917
Chris Mason771ed682008-11-06 22:02:51 -0500918 if (start == 0) {
919 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400920 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
921 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500922 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400923 extent_clear_unlock_delalloc(inode, start, end, NULL,
924 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400925 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400926 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
927 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000928
Chris Mason771ed682008-11-06 22:02:51 -0500929 *nr_written = *nr_written +
930 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
931 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500932 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100933 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100934 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500935 }
936 }
Chris Masonc8b97812008-10-29 14:49:59 -0400937
938 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200939 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400940
Josef Bacik4b46fce2010-05-23 11:00:55 -0400941 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400942 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400943
Chris Masond3977122009-01-05 21:25:51 -0500944 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400945 unsigned long op;
946
Josef Bacik287a0ab2010-03-19 18:07:23 +0000947 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400948 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500949 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800950 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400951 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100952 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500953
David Sterba172ddd62011-04-21 00:48:27 +0200954 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000955 if (!em) {
956 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000957 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000958 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400959 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500960 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500961 ram_size = ins.offset;
962 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400963 em->mod_start = em->start;
964 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400965
Chris Masone6dcd2d2008-07-17 12:53:50 -0400966 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400967 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500968 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400969 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400970 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400971 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400972 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400973
Chris Masond3977122009-01-05 21:25:51 -0500974 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400975 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400976 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400977 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400978 if (ret != -EEXIST) {
979 free_extent_map(em);
980 break;
981 }
982 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400983 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400984 }
Liu Boace68ba2013-04-22 10:53:47 +0000985 if (ret)
986 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400987
Chris Mason98d20f62008-04-14 09:46:10 -0400988 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400989 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500990 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000991 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +0100992 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -0400993
Yan Zheng17d217f2008-12-12 10:03:38 -0500994 if (root->root_key.objectid ==
995 BTRFS_DATA_RELOC_TREE_OBJECTID) {
996 ret = btrfs_reloc_clone_csums(inode, start,
997 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -0400998 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +0100999 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001000 }
1001
Chris Masond3977122009-01-05 21:25:51 -05001002 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001003 break;
Chris Masond3977122009-01-05 21:25:51 -05001004
Chris Masonc8b97812008-10-29 14:49:59 -04001005 /* we're not doing compressed IO, don't unlock the first
1006 * page (which the caller expects to stay locked), don't
1007 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001008 *
1009 * Do set the Private2 bit so we know this page was properly
1010 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001011 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001012 op = unlock ? PAGE_UNLOCK : 0;
1013 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001014
Josef Bacikc2790a22013-07-29 11:20:47 -04001015 extent_clear_unlock_delalloc(inode, start,
1016 start + ram_size - 1, locked_page,
1017 EXTENT_LOCKED | EXTENT_DELALLOC,
1018 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001019 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001020 num_bytes -= cur_alloc_size;
1021 alloc_hint = ins.objectid + ins.offset;
1022 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001023 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001024out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001025 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001026
Filipe Mananad9f85962014-08-25 10:43:00 +01001027out_drop_extent_cache:
1028 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001029out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001030 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001031out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001032 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001033 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1034 EXTENT_DELALLOC | EXTENT_DEFRAG,
1035 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1036 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001037 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001038}
Chris Masonc8b97812008-10-29 14:49:59 -04001039
Chris Mason771ed682008-11-06 22:02:51 -05001040/*
1041 * work queue call back to started compression on a file and pages
1042 */
1043static noinline void async_cow_start(struct btrfs_work *work)
1044{
1045 struct async_cow *async_cow;
1046 int num_added = 0;
1047 async_cow = container_of(work, struct async_cow, work);
1048
1049 compress_file_range(async_cow->inode, async_cow->locked_page,
1050 async_cow->start, async_cow->end, async_cow,
1051 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001052 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001053 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001054 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001055 }
Chris Mason771ed682008-11-06 22:02:51 -05001056}
1057
1058/*
1059 * work queue call back to submit previously compressed pages
1060 */
1061static noinline void async_cow_submit(struct btrfs_work *work)
1062{
1063 struct async_cow *async_cow;
1064 struct btrfs_root *root;
1065 unsigned long nr_pages;
1066
1067 async_cow = container_of(work, struct async_cow, work);
1068
1069 root = async_cow->root;
1070 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1071 PAGE_CACHE_SHIFT;
1072
Josef Bacik66657b32012-08-01 15:36:24 -04001073 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001074 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001075 waitqueue_active(&root->fs_info->async_submit_wait))
1076 wake_up(&root->fs_info->async_submit_wait);
1077
Chris Masond3977122009-01-05 21:25:51 -05001078 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001079 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001080}
Chris Masonc8b97812008-10-29 14:49:59 -04001081
Chris Mason771ed682008-11-06 22:02:51 -05001082static noinline void async_cow_free(struct btrfs_work *work)
1083{
1084 struct async_cow *async_cow;
1085 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001086 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001087 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001088 kfree(async_cow);
1089}
1090
1091static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1092 u64 start, u64 end, int *page_started,
1093 unsigned long *nr_written)
1094{
1095 struct async_cow *async_cow;
1096 struct btrfs_root *root = BTRFS_I(inode)->root;
1097 unsigned long nr_pages;
1098 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001099 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001100
Chris Masona3429ab2009-10-08 12:30:20 -04001101 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1102 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001103 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001104 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001105 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001106 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001107 async_cow->root = root;
1108 async_cow->locked_page = locked_page;
1109 async_cow->start = start;
1110
Wang Shilongf79707b2014-07-17 11:44:09 +08001111 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1112 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001113 cur_end = end;
1114 else
1115 cur_end = min(end, start + 512 * 1024 - 1);
1116
1117 async_cow->end = cur_end;
1118 INIT_LIST_HEAD(&async_cow->extents);
1119
Liu Bo9e0af232014-08-15 23:36:53 +08001120 btrfs_init_work(&async_cow->work,
1121 btrfs_delalloc_helper,
1122 async_cow_start, async_cow_submit,
1123 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001124
Chris Mason771ed682008-11-06 22:02:51 -05001125 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1126 PAGE_CACHE_SHIFT;
1127 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1128
Qu Wenruoafe3d242014-02-28 10:46:07 +08001129 btrfs_queue_work(root->fs_info->delalloc_workers,
1130 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001131
1132 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1133 wait_event(root->fs_info->async_submit_wait,
1134 (atomic_read(&root->fs_info->async_delalloc_pages) <
1135 limit));
1136 }
1137
Chris Masond3977122009-01-05 21:25:51 -05001138 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001139 atomic_read(&root->fs_info->async_delalloc_pages)) {
1140 wait_event(root->fs_info->async_submit_wait,
1141 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1142 0));
1143 }
1144
1145 *nr_written += nr_pages;
1146 start = cur_end + 1;
1147 }
1148 *page_started = 1;
1149 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001150}
1151
Chris Masond3977122009-01-05 21:25:51 -05001152static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001153 u64 bytenr, u64 num_bytes)
1154{
1155 int ret;
1156 struct btrfs_ordered_sum *sums;
1157 LIST_HEAD(list);
1158
Yan Zheng07d400a2009-01-06 11:42:00 -05001159 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001160 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001161 if (ret == 0 && list_empty(&list))
1162 return 0;
1163
1164 while (!list_empty(&list)) {
1165 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1166 list_del(&sums->list);
1167 kfree(sums);
1168 }
1169 return 1;
1170}
1171
Chris Masond352ac62008-09-29 15:18:18 -04001172/*
1173 * when nowcow writeback call back. This checks for snapshots or COW copies
1174 * of the extents that exist in the file, and COWs the file as required.
1175 *
1176 * If no cow copies or snapshots exist, we write directly to the existing
1177 * blocks on disk
1178 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001179static noinline int run_delalloc_nocow(struct inode *inode,
1180 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001181 u64 start, u64 end, int *page_started, int force,
1182 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001183{
Chris Masonbe20aa92007-12-17 20:14:01 -05001184 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001185 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001186 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001187 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001188 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001189 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001190 u64 cow_start;
1191 u64 cur_offset;
1192 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001193 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001194 u64 disk_bytenr;
1195 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001196 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001197 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001198 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001199 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001200 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001201 int nocow;
1202 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001203 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001204 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001205
1206 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001207 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001208 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1209 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001210 EXTENT_DO_ACCOUNTING |
1211 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001212 PAGE_CLEAR_DIRTY |
1213 PAGE_SET_WRITEBACK |
1214 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001215 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001216 }
Li Zefan82d59022011-04-20 10:33:24 +08001217
Liu Bo83eea1f2012-07-10 05:28:39 -06001218 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001219
1220 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001221 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001222 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001223 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001224
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001225 if (IS_ERR(trans)) {
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);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001233 btrfs_free_path(path);
1234 return PTR_ERR(trans);
1235 }
1236
Josef Bacik74b21072011-04-13 12:02:53 -04001237 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001238
Yan Zheng80ff3852008-10-30 14:20:02 -04001239 cow_start = (u64)-1;
1240 cur_offset = start;
1241 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001242 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001243 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001244 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001245 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001246 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1247 leaf = path->nodes[0];
1248 btrfs_item_key_to_cpu(leaf, &found_key,
1249 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001250 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 found_key.type == BTRFS_EXTENT_DATA_KEY)
1252 path->slots[0]--;
1253 }
1254 check_prev = 0;
1255next_slot:
1256 leaf = path->nodes[0];
1257 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1258 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001259 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001260 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 if (ret > 0)
1262 break;
1263 leaf = path->nodes[0];
1264 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001265
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 nocow = 0;
1267 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001268 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001270
Li Zefan33345d012011-04-20 10:31:50 +08001271 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1273 found_key.offset > end)
1274 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001275
Yan Zheng80ff3852008-10-30 14:20:02 -04001276 if (found_key.offset > cur_offset) {
1277 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001278 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 goto out_check;
1280 }
Chris Masonc31f8832008-01-08 15:46:31 -05001281
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 fi = btrfs_item_ptr(leaf, path->slots[0],
1283 struct btrfs_file_extent_item);
1284 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001285
Josef Bacikcc95bef2013-04-04 14:31:27 -04001286 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001287 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1288 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001290 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001291 extent_end = found_key.offset +
1292 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001293 disk_num_bytes =
1294 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 if (extent_end <= start) {
1296 path->slots[0]++;
1297 goto next_slot;
1298 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001299 if (disk_bytenr == 0)
1300 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 if (btrfs_file_extent_compression(leaf, fi) ||
1302 btrfs_file_extent_encryption(leaf, fi) ||
1303 btrfs_file_extent_other_encoding(leaf, fi))
1304 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001305 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1306 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001307 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001309 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001310 found_key.offset -
1311 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001312 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001313 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001314 disk_bytenr += cur_offset - found_key.offset;
1315 num_bytes = min(end + 1, extent_end) - cur_offset;
1316 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001317 * if there are pending snapshots for this root,
1318 * we fall into common COW way.
1319 */
1320 if (!nolock) {
1321 err = btrfs_start_nocow_write(root);
1322 if (!err)
1323 goto out_check;
1324 }
1325 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001326 * force cow if csum exists in the range.
1327 * this ensure that csum for a given extent are
1328 * either valid or do not exist.
1329 */
1330 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1331 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001332 nocow = 1;
1333 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1334 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001335 btrfs_file_extent_inline_len(leaf,
1336 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001337 extent_end = ALIGN(extent_end, root->sectorsize);
1338 } else {
1339 BUG_ON(1);
1340 }
1341out_check:
1342 if (extent_end <= start) {
1343 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001344 if (!nolock && nocow)
1345 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001346 goto next_slot;
1347 }
1348 if (!nocow) {
1349 if (cow_start == (u64)-1)
1350 cow_start = cur_offset;
1351 cur_offset = extent_end;
1352 if (cur_offset > end)
1353 break;
1354 path->slots[0]++;
1355 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001356 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001357
David Sterbab3b4aa72011-04-21 01:20:15 +02001358 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001360 ret = cow_file_range(inode, locked_page,
1361 cow_start, found_key.offset - 1,
1362 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001363 if (ret) {
1364 if (!nolock && nocow)
1365 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001366 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001367 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001368 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001369 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001370
Yan Zhengd899e052008-10-30 14:25:28 -04001371 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1372 struct extent_map *em;
1373 struct extent_map_tree *em_tree;
1374 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001375 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001376 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001377 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001378 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001379 em->len = num_bytes;
1380 em->block_len = num_bytes;
1381 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001382 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001383 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001384 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001385 em->mod_start = em->start;
1386 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001387 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001388 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001389 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001390 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001391 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001392 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001393 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001394 if (ret != -EEXIST) {
1395 free_extent_map(em);
1396 break;
1397 }
1398 btrfs_drop_extent_cache(inode, em->start,
1399 em->start + em->len - 1, 0);
1400 }
1401 type = BTRFS_ORDERED_PREALLOC;
1402 } else {
1403 type = BTRFS_ORDERED_NOCOW;
1404 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001405
1406 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001407 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001408 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001409
Yan, Zhengefa56462010-05-16 10:49:59 -04001410 if (root->root_key.objectid ==
1411 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1412 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1413 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001414 if (ret) {
1415 if (!nolock && nocow)
1416 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001417 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001418 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001419 }
1420
Josef Bacikc2790a22013-07-29 11:20:47 -04001421 extent_clear_unlock_delalloc(inode, cur_offset,
1422 cur_offset + num_bytes - 1,
1423 locked_page, EXTENT_LOCKED |
1424 EXTENT_DELALLOC, PAGE_UNLOCK |
1425 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001426 if (!nolock && nocow)
1427 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001428 cur_offset = extent_end;
1429 if (cur_offset > end)
1430 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001431 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001432 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001433
Josef Bacik17ca04a2012-05-31 15:58:55 -04001434 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001435 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001436 cur_offset = end;
1437 }
1438
Yan Zheng80ff3852008-10-30 14:20:02 -04001439 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001440 ret = cow_file_range(inode, locked_page, cow_start, end,
1441 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001442 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001443 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001444 }
1445
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001446error:
Miao Xiea698d0752012-09-20 01:51:59 -06001447 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001448 if (!ret)
1449 ret = err;
1450
Josef Bacik17ca04a2012-05-31 15:58:55 -04001451 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001452 extent_clear_unlock_delalloc(inode, cur_offset, end,
1453 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001454 EXTENT_DELALLOC | EXTENT_DEFRAG |
1455 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1456 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001457 PAGE_SET_WRITEBACK |
1458 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001459 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001460 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001461}
1462
Wang Shilong47059d92014-07-03 18:22:07 +08001463static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1464{
1465
1466 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1467 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1468 return 0;
1469
1470 /*
1471 * @defrag_bytes is a hint value, no spinlock held here,
1472 * if is not zero, it means the file is defragging.
1473 * Force cow if given extent needs to be defragged.
1474 */
1475 if (BTRFS_I(inode)->defrag_bytes &&
1476 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1477 EXTENT_DEFRAG, 0, NULL))
1478 return 1;
1479
1480 return 0;
1481}
1482
Chris Masond352ac62008-09-29 15:18:18 -04001483/*
1484 * extent_io.c call back to do delayed allocation processing
1485 */
Chris Masonc8b97812008-10-29 14:49:59 -04001486static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001487 u64 start, u64 end, int *page_started,
1488 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001489{
Chris Masonbe20aa92007-12-17 20:14:01 -05001490 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001491 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001492
Wang Shilong47059d92014-07-03 18:22:07 +08001493 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001494 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001495 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001496 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001497 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001498 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001499 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001500 ret = cow_file_range(inode, locked_page, start, end,
1501 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001502 } else {
1503 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1504 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001505 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001506 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001507 }
Chris Masonb888db22007-08-27 16:49:44 -04001508 return ret;
1509}
1510
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001511static void btrfs_split_extent_hook(struct inode *inode,
1512 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001513{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001514 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001515 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001516 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001517
Josef Bacik9e0baf62011-07-15 15:16:44 +00001518 spin_lock(&BTRFS_I(inode)->lock);
1519 BTRFS_I(inode)->outstanding_extents++;
1520 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001521}
1522
1523/*
1524 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1525 * extents so we can keep track of new extents that are just merged onto old
1526 * extents, such as when we are doing sequential writes, so we can properly
1527 * account for the metadata space we'll need.
1528 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001529static void btrfs_merge_extent_hook(struct inode *inode,
1530 struct extent_state *new,
1531 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001532{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001533 /* not delalloc, ignore it */
1534 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001535 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001536
Josef Bacik9e0baf62011-07-15 15:16:44 +00001537 spin_lock(&BTRFS_I(inode)->lock);
1538 BTRFS_I(inode)->outstanding_extents--;
1539 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001540}
1541
Miao Xieeb73c1b2013-05-15 07:48:22 +00001542static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1543 struct inode *inode)
1544{
1545 spin_lock(&root->delalloc_lock);
1546 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1547 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1548 &root->delalloc_inodes);
1549 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1550 &BTRFS_I(inode)->runtime_flags);
1551 root->nr_delalloc_inodes++;
1552 if (root->nr_delalloc_inodes == 1) {
1553 spin_lock(&root->fs_info->delalloc_root_lock);
1554 BUG_ON(!list_empty(&root->delalloc_root));
1555 list_add_tail(&root->delalloc_root,
1556 &root->fs_info->delalloc_roots);
1557 spin_unlock(&root->fs_info->delalloc_root_lock);
1558 }
1559 }
1560 spin_unlock(&root->delalloc_lock);
1561}
1562
1563static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1564 struct inode *inode)
1565{
1566 spin_lock(&root->delalloc_lock);
1567 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1568 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1569 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1570 &BTRFS_I(inode)->runtime_flags);
1571 root->nr_delalloc_inodes--;
1572 if (!root->nr_delalloc_inodes) {
1573 spin_lock(&root->fs_info->delalloc_root_lock);
1574 BUG_ON(list_empty(&root->delalloc_root));
1575 list_del_init(&root->delalloc_root);
1576 spin_unlock(&root->fs_info->delalloc_root_lock);
1577 }
1578 }
1579 spin_unlock(&root->delalloc_lock);
1580}
1581
Chris Masond352ac62008-09-29 15:18:18 -04001582/*
1583 * extent_io.c set_bit_hook, used to track delayed allocation
1584 * bytes in this file, and to maintain the list of inodes that
1585 * have pending delalloc work to be done.
1586 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001587static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001588 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001589{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001590
Wang Shilong47059d92014-07-03 18:22:07 +08001591 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1592 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001593 /*
1594 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001595 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001596 * bit, which is only set or cleared with irqs on
1597 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001598 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001599 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001600 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001601 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001602
Josef Bacik9e0baf62011-07-15 15:16:44 +00001603 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001604 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001605 } else {
1606 spin_lock(&BTRFS_I(inode)->lock);
1607 BTRFS_I(inode)->outstanding_extents++;
1608 spin_unlock(&BTRFS_I(inode)->lock);
1609 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001610
Miao Xie963d6782013-01-29 10:10:51 +00001611 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1612 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001613 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001614 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001615 if (*bits & EXTENT_DEFRAG)
1616 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001617 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001618 &BTRFS_I(inode)->runtime_flags))
1619 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001620 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001621 }
Chris Mason291d6732008-01-29 15:55:23 -05001622}
1623
Chris Masond352ac62008-09-29 15:18:18 -04001624/*
1625 * extent_io.c clear_bit_hook, see set_bit_hook for why
1626 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001627static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001628 struct extent_state *state,
1629 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001630{
Wang Shilong47059d92014-07-03 18:22:07 +08001631 u64 len = state->end + 1 - state->start;
1632
1633 spin_lock(&BTRFS_I(inode)->lock);
1634 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1635 BTRFS_I(inode)->defrag_bytes -= len;
1636 spin_unlock(&BTRFS_I(inode)->lock);
1637
Chris Mason75eff682008-12-15 15:54:40 -05001638 /*
1639 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001640 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001641 * bit, which is only set or cleared with irqs on
1642 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001643 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001644 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001645 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001646
Josef Bacik9e0baf62011-07-15 15:16:44 +00001647 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001648 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001649 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1650 spin_lock(&BTRFS_I(inode)->lock);
1651 BTRFS_I(inode)->outstanding_extents--;
1652 spin_unlock(&BTRFS_I(inode)->lock);
1653 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001654
Josef Bacikb6d08f02013-09-27 14:57:43 -04001655 /*
1656 * We don't reserve metadata space for space cache inodes so we
1657 * don't need to call dellalloc_release_metadata if there is an
1658 * error.
1659 */
1660 if (*bits & EXTENT_DO_ACCOUNTING &&
1661 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001662 btrfs_delalloc_release_metadata(inode, len);
1663
Josef Bacik0cb59c92010-07-02 12:14:14 -04001664 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001665 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001666 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001667
Miao Xie963d6782013-01-29 10:10:51 +00001668 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1669 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001670 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001671 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001672 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001673 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001674 &BTRFS_I(inode)->runtime_flags))
1675 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001676 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001677 }
Chris Mason291d6732008-01-29 15:55:23 -05001678}
1679
Chris Masond352ac62008-09-29 15:18:18 -04001680/*
1681 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1682 * we don't create bios that span stripes or chunks
1683 */
David Woodhouse64a16702009-07-15 23:29:37 +01001684int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001685 size_t size, struct bio *bio,
1686 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001687{
1688 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001689 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001690 u64 length = 0;
1691 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001692 int ret;
1693
Chris Mason771ed682008-11-06 22:02:51 -05001694 if (bio_flags & EXTENT_BIO_COMPRESSED)
1695 return 0;
1696
Kent Overstreet4f024f32013-10-11 15:44:27 -07001697 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001698 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001699 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001700 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001701 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001702 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001703 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001704 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001705 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001706}
1707
Chris Masond352ac62008-09-29 15:18:18 -04001708/*
1709 * in order to insert checksums into the metadata in large chunks,
1710 * we wait until bio submission time. All the pages in the bio are
1711 * checksummed and sums are attached onto the ordered extent record.
1712 *
1713 * At IO completion time the cums attached on the ordered extent record
1714 * are inserted into the btree
1715 */
Chris Masond3977122009-01-05 21:25:51 -05001716static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1717 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001718 unsigned long bio_flags,
1719 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001720{
Chris Mason065631f2008-02-20 12:07:25 -05001721 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001722 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001723
Chris Masond20f7042008-12-08 16:58:54 -05001724 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001725 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001726 return 0;
1727}
Chris Masone0156402008-04-16 11:15:20 -04001728
Chris Mason4a69a412008-11-06 22:03:00 -05001729/*
1730 * in order to insert checksums into the metadata in large chunks,
1731 * we wait until bio submission time. All the pages in the bio are
1732 * checksummed and sums are attached onto the ordered extent record.
1733 *
1734 * At IO completion time the cums attached on the ordered extent record
1735 * are inserted into the btree
1736 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001737static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001738 int mirror_num, unsigned long bio_flags,
1739 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001740{
1741 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001742 int ret;
1743
1744 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1745 if (ret)
1746 bio_endio(bio, ret);
1747 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001748}
1749
Chris Masond352ac62008-09-29 15:18:18 -04001750/*
Chris Masoncad321a2008-12-17 14:51:42 -05001751 * extent_io.c submission hook. This does the right thing for csum calculation
1752 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001753 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001754static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001755 int mirror_num, unsigned long bio_flags,
1756 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001757{
1758 struct btrfs_root *root = BTRFS_I(inode)->root;
1759 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001760 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001761 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001762 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001763
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001764 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001765
Liu Bo83eea1f2012-07-10 05:28:39 -06001766 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001767 metadata = 2;
1768
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001769 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001770 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1771 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001772 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001773
Chris Masond20f7042008-12-08 16:58:54 -05001774 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001775 ret = btrfs_submit_compressed_read(inode, bio,
1776 mirror_num,
1777 bio_flags);
1778 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001779 } else if (!skip_sum) {
1780 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1781 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001782 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001783 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001784 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001785 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001786 /* csum items have already been cloned */
1787 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1788 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001789 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001790 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001791 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001792 bio_flags, bio_offset,
1793 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001794 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001795 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001796 } else if (!skip_sum) {
1797 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1798 if (ret)
1799 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001800 }
1801
Chris Mason0b86a832008-03-24 15:01:56 -04001802mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001803 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1804
1805out:
1806 if (ret < 0)
1807 bio_endio(bio, ret);
1808 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001809}
Chris Mason6885f302008-02-20 16:11:05 -05001810
Chris Masond352ac62008-09-29 15:18:18 -04001811/*
1812 * given a list of ordered sums record them in the inode. This happens
1813 * at IO completion time based on sums calculated at bio submission time.
1814 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001815static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001816 struct inode *inode, u64 file_offset,
1817 struct list_head *list)
1818{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001819 struct btrfs_ordered_sum *sum;
1820
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001821 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001822 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001823 btrfs_csum_file_blocks(trans,
1824 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001825 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001826 }
1827 return 0;
1828}
1829
Josef Bacik2ac55d42010-02-03 19:33:23 +00001830int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1831 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001832{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001833 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001834 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001835 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001836}
1837
Chris Masond352ac62008-09-29 15:18:18 -04001838/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001839struct btrfs_writepage_fixup {
1840 struct page *page;
1841 struct btrfs_work work;
1842};
1843
Christoph Hellwigb2950862008-12-02 09:54:17 -05001844static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001845{
1846 struct btrfs_writepage_fixup *fixup;
1847 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001848 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001849 struct page *page;
1850 struct inode *inode;
1851 u64 page_start;
1852 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001853 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001854
1855 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1856 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001857again:
Chris Mason247e7432008-07-17 12:53:51 -04001858 lock_page(page);
1859 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1860 ClearPageChecked(page);
1861 goto out_page;
1862 }
1863
1864 inode = page->mapping->host;
1865 page_start = page_offset(page);
1866 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1867
Josef Bacik2ac55d42010-02-03 19:33:23 +00001868 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001869 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001870
1871 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001872 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001873 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001874
1875 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1876 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001877 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1878 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001879 unlock_page(page);
1880 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001881 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001882 goto again;
1883 }
Chris Mason247e7432008-07-17 12:53:51 -04001884
Jeff Mahoney87826df2012-02-15 16:23:57 +01001885 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1886 if (ret) {
1887 mapping_set_error(page->mapping, ret);
1888 end_extent_writepage(page, ret, page_start, page_end);
1889 ClearPageChecked(page);
1890 goto out;
1891 }
1892
Josef Bacik2ac55d42010-02-03 19:33:23 +00001893 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001894 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001895 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001896out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001897 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1898 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001899out_page:
1900 unlock_page(page);
1901 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001902 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001903}
1904
1905/*
1906 * There are a few paths in the higher layers of the kernel that directly
1907 * set the page dirty bit without asking the filesystem if it is a
1908 * good idea. This causes problems because we want to make sure COW
1909 * properly happens and the data=ordered rules are followed.
1910 *
Chris Masonc8b97812008-10-29 14:49:59 -04001911 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001912 * hasn't been properly setup for IO. We kick off an async process
1913 * to fix it up. The async helper will wait for ordered extents, set
1914 * the delalloc bit and make it safe to write the page.
1915 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001916static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001917{
1918 struct inode *inode = page->mapping->host;
1919 struct btrfs_writepage_fixup *fixup;
1920 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001921
Chris Mason8b62b722009-09-02 16:53:46 -04001922 /* this page is properly in the ordered list */
1923 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001924 return 0;
1925
1926 if (PageChecked(page))
1927 return -EAGAIN;
1928
1929 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1930 if (!fixup)
1931 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001932
Chris Mason247e7432008-07-17 12:53:51 -04001933 SetPageChecked(page);
1934 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08001935 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
1936 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04001937 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08001938 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001939 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001940}
1941
Yan Zhengd899e052008-10-30 14:25:28 -04001942static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1943 struct inode *inode, u64 file_pos,
1944 u64 disk_bytenr, u64 disk_num_bytes,
1945 u64 num_bytes, u64 ram_bytes,
1946 u8 compression, u8 encryption,
1947 u16 other_encoding, int extent_type)
1948{
1949 struct btrfs_root *root = BTRFS_I(inode)->root;
1950 struct btrfs_file_extent_item *fi;
1951 struct btrfs_path *path;
1952 struct extent_buffer *leaf;
1953 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001954 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001955 int ret;
1956
1957 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001958 if (!path)
1959 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001960
Chris Masona1ed8352009-09-11 12:27:37 -04001961 /*
1962 * we may be replacing one extent in the tree with another.
1963 * The new extent is pinned in the extent map, and we don't want
1964 * to drop it from the cache until it is completely in the btree.
1965 *
1966 * So, tell btrfs_drop_extents to leave this extent in the cache.
1967 * the caller is expected to unpin it and allow it to be merged
1968 * with the others.
1969 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001970 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1971 file_pos + num_bytes, NULL, 0,
1972 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001973 if (ret)
1974 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001975
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001976 if (!extent_inserted) {
1977 ins.objectid = btrfs_ino(inode);
1978 ins.offset = file_pos;
1979 ins.type = BTRFS_EXTENT_DATA_KEY;
1980
1981 path->leave_spinning = 1;
1982 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1983 sizeof(*fi));
1984 if (ret)
1985 goto out;
1986 }
Yan Zhengd899e052008-10-30 14:25:28 -04001987 leaf = path->nodes[0];
1988 fi = btrfs_item_ptr(leaf, path->slots[0],
1989 struct btrfs_file_extent_item);
1990 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1991 btrfs_set_file_extent_type(leaf, fi, extent_type);
1992 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1993 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1994 btrfs_set_file_extent_offset(leaf, fi, 0);
1995 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1996 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1997 btrfs_set_file_extent_compression(leaf, fi, compression);
1998 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1999 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002000
Yan Zhengd899e052008-10-30 14:25:28 -04002001 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002002 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002003
2004 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002005
2006 ins.objectid = disk_bytenr;
2007 ins.offset = disk_num_bytes;
2008 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002009 ret = btrfs_alloc_reserved_file_extent(trans, root,
2010 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002011 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002012out:
Yan Zhengd899e052008-10-30 14:25:28 -04002013 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002014
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002015 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002016}
2017
Liu Bo38c227d2013-01-29 03:18:40 +00002018/* snapshot-aware defrag */
2019struct sa_defrag_extent_backref {
2020 struct rb_node node;
2021 struct old_sa_defrag_extent *old;
2022 u64 root_id;
2023 u64 inum;
2024 u64 file_pos;
2025 u64 extent_offset;
2026 u64 num_bytes;
2027 u64 generation;
2028};
2029
2030struct old_sa_defrag_extent {
2031 struct list_head list;
2032 struct new_sa_defrag_extent *new;
2033
2034 u64 extent_offset;
2035 u64 bytenr;
2036 u64 offset;
2037 u64 len;
2038 int count;
2039};
2040
2041struct new_sa_defrag_extent {
2042 struct rb_root root;
2043 struct list_head head;
2044 struct btrfs_path *path;
2045 struct inode *inode;
2046 u64 file_pos;
2047 u64 len;
2048 u64 bytenr;
2049 u64 disk_len;
2050 u8 compress_type;
2051};
2052
2053static int backref_comp(struct sa_defrag_extent_backref *b1,
2054 struct sa_defrag_extent_backref *b2)
2055{
2056 if (b1->root_id < b2->root_id)
2057 return -1;
2058 else if (b1->root_id > b2->root_id)
2059 return 1;
2060
2061 if (b1->inum < b2->inum)
2062 return -1;
2063 else if (b1->inum > b2->inum)
2064 return 1;
2065
2066 if (b1->file_pos < b2->file_pos)
2067 return -1;
2068 else if (b1->file_pos > b2->file_pos)
2069 return 1;
2070
2071 /*
2072 * [------------------------------] ===> (a range of space)
2073 * |<--->| |<---->| =============> (fs/file tree A)
2074 * |<---------------------------->| ===> (fs/file tree B)
2075 *
2076 * A range of space can refer to two file extents in one tree while
2077 * refer to only one file extent in another tree.
2078 *
2079 * So we may process a disk offset more than one time(two extents in A)
2080 * and locate at the same extent(one extent in B), then insert two same
2081 * backrefs(both refer to the extent in B).
2082 */
2083 return 0;
2084}
2085
2086static void backref_insert(struct rb_root *root,
2087 struct sa_defrag_extent_backref *backref)
2088{
2089 struct rb_node **p = &root->rb_node;
2090 struct rb_node *parent = NULL;
2091 struct sa_defrag_extent_backref *entry;
2092 int ret;
2093
2094 while (*p) {
2095 parent = *p;
2096 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2097
2098 ret = backref_comp(backref, entry);
2099 if (ret < 0)
2100 p = &(*p)->rb_left;
2101 else
2102 p = &(*p)->rb_right;
2103 }
2104
2105 rb_link_node(&backref->node, parent, p);
2106 rb_insert_color(&backref->node, root);
2107}
2108
2109/*
2110 * Note the backref might has changed, and in this case we just return 0.
2111 */
2112static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2113 void *ctx)
2114{
2115 struct btrfs_file_extent_item *extent;
2116 struct btrfs_fs_info *fs_info;
2117 struct old_sa_defrag_extent *old = ctx;
2118 struct new_sa_defrag_extent *new = old->new;
2119 struct btrfs_path *path = new->path;
2120 struct btrfs_key key;
2121 struct btrfs_root *root;
2122 struct sa_defrag_extent_backref *backref;
2123 struct extent_buffer *leaf;
2124 struct inode *inode = new->inode;
2125 int slot;
2126 int ret;
2127 u64 extent_offset;
2128 u64 num_bytes;
2129
2130 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2131 inum == btrfs_ino(inode))
2132 return 0;
2133
2134 key.objectid = root_id;
2135 key.type = BTRFS_ROOT_ITEM_KEY;
2136 key.offset = (u64)-1;
2137
2138 fs_info = BTRFS_I(inode)->root->fs_info;
2139 root = btrfs_read_fs_root_no_name(fs_info, &key);
2140 if (IS_ERR(root)) {
2141 if (PTR_ERR(root) == -ENOENT)
2142 return 0;
2143 WARN_ON(1);
2144 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2145 inum, offset, root_id);
2146 return PTR_ERR(root);
2147 }
2148
2149 key.objectid = inum;
2150 key.type = BTRFS_EXTENT_DATA_KEY;
2151 if (offset > (u64)-1 << 32)
2152 key.offset = 0;
2153 else
2154 key.offset = offset;
2155
2156 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302157 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002158 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002159 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002160
2161 while (1) {
2162 cond_resched();
2163
2164 leaf = path->nodes[0];
2165 slot = path->slots[0];
2166
2167 if (slot >= btrfs_header_nritems(leaf)) {
2168 ret = btrfs_next_leaf(root, path);
2169 if (ret < 0) {
2170 goto out;
2171 } else if (ret > 0) {
2172 ret = 0;
2173 goto out;
2174 }
2175 continue;
2176 }
2177
2178 path->slots[0]++;
2179
2180 btrfs_item_key_to_cpu(leaf, &key, slot);
2181
2182 if (key.objectid > inum)
2183 goto out;
2184
2185 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2186 continue;
2187
2188 extent = btrfs_item_ptr(leaf, slot,
2189 struct btrfs_file_extent_item);
2190
2191 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2192 continue;
2193
Liu Boe68afa42013-07-01 22:13:26 +08002194 /*
2195 * 'offset' refers to the exact key.offset,
2196 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2197 * (key.offset - extent_offset).
2198 */
2199 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002200 continue;
2201
Liu Boe68afa42013-07-01 22:13:26 +08002202 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002203 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002204
Liu Bo38c227d2013-01-29 03:18:40 +00002205 if (extent_offset >= old->extent_offset + old->offset +
2206 old->len || extent_offset + num_bytes <=
2207 old->extent_offset + old->offset)
2208 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002209 break;
2210 }
2211
2212 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2213 if (!backref) {
2214 ret = -ENOENT;
2215 goto out;
2216 }
2217
2218 backref->root_id = root_id;
2219 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002220 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002221 backref->num_bytes = num_bytes;
2222 backref->extent_offset = extent_offset;
2223 backref->generation = btrfs_file_extent_generation(leaf, extent);
2224 backref->old = old;
2225 backref_insert(&new->root, backref);
2226 old->count++;
2227out:
2228 btrfs_release_path(path);
2229 WARN_ON(ret);
2230 return ret;
2231}
2232
2233static noinline bool record_extent_backrefs(struct btrfs_path *path,
2234 struct new_sa_defrag_extent *new)
2235{
2236 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2237 struct old_sa_defrag_extent *old, *tmp;
2238 int ret;
2239
2240 new->path = path;
2241
2242 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002243 ret = iterate_inodes_from_logical(old->bytenr +
2244 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002245 path, record_one_backref,
2246 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002247 if (ret < 0 && ret != -ENOENT)
2248 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002249
2250 /* no backref to be processed for this extent */
2251 if (!old->count) {
2252 list_del(&old->list);
2253 kfree(old);
2254 }
2255 }
2256
2257 if (list_empty(&new->head))
2258 return false;
2259
2260 return true;
2261}
2262
2263static int relink_is_mergable(struct extent_buffer *leaf,
2264 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002265 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002266{
Liu Bo116e0022013-08-02 16:30:40 +08002267 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002268 return 0;
2269
2270 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2271 return 0;
2272
Liu Bo116e0022013-08-02 16:30:40 +08002273 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2274 return 0;
2275
2276 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002277 btrfs_file_extent_other_encoding(leaf, fi))
2278 return 0;
2279
2280 return 1;
2281}
2282
2283/*
2284 * Note the backref might has changed, and in this case we just return 0.
2285 */
2286static noinline int relink_extent_backref(struct btrfs_path *path,
2287 struct sa_defrag_extent_backref *prev,
2288 struct sa_defrag_extent_backref *backref)
2289{
2290 struct btrfs_file_extent_item *extent;
2291 struct btrfs_file_extent_item *item;
2292 struct btrfs_ordered_extent *ordered;
2293 struct btrfs_trans_handle *trans;
2294 struct btrfs_fs_info *fs_info;
2295 struct btrfs_root *root;
2296 struct btrfs_key key;
2297 struct extent_buffer *leaf;
2298 struct old_sa_defrag_extent *old = backref->old;
2299 struct new_sa_defrag_extent *new = old->new;
2300 struct inode *src_inode = new->inode;
2301 struct inode *inode;
2302 struct extent_state *cached = NULL;
2303 int ret = 0;
2304 u64 start;
2305 u64 len;
2306 u64 lock_start;
2307 u64 lock_end;
2308 bool merge = false;
2309 int index;
2310
2311 if (prev && prev->root_id == backref->root_id &&
2312 prev->inum == backref->inum &&
2313 prev->file_pos + prev->num_bytes == backref->file_pos)
2314 merge = true;
2315
2316 /* step 1: get root */
2317 key.objectid = backref->root_id;
2318 key.type = BTRFS_ROOT_ITEM_KEY;
2319 key.offset = (u64)-1;
2320
2321 fs_info = BTRFS_I(src_inode)->root->fs_info;
2322 index = srcu_read_lock(&fs_info->subvol_srcu);
2323
2324 root = btrfs_read_fs_root_no_name(fs_info, &key);
2325 if (IS_ERR(root)) {
2326 srcu_read_unlock(&fs_info->subvol_srcu, index);
2327 if (PTR_ERR(root) == -ENOENT)
2328 return 0;
2329 return PTR_ERR(root);
2330 }
Liu Bo38c227d2013-01-29 03:18:40 +00002331
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002332 if (btrfs_root_readonly(root)) {
2333 srcu_read_unlock(&fs_info->subvol_srcu, index);
2334 return 0;
2335 }
2336
Liu Bo38c227d2013-01-29 03:18:40 +00002337 /* step 2: get inode */
2338 key.objectid = backref->inum;
2339 key.type = BTRFS_INODE_ITEM_KEY;
2340 key.offset = 0;
2341
2342 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2343 if (IS_ERR(inode)) {
2344 srcu_read_unlock(&fs_info->subvol_srcu, index);
2345 return 0;
2346 }
2347
2348 srcu_read_unlock(&fs_info->subvol_srcu, index);
2349
2350 /* step 3: relink backref */
2351 lock_start = backref->file_pos;
2352 lock_end = backref->file_pos + backref->num_bytes - 1;
2353 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2354 0, &cached);
2355
2356 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2357 if (ordered) {
2358 btrfs_put_ordered_extent(ordered);
2359 goto out_unlock;
2360 }
2361
2362 trans = btrfs_join_transaction(root);
2363 if (IS_ERR(trans)) {
2364 ret = PTR_ERR(trans);
2365 goto out_unlock;
2366 }
2367
2368 key.objectid = backref->inum;
2369 key.type = BTRFS_EXTENT_DATA_KEY;
2370 key.offset = backref->file_pos;
2371
2372 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2373 if (ret < 0) {
2374 goto out_free_path;
2375 } else if (ret > 0) {
2376 ret = 0;
2377 goto out_free_path;
2378 }
2379
2380 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2381 struct btrfs_file_extent_item);
2382
2383 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2384 backref->generation)
2385 goto out_free_path;
2386
2387 btrfs_release_path(path);
2388
2389 start = backref->file_pos;
2390 if (backref->extent_offset < old->extent_offset + old->offset)
2391 start += old->extent_offset + old->offset -
2392 backref->extent_offset;
2393
2394 len = min(backref->extent_offset + backref->num_bytes,
2395 old->extent_offset + old->offset + old->len);
2396 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2397
2398 ret = btrfs_drop_extents(trans, root, inode, start,
2399 start + len, 1);
2400 if (ret)
2401 goto out_free_path;
2402again:
2403 key.objectid = btrfs_ino(inode);
2404 key.type = BTRFS_EXTENT_DATA_KEY;
2405 key.offset = start;
2406
Liu Boa09a0a72013-03-11 09:20:58 +00002407 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002408 if (merge) {
2409 struct btrfs_file_extent_item *fi;
2410 u64 extent_len;
2411 struct btrfs_key found_key;
2412
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002413 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002414 if (ret < 0)
2415 goto out_free_path;
2416
2417 path->slots[0]--;
2418 leaf = path->nodes[0];
2419 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2420
2421 fi = btrfs_item_ptr(leaf, path->slots[0],
2422 struct btrfs_file_extent_item);
2423 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2424
Liu Bo116e0022013-08-02 16:30:40 +08002425 if (extent_len + found_key.offset == start &&
2426 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002427 btrfs_set_file_extent_num_bytes(leaf, fi,
2428 extent_len + len);
2429 btrfs_mark_buffer_dirty(leaf);
2430 inode_add_bytes(inode, len);
2431
2432 ret = 1;
2433 goto out_free_path;
2434 } else {
2435 merge = false;
2436 btrfs_release_path(path);
2437 goto again;
2438 }
2439 }
2440
2441 ret = btrfs_insert_empty_item(trans, root, path, &key,
2442 sizeof(*extent));
2443 if (ret) {
2444 btrfs_abort_transaction(trans, root, ret);
2445 goto out_free_path;
2446 }
2447
2448 leaf = path->nodes[0];
2449 item = btrfs_item_ptr(leaf, path->slots[0],
2450 struct btrfs_file_extent_item);
2451 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2452 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2453 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2454 btrfs_set_file_extent_num_bytes(leaf, item, len);
2455 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2456 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2457 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2458 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2459 btrfs_set_file_extent_encryption(leaf, item, 0);
2460 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2461
2462 btrfs_mark_buffer_dirty(leaf);
2463 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002464 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002465
2466 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2467 new->disk_len, 0,
2468 backref->root_id, backref->inum,
2469 new->file_pos, 0); /* start - extent_offset */
2470 if (ret) {
2471 btrfs_abort_transaction(trans, root, ret);
2472 goto out_free_path;
2473 }
2474
2475 ret = 1;
2476out_free_path:
2477 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002478 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002479 btrfs_end_transaction(trans, root);
2480out_unlock:
2481 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2482 &cached, GFP_NOFS);
2483 iput(inode);
2484 return ret;
2485}
2486
Liu Bo6f519562013-10-29 10:45:05 +08002487static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2488{
2489 struct old_sa_defrag_extent *old, *tmp;
2490
2491 if (!new)
2492 return;
2493
2494 list_for_each_entry_safe(old, tmp, &new->head, list) {
2495 list_del(&old->list);
2496 kfree(old);
2497 }
2498 kfree(new);
2499}
2500
Liu Bo38c227d2013-01-29 03:18:40 +00002501static void relink_file_extents(struct new_sa_defrag_extent *new)
2502{
2503 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002504 struct sa_defrag_extent_backref *backref;
2505 struct sa_defrag_extent_backref *prev = NULL;
2506 struct inode *inode;
2507 struct btrfs_root *root;
2508 struct rb_node *node;
2509 int ret;
2510
2511 inode = new->inode;
2512 root = BTRFS_I(inode)->root;
2513
2514 path = btrfs_alloc_path();
2515 if (!path)
2516 return;
2517
2518 if (!record_extent_backrefs(path, new)) {
2519 btrfs_free_path(path);
2520 goto out;
2521 }
2522 btrfs_release_path(path);
2523
2524 while (1) {
2525 node = rb_first(&new->root);
2526 if (!node)
2527 break;
2528 rb_erase(node, &new->root);
2529
2530 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2531
2532 ret = relink_extent_backref(path, prev, backref);
2533 WARN_ON(ret < 0);
2534
2535 kfree(prev);
2536
2537 if (ret == 1)
2538 prev = backref;
2539 else
2540 prev = NULL;
2541 cond_resched();
2542 }
2543 kfree(prev);
2544
2545 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002546out:
Liu Bo6f519562013-10-29 10:45:05 +08002547 free_sa_defrag_extent(new);
2548
Liu Bo38c227d2013-01-29 03:18:40 +00002549 atomic_dec(&root->fs_info->defrag_running);
2550 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002551}
2552
2553static struct new_sa_defrag_extent *
2554record_old_file_extents(struct inode *inode,
2555 struct btrfs_ordered_extent *ordered)
2556{
2557 struct btrfs_root *root = BTRFS_I(inode)->root;
2558 struct btrfs_path *path;
2559 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002560 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002561 struct new_sa_defrag_extent *new;
2562 int ret;
2563
2564 new = kmalloc(sizeof(*new), GFP_NOFS);
2565 if (!new)
2566 return NULL;
2567
2568 new->inode = inode;
2569 new->file_pos = ordered->file_offset;
2570 new->len = ordered->len;
2571 new->bytenr = ordered->start;
2572 new->disk_len = ordered->disk_len;
2573 new->compress_type = ordered->compress_type;
2574 new->root = RB_ROOT;
2575 INIT_LIST_HEAD(&new->head);
2576
2577 path = btrfs_alloc_path();
2578 if (!path)
2579 goto out_kfree;
2580
2581 key.objectid = btrfs_ino(inode);
2582 key.type = BTRFS_EXTENT_DATA_KEY;
2583 key.offset = new->file_pos;
2584
2585 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2586 if (ret < 0)
2587 goto out_free_path;
2588 if (ret > 0 && path->slots[0] > 0)
2589 path->slots[0]--;
2590
2591 /* find out all the old extents for the file range */
2592 while (1) {
2593 struct btrfs_file_extent_item *extent;
2594 struct extent_buffer *l;
2595 int slot;
2596 u64 num_bytes;
2597 u64 offset;
2598 u64 end;
2599 u64 disk_bytenr;
2600 u64 extent_offset;
2601
2602 l = path->nodes[0];
2603 slot = path->slots[0];
2604
2605 if (slot >= btrfs_header_nritems(l)) {
2606 ret = btrfs_next_leaf(root, path);
2607 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002608 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002609 else if (ret > 0)
2610 break;
2611 continue;
2612 }
2613
2614 btrfs_item_key_to_cpu(l, &key, slot);
2615
2616 if (key.objectid != btrfs_ino(inode))
2617 break;
2618 if (key.type != BTRFS_EXTENT_DATA_KEY)
2619 break;
2620 if (key.offset >= new->file_pos + new->len)
2621 break;
2622
2623 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2624
2625 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2626 if (key.offset + num_bytes < new->file_pos)
2627 goto next;
2628
2629 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2630 if (!disk_bytenr)
2631 goto next;
2632
2633 extent_offset = btrfs_file_extent_offset(l, extent);
2634
2635 old = kmalloc(sizeof(*old), GFP_NOFS);
2636 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002637 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002638
2639 offset = max(new->file_pos, key.offset);
2640 end = min(new->file_pos + new->len, key.offset + num_bytes);
2641
2642 old->bytenr = disk_bytenr;
2643 old->extent_offset = extent_offset;
2644 old->offset = offset - key.offset;
2645 old->len = end - offset;
2646 old->new = new;
2647 old->count = 0;
2648 list_add_tail(&old->list, &new->head);
2649next:
2650 path->slots[0]++;
2651 cond_resched();
2652 }
2653
2654 btrfs_free_path(path);
2655 atomic_inc(&root->fs_info->defrag_running);
2656
2657 return new;
2658
Liu Bo38c227d2013-01-29 03:18:40 +00002659out_free_path:
2660 btrfs_free_path(path);
2661out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002662 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002663 return NULL;
2664}
2665
Miao Xiee570fd22014-06-19 10:42:50 +08002666static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2667 u64 start, u64 len)
2668{
2669 struct btrfs_block_group_cache *cache;
2670
2671 cache = btrfs_lookup_block_group(root->fs_info, start);
2672 ASSERT(cache);
2673
2674 spin_lock(&cache->lock);
2675 cache->delalloc_bytes -= len;
2676 spin_unlock(&cache->lock);
2677
2678 btrfs_put_block_group(cache);
2679}
2680
Chris Masond352ac62008-09-29 15:18:18 -04002681/* as ordered data IO finishes, this gets called so we can finish
2682 * an ordered extent if the range of bytes in the file it covers are
2683 * fully written.
2684 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002685static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002686{
Josef Bacik5fd02042012-05-02 14:00:54 -04002687 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002688 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002689 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002690 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002691 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002692 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002693 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002694 int ret = 0;
2695 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002696 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002697 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002698
Liu Bo83eea1f2012-07-10 05:28:39 -06002699 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002700
Josef Bacik5fd02042012-05-02 14:00:54 -04002701 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2702 ret = -EIO;
2703 goto out;
2704 }
2705
Miao Xief6124962014-09-12 18:44:04 +08002706 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2707 ordered_extent->file_offset +
2708 ordered_extent->len - 1);
2709
Josef Bacik77cef2e2013-08-29 13:57:21 -04002710 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2711 truncated = true;
2712 logical_len = ordered_extent->truncated_len;
2713 /* Truncated the entire extent, don't bother adding */
2714 if (!logical_len)
2715 goto out;
2716 }
2717
Yan, Zhengc2167752009-11-12 09:34:21 +00002718 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002719 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002720 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2721 if (nolock)
2722 trans = btrfs_join_transaction_nolock(root);
2723 else
2724 trans = btrfs_join_transaction(root);
2725 if (IS_ERR(trans)) {
2726 ret = PTR_ERR(trans);
2727 trans = NULL;
2728 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002729 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002730 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2731 ret = btrfs_update_inode_fallback(trans, root, inode);
2732 if (ret) /* -ENOMEM or corruption */
2733 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002734 goto out;
2735 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002736
Josef Bacik2ac55d42010-02-03 19:33:23 +00002737 lock_extent_bits(io_tree, ordered_extent->file_offset,
2738 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002739 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002740
Liu Bo38c227d2013-01-29 03:18:40 +00002741 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2742 ordered_extent->file_offset + ordered_extent->len - 1,
2743 EXTENT_DEFRAG, 1, cached_state);
2744 if (ret) {
2745 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002746 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002747 /* the inode is shared */
2748 new = record_old_file_extents(inode, ordered_extent);
2749
2750 clear_extent_bit(io_tree, ordered_extent->file_offset,
2751 ordered_extent->file_offset + ordered_extent->len - 1,
2752 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2753 }
2754
Josef Bacik0cb59c92010-07-02 12:14:14 -04002755 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002756 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002757 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002758 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002759 if (IS_ERR(trans)) {
2760 ret = PTR_ERR(trans);
2761 trans = NULL;
2762 goto out_unlock;
2763 }
Chris Masona79b7d42014-05-22 16:18:52 -07002764
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002765 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002766
Chris Masonc8b97812008-10-29 14:49:59 -04002767 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002768 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002769 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002770 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002771 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002772 ordered_extent->file_offset,
2773 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002774 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002775 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002776 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002777 ret = insert_reserved_file_extent(trans, inode,
2778 ordered_extent->file_offset,
2779 ordered_extent->start,
2780 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002781 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002782 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002783 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002784 if (!ret)
2785 btrfs_release_delalloc_bytes(root,
2786 ordered_extent->start,
2787 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002788 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002789 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2790 ordered_extent->file_offset, ordered_extent->len,
2791 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002792 if (ret < 0) {
2793 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002794 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002795 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002796
Chris Masone6dcd2d2008-07-17 12:53:50 -04002797 add_pending_csums(trans, inode, ordered_extent->file_offset,
2798 &ordered_extent->list);
2799
Josef Bacik6c760c02012-11-09 10:53:21 -05002800 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2801 ret = btrfs_update_inode_fallback(trans, root, inode);
2802 if (ret) { /* -ENOMEM or corruption */
2803 btrfs_abort_transaction(trans, root, ret);
2804 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002805 }
2806 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002807out_unlock:
2808 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2809 ordered_extent->file_offset +
2810 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002811out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002812 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002813 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002814 if (trans)
2815 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002816
Josef Bacik77cef2e2013-08-29 13:57:21 -04002817 if (ret || truncated) {
2818 u64 start, end;
2819
2820 if (truncated)
2821 start = ordered_extent->file_offset + logical_len;
2822 else
2823 start = ordered_extent->file_offset;
2824 end = ordered_extent->file_offset + ordered_extent->len - 1;
2825 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2826
2827 /* Drop the cache for the part of the extent we didn't write. */
2828 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002829
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002830 /*
2831 * If the ordered extent had an IOERR or something else went
2832 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002833 * back to the allocator. We only free the extent in the
2834 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002835 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002836 if ((ret || !logical_len) &&
2837 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002838 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2839 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002840 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002841 }
2842
2843
Josef Bacik5fd02042012-05-02 14:00:54 -04002844 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002845 * This needs to be done to make sure anybody waiting knows we are done
2846 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002847 */
2848 btrfs_remove_ordered_extent(inode, ordered_extent);
2849
Liu Bo38c227d2013-01-29 03:18:40 +00002850 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002851 if (new) {
2852 if (ret) {
2853 free_sa_defrag_extent(new);
2854 atomic_dec(&root->fs_info->defrag_running);
2855 } else {
2856 relink_file_extents(new);
2857 }
2858 }
Liu Bo38c227d2013-01-29 03:18:40 +00002859
Chris Masone6dcd2d2008-07-17 12:53:50 -04002860 /* once for us */
2861 btrfs_put_ordered_extent(ordered_extent);
2862 /* once for the tree */
2863 btrfs_put_ordered_extent(ordered_extent);
2864
Josef Bacik5fd02042012-05-02 14:00:54 -04002865 return ret;
2866}
2867
2868static void finish_ordered_fn(struct btrfs_work *work)
2869{
2870 struct btrfs_ordered_extent *ordered_extent;
2871 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2872 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002873}
2874
Christoph Hellwigb2950862008-12-02 09:54:17 -05002875static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002876 struct extent_state *state, int uptodate)
2877{
Josef Bacik5fd02042012-05-02 14:00:54 -04002878 struct inode *inode = page->mapping->host;
2879 struct btrfs_root *root = BTRFS_I(inode)->root;
2880 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002881 struct btrfs_workqueue *wq;
2882 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002883
liubo1abe9b82011-03-24 11:18:59 +00002884 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2885
Chris Mason8b62b722009-09-02 16:53:46 -04002886 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002887 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2888 end - start + 1, uptodate))
2889 return 0;
2890
Liu Bo9e0af232014-08-15 23:36:53 +08002891 if (btrfs_is_free_space_inode(inode)) {
2892 wq = root->fs_info->endio_freespace_worker;
2893 func = btrfs_freespace_write_helper;
2894 } else {
2895 wq = root->fs_info->endio_write_workers;
2896 func = btrfs_endio_write_helper;
2897 }
Josef Bacik5fd02042012-05-02 14:00:54 -04002898
Liu Bo9e0af232014-08-15 23:36:53 +08002899 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
2900 NULL);
2901 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002902
2903 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002904}
2905
Miao Xiedc380ae2014-09-12 18:43:55 +08002906static int __readpage_endio_check(struct inode *inode,
2907 struct btrfs_io_bio *io_bio,
2908 int icsum, struct page *page,
2909 int pgoff, u64 start, size_t len)
2910{
2911 char *kaddr;
2912 u32 csum_expected;
2913 u32 csum = ~(u32)0;
2914 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2915 DEFAULT_RATELIMIT_BURST);
2916
2917 csum_expected = *(((u32 *)io_bio->csum) + icsum);
2918
2919 kaddr = kmap_atomic(page);
2920 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
2921 btrfs_csum_final(csum, (char *)&csum);
2922 if (csum != csum_expected)
2923 goto zeroit;
2924
2925 kunmap_atomic(kaddr);
2926 return 0;
2927zeroit:
2928 if (__ratelimit(&_rs))
2929 btrfs_info(BTRFS_I(inode)->root->fs_info,
2930 "csum failed ino %llu off %llu csum %u expected csum %u",
2931 btrfs_ino(inode), start, csum, csum_expected);
2932 memset(kaddr + pgoff, 1, len);
2933 flush_dcache_page(page);
2934 kunmap_atomic(kaddr);
2935 if (csum_expected == 0)
2936 return 0;
2937 return -EIO;
2938}
2939
Chris Masond352ac62008-09-29 15:18:18 -04002940/*
Chris Masond352ac62008-09-29 15:18:18 -04002941 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002942 * if there's a match, we allow the bio to finish. If not, the code in
2943 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002944 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002945static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2946 u64 phy_offset, struct page *page,
2947 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002948{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002949 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002950 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002951 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002952 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002953
Chris Masond20f7042008-12-08 16:58:54 -05002954 if (PageChecked(page)) {
2955 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002956 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002957 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002958
2959 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002960 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002961
Yan Zheng17d217f2008-12-12 10:03:38 -05002962 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002963 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002964 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2965 GFP_NOFS);
2966 return 0;
2967 }
2968
Miao Xiefacc8a222013-07-25 19:22:34 +08002969 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08002970 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
2971 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002972}
Chris Masonb888db22007-08-27 16:49:44 -04002973
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002974struct delayed_iput {
2975 struct list_head list;
2976 struct inode *inode;
2977};
2978
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002979/* JDM: If this is fs-wide, why can't we add a pointer to
2980 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002981void btrfs_add_delayed_iput(struct inode *inode)
2982{
2983 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2984 struct delayed_iput *delayed;
2985
2986 if (atomic_add_unless(&inode->i_count, -1, 1))
2987 return;
2988
2989 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2990 delayed->inode = inode;
2991
2992 spin_lock(&fs_info->delayed_iput_lock);
2993 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2994 spin_unlock(&fs_info->delayed_iput_lock);
2995}
2996
2997void btrfs_run_delayed_iputs(struct btrfs_root *root)
2998{
2999 LIST_HEAD(list);
3000 struct btrfs_fs_info *fs_info = root->fs_info;
3001 struct delayed_iput *delayed;
3002 int empty;
3003
3004 spin_lock(&fs_info->delayed_iput_lock);
3005 empty = list_empty(&fs_info->delayed_iputs);
3006 spin_unlock(&fs_info->delayed_iput_lock);
3007 if (empty)
3008 return;
3009
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003010 spin_lock(&fs_info->delayed_iput_lock);
3011 list_splice_init(&fs_info->delayed_iputs, &list);
3012 spin_unlock(&fs_info->delayed_iput_lock);
3013
3014 while (!list_empty(&list)) {
3015 delayed = list_entry(list.next, struct delayed_iput, list);
3016 list_del(&delayed->list);
3017 iput(delayed->inode);
3018 kfree(delayed);
3019 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003020}
3021
Yan, Zhengd68fc572010-05-16 10:49:58 -04003022/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003023 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003024 * files in the subvolume, it removes orphan item and frees block_rsv
3025 * structure.
3026 */
3027void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3028 struct btrfs_root *root)
3029{
Josef Bacik90290e12011-12-02 15:44:12 -05003030 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003031 int ret;
3032
Josef Bacik8a35d952012-05-23 14:26:42 -04003033 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003034 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3035 return;
3036
Josef Bacik90290e12011-12-02 15:44:12 -05003037 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003038 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003039 spin_unlock(&root->orphan_lock);
3040 return;
3041 }
3042
3043 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3044 spin_unlock(&root->orphan_lock);
3045 return;
3046 }
3047
3048 block_rsv = root->orphan_block_rsv;
3049 root->orphan_block_rsv = NULL;
3050 spin_unlock(&root->orphan_lock);
3051
Miao Xie27cdeb72014-04-02 19:51:05 +08003052 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003053 btrfs_root_refs(&root->root_item) > 0) {
3054 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3055 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003056 if (ret)
3057 btrfs_abort_transaction(trans, root, ret);
3058 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003059 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3060 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003061 }
3062
Josef Bacik90290e12011-12-02 15:44:12 -05003063 if (block_rsv) {
3064 WARN_ON(block_rsv->size > 0);
3065 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003066 }
3067}
3068
3069/*
Josef Bacik7b128762008-07-24 12:17:14 -04003070 * This creates an orphan entry for the given inode in case something goes
3071 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003072 *
3073 * NOTE: caller of this function should reserve 5 units of metadata for
3074 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003075 */
3076int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3077{
3078 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003079 struct btrfs_block_rsv *block_rsv = NULL;
3080 int reserve = 0;
3081 int insert = 0;
3082 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003083
Yan, Zhengd68fc572010-05-16 10:49:58 -04003084 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003085 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003086 if (!block_rsv)
3087 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003088 }
3089
Yan, Zhengd68fc572010-05-16 10:49:58 -04003090 spin_lock(&root->orphan_lock);
3091 if (!root->orphan_block_rsv) {
3092 root->orphan_block_rsv = block_rsv;
3093 } else if (block_rsv) {
3094 btrfs_free_block_rsv(root, block_rsv);
3095 block_rsv = NULL;
3096 }
Josef Bacik7b128762008-07-24 12:17:14 -04003097
Josef Bacik8a35d952012-05-23 14:26:42 -04003098 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3099 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003100#if 0
3101 /*
3102 * For proper ENOSPC handling, we should do orphan
3103 * cleanup when mounting. But this introduces backward
3104 * compatibility issue.
3105 */
3106 if (!xchg(&root->orphan_item_inserted, 1))
3107 insert = 2;
3108 else
3109 insert = 1;
3110#endif
3111 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003112 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003113 }
Josef Bacik7b128762008-07-24 12:17:14 -04003114
Josef Bacik72ac3c02012-05-23 14:13:11 -04003115 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3116 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003117 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003118 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003119
Yan, Zhengd68fc572010-05-16 10:49:58 -04003120 /* grab metadata reservation from transaction handle */
3121 if (reserve) {
3122 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003123 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003124 }
3125
3126 /* insert an orphan item to track this unlinked/truncated file */
3127 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003128 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003129 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003130 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003131 if (reserve) {
3132 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3133 &BTRFS_I(inode)->runtime_flags);
3134 btrfs_orphan_release_metadata(inode);
3135 }
3136 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003137 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3138 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003139 btrfs_abort_transaction(trans, root, ret);
3140 return ret;
3141 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003142 }
3143 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003144 }
3145
3146 /* insert an orphan item to track subvolume contains orphan files */
3147 if (insert >= 2) {
3148 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3149 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003150 if (ret && ret != -EEXIST) {
3151 btrfs_abort_transaction(trans, root, ret);
3152 return ret;
3153 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003154 }
3155 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003156}
3157
3158/*
3159 * We have done the truncate/delete so we can go ahead and remove the orphan
3160 * item for this particular inode.
3161 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003162static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3163 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003164{
3165 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003166 int delete_item = 0;
3167 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003168 int ret = 0;
3169
Yan, Zhengd68fc572010-05-16 10:49:58 -04003170 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003171 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3172 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003173 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003174
Josef Bacik72ac3c02012-05-23 14:13:11 -04003175 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3176 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003177 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003178 spin_unlock(&root->orphan_lock);
3179
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003180 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003181 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003182 if (trans)
3183 ret = btrfs_del_orphan_item(trans, root,
3184 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003185 }
Josef Bacik7b128762008-07-24 12:17:14 -04003186
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003187 if (release_rsv)
3188 btrfs_orphan_release_metadata(inode);
3189
Josef Bacik4ef31a42013-08-13 14:10:08 -04003190 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003191}
3192
3193/*
3194 * this cleans up any orphans that may be left on the list from the last use
3195 * of this root.
3196 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003197int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003198{
3199 struct btrfs_path *path;
3200 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003201 struct btrfs_key key, found_key;
3202 struct btrfs_trans_handle *trans;
3203 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003204 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003205 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3206
Yan, Zhengd68fc572010-05-16 10:49:58 -04003207 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003208 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003209
3210 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003211 if (!path) {
3212 ret = -ENOMEM;
3213 goto out;
3214 }
Josef Bacik7b128762008-07-24 12:17:14 -04003215 path->reada = -1;
3216
3217 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003218 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003219 key.offset = (u64)-1;
3220
Josef Bacik7b128762008-07-24 12:17:14 -04003221 while (1) {
3222 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003223 if (ret < 0)
3224 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003225
3226 /*
3227 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003228 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003229 * find the key and see if we have stuff that matches
3230 */
3231 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003232 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003233 if (path->slots[0] == 0)
3234 break;
3235 path->slots[0]--;
3236 }
3237
3238 /* pull out the item */
3239 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003240 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3241
3242 /* make sure the item matches what we want */
3243 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3244 break;
David Sterba962a2982014-06-04 18:41:45 +02003245 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003246 break;
3247
3248 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003249 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003250
3251 /*
3252 * this is where we are basically btrfs_lookup, without the
3253 * crossing root thing. we store the inode number in the
3254 * offset of the orphan item.
3255 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003256
3257 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003258 btrfs_err(root->fs_info,
3259 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003260 ret = -EINVAL;
3261 goto out;
3262 }
3263
3264 last_objectid = found_key.offset;
3265
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003266 found_key.objectid = found_key.offset;
3267 found_key.type = BTRFS_INODE_ITEM_KEY;
3268 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003269 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303270 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003271 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003272 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003273
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003274 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3275 struct btrfs_root *dead_root;
3276 struct btrfs_fs_info *fs_info = root->fs_info;
3277 int is_dead_root = 0;
3278
3279 /*
3280 * this is an orphan in the tree root. Currently these
3281 * could come from 2 sources:
3282 * a) a snapshot deletion in progress
3283 * b) a free space cache inode
3284 * We need to distinguish those two, as the snapshot
3285 * orphan must not get deleted.
3286 * find_dead_roots already ran before us, so if this
3287 * is a snapshot deletion, we should find the root
3288 * in the dead_roots list
3289 */
3290 spin_lock(&fs_info->trans_lock);
3291 list_for_each_entry(dead_root, &fs_info->dead_roots,
3292 root_list) {
3293 if (dead_root->root_key.objectid ==
3294 found_key.objectid) {
3295 is_dead_root = 1;
3296 break;
3297 }
3298 }
3299 spin_unlock(&fs_info->trans_lock);
3300 if (is_dead_root) {
3301 /* prevent this orphan from being found again */
3302 key.offset = found_key.objectid - 1;
3303 continue;
3304 }
3305 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003306 /*
3307 * Inode is already gone but the orphan item is still there,
3308 * kill the orphan item.
3309 */
3310 if (ret == -ESTALE) {
3311 trans = btrfs_start_transaction(root, 1);
3312 if (IS_ERR(trans)) {
3313 ret = PTR_ERR(trans);
3314 goto out;
3315 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003316 btrfs_debug(root->fs_info, "auto deleting %Lu",
3317 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003318 ret = btrfs_del_orphan_item(trans, root,
3319 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003320 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003321 if (ret)
3322 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003323 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003324 }
Josef Bacik7b128762008-07-24 12:17:14 -04003325
Josef Bacik7b128762008-07-24 12:17:14 -04003326 /*
3327 * add this inode to the orphan list so btrfs_orphan_del does
3328 * the proper thing when we hit it
3329 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003330 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3331 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003332 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003333
Josef Bacik7b128762008-07-24 12:17:14 -04003334 /* if we have links, this was a truncate, lets do that */
3335 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303336 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003337 iput(inode);
3338 continue;
3339 }
Josef Bacik7b128762008-07-24 12:17:14 -04003340 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003341
3342 /* 1 for the orphan item deletion. */
3343 trans = btrfs_start_transaction(root, 1);
3344 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003345 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003346 ret = PTR_ERR(trans);
3347 goto out;
3348 }
3349 ret = btrfs_orphan_add(trans, inode);
3350 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003351 if (ret) {
3352 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003353 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003354 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003355
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003356 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003357 if (ret)
3358 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003359 } else {
3360 nr_unlink++;
3361 }
3362
3363 /* this will do delete_inode and everything for us */
3364 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003365 if (ret)
3366 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003367 }
Miao Xie3254c872011-11-10 20:45:05 -05003368 /* release the path since we're done with it */
3369 btrfs_release_path(path);
3370
Yan, Zhengd68fc572010-05-16 10:49:58 -04003371 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3372
3373 if (root->orphan_block_rsv)
3374 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3375 (u64)-1);
3376
Miao Xie27cdeb72014-04-02 19:51:05 +08003377 if (root->orphan_block_rsv ||
3378 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003379 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003380 if (!IS_ERR(trans))
3381 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003382 }
Josef Bacik7b128762008-07-24 12:17:14 -04003383
3384 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003385 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003386 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003387 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003388
3389out:
3390 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003391 btrfs_crit(root->fs_info,
3392 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003393 btrfs_free_path(path);
3394 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003395}
3396
Chris Masond352ac62008-09-29 15:18:18 -04003397/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003398 * very simple check to peek ahead in the leaf looking for xattrs. If we
3399 * don't find any xattrs, we know there can't be any acls.
3400 *
3401 * slot is the slot the inode is in, objectid is the objectid of the inode
3402 */
3403static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003404 int slot, u64 objectid,
3405 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003406{
3407 u32 nritems = btrfs_header_nritems(leaf);
3408 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003409 static u64 xattr_access = 0;
3410 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003411 int scanned = 0;
3412
Josef Bacikf23b5a52013-06-19 10:16:26 -04003413 if (!xattr_access) {
3414 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3415 strlen(POSIX_ACL_XATTR_ACCESS));
3416 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3417 strlen(POSIX_ACL_XATTR_DEFAULT));
3418 }
3419
Chris Mason46a53cc2009-04-27 11:47:50 -04003420 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003421 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003422 while (slot < nritems) {
3423 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3424
3425 /* we found a different objectid, there must not be acls */
3426 if (found_key.objectid != objectid)
3427 return 0;
3428
3429 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003430 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003431 if (*first_xattr_slot == -1)
3432 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003433 if (found_key.offset == xattr_access ||
3434 found_key.offset == xattr_default)
3435 return 1;
3436 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003437
3438 /*
3439 * we found a key greater than an xattr key, there can't
3440 * be any acls later on
3441 */
3442 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3443 return 0;
3444
3445 slot++;
3446 scanned++;
3447
3448 /*
3449 * it goes inode, inode backrefs, xattrs, extents,
3450 * so if there are a ton of hard links to an inode there can
3451 * be a lot of backrefs. Don't waste time searching too hard,
3452 * this is just an optimization
3453 */
3454 if (scanned >= 8)
3455 break;
3456 }
3457 /* we hit the end of the leaf before we found an xattr or
3458 * something larger than an xattr. We have to assume the inode
3459 * has acls
3460 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003461 if (*first_xattr_slot == -1)
3462 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003463 return 1;
3464}
3465
3466/*
Chris Masond352ac62008-09-29 15:18:18 -04003467 * read an inode from the btree into the in-memory inode
3468 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003469static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003470{
3471 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003472 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003473 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003474 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003475 struct btrfs_root *root = BTRFS_I(inode)->root;
3476 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003477 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003478 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003479 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003480 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003481 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003482 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003483
3484 ret = btrfs_fill_inode(inode, &rdev);
3485 if (!ret)
3486 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003487
3488 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003489 if (!path)
3490 goto make_bad;
3491
Chris Mason39279cc2007-06-12 06:35:45 -04003492 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003493
Chris Mason39279cc2007-06-12 06:35:45 -04003494 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003495 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003496 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003497
Chris Mason5f39d392007-10-15 16:14:19 -04003498 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003499
3500 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003501 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003502
Chris Mason5f39d392007-10-15 16:14:19 -04003503 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3504 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003505 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003506 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003507 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3508 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003509 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003510
3511 tspec = btrfs_inode_atime(inode_item);
3512 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3513 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3514
3515 tspec = btrfs_inode_mtime(inode_item);
3516 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3517 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3518
3519 tspec = btrfs_inode_ctime(inode_item);
3520 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3521 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3522
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003523 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003524 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003525 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3526
3527 /*
3528 * If we were modified in the current generation and evicted from memory
3529 * and then re-read we need to do a full sync since we don't have any
3530 * idea about which extents were modified before we were evicted from
3531 * cache.
3532 */
3533 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3534 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3535 &BTRFS_I(inode)->runtime_flags);
3536
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003537 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003538 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003539 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003540 rdev = btrfs_inode_rdev(leaf, inode_item);
3541
Josef Bacikaec74772008-07-24 12:12:38 -04003542 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003543 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003544
3545cache_index:
3546 path->slots[0]++;
3547 if (inode->i_nlink != 1 ||
3548 path->slots[0] >= btrfs_header_nritems(leaf))
3549 goto cache_acl;
3550
3551 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3552 if (location.objectid != btrfs_ino(inode))
3553 goto cache_acl;
3554
3555 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3556 if (location.type == BTRFS_INODE_REF_KEY) {
3557 struct btrfs_inode_ref *ref;
3558
3559 ref = (struct btrfs_inode_ref *)ptr;
3560 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3561 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3562 struct btrfs_inode_extref *extref;
3563
3564 extref = (struct btrfs_inode_extref *)ptr;
3565 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3566 extref);
3567 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003568cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003569 /*
3570 * try to precache a NULL acl entry for files that don't have
3571 * any xattrs or acls
3572 */
Li Zefan33345d012011-04-20 10:31:50 +08003573 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003574 btrfs_ino(inode), &first_xattr_slot);
3575 if (first_xattr_slot != -1) {
3576 path->slots[0] = first_xattr_slot;
3577 ret = btrfs_load_inode_props(inode, path);
3578 if (ret)
3579 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003580 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003581 btrfs_ino(inode),
3582 root->root_key.objectid, ret);
3583 }
3584 btrfs_free_path(path);
3585
Al Viro72c04902009-06-24 16:58:48 -04003586 if (!maybe_acls)
3587 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003588
Chris Mason39279cc2007-06-12 06:35:45 -04003589 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003590 case S_IFREG:
3591 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003592 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003593 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003594 inode->i_fop = &btrfs_file_operations;
3595 inode->i_op = &btrfs_file_inode_operations;
3596 break;
3597 case S_IFDIR:
3598 inode->i_fop = &btrfs_dir_file_operations;
3599 if (root == root->fs_info->tree_root)
3600 inode->i_op = &btrfs_dir_ro_inode_operations;
3601 else
3602 inode->i_op = &btrfs_dir_inode_operations;
3603 break;
3604 case S_IFLNK:
3605 inode->i_op = &btrfs_symlink_inode_operations;
3606 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003607 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003608 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003609 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003610 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003611 init_special_inode(inode, inode->i_mode, rdev);
3612 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003613 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003614
3615 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003616 return;
3617
3618make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003619 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003620 make_bad_inode(inode);
3621}
3622
Chris Masond352ac62008-09-29 15:18:18 -04003623/*
3624 * given a leaf and an inode, copy the inode fields into the leaf
3625 */
Chris Masone02119d2008-09-05 16:13:11 -04003626static void fill_inode_item(struct btrfs_trans_handle *trans,
3627 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003628 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003629 struct inode *inode)
3630{
Liu Bo51fab692012-12-27 09:01:21 +00003631 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003632
Liu Bo51fab692012-12-27 09:01:21 +00003633 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003634
Liu Bo51fab692012-12-27 09:01:21 +00003635 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3636 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3637 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3638 &token);
3639 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3640 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003641
Liu Bo51fab692012-12-27 09:01:21 +00003642 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3643 inode->i_atime.tv_sec, &token);
3644 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3645 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003646
Liu Bo51fab692012-12-27 09:01:21 +00003647 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3648 inode->i_mtime.tv_sec, &token);
3649 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3650 inode->i_mtime.tv_nsec, &token);
3651
3652 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3653 inode->i_ctime.tv_sec, &token);
3654 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3655 inode->i_ctime.tv_nsec, &token);
3656
3657 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3658 &token);
3659 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3660 &token);
3661 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3662 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3663 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3664 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3665 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003666}
3667
Chris Masond352ac62008-09-29 15:18:18 -04003668/*
3669 * copy everything in the in-memory inode into the btree.
3670 */
Chris Mason21151332011-11-10 20:39:08 -05003671static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003672 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003673{
3674 struct btrfs_inode_item *inode_item;
3675 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003676 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003677 int ret;
3678
3679 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003680 if (!path)
3681 return -ENOMEM;
3682
Chris Masonb9473432009-03-13 11:00:37 -04003683 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003684 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3685 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003686 if (ret) {
3687 if (ret > 0)
3688 ret = -ENOENT;
3689 goto failed;
3690 }
3691
Chris Mason5f39d392007-10-15 16:14:19 -04003692 leaf = path->nodes[0];
3693 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003694 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003695
Chris Masone02119d2008-09-05 16:13:11 -04003696 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003697 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003698 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003699 ret = 0;
3700failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003701 btrfs_free_path(path);
3702 return ret;
3703}
3704
Chris Masond352ac62008-09-29 15:18:18 -04003705/*
Chris Mason21151332011-11-10 20:39:08 -05003706 * copy everything in the in-memory inode into the btree.
3707 */
3708noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3709 struct btrfs_root *root, struct inode *inode)
3710{
3711 int ret;
3712
3713 /*
3714 * If the inode is a free space inode, we can deadlock during commit
3715 * if we put it into the delayed code.
3716 *
3717 * The data relocation inode should also be directly updated
3718 * without delay
3719 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003720 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003721 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3722 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003723 btrfs_update_root_times(trans, root);
3724
Chris Mason21151332011-11-10 20:39:08 -05003725 ret = btrfs_delayed_update_inode(trans, root, inode);
3726 if (!ret)
3727 btrfs_set_inode_last_trans(trans, inode);
3728 return ret;
3729 }
3730
3731 return btrfs_update_inode_item(trans, root, inode);
3732}
3733
Josef Bacikbe6aef62012-10-22 15:43:12 -04003734noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3735 struct btrfs_root *root,
3736 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003737{
3738 int ret;
3739
3740 ret = btrfs_update_inode(trans, root, inode);
3741 if (ret == -ENOSPC)
3742 return btrfs_update_inode_item(trans, root, inode);
3743 return ret;
3744}
3745
3746/*
Chris Masond352ac62008-09-29 15:18:18 -04003747 * unlink helper that gets used here in inode.c and in the tree logging
3748 * recovery code. It remove a link in a directory with a given name, and
3749 * also drops the back refs in the inode to the directory
3750 */
Al Viro92986792011-03-04 17:14:37 +00003751static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3752 struct btrfs_root *root,
3753 struct inode *dir, struct inode *inode,
3754 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003755{
3756 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003757 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003758 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003759 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003760 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003761 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003762 u64 ino = btrfs_ino(inode);
3763 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003764
3765 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003766 if (!path) {
3767 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003768 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003769 }
3770
Chris Masonb9473432009-03-13 11:00:37 -04003771 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003772 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003773 name, name_len, -1);
3774 if (IS_ERR(di)) {
3775 ret = PTR_ERR(di);
3776 goto err;
3777 }
3778 if (!di) {
3779 ret = -ENOENT;
3780 goto err;
3781 }
Chris Mason5f39d392007-10-15 16:14:19 -04003782 leaf = path->nodes[0];
3783 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003784 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003785 if (ret)
3786 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003787 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003788
Miao Xie67de1172013-12-26 13:07:06 +08003789 /*
3790 * If we don't have dir index, we have to get it by looking up
3791 * the inode ref, since we get the inode ref, remove it directly,
3792 * it is unnecessary to do delayed deletion.
3793 *
3794 * But if we have dir index, needn't search inode ref to get it.
3795 * Since the inode ref is close to the inode item, it is better
3796 * that we delay to delete it, and just do this deletion when
3797 * we update the inode item.
3798 */
3799 if (BTRFS_I(inode)->dir_index) {
3800 ret = btrfs_delayed_delete_inode_ref(inode);
3801 if (!ret) {
3802 index = BTRFS_I(inode)->dir_index;
3803 goto skip_backref;
3804 }
3805 }
3806
Li Zefan33345d012011-04-20 10:31:50 +08003807 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3808 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003809 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003810 btrfs_info(root->fs_info,
3811 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003812 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003813 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003814 goto err;
3815 }
Miao Xie67de1172013-12-26 13:07:06 +08003816skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003817 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003818 if (ret) {
3819 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003820 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003821 }
Chris Mason39279cc2007-06-12 06:35:45 -04003822
Chris Masone02119d2008-09-05 16:13:11 -04003823 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003824 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003825 if (ret != 0 && ret != -ENOENT) {
3826 btrfs_abort_transaction(trans, root, ret);
3827 goto err;
3828 }
Chris Masone02119d2008-09-05 16:13:11 -04003829
3830 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3831 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003832 if (ret == -ENOENT)
3833 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003834 else if (ret)
3835 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003836err:
3837 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003838 if (ret)
3839 goto out;
3840
3841 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003842 inode_inc_iversion(inode);
3843 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003844 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003845 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003846out:
Chris Mason39279cc2007-06-12 06:35:45 -04003847 return ret;
3848}
3849
Al Viro92986792011-03-04 17:14:37 +00003850int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3851 struct btrfs_root *root,
3852 struct inode *dir, struct inode *inode,
3853 const char *name, int name_len)
3854{
3855 int ret;
3856 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3857 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003858 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003859 ret = btrfs_update_inode(trans, root, inode);
3860 }
3861 return ret;
3862}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003863
3864/*
3865 * helper to start transaction for unlink and rmdir.
3866 *
Josef Bacikd52be812013-05-29 14:54:47 -04003867 * unlink and rmdir are special in btrfs, they do not always free space, so
3868 * if we cannot make our reservations the normal way try and see if there is
3869 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3870 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003871 */
Josef Bacikd52be812013-05-29 14:54:47 -04003872static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003873{
3874 struct btrfs_trans_handle *trans;
3875 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003876 int ret;
3877
Josef Bacike70bea52011-10-11 14:18:24 -04003878 /*
3879 * 1 for the possible orphan item
3880 * 1 for the dir item
3881 * 1 for the dir index
3882 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003883 * 1 for the inode
3884 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003885 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003886 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3887 return trans;
3888
Josef Bacikd52be812013-05-29 14:54:47 -04003889 if (PTR_ERR(trans) == -ENOSPC) {
3890 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003891
Josef Bacikd52be812013-05-29 14:54:47 -04003892 trans = btrfs_start_transaction(root, 0);
3893 if (IS_ERR(trans))
3894 return trans;
3895 ret = btrfs_cond_migrate_bytes(root->fs_info,
3896 &root->fs_info->trans_block_rsv,
3897 num_bytes, 5);
3898 if (ret) {
3899 btrfs_end_transaction(trans, root);
3900 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003901 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003902 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003903 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003904 }
Josef Bacikd52be812013-05-29 14:54:47 -04003905 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003906}
3907
Chris Mason39279cc2007-06-12 06:35:45 -04003908static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3909{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003910 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003911 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003912 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003913 int ret;
3914
Josef Bacikd52be812013-05-29 14:54:47 -04003915 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003916 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003917 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003918
Chris Mason12fcfd22009-03-24 10:24:20 -04003919 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3920
Chris Masone02119d2008-09-05 16:13:11 -04003921 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3922 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003923 if (ret)
3924 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003925
Yan, Zhenga22285a2010-05-16 10:48:46 -04003926 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003927 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003928 if (ret)
3929 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003930 }
Josef Bacik7b128762008-07-24 12:17:14 -04003931
Tsutomu Itohb5324022011-07-19 07:27:20 +00003932out:
Josef Bacikd52be812013-05-29 14:54:47 -04003933 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003934 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003935 return ret;
3936}
3937
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003938int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3939 struct btrfs_root *root,
3940 struct inode *dir, u64 objectid,
3941 const char *name, int name_len)
3942{
3943 struct btrfs_path *path;
3944 struct extent_buffer *leaf;
3945 struct btrfs_dir_item *di;
3946 struct btrfs_key key;
3947 u64 index;
3948 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003949 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003950
3951 path = btrfs_alloc_path();
3952 if (!path)
3953 return -ENOMEM;
3954
Li Zefan33345d012011-04-20 10:31:50 +08003955 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003956 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003957 if (IS_ERR_OR_NULL(di)) {
3958 if (!di)
3959 ret = -ENOENT;
3960 else
3961 ret = PTR_ERR(di);
3962 goto out;
3963 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003964
3965 leaf = path->nodes[0];
3966 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3967 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3968 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003969 if (ret) {
3970 btrfs_abort_transaction(trans, root, ret);
3971 goto out;
3972 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003973 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003974
3975 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3976 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003977 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003978 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003979 if (ret != -ENOENT) {
3980 btrfs_abort_transaction(trans, root, ret);
3981 goto out;
3982 }
Li Zefan33345d012011-04-20 10:31:50 +08003983 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003984 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003985 if (IS_ERR_OR_NULL(di)) {
3986 if (!di)
3987 ret = -ENOENT;
3988 else
3989 ret = PTR_ERR(di);
3990 btrfs_abort_transaction(trans, root, ret);
3991 goto out;
3992 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003993
3994 leaf = path->nodes[0];
3995 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003996 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003997 index = key.offset;
3998 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003999 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004000
Miao Xie16cdcec2011-04-22 18:12:22 +08004001 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004002 if (ret) {
4003 btrfs_abort_transaction(trans, root, ret);
4004 goto out;
4005 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004006
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004007 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004008 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004009 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004010 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004011 if (ret)
4012 btrfs_abort_transaction(trans, root, ret);
4013out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004014 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004015 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004016}
4017
Chris Mason39279cc2007-06-12 06:35:45 -04004018static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4019{
4020 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004021 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004022 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004023 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004024
David Sterbab3ae2442012-09-13 16:04:34 -06004025 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004026 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004027 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4028 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004029
Josef Bacikd52be812013-05-29 14:54:47 -04004030 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004031 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004032 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004033
Li Zefan33345d012011-04-20 10:31:50 +08004034 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004035 err = btrfs_unlink_subvol(trans, root, dir,
4036 BTRFS_I(inode)->location.objectid,
4037 dentry->d_name.name,
4038 dentry->d_name.len);
4039 goto out;
4040 }
4041
Josef Bacik7b128762008-07-24 12:17:14 -04004042 err = btrfs_orphan_add(trans, inode);
4043 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004044 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004045
Chris Mason39279cc2007-06-12 06:35:45 -04004046 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004047 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4048 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004049 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004050 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004051out:
Josef Bacikd52be812013-05-29 14:54:47 -04004052 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004053 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004054
Chris Mason39279cc2007-06-12 06:35:45 -04004055 return err;
4056}
4057
Chris Mason323ac952008-10-01 19:05:46 -04004058/*
Chris Mason39279cc2007-06-12 06:35:45 -04004059 * this can truncate away extent items, csum items and directory items.
4060 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004061 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004062 *
4063 * csum items that cross the new i_size are truncated to the new size
4064 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004065 *
4066 * min_type is the minimum key type to truncate down to. If set to 0, this
4067 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004068 */
Yan, Zheng80825102009-11-12 09:35:36 +00004069int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4070 struct btrfs_root *root,
4071 struct inode *inode,
4072 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004073{
Chris Mason39279cc2007-06-12 06:35:45 -04004074 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004075 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004076 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004077 struct btrfs_key key;
4078 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004079 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004080 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004081 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004082 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004083 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004084 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004085 int found_extent;
4086 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004087 int pending_del_nr = 0;
4088 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004089 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004090 int ret;
4091 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004092 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004093
4094 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004095
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004096 path = btrfs_alloc_path();
4097 if (!path)
4098 return -ENOMEM;
4099 path->reada = -1;
4100
Josef Bacik5dc562c2012-08-17 13:14:17 -04004101 /*
4102 * We want to drop from the next block forward in case this new size is
4103 * not block aligned since we will be keeping the last block of the
4104 * extent just the way it is.
4105 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004106 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4107 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004108 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4109 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004110
Miao Xie16cdcec2011-04-22 18:12:22 +08004111 /*
4112 * This function is also used to drop the items in the log tree before
4113 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4114 * it is used to drop the loged items. So we shouldn't kill the delayed
4115 * items.
4116 */
4117 if (min_type == 0 && root == BTRFS_I(inode)->root)
4118 btrfs_kill_delayed_inode_items(inode);
4119
Li Zefan33345d012011-04-20 10:31:50 +08004120 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004121 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004122 key.type = (u8)-1;
4123
Chris Mason85e21ba2008-01-29 15:11:36 -05004124search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004125 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004126 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004127 if (ret < 0) {
4128 err = ret;
4129 goto out;
4130 }
Chris Masond3977122009-01-05 21:25:51 -05004131
Chris Mason85e21ba2008-01-29 15:11:36 -05004132 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004133 /* there are no items in the tree for us to truncate, we're
4134 * done
4135 */
Yan, Zheng80825102009-11-12 09:35:36 +00004136 if (path->slots[0] == 0)
4137 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004138 path->slots[0]--;
4139 }
4140
Chris Masond3977122009-01-05 21:25:51 -05004141 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004142 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004143 leaf = path->nodes[0];
4144 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004145 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004146
Li Zefan33345d012011-04-20 10:31:50 +08004147 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004148 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004149
Chris Mason85e21ba2008-01-29 15:11:36 -05004150 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004151 break;
4152
Chris Mason5f39d392007-10-15 16:14:19 -04004153 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004154 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004155 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004156 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004157 extent_type = btrfs_file_extent_type(leaf, fi);
4158 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004159 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004160 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004161 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004162 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004163 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004164 }
Yan008630c2007-11-07 13:31:09 -05004165 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004166 }
Yan, Zheng80825102009-11-12 09:35:36 +00004167 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004168 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004169 } else {
4170 if (item_end < new_size)
4171 break;
4172 if (found_key.offset >= new_size)
4173 del_item = 1;
4174 else
4175 del_item = 0;
4176 }
Chris Mason39279cc2007-06-12 06:35:45 -04004177 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004178 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004179 if (found_type != BTRFS_EXTENT_DATA_KEY)
4180 goto delete;
4181
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004182 if (del_item)
4183 last_size = found_key.offset;
4184 else
4185 last_size = new_size;
4186
Chris Mason179e29e2007-11-01 11:28:41 -04004187 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004188 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004189 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004190 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004191 u64 orig_num_bytes =
4192 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004193 extent_num_bytes = ALIGN(new_size -
4194 found_key.offset,
4195 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004196 btrfs_set_file_extent_num_bytes(leaf, fi,
4197 extent_num_bytes);
4198 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004199 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004200 if (test_bit(BTRFS_ROOT_REF_COWS,
4201 &root->state) &&
4202 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004203 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004204 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004205 } else {
Chris Masondb945352007-10-15 16:15:53 -04004206 extent_num_bytes =
4207 btrfs_file_extent_disk_num_bytes(leaf,
4208 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004209 extent_offset = found_key.offset -
4210 btrfs_file_extent_offset(leaf, fi);
4211
Chris Mason39279cc2007-06-12 06:35:45 -04004212 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004213 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004214 if (extent_start != 0) {
4215 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004216 if (test_bit(BTRFS_ROOT_REF_COWS,
4217 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004218 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004219 }
4220 }
Chris Mason90692182008-02-08 13:49:28 -05004221 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004222 /*
4223 * we can't truncate inline items that have had
4224 * special encodings
4225 */
4226 if (!del_item &&
4227 btrfs_file_extent_compression(leaf, fi) == 0 &&
4228 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4229 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004230 u32 size = new_size - found_key.offset;
4231
Miao Xie27cdeb72014-04-02 19:51:05 +08004232 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004233 inode_sub_bytes(inode, item_end + 1 -
4234 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004235
4236 /*
4237 * update the ram bytes to properly reflect
4238 * the new size of our item
4239 */
4240 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004241 size =
4242 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004243 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004244 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4245 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004246 inode_sub_bytes(inode, item_end + 1 -
4247 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004248 }
Chris Mason39279cc2007-06-12 06:35:45 -04004249 }
Chris Mason179e29e2007-11-01 11:28:41 -04004250delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004251 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004252 if (!pending_del_nr) {
4253 /* no pending yet, add ourselves */
4254 pending_del_slot = path->slots[0];
4255 pending_del_nr = 1;
4256 } else if (pending_del_nr &&
4257 path->slots[0] + 1 == pending_del_slot) {
4258 /* hop on the pending chunk */
4259 pending_del_nr++;
4260 pending_del_slot = path->slots[0];
4261 } else {
Chris Masond3977122009-01-05 21:25:51 -05004262 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004263 }
Chris Mason39279cc2007-06-12 06:35:45 -04004264 } else {
4265 break;
4266 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004267 if (found_extent &&
4268 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4269 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004270 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004271 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004272 extent_num_bytes, 0,
4273 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004274 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004275 BUG_ON(ret);
4276 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004277
Yan, Zheng80825102009-11-12 09:35:36 +00004278 if (found_type == BTRFS_INODE_ITEM_KEY)
4279 break;
4280
4281 if (path->slots[0] == 0 ||
4282 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004283 if (pending_del_nr) {
4284 ret = btrfs_del_items(trans, root, path,
4285 pending_del_slot,
4286 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004287 if (ret) {
4288 btrfs_abort_transaction(trans,
4289 root, ret);
4290 goto error;
4291 }
Yan, Zheng80825102009-11-12 09:35:36 +00004292 pending_del_nr = 0;
4293 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004294 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004295 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004296 } else {
4297 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004298 }
Chris Mason39279cc2007-06-12 06:35:45 -04004299 }
Yan, Zheng80825102009-11-12 09:35:36 +00004300out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004301 if (pending_del_nr) {
4302 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4303 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004304 if (ret)
4305 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004306 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004307error:
Filipe Mananadac57052014-08-29 20:54:26 +01004308 if (last_size != (u64)-1 &&
4309 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004310 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004311 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004312 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004313}
4314
Chris Masona52d9a82007-08-27 16:49:44 -04004315/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004316 * btrfs_truncate_page - read, zero a chunk and write a page
4317 * @inode - inode that we're zeroing
4318 * @from - the offset to start zeroing
4319 * @len - the length to zero, 0 to zero the entire range respective to the
4320 * offset
4321 * @front - zero up to the offset instead of from the offset on
4322 *
4323 * This will find the page for the "from" offset and cow the page and zero the
4324 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004325 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004326int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4327 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004328{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004329 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004330 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004331 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4332 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004333 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004334 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004335 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004336 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4337 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4338 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004339 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004340 int ret = 0;
4341 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004342 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004343
Josef Bacik2aaa6652012-08-29 14:27:18 -04004344 if ((offset & (blocksize - 1)) == 0 &&
4345 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004346 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004347 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004348 if (ret)
4349 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004350
Chris Mason211c17f2008-05-15 09:13:45 -04004351again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004352 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004353 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004354 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004355 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004356 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004357 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004358
4359 page_start = page_offset(page);
4360 page_end = page_start + PAGE_CACHE_SIZE - 1;
4361
Chris Masona52d9a82007-08-27 16:49:44 -04004362 if (!PageUptodate(page)) {
4363 ret = btrfs_readpage(NULL, page);
4364 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004365 if (page->mapping != mapping) {
4366 unlock_page(page);
4367 page_cache_release(page);
4368 goto again;
4369 }
Chris Masona52d9a82007-08-27 16:49:44 -04004370 if (!PageUptodate(page)) {
4371 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004372 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004373 }
4374 }
Chris Mason211c17f2008-05-15 09:13:45 -04004375 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004376
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004377 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004378 set_page_extent_mapped(page);
4379
4380 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4381 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004382 unlock_extent_cached(io_tree, page_start, page_end,
4383 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004384 unlock_page(page);
4385 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004386 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004387 btrfs_put_ordered_extent(ordered);
4388 goto again;
4389 }
4390
Josef Bacik2ac55d42010-02-03 19:33:23 +00004391 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004392 EXTENT_DIRTY | EXTENT_DELALLOC |
4393 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004394 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004395
Josef Bacik2ac55d42010-02-03 19:33:23 +00004396 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4397 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004398 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004399 unlock_extent_cached(io_tree, page_start, page_end,
4400 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004401 goto out_unlock;
4402 }
4403
Chris Masone6dcd2d2008-07-17 12:53:50 -04004404 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004405 if (!len)
4406 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004407 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004408 if (front)
4409 memset(kaddr, 0, offset);
4410 else
4411 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004412 flush_dcache_page(page);
4413 kunmap(page);
4414 }
Chris Mason247e7432008-07-17 12:53:51 -04004415 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004416 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004417 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4418 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004419
Chris Mason89642222008-07-24 09:41:53 -04004420out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004421 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004422 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004423 unlock_page(page);
4424 page_cache_release(page);
4425out:
4426 return ret;
4427}
4428
Josef Bacik16e75492013-10-22 12:18:51 -04004429static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4430 u64 offset, u64 len)
4431{
4432 struct btrfs_trans_handle *trans;
4433 int ret;
4434
4435 /*
4436 * Still need to make sure the inode looks like it's been updated so
4437 * that any holes get logged if we fsync.
4438 */
4439 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4440 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4441 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4442 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4443 return 0;
4444 }
4445
4446 /*
4447 * 1 - for the one we're dropping
4448 * 1 - for the one we're adding
4449 * 1 - for updating the inode.
4450 */
4451 trans = btrfs_start_transaction(root, 3);
4452 if (IS_ERR(trans))
4453 return PTR_ERR(trans);
4454
4455 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4456 if (ret) {
4457 btrfs_abort_transaction(trans, root, ret);
4458 btrfs_end_transaction(trans, root);
4459 return ret;
4460 }
4461
4462 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4463 0, 0, len, 0, len, 0, 0, 0);
4464 if (ret)
4465 btrfs_abort_transaction(trans, root, ret);
4466 else
4467 btrfs_update_inode(trans, root, inode);
4468 btrfs_end_transaction(trans, root);
4469 return ret;
4470}
4471
Josef Bacik695a0d02011-03-04 15:46:53 -05004472/*
4473 * This function puts in dummy file extents for the area we're creating a hole
4474 * for. So if we are truncating this file to a larger size we need to insert
4475 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4476 * the range between oldsize and size
4477 */
Josef Bacika41ad392011-01-31 15:30:16 -05004478int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004479{
Yan Zheng9036c102008-10-30 14:19:41 -04004480 struct btrfs_root *root = BTRFS_I(inode)->root;
4481 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004482 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004483 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004484 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004485 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4486 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004487 u64 last_byte;
4488 u64 cur_offset;
4489 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004490 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004491
Josef Bacika71754f2013-06-17 17:14:39 -04004492 /*
4493 * If our size started in the middle of a page we need to zero out the
4494 * rest of the page before we expand the i_size, otherwise we could
4495 * expose stale data.
4496 */
4497 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4498 if (err)
4499 return err;
4500
Yan Zheng9036c102008-10-30 14:19:41 -04004501 if (size <= hole_start)
4502 return 0;
4503
Yan Zheng9036c102008-10-30 14:19:41 -04004504 while (1) {
4505 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004506
Josef Bacik2ac55d42010-02-03 19:33:23 +00004507 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004508 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004509 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4510 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004511 if (!ordered)
4512 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004513 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4514 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004515 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004516 btrfs_put_ordered_extent(ordered);
4517 }
4518
Yan Zheng9036c102008-10-30 14:19:41 -04004519 cur_offset = hole_start;
4520 while (1) {
4521 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4522 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004523 if (IS_ERR(em)) {
4524 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004525 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004526 break;
4527 }
Yan Zheng9036c102008-10-30 14:19:41 -04004528 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004529 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004530 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004531 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004532 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004533
Josef Bacik16e75492013-10-22 12:18:51 -04004534 err = maybe_insert_hole(root, inode, cur_offset,
4535 hole_size);
4536 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004537 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004538 btrfs_drop_extent_cache(inode, cur_offset,
4539 cur_offset + hole_size - 1, 0);
4540 hole_em = alloc_extent_map();
4541 if (!hole_em) {
4542 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4543 &BTRFS_I(inode)->runtime_flags);
4544 goto next;
4545 }
4546 hole_em->start = cur_offset;
4547 hole_em->len = hole_size;
4548 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004549
Josef Bacik5dc562c2012-08-17 13:14:17 -04004550 hole_em->block_start = EXTENT_MAP_HOLE;
4551 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004552 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004553 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004554 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4555 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004556 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004557
4558 while (1) {
4559 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004560 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004561 write_unlock(&em_tree->lock);
4562 if (err != -EEXIST)
4563 break;
4564 btrfs_drop_extent_cache(inode, cur_offset,
4565 cur_offset +
4566 hole_size - 1, 0);
4567 }
4568 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004569 }
Josef Bacik16e75492013-10-22 12:18:51 -04004570next:
Yan Zheng9036c102008-10-30 14:19:41 -04004571 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004572 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004573 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004574 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004575 break;
4576 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004577 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004578 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4579 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004580 return err;
4581}
4582
Eric Sandeen3972f262013-01-12 02:57:22 +00004583static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004584{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004585 struct btrfs_root *root = BTRFS_I(inode)->root;
4586 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004587 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004588 loff_t newsize = attr->ia_size;
4589 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004590 int ret;
4591
Eric Sandeen3972f262013-01-12 02:57:22 +00004592 /*
4593 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4594 * special case where we need to update the times despite not having
4595 * these flags set. For all other operations the VFS set these flags
4596 * explicitly if it wants a timestamp update.
4597 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004598 if (newsize != oldsize) {
4599 inode_inc_iversion(inode);
4600 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4601 inode->i_ctime = inode->i_mtime =
4602 current_fs_time(inode->i_sb);
4603 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004604
Josef Bacika41ad392011-01-31 15:30:16 -05004605 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004606 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004607 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004608 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004609 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004610
Miao Xief4a2f4c2011-12-14 20:12:01 -05004611 trans = btrfs_start_transaction(root, 1);
4612 if (IS_ERR(trans))
4613 return PTR_ERR(trans);
4614
4615 i_size_write(inode, newsize);
4616 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4617 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004618 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004619 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004620
Josef Bacika41ad392011-01-31 15:30:16 -05004621 /*
4622 * We're truncating a file that used to have good data down to
4623 * zero. Make sure it gets into the ordered flush list so that
4624 * any new writes get down to disk quickly.
4625 */
4626 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004627 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4628 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004629
Josef Bacikf3fe8202013-01-07 17:03:21 -05004630 /*
4631 * 1 for the orphan item we're going to add
4632 * 1 for the orphan item deletion.
4633 */
4634 trans = btrfs_start_transaction(root, 2);
4635 if (IS_ERR(trans))
4636 return PTR_ERR(trans);
4637
4638 /*
4639 * We need to do this in case we fail at _any_ point during the
4640 * actual truncate. Once we do the truncate_setsize we could
4641 * invalidate pages which forces any outstanding ordered io to
4642 * be instantly completed which will give us extents that need
4643 * to be truncated. If we fail to get an orphan inode down we
4644 * could have left over extents that were never meant to live,
4645 * so we need to garuntee from this point on that everything
4646 * will be consistent.
4647 */
4648 ret = btrfs_orphan_add(trans, inode);
4649 btrfs_end_transaction(trans, root);
4650 if (ret)
4651 return ret;
4652
Josef Bacika41ad392011-01-31 15:30:16 -05004653 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4654 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004655
4656 /* Disable nonlocked read DIO to avoid the end less truncate */
4657 btrfs_inode_block_unlocked_dio(inode);
4658 inode_dio_wait(inode);
4659 btrfs_inode_resume_unlocked_dio(inode);
4660
Josef Bacika41ad392011-01-31 15:30:16 -05004661 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004662 if (ret && inode->i_nlink) {
4663 int err;
4664
4665 /*
4666 * failed to truncate, disk_i_size is only adjusted down
4667 * as we remove extents, so it should represent the true
4668 * size of the inode, so reset the in memory size and
4669 * delete our orphan entry.
4670 */
4671 trans = btrfs_join_transaction(root);
4672 if (IS_ERR(trans)) {
4673 btrfs_orphan_del(NULL, inode);
4674 return ret;
4675 }
4676 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4677 err = btrfs_orphan_del(trans, inode);
4678 if (err)
4679 btrfs_abort_transaction(trans, root, err);
4680 btrfs_end_transaction(trans, root);
4681 }
Yan, Zheng80825102009-11-12 09:35:36 +00004682 }
4683
Josef Bacika41ad392011-01-31 15:30:16 -05004684 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004685}
4686
Chris Mason39279cc2007-06-12 06:35:45 -04004687static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4688{
4689 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004690 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004691 int err;
4692
Li Zefanb83cc962010-12-20 16:04:08 +08004693 if (btrfs_root_readonly(root))
4694 return -EROFS;
4695
Chris Mason39279cc2007-06-12 06:35:45 -04004696 err = inode_change_ok(inode, attr);
4697 if (err)
4698 return err;
4699
Chris Mason5a3f23d2009-03-31 13:27:11 -04004700 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004701 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004702 if (err)
4703 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004704 }
Yan Zheng9036c102008-10-30 14:19:41 -04004705
Christoph Hellwig10257742010-06-04 11:30:02 +02004706 if (attr->ia_valid) {
4707 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004708 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004709 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004710
Josef Bacik22c44fe2011-11-30 10:45:38 -05004711 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004712 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004713 }
4714
Chris Mason39279cc2007-06-12 06:35:45 -04004715 return err;
4716}
Chris Mason61295eb2008-01-14 16:24:38 -05004717
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004718/*
4719 * While truncating the inode pages during eviction, we get the VFS calling
4720 * btrfs_invalidatepage() against each page of the inode. This is slow because
4721 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4722 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4723 * extent_state structures over and over, wasting lots of time.
4724 *
4725 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4726 * those expensive operations on a per page basis and do only the ordered io
4727 * finishing, while we release here the extent_map and extent_state structures,
4728 * without the excessive merging and splitting.
4729 */
4730static void evict_inode_truncate_pages(struct inode *inode)
4731{
4732 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4733 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4734 struct rb_node *node;
4735
4736 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004737 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004738
4739 write_lock(&map_tree->lock);
4740 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4741 struct extent_map *em;
4742
4743 node = rb_first(&map_tree->map);
4744 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004745 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4746 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004747 remove_extent_mapping(map_tree, em);
4748 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004749 if (need_resched()) {
4750 write_unlock(&map_tree->lock);
4751 cond_resched();
4752 write_lock(&map_tree->lock);
4753 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004754 }
4755 write_unlock(&map_tree->lock);
4756
4757 spin_lock(&io_tree->lock);
4758 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4759 struct extent_state *state;
4760 struct extent_state *cached_state = NULL;
4761
4762 node = rb_first(&io_tree->state);
4763 state = rb_entry(node, struct extent_state, rb_node);
4764 atomic_inc(&state->refs);
4765 spin_unlock(&io_tree->lock);
4766
4767 lock_extent_bits(io_tree, state->start, state->end,
4768 0, &cached_state);
4769 clear_extent_bit(io_tree, state->start, state->end,
4770 EXTENT_LOCKED | EXTENT_DIRTY |
4771 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4772 EXTENT_DEFRAG, 1, 1,
4773 &cached_state, GFP_NOFS);
4774 free_extent_state(state);
4775
Filipe Manana7064dd52014-08-08 02:47:05 +01004776 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004777 spin_lock(&io_tree->lock);
4778 }
4779 spin_unlock(&io_tree->lock);
4780}
4781
Al Virobd555972010-06-07 11:35:40 -04004782void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004783{
4784 struct btrfs_trans_handle *trans;
4785 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004786 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004787 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004788 int ret;
4789
liubo1abe9b82011-03-24 11:18:59 +00004790 trace_btrfs_inode_evict(inode);
4791
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004792 evict_inode_truncate_pages(inode);
4793
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004794 if (inode->i_nlink &&
4795 ((btrfs_root_refs(&root->root_item) != 0 &&
4796 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4797 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004798 goto no_delete;
4799
Chris Mason39279cc2007-06-12 06:35:45 -04004800 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004801 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004802 goto no_delete;
4803 }
Al Virobd555972010-06-07 11:35:40 -04004804 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004805 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004806
Miao Xief6124962014-09-12 18:44:04 +08004807 btrfs_free_io_failure_record(inode, 0, (u64)-1);
4808
Yan, Zhengc71bf092009-11-12 09:34:40 +00004809 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004810 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004811 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004812 goto no_delete;
4813 }
4814
Yan, Zheng76dda932009-09-21 16:00:26 -04004815 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004816 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4817 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004818 goto no_delete;
4819 }
4820
Miao Xie0e8c36a2012-12-19 06:59:51 +00004821 ret = btrfs_commit_inode_delayed_inode(inode);
4822 if (ret) {
4823 btrfs_orphan_del(NULL, inode);
4824 goto no_delete;
4825 }
4826
Miao Xie66d8f3d2012-09-06 04:02:28 -06004827 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004828 if (!rsv) {
4829 btrfs_orphan_del(NULL, inode);
4830 goto no_delete;
4831 }
Josef Bacik4a338542011-08-29 11:01:31 -04004832 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004833 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004834 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004835
Chris Masondbe674a2008-07-17 12:54:05 -04004836 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004837
Josef Bacik4289a662011-08-05 13:22:24 -04004838 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004839 * This is a bit simpler than btrfs_truncate since we've already
4840 * reserved our space for our orphan item in the unlink, so we just
4841 * need to reserve some slack space in case we add bytes and update
4842 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004843 */
Yan, Zheng80825102009-11-12 09:35:36 +00004844 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004845 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4846 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004847
Josef Bacik726c35f2011-09-26 15:46:06 -04004848 /*
4849 * Try and steal from the global reserve since we will
4850 * likely not use this space anyway, we want to try as
4851 * hard as possible to get this to work.
4852 */
4853 if (ret)
4854 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4855
Yan, Zhengd68fc572010-05-16 10:49:58 -04004856 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004857 btrfs_warn(root->fs_info,
4858 "Could not get space for a delete, will truncate on mount %d",
4859 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004860 btrfs_orphan_del(NULL, inode);
4861 btrfs_free_block_rsv(root, rsv);
4862 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004863 }
4864
Miao Xie0e8c36a2012-12-19 06:59:51 +00004865 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004866 if (IS_ERR(trans)) {
4867 btrfs_orphan_del(NULL, inode);
4868 btrfs_free_block_rsv(root, rsv);
4869 goto no_delete;
4870 }
4871
4872 trans->block_rsv = rsv;
4873
Yan, Zhengd68fc572010-05-16 10:49:58 -04004874 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004875 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004876 break;
4877
Miao Xie8407aa42012-09-07 01:43:32 -06004878 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004879 btrfs_end_transaction(trans, root);
4880 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004881 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004882 }
4883
Josef Bacik4289a662011-08-05 13:22:24 -04004884 btrfs_free_block_rsv(root, rsv);
4885
Josef Bacik4ef31a42013-08-13 14:10:08 -04004886 /*
4887 * Errors here aren't a big deal, it just means we leave orphan items
4888 * in the tree. They will be cleaned up on the next mount.
4889 */
Yan, Zheng80825102009-11-12 09:35:36 +00004890 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004891 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004892 btrfs_orphan_del(trans, inode);
4893 } else {
4894 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004895 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004896
Josef Bacik4289a662011-08-05 13:22:24 -04004897 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004898 if (!(root == root->fs_info->tree_root ||
4899 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004900 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004901
Chris Mason54aa1f42007-06-22 14:16:25 -04004902 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004903 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004904no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004905 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004906 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004907 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004908}
4909
4910/*
4911 * this returns the key found in the dir entry in the location pointer.
4912 * If no dir entries were found, location->objectid is 0.
4913 */
4914static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4915 struct btrfs_key *location)
4916{
4917 const char *name = dentry->d_name.name;
4918 int namelen = dentry->d_name.len;
4919 struct btrfs_dir_item *di;
4920 struct btrfs_path *path;
4921 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004922 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004923
4924 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004925 if (!path)
4926 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004927
Li Zefan33345d012011-04-20 10:31:50 +08004928 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004929 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004930 if (IS_ERR(di))
4931 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004932
David Sterbac7040052011-04-19 18:00:01 +02004933 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004934 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004935
Chris Mason5f39d392007-10-15 16:14:19 -04004936 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004937out:
Chris Mason39279cc2007-06-12 06:35:45 -04004938 btrfs_free_path(path);
4939 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004940out_err:
4941 location->objectid = 0;
4942 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004943}
4944
4945/*
4946 * when we hit a tree root in a directory, the btrfs part of the inode
4947 * needs to be changed to reflect the root directory of the tree root. This
4948 * is kind of like crossing a mount point.
4949 */
4950static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004951 struct inode *dir,
4952 struct dentry *dentry,
4953 struct btrfs_key *location,
4954 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004955{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004956 struct btrfs_path *path;
4957 struct btrfs_root *new_root;
4958 struct btrfs_root_ref *ref;
4959 struct extent_buffer *leaf;
4960 int ret;
4961 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004962
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004963 path = btrfs_alloc_path();
4964 if (!path) {
4965 err = -ENOMEM;
4966 goto out;
4967 }
Chris Mason39279cc2007-06-12 06:35:45 -04004968
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004969 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004970 ret = btrfs_find_item(root->fs_info->tree_root, path,
4971 BTRFS_I(dir)->root->root_key.objectid,
4972 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004973 if (ret) {
4974 if (ret < 0)
4975 err = ret;
4976 goto out;
4977 }
Chris Mason39279cc2007-06-12 06:35:45 -04004978
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004979 leaf = path->nodes[0];
4980 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004981 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004982 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4983 goto out;
4984
4985 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4986 (unsigned long)(ref + 1),
4987 dentry->d_name.len);
4988 if (ret)
4989 goto out;
4990
David Sterbab3b4aa72011-04-21 01:20:15 +02004991 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004992
4993 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4994 if (IS_ERR(new_root)) {
4995 err = PTR_ERR(new_root);
4996 goto out;
4997 }
4998
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004999 *sub_root = new_root;
5000 location->objectid = btrfs_root_dirid(&new_root->root_item);
5001 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005002 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005003 err = 0;
5004out:
5005 btrfs_free_path(path);
5006 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005007}
5008
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005009static void inode_tree_add(struct inode *inode)
5010{
5011 struct btrfs_root *root = BTRFS_I(inode)->root;
5012 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005013 struct rb_node **p;
5014 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005015 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005016 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005017
Al Viro1d3382cb2010-10-23 15:19:20 -04005018 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005019 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005020 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005021 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005022 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005023 while (*p) {
5024 parent = *p;
5025 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5026
Li Zefan33345d012011-04-20 10:31:50 +08005027 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005028 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005029 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005030 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005031 else {
5032 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005033 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005034 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005035 RB_CLEAR_NODE(parent);
5036 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005037 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005038 }
5039 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005040 rb_link_node(new, parent, p);
5041 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005042 spin_unlock(&root->inode_lock);
5043}
5044
5045static void inode_tree_del(struct inode *inode)
5046{
5047 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005048 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005049
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005050 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005051 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005052 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005053 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005054 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005055 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005056 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005057
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005058 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005059 synchronize_srcu(&root->fs_info->subvol_srcu);
5060 spin_lock(&root->inode_lock);
5061 empty = RB_EMPTY_ROOT(&root->inode_tree);
5062 spin_unlock(&root->inode_lock);
5063 if (empty)
5064 btrfs_add_dead_root(root);
5065 }
5066}
5067
Jeff Mahoney143bede2012-03-01 14:56:26 +01005068void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005069{
5070 struct rb_node *node;
5071 struct rb_node *prev;
5072 struct btrfs_inode *entry;
5073 struct inode *inode;
5074 u64 objectid = 0;
5075
Liu Bo7813b3d2014-02-10 17:37:25 +08005076 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5077 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005078
5079 spin_lock(&root->inode_lock);
5080again:
5081 node = root->inode_tree.rb_node;
5082 prev = NULL;
5083 while (node) {
5084 prev = node;
5085 entry = rb_entry(node, struct btrfs_inode, rb_node);
5086
Li Zefan33345d012011-04-20 10:31:50 +08005087 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005088 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005089 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005090 node = node->rb_right;
5091 else
5092 break;
5093 }
5094 if (!node) {
5095 while (prev) {
5096 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005097 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005098 node = prev;
5099 break;
5100 }
5101 prev = rb_next(prev);
5102 }
5103 }
5104 while (node) {
5105 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005106 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005107 inode = igrab(&entry->vfs_inode);
5108 if (inode) {
5109 spin_unlock(&root->inode_lock);
5110 if (atomic_read(&inode->i_count) > 1)
5111 d_prune_aliases(inode);
5112 /*
Al Viro45321ac2010-06-07 13:43:19 -04005113 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005114 * the inode cache when its usage count
5115 * hits zero.
5116 */
5117 iput(inode);
5118 cond_resched();
5119 spin_lock(&root->inode_lock);
5120 goto again;
5121 }
5122
5123 if (cond_resched_lock(&root->inode_lock))
5124 goto again;
5125
5126 node = rb_next(node);
5127 }
5128 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005129}
5130
Chris Masone02119d2008-09-05 16:13:11 -04005131static int btrfs_init_locked_inode(struct inode *inode, void *p)
5132{
5133 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005134 inode->i_ino = args->location->objectid;
5135 memcpy(&BTRFS_I(inode)->location, args->location,
5136 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005137 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005138 return 0;
5139}
5140
5141static int btrfs_find_actor(struct inode *inode, void *opaque)
5142{
5143 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005144 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005145 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005146}
5147
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005148static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005149 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005150 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005151{
5152 struct inode *inode;
5153 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005154 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005155
Chris Mason90d3e592014-01-09 17:28:00 -08005156 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005157 args.root = root;
5158
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005159 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005160 btrfs_init_locked_inode,
5161 (void *)&args);
5162 return inode;
5163}
5164
Balaji Rao1a54ef82008-07-21 02:01:04 +05305165/* Get an inode object given its location and corresponding root.
5166 * Returns in *is_new if the inode was read from disk
5167 */
5168struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005169 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305170{
5171 struct inode *inode;
5172
Chris Mason90d3e592014-01-09 17:28:00 -08005173 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305174 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005175 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305176
5177 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305178 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005179 if (!is_bad_inode(inode)) {
5180 inode_tree_add(inode);
5181 unlock_new_inode(inode);
5182 if (new)
5183 *new = 1;
5184 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005185 unlock_new_inode(inode);
5186 iput(inode);
5187 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005188 }
5189 }
5190
Balaji Rao1a54ef82008-07-21 02:01:04 +05305191 return inode;
5192}
5193
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005194static struct inode *new_simple_dir(struct super_block *s,
5195 struct btrfs_key *key,
5196 struct btrfs_root *root)
5197{
5198 struct inode *inode = new_inode(s);
5199
5200 if (!inode)
5201 return ERR_PTR(-ENOMEM);
5202
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005203 BTRFS_I(inode)->root = root;
5204 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005205 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005206
5207 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005208 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005209 inode->i_fop = &simple_dir_operations;
5210 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5211 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5212
5213 return inode;
5214}
5215
Chris Mason3de45862008-11-17 21:02:50 -05005216struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005217{
Chris Masond3977122009-01-05 21:25:51 -05005218 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005219 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005220 struct btrfs_root *sub_root = root;
5221 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005222 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005223 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005224
5225 if (dentry->d_name.len > BTRFS_NAME_LEN)
5226 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005227
Jeff Layton39e3c952012-11-28 11:30:53 -05005228 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005229 if (ret < 0)
5230 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005231
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005232 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005233 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005234
5235 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005236 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005237 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005238 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005239
5240 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5241
Yan, Zheng76dda932009-09-21 16:00:26 -04005242 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005243 ret = fixup_tree_root_location(root, dir, dentry,
5244 &location, &sub_root);
5245 if (ret < 0) {
5246 if (ret != -ENOENT)
5247 inode = ERR_PTR(ret);
5248 else
5249 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5250 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005251 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005252 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005253 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5254
Julia Lawall34d19ba2011-01-24 19:55:19 +00005255 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005256 down_read(&root->fs_info->cleanup_work_sem);
5257 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005258 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005259 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005260 if (ret) {
5261 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005262 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005263 }
Filipe Manana9c3b3062014-07-31 14:41:07 +01005264 /*
5265 * If orphan cleanup did remove any orphans, it means the tree
5266 * was modified and therefore the commit root is not the same as
5267 * the current root anymore. This is a problem, because send
5268 * uses the commit root and therefore can see inode items that
5269 * don't exist in the current root anymore, and for example make
5270 * calls to btrfs_iget, which will do tree lookups based on the
5271 * current root and not on the commit root. Those lookups will
5272 * fail, returning a -ESTALE error, and making send fail with
5273 * that error. So make sure a send does not see any orphans we
5274 * have just removed, and that it will see the same inodes
5275 * regardless of whether a transaction commit happened before
5276 * it started (meaning that the commit root will be the same as
5277 * the current root) or not.
5278 */
5279 if (sub_root->node != sub_root->commit_root) {
5280 u64 sub_flags = btrfs_root_flags(&sub_root->root_item);
5281
5282 if (sub_flags & BTRFS_ROOT_SUBVOL_RDONLY) {
5283 struct extent_buffer *eb;
5284
5285 /*
5286 * Assert we can't have races between dentry
5287 * lookup called through the snapshot creation
5288 * ioctl and the VFS.
5289 */
5290 ASSERT(mutex_is_locked(&dir->i_mutex));
5291
5292 down_write(&root->fs_info->commit_root_sem);
5293 eb = sub_root->commit_root;
5294 sub_root->commit_root =
5295 btrfs_root_node(sub_root);
5296 up_write(&root->fs_info->commit_root_sem);
5297 free_extent_buffer(eb);
5298 }
5299 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005300 }
5301
Chris Mason3de45862008-11-17 21:02:50 -05005302 return inode;
5303}
5304
Nick Pigginfe15ce42011-01-07 17:49:23 +11005305static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005306{
5307 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005308 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005309
Li Zefan848cce02012-02-21 17:04:28 +08005310 if (!inode && !IS_ROOT(dentry))
5311 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005312
Li Zefan848cce02012-02-21 17:04:28 +08005313 if (inode) {
5314 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005315 if (btrfs_root_refs(&root->root_item) == 0)
5316 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005317
5318 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5319 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005320 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005321 return 0;
5322}
5323
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005324static void btrfs_dentry_release(struct dentry *dentry)
5325{
Daeseok Youn944a4512014-04-14 15:37:02 +09005326 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005327}
5328
Chris Mason3de45862008-11-17 21:02:50 -05005329static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005330 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005331{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005332 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005333
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005334 inode = btrfs_lookup_dentry(dir, dentry);
5335 if (IS_ERR(inode)) {
5336 if (PTR_ERR(inode) == -ENOENT)
5337 inode = NULL;
5338 else
5339 return ERR_CAST(inode);
5340 }
5341
Josef Bacik3a0dfa62014-02-14 13:43:48 -05005342 return d_materialise_unique(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005343}
5344
Miao Xie16cdcec2011-04-22 18:12:22 +08005345unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005346 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5347};
5348
Al Viro9cdda8d2013-05-22 16:48:09 -04005349static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005350{
Al Viro9cdda8d2013-05-22 16:48:09 -04005351 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005352 struct btrfs_root *root = BTRFS_I(inode)->root;
5353 struct btrfs_item *item;
5354 struct btrfs_dir_item *di;
5355 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005356 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005357 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005358 struct list_head ins_list;
5359 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005360 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005361 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005362 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005363 unsigned char d_type;
5364 int over = 0;
5365 u32 di_cur;
5366 u32 di_total;
5367 u32 di_len;
5368 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005369 char tmp_name[32];
5370 char *name_ptr;
5371 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005372 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005373
5374 /* FIXME, use a real flag for deciding about the key type */
5375 if (root->fs_info->tree_root == root)
5376 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005377
Al Viro9cdda8d2013-05-22 16:48:09 -04005378 if (!dir_emit_dots(file, ctx))
5379 return 0;
5380
David Woodhouse49593bf2008-08-17 17:08:36 +01005381 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005382 if (!path)
5383 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005384
Josef Bacik026fd312011-05-13 10:32:11 -04005385 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005386
Miao Xie16cdcec2011-04-22 18:12:22 +08005387 if (key_type == BTRFS_DIR_INDEX_KEY) {
5388 INIT_LIST_HEAD(&ins_list);
5389 INIT_LIST_HEAD(&del_list);
5390 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5391 }
5392
David Sterba962a2982014-06-04 18:41:45 +02005393 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005394 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005395 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005396
Chris Mason39279cc2007-06-12 06:35:45 -04005397 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5398 if (ret < 0)
5399 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005400
5401 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005402 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005403 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005404 if (slot >= btrfs_header_nritems(leaf)) {
5405 ret = btrfs_next_leaf(root, path);
5406 if (ret < 0)
5407 goto err;
5408 else if (ret > 0)
5409 break;
5410 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005411 }
Chris Mason3de45862008-11-17 21:02:50 -05005412
Ross Kirkdd3cc162013-09-16 15:58:09 +01005413 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005414 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5415
5416 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005417 break;
David Sterba962a2982014-06-04 18:41:45 +02005418 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005419 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005420 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005421 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005422 if (key_type == BTRFS_DIR_INDEX_KEY &&
5423 btrfs_should_delete_dir_index(&del_list,
5424 found_key.offset))
5425 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005426
Al Viro9cdda8d2013-05-22 16:48:09 -04005427 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005428 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005429
Chris Mason39279cc2007-06-12 06:35:45 -04005430 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5431 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005432 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005433
5434 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005435 struct btrfs_key location;
5436
Josef Bacik22a94d42011-03-16 16:47:17 -04005437 if (verify_dir_item(root, leaf, di))
5438 break;
5439
Chris Mason5f39d392007-10-15 16:14:19 -04005440 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005441 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005442 name_ptr = tmp_name;
5443 } else {
5444 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005445 if (!name_ptr) {
5446 ret = -ENOMEM;
5447 goto err;
5448 }
Chris Mason5f39d392007-10-15 16:14:19 -04005449 }
5450 read_extent_buffer(leaf, name_ptr,
5451 (unsigned long)(di + 1), name_len);
5452
5453 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5454 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005455
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005456
Chris Mason3de45862008-11-17 21:02:50 -05005457 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005458 * skip it.
5459 *
5460 * In contrast to old kernels, we insert the snapshot's
5461 * dir item and dir index after it has been created, so
5462 * we won't find a reference to our own snapshot. We
5463 * still keep the following code for backward
5464 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005465 */
5466 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5467 location.objectid == root->root_key.objectid) {
5468 over = 0;
5469 goto skip;
5470 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005471 over = !dir_emit(ctx, name_ptr, name_len,
5472 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005473
Chris Mason3de45862008-11-17 21:02:50 -05005474skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005475 if (name_ptr != tmp_name)
5476 kfree(name_ptr);
5477
Chris Mason39279cc2007-06-12 06:35:45 -04005478 if (over)
5479 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005480 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005481 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005482 di_cur += di_len;
5483 di = (struct btrfs_dir_item *)((char *)di + di_len);
5484 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005485next:
5486 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005487 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005488
Miao Xie16cdcec2011-04-22 18:12:22 +08005489 if (key_type == BTRFS_DIR_INDEX_KEY) {
5490 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005491 ctx->pos++;
5492 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005493 if (ret)
5494 goto nopos;
5495 }
5496
David Woodhouse49593bf2008-08-17 17:08:36 +01005497 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005498 ctx->pos++;
5499
5500 /*
5501 * Stop new entries from being returned after we return the last
5502 * entry.
5503 *
5504 * New directory entries are assigned a strictly increasing
5505 * offset. This means that new entries created during readdir
5506 * are *guaranteed* to be seen in the future by that readdir.
5507 * This has broken buggy programs which operate on names as
5508 * they're returned by readdir. Until we re-use freed offsets
5509 * we have this hack to stop new entries from being returned
5510 * under the assumption that they'll never reach this huge
5511 * offset.
5512 *
5513 * This is being careful not to overflow 32bit loff_t unless the
5514 * last entry requires it because doing so has broken 32bit apps
5515 * in the past.
5516 */
5517 if (key_type == BTRFS_DIR_INDEX_KEY) {
5518 if (ctx->pos >= INT_MAX)
5519 ctx->pos = LLONG_MAX;
5520 else
5521 ctx->pos = INT_MAX;
5522 }
Chris Mason39279cc2007-06-12 06:35:45 -04005523nopos:
5524 ret = 0;
5525err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005526 if (key_type == BTRFS_DIR_INDEX_KEY)
5527 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005528 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005529 return ret;
5530}
5531
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005532int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005533{
5534 struct btrfs_root *root = BTRFS_I(inode)->root;
5535 struct btrfs_trans_handle *trans;
5536 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005537 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005538
Josef Bacik72ac3c02012-05-23 14:13:11 -04005539 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005540 return 0;
5541
Liu Bo83eea1f2012-07-10 05:28:39 -06005542 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005543 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005544
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005545 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005546 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005547 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005548 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005549 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005550 if (IS_ERR(trans))
5551 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005552 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005553 }
5554 return ret;
5555}
5556
5557/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005558 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005559 * inode changes. But, it is most likely to find the inode in cache.
5560 * FIXME, needs more benchmarking...there are no reasons other than performance
5561 * to keep or drop this code.
5562 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005563static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005564{
5565 struct btrfs_root *root = BTRFS_I(inode)->root;
5566 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005567 int ret;
5568
Josef Bacik72ac3c02012-05-23 14:13:11 -04005569 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005570 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005571
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005572 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005573 if (IS_ERR(trans))
5574 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005575
5576 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005577 if (ret && ret == -ENOSPC) {
5578 /* whoops, lets try again with the full transaction */
5579 btrfs_end_transaction(trans, root);
5580 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005581 if (IS_ERR(trans))
5582 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005583
Chris Mason94b60442010-05-26 11:02:00 -04005584 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005585 }
Chris Mason39279cc2007-06-12 06:35:45 -04005586 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005587 if (BTRFS_I(inode)->delayed_node)
5588 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005589
5590 return ret;
5591}
5592
5593/*
5594 * This is a copy of file_update_time. We need this so we can return error on
5595 * ENOSPC for updating the inode in the case of file write and mmap writes.
5596 */
Josef Bacike41f9412012-03-26 09:46:47 -04005597static int btrfs_update_time(struct inode *inode, struct timespec *now,
5598 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005599{
Alexander Block2bc5565282012-06-15 09:49:33 +02005600 struct btrfs_root *root = BTRFS_I(inode)->root;
5601
5602 if (btrfs_root_readonly(root))
5603 return -EROFS;
5604
Josef Bacike41f9412012-03-26 09:46:47 -04005605 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005606 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005607 if (flags & S_CTIME)
5608 inode->i_ctime = *now;
5609 if (flags & S_MTIME)
5610 inode->i_mtime = *now;
5611 if (flags & S_ATIME)
5612 inode->i_atime = *now;
5613 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005614}
5615
Chris Masond352ac62008-09-29 15:18:18 -04005616/*
5617 * find the highest existing sequence number in a directory
5618 * and then set the in-memory index_cnt variable to reflect
5619 * free sequence numbers
5620 */
Josef Bacikaec74772008-07-24 12:12:38 -04005621static int btrfs_set_inode_index_count(struct inode *inode)
5622{
5623 struct btrfs_root *root = BTRFS_I(inode)->root;
5624 struct btrfs_key key, found_key;
5625 struct btrfs_path *path;
5626 struct extent_buffer *leaf;
5627 int ret;
5628
Li Zefan33345d012011-04-20 10:31:50 +08005629 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005630 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005631 key.offset = (u64)-1;
5632
5633 path = btrfs_alloc_path();
5634 if (!path)
5635 return -ENOMEM;
5636
5637 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5638 if (ret < 0)
5639 goto out;
5640 /* FIXME: we should be able to handle this */
5641 if (ret == 0)
5642 goto out;
5643 ret = 0;
5644
5645 /*
5646 * MAGIC NUMBER EXPLANATION:
5647 * since we search a directory based on f_pos we have to start at 2
5648 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5649 * else has to start at 2
5650 */
5651 if (path->slots[0] == 0) {
5652 BTRFS_I(inode)->index_cnt = 2;
5653 goto out;
5654 }
5655
5656 path->slots[0]--;
5657
5658 leaf = path->nodes[0];
5659 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5660
Li Zefan33345d012011-04-20 10:31:50 +08005661 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005662 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005663 BTRFS_I(inode)->index_cnt = 2;
5664 goto out;
5665 }
5666
5667 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5668out:
5669 btrfs_free_path(path);
5670 return ret;
5671}
5672
Chris Masond352ac62008-09-29 15:18:18 -04005673/*
5674 * helper to find a free sequence number in a given directory. This current
5675 * code is very simple, later versions will do smarter things in the btree
5676 */
Chris Mason3de45862008-11-17 21:02:50 -05005677int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005678{
5679 int ret = 0;
5680
5681 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005682 ret = btrfs_inode_delayed_dir_index_count(dir);
5683 if (ret) {
5684 ret = btrfs_set_inode_index_count(dir);
5685 if (ret)
5686 return ret;
5687 }
Josef Bacikaec74772008-07-24 12:12:38 -04005688 }
5689
Chris Mason00e4e6b2008-08-05 11:18:09 -04005690 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005691 BTRFS_I(dir)->index_cnt++;
5692
5693 return ret;
5694}
5695
Chris Masonb0d5d102014-09-08 13:08:51 -07005696static int btrfs_insert_inode_locked(struct inode *inode)
5697{
5698 struct btrfs_iget_args args;
5699 args.location = &BTRFS_I(inode)->location;
5700 args.root = BTRFS_I(inode)->root;
5701
5702 return insert_inode_locked4(inode,
5703 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5704 btrfs_find_actor, &args);
5705}
5706
Chris Mason39279cc2007-06-12 06:35:45 -04005707static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5708 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005709 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005710 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005711 u64 ref_objectid, u64 objectid,
5712 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005713{
5714 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005715 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005716 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005717 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005718 struct btrfs_inode_ref *ref;
5719 struct btrfs_key key[2];
5720 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005721 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005722 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005723 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005724
Chris Mason5f39d392007-10-15 16:14:19 -04005725 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005726 if (!path)
5727 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005728
Chris Mason39279cc2007-06-12 06:35:45 -04005729 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005730 if (!inode) {
5731 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005732 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005733 }
Chris Mason39279cc2007-06-12 06:35:45 -04005734
Li Zefan581bb052011-04-20 10:06:11 +08005735 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005736 * O_TMPFILE, set link count to 0, so that after this point,
5737 * we fill in an inode item with the correct link count.
5738 */
5739 if (!name)
5740 set_nlink(inode, 0);
5741
5742 /*
Li Zefan581bb052011-04-20 10:06:11 +08005743 * we have to initialize this early, so we can reclaim the inode
5744 * number if we fail afterwards in this function.
5745 */
5746 inode->i_ino = objectid;
5747
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005748 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005749 trace_btrfs_inode_request(dir);
5750
Chris Mason3de45862008-11-17 21:02:50 -05005751 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005752 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005753 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005754 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005755 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005756 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005757 } else if (dir) {
5758 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005759 }
5760 /*
5761 * index_cnt is ignored for everything but a dir,
5762 * btrfs_get_inode_index_count has an explanation for the magic
5763 * number
5764 */
5765 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005766 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005767 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005768 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005769 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005770
Josef Bacik5dc562c2012-08-17 13:14:17 -04005771 /*
5772 * We could have gotten an inode number from somebody who was fsynced
5773 * and then removed in this same transaction, so let's just set full
5774 * sync since it will be a full sync anyway and this will blow away the
5775 * old info in the log.
5776 */
5777 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5778
Chris Mason9c583092008-01-29 15:15:18 -05005779 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005780 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005781 key[0].offset = 0;
5782
Chris Mason9c583092008-01-29 15:15:18 -05005783 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005784
5785 if (name) {
5786 /*
5787 * Start new inodes with an inode_ref. This is slightly more
5788 * efficient for small numbers of hard links since they will
5789 * be packed into one item. Extended refs will kick in if we
5790 * add more hard links than can fit in the ref item.
5791 */
5792 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005793 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005794 key[1].offset = ref_objectid;
5795
5796 sizes[1] = name_len + sizeof(*ref);
5797 }
Chris Mason9c583092008-01-29 15:15:18 -05005798
Chris Masonb0d5d102014-09-08 13:08:51 -07005799 location = &BTRFS_I(inode)->location;
5800 location->objectid = objectid;
5801 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005802 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005803
5804 ret = btrfs_insert_inode_locked(inode);
5805 if (ret < 0)
5806 goto fail;
5807
Chris Masonb9473432009-03-13 11:00:37 -04005808 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005809 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005810 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005811 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005812
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005813 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005814 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005815 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005816 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5817 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005818 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5819 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005820 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005821
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005822 if (name) {
5823 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5824 struct btrfs_inode_ref);
5825 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5826 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5827 ptr = (unsigned long)(ref + 1);
5828 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5829 }
Chris Mason9c583092008-01-29 15:15:18 -05005830
Chris Mason5f39d392007-10-15 16:14:19 -04005831 btrfs_mark_buffer_dirty(path->nodes[0]);
5832 btrfs_free_path(path);
5833
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005834 btrfs_inherit_iflags(inode, dir);
5835
Al Viro569254b2011-07-24 17:08:40 -04005836 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005837 if (btrfs_test_opt(root, NODATASUM))
5838 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005839 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005840 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5841 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005842 }
5843
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005844 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005845
5846 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005847 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005848
Alexander Block8ea05e32012-07-25 17:35:53 +02005849 btrfs_update_root_times(trans, root);
5850
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005851 ret = btrfs_inode_inherit_props(trans, inode, dir);
5852 if (ret)
5853 btrfs_err(root->fs_info,
5854 "error inheriting props for ino %llu (root %llu): %d",
5855 btrfs_ino(inode), root->root_key.objectid, ret);
5856
Chris Mason39279cc2007-06-12 06:35:45 -04005857 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005858
5859fail_unlock:
5860 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005861fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005862 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005863 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005864 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005865 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005866 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005867}
5868
5869static inline u8 btrfs_inode_type(struct inode *inode)
5870{
5871 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5872}
5873
Chris Masond352ac62008-09-29 15:18:18 -04005874/*
5875 * utility function to add 'inode' into 'parent_inode' with
5876 * a give name and a given sequence number.
5877 * if 'add_backref' is true, also insert a backref from the
5878 * inode to the parent directory.
5879 */
Chris Masone02119d2008-09-05 16:13:11 -04005880int btrfs_add_link(struct btrfs_trans_handle *trans,
5881 struct inode *parent_inode, struct inode *inode,
5882 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005883{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005884 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005885 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005886 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005887 u64 ino = btrfs_ino(inode);
5888 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005889
Li Zefan33345d012011-04-20 10:31:50 +08005890 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005891 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5892 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005893 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02005894 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005895 key.offset = 0;
5896 }
Chris Mason39279cc2007-06-12 06:35:45 -04005897
Li Zefan33345d012011-04-20 10:31:50 +08005898 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005899 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5900 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005901 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005902 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005903 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5904 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005905 }
5906
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005907 /* Nothing to clean up yet */
5908 if (ret)
5909 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005910
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005911 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5912 parent_inode, &key,
5913 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005914 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005915 goto fail_dir_item;
5916 else if (ret) {
5917 btrfs_abort_transaction(trans, root, ret);
5918 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005919 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005920
5921 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5922 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005923 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005924 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5925 ret = btrfs_update_inode(trans, root, parent_inode);
5926 if (ret)
5927 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005928 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005929
5930fail_dir_item:
5931 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5932 u64 local_index;
5933 int err;
5934 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5935 key.objectid, root->root_key.objectid,
5936 parent_ino, &local_index, name, name_len);
5937
5938 } else if (add_backref) {
5939 u64 local_index;
5940 int err;
5941
5942 err = btrfs_del_inode_ref(trans, root, name, name_len,
5943 ino, parent_ino, &local_index);
5944 }
5945 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005946}
5947
5948static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005949 struct inode *dir, struct dentry *dentry,
5950 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005951{
Josef Bacika1b075d2010-11-19 20:36:11 +00005952 int err = btrfs_add_link(trans, dir, inode,
5953 dentry->d_name.name, dentry->d_name.len,
5954 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005955 if (err > 0)
5956 err = -EEXIST;
5957 return err;
5958}
5959
Josef Bacik618e21d2007-07-11 10:18:17 -04005960static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005961 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005962{
5963 struct btrfs_trans_handle *trans;
5964 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005965 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005966 int err;
5967 int drop_inode = 0;
5968 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005969 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005970
5971 if (!new_valid_dev(rdev))
5972 return -EINVAL;
5973
Josef Bacik9ed74f22009-09-11 16:12:44 -04005974 /*
5975 * 2 for inode item and ref
5976 * 2 for dir items
5977 * 1 for xattr if selinux is on
5978 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005979 trans = btrfs_start_transaction(root, 5);
5980 if (IS_ERR(trans))
5981 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005982
Li Zefan581bb052011-04-20 10:06:11 +08005983 err = btrfs_find_free_ino(root, &objectid);
5984 if (err)
5985 goto out_unlock;
5986
Josef Bacikaec74772008-07-24 12:12:38 -04005987 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005988 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005989 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005990 if (IS_ERR(inode)) {
5991 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005992 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005993 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005994
Casey Schauflerad19db72011-12-15 10:09:07 -05005995 /*
5996 * If the active LSM wants to access the inode during
5997 * d_instantiate it needs these. Smack checks to see
5998 * if the filesystem supports xattrs by looking at the
5999 * ops vector.
6000 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006001 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006002 init_special_inode(inode, inode->i_mode, rdev);
6003
6004 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006005 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006006 goto out_unlock_inode;
6007
6008 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6009 if (err) {
6010 goto out_unlock_inode;
6011 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006012 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006013 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006014 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006015 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006016
Josef Bacik618e21d2007-07-11 10:18:17 -04006017out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006018 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006019 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006020 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006021 if (drop_inode) {
6022 inode_dec_link_count(inode);
6023 iput(inode);
6024 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006025 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006026
6027out_unlock_inode:
6028 drop_inode = 1;
6029 unlock_new_inode(inode);
6030 goto out_unlock;
6031
Josef Bacik618e21d2007-07-11 10:18:17 -04006032}
6033
Chris Mason39279cc2007-06-12 06:35:45 -04006034static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006035 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006036{
6037 struct btrfs_trans_handle *trans;
6038 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006039 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006040 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006041 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006042 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006043 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006044
Josef Bacik9ed74f22009-09-11 16:12:44 -04006045 /*
6046 * 2 for inode item and ref
6047 * 2 for dir items
6048 * 1 for xattr if selinux is on
6049 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006050 trans = btrfs_start_transaction(root, 5);
6051 if (IS_ERR(trans))
6052 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006053
Li Zefan581bb052011-04-20 10:06:11 +08006054 err = btrfs_find_free_ino(root, &objectid);
6055 if (err)
6056 goto out_unlock;
6057
Josef Bacikaec74772008-07-24 12:12:38 -04006058 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006059 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006060 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006061 if (IS_ERR(inode)) {
6062 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006063 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006064 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006065 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006066 /*
6067 * If the active LSM wants to access the inode during
6068 * d_instantiate it needs these. Smack checks to see
6069 * if the filesystem supports xattrs by looking at the
6070 * ops vector.
6071 */
6072 inode->i_fop = &btrfs_file_operations;
6073 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006074 inode->i_mapping->a_ops = &btrfs_aops;
6075 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
6076
6077 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6078 if (err)
6079 goto out_unlock_inode;
6080
6081 err = btrfs_update_inode(trans, root, inode);
6082 if (err)
6083 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006084
Josef Bacika1b075d2010-11-19 20:36:11 +00006085 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006086 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006087 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006088
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006089 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006090 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006091 d_instantiate(dentry, inode);
6092
Chris Mason39279cc2007-06-12 06:35:45 -04006093out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006094 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006095 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006096 inode_dec_link_count(inode);
6097 iput(inode);
6098 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006099 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006100 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006101 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006102
6103out_unlock_inode:
6104 unlock_new_inode(inode);
6105 goto out_unlock;
6106
Chris Mason39279cc2007-06-12 06:35:45 -04006107}
6108
6109static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6110 struct dentry *dentry)
6111{
6112 struct btrfs_trans_handle *trans;
6113 struct btrfs_root *root = BTRFS_I(dir)->root;
6114 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006115 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006116 int err;
6117 int drop_inode = 0;
6118
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006119 /* do not allow sys_link's with other subvols of the same device */
6120 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006121 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006122
Mark Fashehf1863732012-08-08 11:32:27 -07006123 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006124 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006125
Chris Mason3de45862008-11-17 21:02:50 -05006126 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006127 if (err)
6128 goto fail;
6129
Yan, Zhenga22285a2010-05-16 10:48:46 -04006130 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006131 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006132 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006133 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006134 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006135 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006136 if (IS_ERR(trans)) {
6137 err = PTR_ERR(trans);
6138 goto fail;
6139 }
Chris Mason5f39d392007-10-15 16:14:19 -04006140
Miao Xie67de1172013-12-26 13:07:06 +08006141 /* There are several dir indexes for this inode, clear the cache. */
6142 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006143 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006144 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006145 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006146 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006147 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006148
Josef Bacika1b075d2010-11-19 20:36:11 +00006149 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006150
Yan, Zhenga5719522009-09-24 09:17:31 -04006151 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006152 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006153 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006154 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006155 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006156 if (err)
6157 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006158 if (inode->i_nlink == 1) {
6159 /*
6160 * If new hard link count is 1, it's a file created
6161 * with open(2) O_TMPFILE flag.
6162 */
6163 err = btrfs_orphan_del(trans, inode);
6164 if (err)
6165 goto fail;
6166 }
Al Viro08c422c2011-12-23 07:58:13 -05006167 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006168 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006169 }
Chris Mason39279cc2007-06-12 06:35:45 -04006170
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006171 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006172 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006173fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006174 if (drop_inode) {
6175 inode_dec_link_count(inode);
6176 iput(inode);
6177 }
Liu Bob53d3f52012-11-14 14:34:34 +00006178 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006179 return err;
6180}
6181
Al Viro18bb1db2011-07-26 01:41:39 -04006182static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006183{
Chris Masonb9d86662008-05-02 16:13:49 -04006184 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006185 struct btrfs_trans_handle *trans;
6186 struct btrfs_root *root = BTRFS_I(dir)->root;
6187 int err = 0;
6188 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006189 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006190 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006191
Josef Bacik9ed74f22009-09-11 16:12:44 -04006192 /*
6193 * 2 items for inode and ref
6194 * 2 items for dir items
6195 * 1 for xattr if selinux is on
6196 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006197 trans = btrfs_start_transaction(root, 5);
6198 if (IS_ERR(trans))
6199 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006200
Li Zefan581bb052011-04-20 10:06:11 +08006201 err = btrfs_find_free_ino(root, &objectid);
6202 if (err)
6203 goto out_fail;
6204
Josef Bacikaec74772008-07-24 12:12:38 -04006205 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006206 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006207 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006208 if (IS_ERR(inode)) {
6209 err = PTR_ERR(inode);
6210 goto out_fail;
6211 }
Chris Mason5f39d392007-10-15 16:14:19 -04006212
Chris Mason39279cc2007-06-12 06:35:45 -04006213 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006214 /* these must be set before we unlock the inode */
6215 inode->i_op = &btrfs_dir_inode_operations;
6216 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006217
Eric Paris2a7dba32011-02-01 11:05:39 -05006218 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006219 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006220 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006221
Chris Masondbe674a2008-07-17 12:54:05 -04006222 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006223 err = btrfs_update_inode(trans, root, inode);
6224 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006225 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006226
Josef Bacika1b075d2010-11-19 20:36:11 +00006227 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6228 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006229 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006230 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006231
Chris Mason39279cc2007-06-12 06:35:45 -04006232 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006233 /*
6234 * mkdir is special. We're unlocking after we call d_instantiate
6235 * to avoid a race with nfsd calling d_instantiate.
6236 */
6237 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006238 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006239
6240out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006241 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006242 if (drop_on_err)
6243 iput(inode);
Josef Bacikc581afc2014-02-06 16:06:06 -05006244 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006245 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006246 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006247
6248out_fail_inode:
6249 unlock_new_inode(inode);
6250 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006251}
6252
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006253/* Find next extent map of a given extent map, caller needs to ensure locks */
6254static struct extent_map *next_extent_map(struct extent_map *em)
6255{
6256 struct rb_node *next;
6257
6258 next = rb_next(&em->rb_node);
6259 if (!next)
6260 return NULL;
6261 return container_of(next, struct extent_map, rb_node);
6262}
6263
6264static struct extent_map *prev_extent_map(struct extent_map *em)
6265{
6266 struct rb_node *prev;
6267
6268 prev = rb_prev(&em->rb_node);
6269 if (!prev)
6270 return NULL;
6271 return container_of(prev, struct extent_map, rb_node);
6272}
6273
Chris Masond352ac62008-09-29 15:18:18 -04006274/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006275 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006276 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006277 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006278 */
Chris Mason3b951512008-04-17 11:29:12 -04006279static int merge_extent_mapping(struct extent_map_tree *em_tree,
6280 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006281 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006282 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006283{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006284 struct extent_map *prev;
6285 struct extent_map *next;
6286 u64 start;
6287 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006288 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006289
Chris Masone6dcd2d2008-07-17 12:53:50 -04006290 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006291
6292 if (existing->start > map_start) {
6293 next = existing;
6294 prev = prev_extent_map(next);
6295 } else {
6296 prev = existing;
6297 next = next_extent_map(prev);
6298 }
6299
6300 start = prev ? extent_map_end(prev) : em->start;
6301 start = max_t(u64, start, em->start);
6302 end = next ? next->start : extent_map_end(em);
6303 end = min_t(u64, end, extent_map_end(em));
6304 start_diff = start - em->start;
6305 em->start = start;
6306 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006307 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6308 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006309 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006310 em->block_len -= start_diff;
6311 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006312 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006313}
6314
Chris Masonc8b97812008-10-29 14:49:59 -04006315static noinline int uncompress_inline(struct btrfs_path *path,
6316 struct inode *inode, struct page *page,
6317 size_t pg_offset, u64 extent_offset,
6318 struct btrfs_file_extent_item *item)
6319{
6320 int ret;
6321 struct extent_buffer *leaf = path->nodes[0];
6322 char *tmp;
6323 size_t max_size;
6324 unsigned long inline_size;
6325 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006326 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006327
6328 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006329 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006330 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6331 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006332 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006333 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006334 if (!tmp)
6335 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006336 ptr = btrfs_file_extent_inline_start(item);
6337
6338 read_extent_buffer(leaf, tmp, ptr, inline_size);
6339
Chris Mason5b050f02008-11-11 09:34:41 -05006340 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006341 ret = btrfs_decompress(compress_type, tmp, page,
6342 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006343 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006344 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006345}
6346
Chris Masond352ac62008-09-29 15:18:18 -04006347/*
6348 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006349 * the ugly parts come from merging extents from the disk with the in-ram
6350 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006351 * where the in-ram extents might be locked pending data=ordered completion.
6352 *
6353 * This also copies inline extents directly into the page.
6354 */
Chris Masond3977122009-01-05 21:25:51 -05006355
Chris Masona52d9a82007-08-27 16:49:44 -04006356struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006357 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006358 int create)
6359{
6360 int ret;
6361 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006362 u64 extent_start = 0;
6363 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006364 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006365 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006366 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006367 struct btrfs_root *root = BTRFS_I(inode)->root;
6368 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006369 struct extent_buffer *leaf;
6370 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006371 struct extent_map *em = NULL;
6372 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006373 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006374 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006375 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006376
Chris Masona52d9a82007-08-27 16:49:44 -04006377again:
Chris Mason890871b2009-09-02 16:24:52 -04006378 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006379 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006380 if (em)
6381 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006382 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006383
Chris Masona52d9a82007-08-27 16:49:44 -04006384 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006385 if (em->start > start || em->start + em->len <= start)
6386 free_extent_map(em);
6387 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006388 free_extent_map(em);
6389 else
6390 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006391 }
David Sterba172ddd62011-04-21 00:48:27 +02006392 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006393 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006394 err = -ENOMEM;
6395 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006396 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006397 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006398 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006399 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006400 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006401 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006402
6403 if (!path) {
6404 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006405 if (!path) {
6406 err = -ENOMEM;
6407 goto out;
6408 }
6409 /*
6410 * Chances are we'll be called again, so go ahead and do
6411 * readahead
6412 */
6413 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006414 }
6415
Chris Mason179e29e2007-11-01 11:28:41 -04006416 ret = btrfs_lookup_file_extent(trans, root, path,
6417 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006418 if (ret < 0) {
6419 err = ret;
6420 goto out;
6421 }
6422
6423 if (ret != 0) {
6424 if (path->slots[0] == 0)
6425 goto not_found;
6426 path->slots[0]--;
6427 }
6428
Chris Mason5f39d392007-10-15 16:14:19 -04006429 leaf = path->nodes[0];
6430 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006431 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006432 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006433 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006434 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006435 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006436 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006437 /*
6438 * If we backup past the first extent we want to move forward
6439 * and see if there is an extent in front of us, otherwise we'll
6440 * say there is a hole for our whole search range which can
6441 * cause problems.
6442 */
6443 extent_end = start;
6444 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006445 }
6446
Chris Mason5f39d392007-10-15 16:14:19 -04006447 found_type = btrfs_file_extent_type(leaf, item);
6448 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006449 if (found_type == BTRFS_FILE_EXTENT_REG ||
6450 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006451 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006452 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006453 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6454 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006455 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006456 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006457 }
Josef Bacik25a50342013-10-14 12:08:38 -04006458next:
Yan Zheng9036c102008-10-30 14:19:41 -04006459 if (start >= extent_end) {
6460 path->slots[0]++;
6461 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6462 ret = btrfs_next_leaf(root, path);
6463 if (ret < 0) {
6464 err = ret;
6465 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006466 }
Yan Zheng9036c102008-10-30 14:19:41 -04006467 if (ret > 0)
6468 goto not_found;
6469 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006470 }
Yan Zheng9036c102008-10-30 14:19:41 -04006471 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6472 if (found_key.objectid != objectid ||
6473 found_key.type != BTRFS_EXTENT_DATA_KEY)
6474 goto not_found;
6475 if (start + len <= found_key.offset)
6476 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006477 if (start > found_key.offset)
6478 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006479 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006480 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006481 em->len = found_key.offset - start;
6482 goto not_found_em;
6483 }
6484
Filipe Manana7ffbb592014-06-09 03:48:05 +01006485 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6486
Yan Zhengd899e052008-10-30 14:25:28 -04006487 if (found_type == BTRFS_FILE_EXTENT_REG ||
6488 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006489 goto insert;
6490 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006491 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006492 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006493 size_t size;
6494 size_t extent_offset;
6495 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006496
Filipe Manana7ffbb592014-06-09 03:48:05 +01006497 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006498 goto out;
Yan689f9342007-10-29 11:41:07 -04006499
Chris Mason514ac8a2014-01-03 21:07:00 -08006500 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006501 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006502 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006503 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006504 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006505 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006506 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006507 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006508 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006509 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006510 if (btrfs_file_extent_compression(leaf, item) !=
6511 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006512 ret = uncompress_inline(path, inode, page,
6513 pg_offset,
6514 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006515 if (ret) {
6516 err = ret;
6517 goto out;
6518 }
Chris Masonc8b97812008-10-29 14:49:59 -04006519 } else {
6520 map = kmap(page);
6521 read_extent_buffer(leaf, map + pg_offset, ptr,
6522 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006523 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6524 memset(map + pg_offset + copy_size, 0,
6525 PAGE_CACHE_SIZE - pg_offset -
6526 copy_size);
6527 }
Chris Masonc8b97812008-10-29 14:49:59 -04006528 kunmap(page);
6529 }
Chris Mason179e29e2007-11-01 11:28:41 -04006530 flush_dcache_page(page);
6531 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006532 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006533 if (!trans) {
6534 kunmap(page);
6535 free_extent_map(em);
6536 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006537
David Sterbab3b4aa72011-04-21 01:20:15 +02006538 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006539 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006540
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006541 if (IS_ERR(trans))
6542 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006543 goto again;
6544 }
Chris Masonc8b97812008-10-29 14:49:59 -04006545 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006546 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006547 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006548 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006549 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006550 }
Chris Masond1310b22008-01-24 16:13:08 -05006551 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006552 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006553 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006554 }
6555not_found:
6556 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006557 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006558 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006559not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006560 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006561 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006562insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006563 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006564 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006565 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006566 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006567 err = -EIO;
6568 goto out;
6569 }
Chris Masond1310b22008-01-24 16:13:08 -05006570
6571 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006572 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006573 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006574 /* it is possible that someone inserted the extent into the tree
6575 * while we had the lock dropped. It is also possible that
6576 * an overlapping map exists in the tree
6577 */
Chris Masona52d9a82007-08-27 16:49:44 -04006578 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006579 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006580
6581 ret = 0;
6582
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006583 existing = search_extent_mapping(em_tree, start, len);
6584 /*
6585 * existing will always be non-NULL, since there must be
6586 * extent causing the -EEXIST.
6587 */
6588 if (start >= extent_map_end(existing) ||
6589 start + len <= existing->start) {
6590 /*
6591 * The existing extent map is the one nearest to
6592 * the [start, start + len) range which overlaps
6593 */
6594 err = merge_extent_mapping(em_tree, existing,
6595 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006596 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006597 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006598 free_extent_map(em);
6599 em = NULL;
6600 }
6601 } else {
6602 free_extent_map(em);
6603 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006604 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006605 }
Chris Masona52d9a82007-08-27 16:49:44 -04006606 }
Chris Mason890871b2009-09-02 16:24:52 -04006607 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006608out:
liubo1abe9b82011-03-24 11:18:59 +00006609
Steven Rostedt4cd85872013-11-14 22:57:29 -05006610 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006611
Chris Masonf4219502008-07-22 11:18:09 -04006612 if (path)
6613 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006614 if (trans) {
6615 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006616 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006617 err = ret;
6618 }
Chris Masona52d9a82007-08-27 16:49:44 -04006619 if (err) {
6620 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006621 return ERR_PTR(err);
6622 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006623 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006624 return em;
6625}
6626
Chris Masonec29ed52011-02-23 16:23:20 -05006627struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6628 size_t pg_offset, u64 start, u64 len,
6629 int create)
6630{
6631 struct extent_map *em;
6632 struct extent_map *hole_em = NULL;
6633 u64 range_start = start;
6634 u64 end;
6635 u64 found;
6636 u64 found_end;
6637 int err = 0;
6638
6639 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6640 if (IS_ERR(em))
6641 return em;
6642 if (em) {
6643 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006644 * if our em maps to
6645 * - a hole or
6646 * - a pre-alloc extent,
6647 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006648 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006649 if (em->block_start != EXTENT_MAP_HOLE &&
6650 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006651 return em;
6652 else
6653 hole_em = em;
6654 }
6655
6656 /* check to see if we've wrapped (len == -1 or similar) */
6657 end = start + len;
6658 if (end < start)
6659 end = (u64)-1;
6660 else
6661 end -= 1;
6662
6663 em = NULL;
6664
6665 /* ok, we didn't find anything, lets look for delalloc */
6666 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6667 end, len, EXTENT_DELALLOC, 1);
6668 found_end = range_start + found;
6669 if (found_end < range_start)
6670 found_end = (u64)-1;
6671
6672 /*
6673 * we didn't find anything useful, return
6674 * the original results from get_extent()
6675 */
6676 if (range_start > end || found_end <= start) {
6677 em = hole_em;
6678 hole_em = NULL;
6679 goto out;
6680 }
6681
6682 /* adjust the range_start to make sure it doesn't
6683 * go backwards from the start they passed in
6684 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306685 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006686 found = found_end - range_start;
6687
6688 if (found > 0) {
6689 u64 hole_start = start;
6690 u64 hole_len = len;
6691
David Sterba172ddd62011-04-21 00:48:27 +02006692 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006693 if (!em) {
6694 err = -ENOMEM;
6695 goto out;
6696 }
6697 /*
6698 * when btrfs_get_extent can't find anything it
6699 * returns one huge hole
6700 *
6701 * make sure what it found really fits our range, and
6702 * adjust to make sure it is based on the start from
6703 * the caller
6704 */
6705 if (hole_em) {
6706 u64 calc_end = extent_map_end(hole_em);
6707
6708 if (calc_end <= start || (hole_em->start > end)) {
6709 free_extent_map(hole_em);
6710 hole_em = NULL;
6711 } else {
6712 hole_start = max(hole_em->start, start);
6713 hole_len = calc_end - hole_start;
6714 }
6715 }
6716 em->bdev = NULL;
6717 if (hole_em && range_start > hole_start) {
6718 /* our hole starts before our delalloc, so we
6719 * have to return just the parts of the hole
6720 * that go until the delalloc starts
6721 */
6722 em->len = min(hole_len,
6723 range_start - hole_start);
6724 em->start = hole_start;
6725 em->orig_start = hole_start;
6726 /*
6727 * don't adjust block start at all,
6728 * it is fixed at EXTENT_MAP_HOLE
6729 */
6730 em->block_start = hole_em->block_start;
6731 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006732 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6733 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006734 } else {
6735 em->start = range_start;
6736 em->len = found;
6737 em->orig_start = range_start;
6738 em->block_start = EXTENT_MAP_DELALLOC;
6739 em->block_len = found;
6740 }
6741 } else if (hole_em) {
6742 return hole_em;
6743 }
6744out:
6745
6746 free_extent_map(hole_em);
6747 if (err) {
6748 free_extent_map(em);
6749 return ERR_PTR(err);
6750 }
6751 return em;
6752}
6753
Josef Bacik4b46fce2010-05-23 11:00:55 -04006754static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6755 u64 start, u64 len)
6756{
6757 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006758 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006759 struct btrfs_key ins;
6760 u64 alloc_hint;
6761 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006762
Josef Bacik4b46fce2010-05-23 11:00:55 -04006763 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006764 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08006765 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006766 if (ret)
6767 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006768
Josef Bacik70c8a912012-10-11 16:54:30 -04006769 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006770 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006771 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08006772 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006773 return em;
6774 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006775
6776 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6777 ins.offset, ins.offset, 0);
6778 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08006779 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006780 free_extent_map(em);
6781 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006782 }
Josef Bacik00361582013-08-14 14:02:47 -04006783
Josef Bacik4b46fce2010-05-23 11:00:55 -04006784 return em;
6785}
6786
Chris Mason46bfbb52010-05-26 11:04:10 -04006787/*
6788 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6789 * block must be cow'd
6790 */
Josef Bacik00361582013-08-14 14:02:47 -04006791noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006792 u64 *orig_start, u64 *orig_block_len,
6793 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006794{
Josef Bacik00361582013-08-14 14:02:47 -04006795 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006796 struct btrfs_path *path;
6797 int ret;
6798 struct extent_buffer *leaf;
6799 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006800 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006801 struct btrfs_file_extent_item *fi;
6802 struct btrfs_key key;
6803 u64 disk_bytenr;
6804 u64 backref_offset;
6805 u64 extent_end;
6806 u64 num_bytes;
6807 int slot;
6808 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006809 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006810
Chris Mason46bfbb52010-05-26 11:04:10 -04006811 path = btrfs_alloc_path();
6812 if (!path)
6813 return -ENOMEM;
6814
Josef Bacik00361582013-08-14 14:02:47 -04006815 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006816 offset, 0);
6817 if (ret < 0)
6818 goto out;
6819
6820 slot = path->slots[0];
6821 if (ret == 1) {
6822 if (slot == 0) {
6823 /* can't find the item, must cow */
6824 ret = 0;
6825 goto out;
6826 }
6827 slot--;
6828 }
6829 ret = 0;
6830 leaf = path->nodes[0];
6831 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006832 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006833 key.type != BTRFS_EXTENT_DATA_KEY) {
6834 /* not our file or wrong item type, must cow */
6835 goto out;
6836 }
6837
6838 if (key.offset > offset) {
6839 /* Wrong offset, must cow */
6840 goto out;
6841 }
6842
6843 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6844 found_type = btrfs_file_extent_type(leaf, fi);
6845 if (found_type != BTRFS_FILE_EXTENT_REG &&
6846 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6847 /* not a regular extent, must cow */
6848 goto out;
6849 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006850
6851 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6852 goto out;
6853
Miao Xiee77751a2013-12-27 21:11:50 +08006854 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6855 if (extent_end <= offset)
6856 goto out;
6857
Chris Mason46bfbb52010-05-26 11:04:10 -04006858 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006859 if (disk_bytenr == 0)
6860 goto out;
6861
6862 if (btrfs_file_extent_compression(leaf, fi) ||
6863 btrfs_file_extent_encryption(leaf, fi) ||
6864 btrfs_file_extent_other_encoding(leaf, fi))
6865 goto out;
6866
Chris Mason46bfbb52010-05-26 11:04:10 -04006867 backref_offset = btrfs_file_extent_offset(leaf, fi);
6868
Josef Bacik7ee9e442013-06-21 16:37:03 -04006869 if (orig_start) {
6870 *orig_start = key.offset - backref_offset;
6871 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6872 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6873 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006874
Chris Mason46bfbb52010-05-26 11:04:10 -04006875 if (btrfs_extent_readonly(root, disk_bytenr))
6876 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006877
6878 num_bytes = min(offset + *len, extent_end) - offset;
6879 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6880 u64 range_end;
6881
6882 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6883 ret = test_range_bit(io_tree, offset, range_end,
6884 EXTENT_DELALLOC, 0, NULL);
6885 if (ret) {
6886 ret = -EAGAIN;
6887 goto out;
6888 }
6889 }
6890
Josef Bacik1bda19e2013-10-18 12:10:36 -04006891 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006892
6893 /*
6894 * look for other files referencing this extent, if we
6895 * find any we must cow
6896 */
Josef Bacik00361582013-08-14 14:02:47 -04006897 trans = btrfs_join_transaction(root);
6898 if (IS_ERR(trans)) {
6899 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006900 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006901 }
6902
6903 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6904 key.offset - backref_offset, disk_bytenr);
6905 btrfs_end_transaction(trans, root);
6906 if (ret) {
6907 ret = 0;
6908 goto out;
6909 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006910
6911 /*
6912 * adjust disk_bytenr and num_bytes to cover just the bytes
6913 * in this extent we are about to write. If there
6914 * are any csums in that range we have to cow in order
6915 * to keep the csums correct
6916 */
6917 disk_bytenr += backref_offset;
6918 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006919 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6920 goto out;
6921 /*
6922 * all of the above have passed, it is safe to overwrite this extent
6923 * without cow
6924 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006925 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006926 ret = 1;
6927out:
6928 btrfs_free_path(path);
6929 return ret;
6930}
6931
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006932bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
6933{
6934 struct radix_tree_root *root = &inode->i_mapping->page_tree;
6935 int found = false;
6936 void **pagep = NULL;
6937 struct page *page = NULL;
6938 int start_idx;
6939 int end_idx;
6940
6941 start_idx = start >> PAGE_CACHE_SHIFT;
6942
6943 /*
6944 * end is the last byte in the last page. end == start is legal
6945 */
6946 end_idx = end >> PAGE_CACHE_SHIFT;
6947
6948 rcu_read_lock();
6949
6950 /* Most of the code in this while loop is lifted from
6951 * find_get_page. It's been modified to begin searching from a
6952 * page and return just the first page found in that range. If the
6953 * found idx is less than or equal to the end idx then we know that
6954 * a page exists. If no pages are found or if those pages are
6955 * outside of the range then we're fine (yay!) */
6956 while (page == NULL &&
6957 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
6958 page = radix_tree_deref_slot(pagep);
6959 if (unlikely(!page))
6960 break;
6961
6962 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01006963 if (radix_tree_deref_retry(page)) {
6964 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006965 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01006966 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006967 /*
6968 * Otherwise, shmem/tmpfs must be storing a swap entry
6969 * here as an exceptional entry: so return it without
6970 * attempting to raise page count.
6971 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01006972 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006973 break; /* TODO: Is this relevant for this use case? */
6974 }
6975
Filipe Manana91405152014-06-05 13:22:24 +01006976 if (!page_cache_get_speculative(page)) {
6977 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006978 continue;
Filipe Manana91405152014-06-05 13:22:24 +01006979 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006980
6981 /*
6982 * Has the page moved?
6983 * This is part of the lockless pagecache protocol. See
6984 * include/linux/pagemap.h for details.
6985 */
6986 if (unlikely(page != *pagep)) {
6987 page_cache_release(page);
6988 page = NULL;
6989 }
6990 }
6991
6992 if (page) {
6993 if (page->index <= end_idx)
6994 found = true;
6995 page_cache_release(page);
6996 }
6997
6998 rcu_read_unlock();
6999 return found;
7000}
7001
Josef Bacikeb838e72012-07-31 16:28:48 -04007002static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7003 struct extent_state **cached_state, int writing)
7004{
7005 struct btrfs_ordered_extent *ordered;
7006 int ret = 0;
7007
7008 while (1) {
7009 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7010 0, cached_state);
7011 /*
7012 * We're concerned with the entire range that we're going to be
7013 * doing DIO to, so we need to make sure theres no ordered
7014 * extents in this range.
7015 */
7016 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7017 lockend - lockstart + 1);
7018
7019 /*
7020 * We need to make sure there are no buffered pages in this
7021 * range either, we could have raced between the invalidate in
7022 * generic_file_direct_write and locking the extent. The
7023 * invalidate needs to happen so that reads after a write do not
7024 * get stale data.
7025 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007026 if (!ordered &&
7027 (!writing ||
7028 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007029 break;
7030
7031 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7032 cached_state, GFP_NOFS);
7033
7034 if (ordered) {
7035 btrfs_start_ordered_extent(inode, ordered, 1);
7036 btrfs_put_ordered_extent(ordered);
7037 } else {
7038 /* Screw you mmap */
7039 ret = filemap_write_and_wait_range(inode->i_mapping,
7040 lockstart,
7041 lockend);
7042 if (ret)
7043 break;
7044
7045 /*
7046 * If we found a page that couldn't be invalidated just
7047 * fall back to buffered.
7048 */
7049 ret = invalidate_inode_pages2_range(inode->i_mapping,
7050 lockstart >> PAGE_CACHE_SHIFT,
7051 lockend >> PAGE_CACHE_SHIFT);
7052 if (ret)
7053 break;
7054 }
7055
7056 cond_resched();
7057 }
7058
7059 return ret;
7060}
7061
Josef Bacik69ffb542012-09-11 15:40:07 -04007062static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7063 u64 len, u64 orig_start,
7064 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007065 u64 orig_block_len, u64 ram_bytes,
7066 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007067{
7068 struct extent_map_tree *em_tree;
7069 struct extent_map *em;
7070 struct btrfs_root *root = BTRFS_I(inode)->root;
7071 int ret;
7072
7073 em_tree = &BTRFS_I(inode)->extent_tree;
7074 em = alloc_extent_map();
7075 if (!em)
7076 return ERR_PTR(-ENOMEM);
7077
7078 em->start = start;
7079 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007080 em->mod_start = start;
7081 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007082 em->len = len;
7083 em->block_len = block_len;
7084 em->block_start = block_start;
7085 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007086 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007087 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007088 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007089 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7090 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007091 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007092
7093 do {
7094 btrfs_drop_extent_cache(inode, em->start,
7095 em->start + em->len - 1, 0);
7096 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007097 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007098 write_unlock(&em_tree->lock);
7099 } while (ret == -EEXIST);
7100
7101 if (ret) {
7102 free_extent_map(em);
7103 return ERR_PTR(ret);
7104 }
7105
7106 return em;
7107}
7108
7109
Josef Bacik4b46fce2010-05-23 11:00:55 -04007110static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7111 struct buffer_head *bh_result, int create)
7112{
7113 struct extent_map *em;
7114 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007115 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007116 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007117 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007118 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007119 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007120 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007121
Miao Xie172a5042013-02-21 02:48:22 -07007122 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04007123 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007124 else
Josef Bacikc3298612012-08-03 16:49:19 -04007125 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007126
Josef Bacikc3298612012-08-03 16:49:19 -04007127 lockstart = start;
7128 lockend = start + len - 1;
7129
Josef Bacikeb838e72012-07-31 16:28:48 -04007130 /*
7131 * If this errors out it's because we couldn't invalidate pagecache for
7132 * this range and we need to fallback to buffered.
7133 */
7134 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7135 return -ENOTBLK;
7136
Josef Bacik4b46fce2010-05-23 11:00:55 -04007137 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007138 if (IS_ERR(em)) {
7139 ret = PTR_ERR(em);
7140 goto unlock_err;
7141 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007142
7143 /*
7144 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7145 * io. INLINE is special, and we could probably kludge it in here, but
7146 * it's still buffered so for safety lets just fall back to the generic
7147 * buffered path.
7148 *
7149 * For COMPRESSED we _have_ to read the entire extent in so we can
7150 * decompress it, so there will be buffering required no matter what we
7151 * do, so go ahead and fallback to buffered.
7152 *
7153 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7154 * to buffered IO. Don't blame me, this is the price we pay for using
7155 * the generic code.
7156 */
7157 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7158 em->block_start == EXTENT_MAP_INLINE) {
7159 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007160 ret = -ENOTBLK;
7161 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007162 }
7163
7164 /* Just a good old fashioned hole, return */
7165 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7166 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7167 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007168 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007169 }
7170
7171 /*
7172 * We don't allocate a new extent in the following cases
7173 *
7174 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7175 * existing extent.
7176 * 2) The extent is marked as PREALLOC. We're good to go here and can
7177 * just use the extent.
7178 *
7179 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007180 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007181 len = min(len, em->len - (start - em->start));
7182 lockstart = start + len;
7183 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007184 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007185
7186 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7187 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7188 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007189 int type;
7190 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04007191 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007192
7193 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7194 type = BTRFS_ORDERED_PREALLOC;
7195 else
7196 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007197 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007198 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007199
Josef Bacik00361582013-08-14 14:02:47 -04007200 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007201 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007202 if (type == BTRFS_ORDERED_PREALLOC) {
7203 free_extent_map(em);
7204 em = create_pinned_em(inode, start, len,
7205 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007206 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007207 orig_block_len,
7208 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007209 if (IS_ERR(em)) {
7210 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007211 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007212 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007213 }
7214
Chris Mason46bfbb52010-05-26 11:04:10 -04007215 ret = btrfs_add_ordered_extent_dio(inode, start,
7216 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007217 if (ret) {
7218 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007219 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007220 }
7221 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007222 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007223 }
Josef Bacik00361582013-08-14 14:02:47 -04007224
Chris Mason46bfbb52010-05-26 11:04:10 -04007225 /*
7226 * this will cow the extent, reset the len in case we changed
7227 * it above
7228 */
7229 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007230 free_extent_map(em);
7231 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007232 if (IS_ERR(em)) {
7233 ret = PTR_ERR(em);
7234 goto unlock_err;
7235 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007236 len = min(len, em->len - (start - em->start));
7237unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007238 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7239 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007240 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007241 bh_result->b_bdev = em->bdev;
7242 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007243 if (create) {
7244 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7245 set_buffer_new(bh_result);
7246
7247 /*
7248 * Need to update the i_size under the extent lock so buffered
7249 * readers will get the updated i_size when we unlock.
7250 */
7251 if (start + len > i_size_read(inode))
7252 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007253
Miao Xie172a5042013-02-21 02:48:22 -07007254 spin_lock(&BTRFS_I(inode)->lock);
7255 BTRFS_I(inode)->outstanding_extents++;
7256 spin_unlock(&BTRFS_I(inode)->lock);
7257
Miao Xie09348562013-02-07 10:12:07 +00007258 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7259 lockstart + len - 1, EXTENT_DELALLOC, NULL,
7260 &cached_state, GFP_NOFS);
7261 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04007262 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007263
Josef Bacikeb838e72012-07-31 16:28:48 -04007264 /*
7265 * In the case of write we need to clear and unlock the entire range,
7266 * in the case of read we need to unlock only the end area that we
7267 * aren't using if there is any left over space.
7268 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007269 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007270 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7271 lockend, unlock_bits, 1, 0,
7272 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007273 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007274 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007275 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007276
Josef Bacik4b46fce2010-05-23 11:00:55 -04007277 free_extent_map(em);
7278
7279 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007280
7281unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007282 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7283 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7284 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007285}
7286
Miao Xie8b110e32014-09-12 18:44:03 +08007287static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7288 int rw, int mirror_num)
7289{
7290 struct btrfs_root *root = BTRFS_I(inode)->root;
7291 int ret;
7292
7293 BUG_ON(rw & REQ_WRITE);
7294
7295 bio_get(bio);
7296
7297 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7298 BTRFS_WQ_ENDIO_DIO_REPAIR);
7299 if (ret)
7300 goto err;
7301
7302 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7303err:
7304 bio_put(bio);
7305 return ret;
7306}
7307
7308static int btrfs_check_dio_repairable(struct inode *inode,
7309 struct bio *failed_bio,
7310 struct io_failure_record *failrec,
7311 int failed_mirror)
7312{
7313 int num_copies;
7314
7315 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7316 failrec->logical, failrec->len);
7317 if (num_copies == 1) {
7318 /*
7319 * we only have a single copy of the data, so don't bother with
7320 * all the retry and error correction code that follows. no
7321 * matter what the error is, it is very likely to persist.
7322 */
7323 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7324 num_copies, failrec->this_mirror, failed_mirror);
7325 return 0;
7326 }
7327
7328 failrec->failed_mirror = failed_mirror;
7329 failrec->this_mirror++;
7330 if (failrec->this_mirror == failed_mirror)
7331 failrec->this_mirror++;
7332
7333 if (failrec->this_mirror > num_copies) {
7334 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7335 num_copies, failrec->this_mirror, failed_mirror);
7336 return 0;
7337 }
7338
7339 return 1;
7340}
7341
7342static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7343 struct page *page, u64 start, u64 end,
7344 int failed_mirror, bio_end_io_t *repair_endio,
7345 void *repair_arg)
7346{
7347 struct io_failure_record *failrec;
7348 struct bio *bio;
7349 int isector;
7350 int read_mode;
7351 int ret;
7352
7353 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7354
7355 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7356 if (ret)
7357 return ret;
7358
7359 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7360 failed_mirror);
7361 if (!ret) {
7362 free_io_failure(inode, failrec);
7363 return -EIO;
7364 }
7365
7366 if (failed_bio->bi_vcnt > 1)
7367 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7368 else
7369 read_mode = READ_SYNC;
7370
7371 isector = start - btrfs_io_bio(failed_bio)->logical;
7372 isector >>= inode->i_sb->s_blocksize_bits;
7373 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7374 0, isector, repair_endio, repair_arg);
7375 if (!bio) {
7376 free_io_failure(inode, failrec);
7377 return -EIO;
7378 }
7379
7380 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7381 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7382 read_mode, failrec->this_mirror, failrec->in_validation);
7383
7384 ret = submit_dio_repair_bio(inode, bio, read_mode,
7385 failrec->this_mirror);
7386 if (ret) {
7387 free_io_failure(inode, failrec);
7388 bio_put(bio);
7389 }
7390
7391 return ret;
7392}
7393
7394struct btrfs_retry_complete {
7395 struct completion done;
7396 struct inode *inode;
7397 u64 start;
7398 int uptodate;
7399};
7400
7401static void btrfs_retry_endio_nocsum(struct bio *bio, int err)
7402{
7403 struct btrfs_retry_complete *done = bio->bi_private;
7404 struct bio_vec *bvec;
7405 int i;
7406
7407 if (err)
7408 goto end;
7409
7410 done->uptodate = 1;
7411 bio_for_each_segment_all(bvec, bio, i)
7412 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7413end:
7414 complete(&done->done);
7415 bio_put(bio);
7416}
7417
7418static int __btrfs_correct_data_nocsum(struct inode *inode,
7419 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007420{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007421 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007422 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007423 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007424 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007425 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007426
Miao Xiec1dc0892014-09-12 18:43:56 +08007427 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007428 done.inode = inode;
7429
7430 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7431try_again:
7432 done.uptodate = 0;
7433 done.start = start;
7434 init_completion(&done.done);
7435
7436 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7437 start + bvec->bv_len - 1,
7438 io_bio->mirror_num,
7439 btrfs_retry_endio_nocsum, &done);
7440 if (ret)
7441 return ret;
7442
7443 wait_for_completion(&done.done);
7444
7445 if (!done.uptodate) {
7446 /* We might have another mirror, so try again */
7447 goto try_again;
7448 }
7449
7450 start += bvec->bv_len;
7451 }
7452
7453 return 0;
7454}
7455
7456static void btrfs_retry_endio(struct bio *bio, int err)
7457{
7458 struct btrfs_retry_complete *done = bio->bi_private;
7459 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7460 struct bio_vec *bvec;
7461 int uptodate;
7462 int ret;
7463 int i;
7464
7465 if (err)
7466 goto end;
7467
7468 uptodate = 1;
7469 bio_for_each_segment_all(bvec, bio, i) {
7470 ret = __readpage_endio_check(done->inode, io_bio, i,
7471 bvec->bv_page, 0,
7472 done->start, bvec->bv_len);
7473 if (!ret)
7474 clean_io_failure(done->inode, done->start,
7475 bvec->bv_page, 0);
7476 else
7477 uptodate = 0;
7478 }
7479
7480 done->uptodate = uptodate;
7481end:
7482 complete(&done->done);
7483 bio_put(bio);
7484}
7485
7486static int __btrfs_subio_endio_read(struct inode *inode,
7487 struct btrfs_io_bio *io_bio, int err)
7488{
7489 struct bio_vec *bvec;
7490 struct btrfs_retry_complete done;
7491 u64 start;
7492 u64 offset = 0;
7493 int i;
7494 int ret;
7495
7496 err = 0;
7497 start = io_bio->logical;
7498 done.inode = inode;
7499
Miao Xiec1dc0892014-09-12 18:43:56 +08007500 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007501 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7502 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007503 if (likely(!ret))
7504 goto next;
7505try_again:
7506 done.uptodate = 0;
7507 done.start = start;
7508 init_completion(&done.done);
7509
7510 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7511 start + bvec->bv_len - 1,
7512 io_bio->mirror_num,
7513 btrfs_retry_endio, &done);
7514 if (ret) {
7515 err = ret;
7516 goto next;
7517 }
7518
7519 wait_for_completion(&done.done);
7520
7521 if (!done.uptodate) {
7522 /* We might have another mirror, so try again */
7523 goto try_again;
7524 }
7525next:
7526 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007527 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007528 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007529
7530 return err;
7531}
7532
Miao Xie8b110e32014-09-12 18:44:03 +08007533static int btrfs_subio_endio_read(struct inode *inode,
7534 struct btrfs_io_bio *io_bio, int err)
7535{
7536 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7537
7538 if (skip_csum) {
7539 if (unlikely(err))
7540 return __btrfs_correct_data_nocsum(inode, io_bio);
7541 else
7542 return 0;
7543 } else {
7544 return __btrfs_subio_endio_read(inode, io_bio, err);
7545 }
7546}
7547
Miao Xiec1dc0892014-09-12 18:43:56 +08007548static void btrfs_endio_direct_read(struct bio *bio, int err)
7549{
7550 struct btrfs_dio_private *dip = bio->bi_private;
7551 struct inode *inode = dip->inode;
7552 struct bio *dio_bio;
7553 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7554
Miao Xie8b110e32014-09-12 18:44:03 +08007555 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7556 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007557
Josef Bacik4b46fce2010-05-23 11:00:55 -04007558 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007559 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007560 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007561
Josef Bacik4b46fce2010-05-23 11:00:55 -04007562 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007563
7564 /* If we had a csum failure make sure to clear the uptodate flag */
7565 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007566 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7567 dio_end_io(dio_bio, err);
Miao Xie23ea8e52014-09-12 18:43:54 +08007568
7569 if (io_bio->end_io)
7570 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007571 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007572}
7573
7574static void btrfs_endio_direct_write(struct bio *bio, int err)
7575{
7576 struct btrfs_dio_private *dip = bio->bi_private;
7577 struct inode *inode = dip->inode;
7578 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007579 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007580 u64 ordered_offset = dip->logical_offset;
7581 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007582 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007583 int ret;
7584
7585 if (err)
7586 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007587again:
7588 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7589 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007590 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007591 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007592 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007593
Liu Bo9e0af232014-08-15 23:36:53 +08007594 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7595 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007596 btrfs_queue_work(root->fs_info->endio_write_workers,
7597 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007598out_test:
7599 /*
7600 * our bio might span multiple ordered extents. If we haven't
7601 * completed the accounting for the whole dio, go back and try again
7602 */
7603 if (ordered_offset < dip->logical_offset + dip->bytes) {
7604 ordered_bytes = dip->logical_offset + dip->bytes -
7605 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007606 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007607 goto again;
7608 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007609out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007610 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007611
Josef Bacik4b46fce2010-05-23 11:00:55 -04007612 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007613
7614 /* If we had an error make sure to clear the uptodate flag */
7615 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007616 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7617 dio_end_io(dio_bio, err);
7618 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007619}
7620
Chris Masoneaf25d92010-05-25 09:48:28 -04007621static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7622 struct bio *bio, int mirror_num,
7623 unsigned long bio_flags, u64 offset)
7624{
7625 int ret;
7626 struct btrfs_root *root = BTRFS_I(inode)->root;
7627 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007628 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007629 return 0;
7630}
7631
Miao Xiee65e1532010-11-22 03:04:43 +00007632static void btrfs_end_dio_bio(struct bio *bio, int err)
7633{
7634 struct btrfs_dio_private *dip = bio->bi_private;
7635
Miao Xie8b110e32014-09-12 18:44:03 +08007636 if (err)
7637 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
7638 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7639 btrfs_ino(dip->inode), bio->bi_rw,
7640 (unsigned long long)bio->bi_iter.bi_sector,
7641 bio->bi_iter.bi_size, err);
7642
7643 if (dip->subio_endio)
7644 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007645
7646 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007647 dip->errors = 1;
7648
7649 /*
7650 * before atomic variable goto zero, we must make sure
7651 * dip->errors is perceived to be set.
7652 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007653 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007654 }
7655
7656 /* if there are more bios still pending for this dio, just exit */
7657 if (!atomic_dec_and_test(&dip->pending_bios))
7658 goto out;
7659
Chris Mason9be33952013-05-17 18:30:14 -04007660 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007661 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007662 } else {
7663 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007664 bio_endio(dip->orig_bio, 0);
7665 }
7666out:
7667 bio_put(bio);
7668}
7669
7670static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7671 u64 first_sector, gfp_t gfp_flags)
7672{
7673 int nr_vecs = bio_get_nr_vecs(bdev);
7674 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7675}
7676
Miao Xiec1dc0892014-09-12 18:43:56 +08007677static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
7678 struct inode *inode,
7679 struct btrfs_dio_private *dip,
7680 struct bio *bio,
7681 u64 file_offset)
7682{
7683 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7684 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
7685 int ret;
7686
7687 /*
7688 * We load all the csum data we need when we submit
7689 * the first bio to reduce the csum tree search and
7690 * contention.
7691 */
7692 if (dip->logical_offset == file_offset) {
7693 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
7694 file_offset);
7695 if (ret)
7696 return ret;
7697 }
7698
7699 if (bio == dip->orig_bio)
7700 return 0;
7701
7702 file_offset -= dip->logical_offset;
7703 file_offset >>= inode->i_sb->s_blocksize_bits;
7704 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
7705
7706 return 0;
7707}
7708
Miao Xiee65e1532010-11-22 03:04:43 +00007709static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7710 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007711 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007712{
Miao Xiefacc8a222013-07-25 19:22:34 +08007713 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007714 int write = rw & REQ_WRITE;
7715 struct btrfs_root *root = BTRFS_I(inode)->root;
7716 int ret;
7717
Josef Bacikb812ce22012-11-16 13:56:32 -05007718 if (async_submit)
7719 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7720
Miao Xiee65e1532010-11-22 03:04:43 +00007721 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007722
7723 if (!write) {
7724 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7725 if (ret)
7726 goto err;
7727 }
Miao Xiee65e1532010-11-22 03:04:43 +00007728
Josef Bacik1ae39932011-04-06 14:41:34 -04007729 if (skip_sum)
7730 goto map;
7731
7732 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007733 ret = btrfs_wq_submit_bio(root->fs_info,
7734 inode, rw, bio, 0, 0,
7735 file_offset,
7736 __btrfs_submit_bio_start_direct_io,
7737 __btrfs_submit_bio_done);
7738 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007739 } else if (write) {
7740 /*
7741 * If we aren't doing async submit, calculate the csum of the
7742 * bio now.
7743 */
7744 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7745 if (ret)
7746 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007747 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08007748 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
7749 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007750 if (ret)
7751 goto err;
7752 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007753map:
7754 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007755err:
7756 bio_put(bio);
7757 return ret;
7758}
7759
7760static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7761 int skip_sum)
7762{
7763 struct inode *inode = dip->inode;
7764 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007765 struct bio *bio;
7766 struct bio *orig_bio = dip->orig_bio;
7767 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007768 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007769 u64 file_offset = dip->logical_offset;
7770 u64 submit_len = 0;
7771 u64 map_length;
7772 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08007773 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04007774 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007775
Kent Overstreet4f024f32013-10-11 15:44:27 -07007776 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007777 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007778 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007779 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007780 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08007781
Kent Overstreet4f024f32013-10-11 15:44:27 -07007782 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007783 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007784 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04007785 goto submit;
7786 }
7787
David Woodhouse53b381b2013-01-29 18:40:14 -05007788 /* async crcs make it difficult to collect full stripe writes. */
7789 if (btrfs_get_alloc_profile(root, 1) &
7790 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7791 async_submit = 0;
7792 else
7793 async_submit = 1;
7794
Josef Bacik02f57c72011-04-06 14:25:44 -04007795 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7796 if (!bio)
7797 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08007798
Josef Bacik02f57c72011-04-06 14:25:44 -04007799 bio->bi_private = dip;
7800 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007801 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04007802 atomic_inc(&dip->pending_bios);
7803
Miao Xiee65e1532010-11-22 03:04:43 +00007804 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7805 if (unlikely(map_length < submit_len + bvec->bv_len ||
7806 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7807 bvec->bv_offset) < bvec->bv_len)) {
7808 /*
7809 * inc the count before we submit the bio so
7810 * we know the end IO handler won't happen before
7811 * we inc the count. Otherwise, the dip might get freed
7812 * before we're done setting it up
7813 */
7814 atomic_inc(&dip->pending_bios);
7815 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7816 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007817 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007818 if (ret) {
7819 bio_put(bio);
7820 atomic_dec(&dip->pending_bios);
7821 goto out_err;
7822 }
7823
Miao Xiee65e1532010-11-22 03:04:43 +00007824 start_sector += submit_len >> 9;
7825 file_offset += submit_len;
7826
7827 submit_len = 0;
7828 nr_pages = 0;
7829
7830 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7831 start_sector, GFP_NOFS);
7832 if (!bio)
7833 goto out_err;
7834 bio->bi_private = dip;
7835 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007836 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007837
Kent Overstreet4f024f32013-10-11 15:44:27 -07007838 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007839 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007840 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007841 &map_length, NULL, 0);
7842 if (ret) {
7843 bio_put(bio);
7844 goto out_err;
7845 }
7846 } else {
7847 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307848 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007849 bvec++;
7850 }
7851 }
7852
Josef Bacik02f57c72011-04-06 14:25:44 -04007853submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007854 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007855 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007856 if (!ret)
7857 return 0;
7858
7859 bio_put(bio);
7860out_err:
7861 dip->errors = 1;
7862 /*
7863 * before atomic variable goto zero, we must
7864 * make sure dip->errors is perceived to be set.
7865 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007866 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007867 if (atomic_dec_and_test(&dip->pending_bios))
7868 bio_io_error(dip->orig_bio);
7869
7870 /* bio_end_io() will handle error, so we needn't return it */
7871 return 0;
7872}
7873
Chris Mason9be33952013-05-17 18:30:14 -04007874static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7875 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007876{
7877 struct btrfs_root *root = BTRFS_I(inode)->root;
7878 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007879 struct bio *io_bio;
Miao Xie23ea8e52014-09-12 18:43:54 +08007880 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007881 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007882 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007883 int ret = 0;
7884
7885 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7886
Chris Mason9be33952013-05-17 18:30:14 -04007887 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007888 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007889 ret = -ENOMEM;
7890 goto free_ordered;
7891 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007892
Miao Xiec1dc0892014-09-12 18:43:56 +08007893 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007894 if (!dip) {
7895 ret = -ENOMEM;
7896 goto free_io_bio;
7897 }
7898
7899 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007900 dip->inode = inode;
7901 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007902 dip->bytes = dio_bio->bi_iter.bi_size;
7903 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04007904 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04007905 dip->orig_bio = io_bio;
7906 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007907 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08007908 btrfs_bio = btrfs_io_bio(io_bio);
7909 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007910
Miao Xiec1dc0892014-09-12 18:43:56 +08007911 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04007912 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08007913 } else {
Chris Mason9be33952013-05-17 18:30:14 -04007914 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08007915 dip->subio_endio = btrfs_subio_endio_read;
7916 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007917
Miao Xiee65e1532010-11-22 03:04:43 +00007918 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7919 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007920 return;
Chris Mason9be33952013-05-17 18:30:14 -04007921
Miao Xie23ea8e52014-09-12 18:43:54 +08007922 if (btrfs_bio->end_io)
7923 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04007924free_io_bio:
7925 bio_put(io_bio);
7926
Josef Bacik4b46fce2010-05-23 11:00:55 -04007927free_ordered:
7928 /*
7929 * If this is a write, we need to clean up the reserved space and kill
7930 * the ordered extent.
7931 */
7932 if (write) {
7933 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007934 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007935 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7936 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7937 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08007938 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007939 btrfs_put_ordered_extent(ordered);
7940 btrfs_put_ordered_extent(ordered);
7941 }
Chris Mason9be33952013-05-17 18:30:14 -04007942 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007943}
7944
Chris Mason5a5f79b2010-05-26 21:33:37 -04007945static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04007946 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04007947{
7948 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007949 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007950 unsigned blocksize_mask = root->sectorsize - 1;
7951 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007952
7953 if (offset & blocksize_mask)
7954 goto out;
7955
Al Viro28060d52014-03-22 05:15:17 -04007956 if (iov_iter_alignment(iter) & blocksize_mask)
7957 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007958
Al Viro28060d52014-03-22 05:15:17 -04007959 /* If this is a write we don't need to check anymore */
7960 if (rw & WRITE)
7961 return 0;
7962 /*
7963 * Check to make sure we don't have duplicate iov_base's in this
7964 * iovec, if so return EINVAL, otherwise we'll get csum errors
7965 * when reading back.
7966 */
7967 for (seg = 0; seg < iter->nr_segs; seg++) {
7968 for (i = seg + 1; i < iter->nr_segs; i++) {
7969 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00007970 goto out;
7971 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007972 }
7973 retval = 0;
7974out:
7975 return retval;
7976}
Josef Bacikeb838e72012-07-31 16:28:48 -04007977
Chris Mason16432982008-04-10 10:23:21 -04007978static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05007979 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04007980{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007981 struct file *file = iocb->ki_filp;
7982 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007983 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007984 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007985 bool wakeup = true;
7986 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007987 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007988
Al Viro28060d52014-03-22 05:15:17 -04007989 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007990 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007991
Miao Xie38851cc2013-02-08 07:04:11 +00007992 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007993 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00007994
Josef Bacik0e267c42013-07-02 10:38:02 -04007995 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08007996 * The generic stuff only does filemap_write_and_wait_range, which
7997 * isn't enough if we've written compressed pages to this area, so
7998 * we need to flush the dirty pages again to make absolutely sure
7999 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008000 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008001 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008002 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8003 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008004 filemap_fdatawrite_range(inode->i_mapping, offset,
8005 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008006
Miao Xie09348562013-02-07 10:12:07 +00008007 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008008 /*
8009 * If the write DIO is beyond the EOF, we need update
8010 * the isize, but it is protected by i_mutex. So we can
8011 * not unlock the i_mutex at this case.
8012 */
8013 if (offset + count <= inode->i_size) {
8014 mutex_unlock(&inode->i_mutex);
8015 relock = true;
8016 }
Miao Xie09348562013-02-07 10:12:07 +00008017 ret = btrfs_delalloc_reserve_space(inode, count);
8018 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008019 goto out;
8020 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8021 &BTRFS_I(inode)->runtime_flags))) {
8022 inode_dio_done(inode);
8023 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8024 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008025 }
8026
8027 ret = __blockdev_direct_IO(rw, iocb, inode,
8028 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05008029 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00008030 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00008031 if (rw & WRITE) {
8032 if (ret < 0 && ret != -EIOCBQUEUED)
8033 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07008034 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00008035 btrfs_delalloc_release_space(inode,
8036 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07008037 else
8038 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00008039 }
Miao Xie38851cc2013-02-08 07:04:11 +00008040out:
Miao Xie2e60a512013-02-08 07:01:08 +00008041 if (wakeup)
8042 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008043 if (relock)
8044 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008045
8046 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008047}
8048
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008049#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8050
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008051static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8052 __u64 start, __u64 len)
8053{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008054 int ret;
8055
8056 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8057 if (ret)
8058 return ret;
8059
Chris Masonec29ed52011-02-23 16:23:20 -05008060 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008061}
8062
Chris Mason9ebefb182007-06-15 13:50:00 -04008063int btrfs_readpage(struct file *file, struct page *page)
8064{
Chris Masond1310b22008-01-24 16:13:08 -05008065 struct extent_io_tree *tree;
8066 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008067 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008068}
Chris Mason1832a6d2007-12-21 16:27:21 -05008069
Chris Mason39279cc2007-06-12 06:35:45 -04008070static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8071{
Chris Masond1310b22008-01-24 16:13:08 -05008072 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04008073
8074
8075 if (current->flags & PF_MEMALLOC) {
8076 redirty_page_for_writepage(wbc, page);
8077 unlock_page(page);
8078 return 0;
8079 }
Chris Masond1310b22008-01-24 16:13:08 -05008080 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008081 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8082}
Chris Mason39279cc2007-06-12 06:35:45 -04008083
Eric Sandeen48a3b632013-04-25 20:41:01 +00008084static int btrfs_writepages(struct address_space *mapping,
8085 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008086{
Chris Masond1310b22008-01-24 16:13:08 -05008087 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008088
Chris Masond1310b22008-01-24 16:13:08 -05008089 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008090 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8091}
8092
Chris Mason3ab2fb52007-11-08 10:59:22 -05008093static int
8094btrfs_readpages(struct file *file, struct address_space *mapping,
8095 struct list_head *pages, unsigned nr_pages)
8096{
Chris Masond1310b22008-01-24 16:13:08 -05008097 struct extent_io_tree *tree;
8098 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008099 return extent_readpages(tree, mapping, pages, nr_pages,
8100 btrfs_get_extent);
8101}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008102static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008103{
Chris Masond1310b22008-01-24 16:13:08 -05008104 struct extent_io_tree *tree;
8105 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008106 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008107
Chris Masond1310b22008-01-24 16:13:08 -05008108 tree = &BTRFS_I(page->mapping->host)->io_tree;
8109 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008110 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008111 if (ret == 1) {
8112 ClearPagePrivate(page);
8113 set_page_private(page, 0);
8114 page_cache_release(page);
8115 }
8116 return ret;
8117}
Chris Mason39279cc2007-06-12 06:35:45 -04008118
Chris Masone6dcd2d2008-07-17 12:53:50 -04008119static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8120{
Chris Mason98509cf2008-09-11 15:51:43 -04008121 if (PageWriteback(page) || PageDirty(page))
8122 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008123 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008124}
8125
Lukas Czernerd47992f2013-05-21 23:17:23 -04008126static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8127 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008128{
Josef Bacik5fd02042012-05-02 14:00:54 -04008129 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008130 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008131 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008132 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008133 u64 page_start = page_offset(page);
8134 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008135 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008136
Chris Mason8b62b722009-09-02 16:53:46 -04008137 /*
8138 * we have the page locked, so new writeback can't start,
8139 * and the dirty bit won't be cleared while we are here.
8140 *
8141 * Wait for IO on this page so that we can safely clear
8142 * the PagePrivate2 bit and do ordered accounting
8143 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008144 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008145
Josef Bacik5fd02042012-05-02 14:00:54 -04008146 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008147 if (offset) {
8148 btrfs_releasepage(page, GFP_NOFS);
8149 return;
8150 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008151
8152 if (!inode_evicting)
8153 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8154 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008155 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008156 /*
8157 * IO on this page will never be started, so we need
8158 * to account for any ordered extents now
8159 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008160 if (!inode_evicting)
8161 clear_extent_bit(tree, page_start, page_end,
8162 EXTENT_DIRTY | EXTENT_DELALLOC |
8163 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8164 EXTENT_DEFRAG, 1, 0, &cached_state,
8165 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008166 /*
8167 * whoever cleared the private bit is responsible
8168 * for the finish_ordered_io
8169 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008170 if (TestClearPagePrivate2(page)) {
8171 struct btrfs_ordered_inode_tree *tree;
8172 u64 new_len;
8173
8174 tree = &BTRFS_I(inode)->ordered_tree;
8175
8176 spin_lock_irq(&tree->lock);
8177 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8178 new_len = page_start - ordered->file_offset;
8179 if (new_len < ordered->truncated_len)
8180 ordered->truncated_len = new_len;
8181 spin_unlock_irq(&tree->lock);
8182
8183 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8184 page_start,
8185 PAGE_CACHE_SIZE, 1))
8186 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008187 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008188 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008189 if (!inode_evicting) {
8190 cached_state = NULL;
8191 lock_extent_bits(tree, page_start, page_end, 0,
8192 &cached_state);
8193 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008194 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008195
8196 if (!inode_evicting) {
8197 clear_extent_bit(tree, page_start, page_end,
8198 EXTENT_LOCKED | EXTENT_DIRTY |
8199 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8200 EXTENT_DEFRAG, 1, 1,
8201 &cached_state, GFP_NOFS);
8202
8203 __btrfs_releasepage(page, GFP_NOFS);
8204 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008205
Chris Mason4a096752008-07-21 10:29:44 -04008206 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008207 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008208 ClearPagePrivate(page);
8209 set_page_private(page, 0);
8210 page_cache_release(page);
8211 }
Chris Mason39279cc2007-06-12 06:35:45 -04008212}
8213
Chris Mason9ebefb182007-06-15 13:50:00 -04008214/*
8215 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8216 * called from a page fault handler when a page is first dirtied. Hence we must
8217 * be careful to check for EOF conditions here. We set the page up correctly
8218 * for a written page which means we get ENOSPC checking when writing into
8219 * holes and correct delalloc and unwritten extent mapping on filesystems that
8220 * support these features.
8221 *
8222 * We are not allowed to take the i_mutex here so we have to play games to
8223 * protect against truncate races as the page could now be beyond EOF. Because
8224 * vmtruncate() writes the inode size before removing pages, once we have the
8225 * page lock we can determine safely if the page is beyond EOF. If it is not
8226 * beyond EOF, then the page is guaranteed safe against truncation until we
8227 * unlock the page.
8228 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008229int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008230{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008231 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008232 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008233 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008234 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8235 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008236 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008237 char *kaddr;
8238 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008239 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008240 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008241 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008242 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008243 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008244
Jan Karab2b5ef52012-06-12 16:20:45 +02008245 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04008246 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008247 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008248 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008249 reserved = 1;
8250 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008251 if (ret) {
8252 if (ret == -ENOMEM)
8253 ret = VM_FAULT_OOM;
8254 else /* -ENOSPC, -EIO, etc */
8255 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008256 if (reserved)
8257 goto out;
8258 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008259 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008260
Nick Piggin56a76f82009-03-31 15:23:23 -07008261 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008262again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008263 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008264 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008265 page_start = page_offset(page);
8266 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04008267
Chris Mason9ebefb182007-06-15 13:50:00 -04008268 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008269 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008270 /* page got truncated out from underneath us */
8271 goto out_unlock;
8272 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008273 wait_on_page_writeback(page);
8274
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008275 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008276 set_page_extent_mapped(page);
8277
Chris Masoneb84ae02008-07-17 13:53:27 -04008278 /*
8279 * we can't set the delalloc bits if there are pending ordered
8280 * extents. Drop our locks and wait for them to finish
8281 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008282 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8283 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008284 unlock_extent_cached(io_tree, page_start, page_end,
8285 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008286 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008287 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008288 btrfs_put_ordered_extent(ordered);
8289 goto again;
8290 }
8291
Josef Bacikfbf19082009-10-01 17:10:23 -04008292 /*
8293 * XXX - page_mkwrite gets called every time the page is dirtied, even
8294 * if it was already dirty, so for space accounting reasons we need to
8295 * clear any delalloc bits for the range we are fixing to save. There
8296 * is probably a better way to do this, but for now keep consistent with
8297 * prepare_pages in the normal write path.
8298 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008299 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008300 EXTENT_DIRTY | EXTENT_DELALLOC |
8301 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008302 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008303
Josef Bacik2ac55d42010-02-03 19:33:23 +00008304 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8305 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008306 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008307 unlock_extent_cached(io_tree, page_start, page_end,
8308 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008309 ret = VM_FAULT_SIGBUS;
8310 goto out_unlock;
8311 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008312 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008313
8314 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008315 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008316 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008317 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008318 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008319
Chris Masone6dcd2d2008-07-17 12:53:50 -04008320 if (zero_start != PAGE_CACHE_SIZE) {
8321 kaddr = kmap(page);
8322 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8323 flush_dcache_page(page);
8324 kunmap(page);
8325 }
Chris Mason247e7432008-07-17 12:53:51 -04008326 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008327 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008328 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008329
Chris Mason257c62e2009-10-13 13:21:08 -04008330 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8331 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008332 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008333
Josef Bacik2ac55d42010-02-03 19:33:23 +00008334 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008335
8336out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008337 if (!ret) {
8338 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008339 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008340 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008341 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008342out:
Josef Bacikec39e182012-01-12 19:10:12 -05008343 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008344out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008345 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008346 return ret;
8347}
8348
Josef Bacika41ad392011-01-31 15:30:16 -05008349static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008350{
8351 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008352 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008353 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008354 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008355 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008356 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008357 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008358
Josef Bacik0ef8b722013-10-25 16:13:35 -04008359 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8360 (u64)-1);
8361 if (ret)
8362 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008363
Josef Bacikfcb80c22011-05-03 10:40:22 -04008364 /*
8365 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8366 * 3 things going on here
8367 *
8368 * 1) We need to reserve space for our orphan item and the space to
8369 * delete our orphan item. Lord knows we don't want to have a dangling
8370 * orphan item because we didn't reserve space to remove it.
8371 *
8372 * 2) We need to reserve space to update our inode.
8373 *
8374 * 3) We need to have something to cache all the space that is going to
8375 * be free'd up by the truncate operation, but also have some slack
8376 * space reserved in case it uses space during the truncate (thank you
8377 * very much snapshotting).
8378 *
8379 * And we need these to all be seperate. The fact is we can use alot of
8380 * space doing the truncate, and we have no earthly idea how much space
8381 * we will use, so we need the truncate reservation to be seperate so it
8382 * doesn't end up using space reserved for updating the inode or
8383 * removing the orphan item. We also need to be able to stop the
8384 * transaction and start a new one, which means we need to be able to
8385 * update the inode several times, and we have no idea of knowing how
8386 * many times that will be, so we can't just reserve 1 item for the
8387 * entirety of the opration, so that has to be done seperately as well.
8388 * Then there is the orphan item, which does indeed need to be held on
8389 * to for the whole operation, and we need nobody to touch this reserved
8390 * space except the orphan code.
8391 *
8392 * So that leaves us with
8393 *
8394 * 1) root->orphan_block_rsv - for the orphan deletion.
8395 * 2) rsv - for the truncate reservation, which we will steal from the
8396 * transaction reservation.
8397 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8398 * updating the inode.
8399 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008400 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008401 if (!rsv)
8402 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008403 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008404 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008405
Josef Bacik907cbce2011-08-08 13:46:15 -04008406 /*
Josef Bacik07127182011-08-19 10:29:59 -04008407 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008408 * 1 for updating the inode.
8409 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008410 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008411 if (IS_ERR(trans)) {
8412 err = PTR_ERR(trans);
8413 goto out;
8414 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008415
Josef Bacik907cbce2011-08-08 13:46:15 -04008416 /* Migrate the slack space for the truncate to our reserve */
8417 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8418 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008419 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008420
Chris Mason5a3f23d2009-03-31 13:27:11 -04008421 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008422 * So if we truncate and then write and fsync we normally would just
8423 * write the extents that changed, which is a problem if we need to
8424 * first truncate that entire inode. So set this flag so we write out
8425 * all of the extents in the inode to the sync log so we're completely
8426 * safe.
8427 */
8428 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008429 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008430
Yan, Zheng80825102009-11-12 09:35:36 +00008431 while (1) {
8432 ret = btrfs_truncate_inode_items(trans, root, inode,
8433 inode->i_size,
8434 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008435 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05008436 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008437 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008438 }
Chris Mason39279cc2007-06-12 06:35:45 -04008439
Josef Bacikfcb80c22011-05-03 10:40:22 -04008440 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008441 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008442 if (ret) {
8443 err = ret;
8444 break;
8445 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008446
Yan, Zheng80825102009-11-12 09:35:36 +00008447 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008448 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008449
8450 trans = btrfs_start_transaction(root, 2);
8451 if (IS_ERR(trans)) {
8452 ret = err = PTR_ERR(trans);
8453 trans = NULL;
8454 break;
8455 }
8456
8457 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8458 rsv, min_size);
8459 BUG_ON(ret); /* shouldn't happen */
8460 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008461 }
8462
8463 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008464 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008465 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008466 if (ret)
8467 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008468 }
8469
Chris Mason917c16b2011-11-08 14:49:59 -05008470 if (trans) {
8471 trans->block_rsv = &root->fs_info->trans_block_rsv;
8472 ret = btrfs_update_inode(trans, root, inode);
8473 if (ret && !err)
8474 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008475
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008476 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008477 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008478 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008479
8480out:
8481 btrfs_free_block_rsv(root, rsv);
8482
Josef Bacik3893e332011-01-31 16:03:11 -05008483 if (ret && !err)
8484 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008485
Josef Bacik3893e332011-01-31 16:03:11 -05008486 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008487}
8488
Sven Wegener3b963622008-06-09 21:57:42 -04008489/*
Chris Masond352ac62008-09-29 15:18:18 -04008490 * create a new subvolume directory/inode (helper for the ioctl).
8491 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008492int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008493 struct btrfs_root *new_root,
8494 struct btrfs_root *parent_root,
8495 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008496{
Chris Mason39279cc2007-06-12 06:35:45 -04008497 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008498 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008499 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008500
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008501 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8502 new_dirid, new_dirid,
8503 S_IFDIR | (~current_umask() & S_IRWXUGO),
8504 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008505 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008506 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008507 inode->i_op = &btrfs_dir_inode_operations;
8508 inode->i_fop = &btrfs_dir_file_operations;
8509
Miklos Szeredibfe86842011-10-28 14:13:29 +02008510 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008511 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008512 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008513
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008514 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8515 if (err)
8516 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008517 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008518 new_root->root_key.objectid, err);
8519
Yan, Zheng76dda932009-09-21 16:00:26 -04008520 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008521
Yan, Zheng76dda932009-09-21 16:00:26 -04008522 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008523 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008524}
8525
Chris Mason39279cc2007-06-12 06:35:45 -04008526struct inode *btrfs_alloc_inode(struct super_block *sb)
8527{
8528 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008529 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008530
8531 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8532 if (!ei)
8533 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008534
8535 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008536 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008537 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008538 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008539 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008540 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008541 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008542 ei->disk_i_size = 0;
8543 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008544 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008545 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008546 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008547 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008548 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008549
Josef Bacik9e0baf62011-07-15 15:16:44 +00008550 spin_lock_init(&ei->lock);
8551 ei->outstanding_extents = 0;
8552 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008553
Josef Bacik72ac3c02012-05-23 14:13:11 -04008554 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008555 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008556
Miao Xie16cdcec2011-04-22 18:12:22 +08008557 ei->delayed_node = NULL;
8558
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008559 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008560 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008561 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8562 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008563 ei->io_tree.track_uptodate = 1;
8564 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008565 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008566 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008567 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008568 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008569 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008570 RB_CLEAR_NODE(&ei->rb_node);
8571
8572 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008573}
8574
Josef Bacikaaedb552013-10-11 14:44:09 -04008575#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8576void btrfs_test_destroy_inode(struct inode *inode)
8577{
8578 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8579 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8580}
8581#endif
8582
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008583static void btrfs_i_callback(struct rcu_head *head)
8584{
8585 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008586 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8587}
8588
Chris Mason39279cc2007-06-12 06:35:45 -04008589void btrfs_destroy_inode(struct inode *inode)
8590{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008591 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008592 struct btrfs_root *root = BTRFS_I(inode)->root;
8593
Al Virob3d9b7a2012-06-09 13:51:19 -04008594 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008595 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008596 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8597 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008598 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8599 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008600 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008601
Chris Mason5a3f23d2009-03-31 13:27:11 -04008602 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008603 * This can happen where we create an inode, but somebody else also
8604 * created the same inode and we need to destroy the one we already
8605 * created.
8606 */
8607 if (!root)
8608 goto free;
8609
Josef Bacik8a35d952012-05-23 14:26:42 -04008610 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8611 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008612 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008613 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008614 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008615 }
Josef Bacik7b128762008-07-24 12:17:14 -04008616
Chris Masond3977122009-01-05 21:25:51 -05008617 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008618 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8619 if (!ordered)
8620 break;
8621 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008622 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008623 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008624 btrfs_remove_ordered_extent(inode, ordered);
8625 btrfs_put_ordered_extent(ordered);
8626 btrfs_put_ordered_extent(ordered);
8627 }
8628 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008629 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008630 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008631free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008632 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008633}
8634
Al Viro45321ac2010-06-07 13:43:19 -04008635int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008636{
8637 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008638
Naohiro Aota6379ef92013-06-06 09:56:34 +00008639 if (root == NULL)
8640 return 1;
8641
Liu Bofa6ac872013-02-20 14:10:23 +00008642 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008643 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008644 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008645 else
Al Viro45321ac2010-06-07 13:43:19 -04008646 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008647}
8648
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008649static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008650{
8651 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8652
8653 inode_init_once(&ei->vfs_inode);
8654}
8655
8656void btrfs_destroy_cachep(void)
8657{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008658 /*
8659 * Make sure all delayed rcu free inodes are flushed before we
8660 * destroy cache.
8661 */
8662 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008663 if (btrfs_inode_cachep)
8664 kmem_cache_destroy(btrfs_inode_cachep);
8665 if (btrfs_trans_handle_cachep)
8666 kmem_cache_destroy(btrfs_trans_handle_cachep);
8667 if (btrfs_transaction_cachep)
8668 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008669 if (btrfs_path_cachep)
8670 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008671 if (btrfs_free_space_cachep)
8672 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008673 if (btrfs_delalloc_work_cachep)
8674 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008675}
8676
8677int btrfs_init_cachep(void)
8678{
David Sterba837e1972012-09-07 03:00:48 -06008679 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008680 sizeof(struct btrfs_inode), 0,
8681 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008682 if (!btrfs_inode_cachep)
8683 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008684
David Sterba837e1972012-09-07 03:00:48 -06008685 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008686 sizeof(struct btrfs_trans_handle), 0,
8687 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008688 if (!btrfs_trans_handle_cachep)
8689 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008690
David Sterba837e1972012-09-07 03:00:48 -06008691 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008692 sizeof(struct btrfs_transaction), 0,
8693 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008694 if (!btrfs_transaction_cachep)
8695 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008696
David Sterba837e1972012-09-07 03:00:48 -06008697 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008698 sizeof(struct btrfs_path), 0,
8699 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008700 if (!btrfs_path_cachep)
8701 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008702
David Sterba837e1972012-09-07 03:00:48 -06008703 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008704 sizeof(struct btrfs_free_space), 0,
8705 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8706 if (!btrfs_free_space_cachep)
8707 goto fail;
8708
Miao Xie8ccf6f192012-10-25 09:28:04 +00008709 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8710 sizeof(struct btrfs_delalloc_work), 0,
8711 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8712 NULL);
8713 if (!btrfs_delalloc_work_cachep)
8714 goto fail;
8715
Chris Mason39279cc2007-06-12 06:35:45 -04008716 return 0;
8717fail:
8718 btrfs_destroy_cachep();
8719 return -ENOMEM;
8720}
8721
8722static int btrfs_getattr(struct vfsmount *mnt,
8723 struct dentry *dentry, struct kstat *stat)
8724{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008725 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008726 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008727 u32 blocksize = inode->i_sb->s_blocksize;
8728
Chris Mason39279cc2007-06-12 06:35:45 -04008729 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008730 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008731 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008732
8733 spin_lock(&BTRFS_I(inode)->lock);
8734 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8735 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008736 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008737 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008738 return 0;
8739}
8740
Chris Masond3977122009-01-05 21:25:51 -05008741static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8742 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008743{
8744 struct btrfs_trans_handle *trans;
8745 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008746 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008747 struct inode *new_inode = new_dentry->d_inode;
8748 struct inode *old_inode = old_dentry->d_inode;
8749 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008750 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008751 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008752 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008753 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008754
Li Zefan33345d012011-04-20 10:31:50 +08008755 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008756 return -EPERM;
8757
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008758 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008759 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008760 return -EXDEV;
8761
Li Zefan33345d012011-04-20 10:31:50 +08008762 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8763 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008764 return -ENOTEMPTY;
8765
Chris Mason39279cc2007-06-12 06:35:45 -04008766 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008767 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008768 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008769
8770
8771 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008772 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008773 new_dentry->d_name.name,
8774 new_dentry->d_name.len);
8775
8776 if (ret) {
8777 if (ret == -EEXIST) {
8778 /* we shouldn't get
8779 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308780 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008781 return ret;
8782 }
8783 } else {
8784 /* maybe -EOVERFLOW */
8785 return ret;
8786 }
8787 }
8788 ret = 0;
8789
Chris Mason5a3f23d2009-03-31 13:27:11 -04008790 /*
Chris Mason8d875f92014-08-12 10:47:42 -07008791 * we're using rename to replace one file with another. Start IO on it
8792 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04008793 */
Chris Mason8d875f92014-08-12 10:47:42 -07008794 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04008795 filemap_flush(old_inode->i_mapping);
8796
Yan, Zheng76dda932009-09-21 16:00:26 -04008797 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008798 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008799 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008800 /*
8801 * We want to reserve the absolute worst case amount of items. So if
8802 * both inodes are subvols and we need to unlink them then that would
8803 * require 4 item modifications, but if they are both normal inodes it
8804 * would require 5 item modifications, so we'll assume their normal
8805 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8806 * should cover the worst case number of items we'll modify.
8807 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008808 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008809 if (IS_ERR(trans)) {
8810 ret = PTR_ERR(trans);
8811 goto out_notrans;
8812 }
Chris Mason5f39d392007-10-15 16:14:19 -04008813
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008814 if (dest != root)
8815 btrfs_record_root_in_trans(trans, dest);
8816
Yan, Zhenga5719522009-09-24 09:17:31 -04008817 ret = btrfs_set_inode_index(new_dir, &index);
8818 if (ret)
8819 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008820
Miao Xie67de1172013-12-26 13:07:06 +08008821 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008822 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008823 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08008824 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008825 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008826 ret = btrfs_insert_inode_ref(trans, dest,
8827 new_dentry->d_name.name,
8828 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008829 old_ino,
8830 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008831 if (ret)
8832 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008833 /*
8834 * this is an ugly little race, but the rename is required
8835 * to make sure that if we crash, the inode is either at the
8836 * old name or the new one. pinning the log transaction lets
8837 * us make sure we don't allow a log commit to come in after
8838 * we unlink the name but before we add the new name back in.
8839 */
8840 btrfs_pin_log_trans(root);
8841 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04008842
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008843 inode_inc_iversion(old_dir);
8844 inode_inc_iversion(new_dir);
8845 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008846 old_dir->i_ctime = old_dir->i_mtime = ctime;
8847 new_dir->i_ctime = new_dir->i_mtime = ctime;
8848 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008849
Chris Mason12fcfd22009-03-24 10:24:20 -04008850 if (old_dentry->d_parent != new_dentry->d_parent)
8851 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8852
Li Zefan33345d012011-04-20 10:31:50 +08008853 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008854 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8855 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8856 old_dentry->d_name.name,
8857 old_dentry->d_name.len);
8858 } else {
Al Viro92986792011-03-04 17:14:37 +00008859 ret = __btrfs_unlink_inode(trans, root, old_dir,
8860 old_dentry->d_inode,
8861 old_dentry->d_name.name,
8862 old_dentry->d_name.len);
8863 if (!ret)
8864 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008865 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008866 if (ret) {
8867 btrfs_abort_transaction(trans, root, ret);
8868 goto out_fail;
8869 }
Chris Mason39279cc2007-06-12 06:35:45 -04008870
8871 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008872 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008873 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008874 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008875 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8876 root_objectid = BTRFS_I(new_inode)->location.objectid;
8877 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8878 root_objectid,
8879 new_dentry->d_name.name,
8880 new_dentry->d_name.len);
8881 BUG_ON(new_inode->i_nlink == 0);
8882 } else {
8883 ret = btrfs_unlink_inode(trans, dest, new_dir,
8884 new_dentry->d_inode,
8885 new_dentry->d_name.name,
8886 new_dentry->d_name.len);
8887 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008888 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008889 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008890 if (ret) {
8891 btrfs_abort_transaction(trans, root, ret);
8892 goto out_fail;
8893 }
Chris Mason39279cc2007-06-12 06:35:45 -04008894 }
Josef Bacikaec74772008-07-24 12:12:38 -04008895
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008896 ret = btrfs_add_link(trans, new_dir, old_inode,
8897 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008898 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008899 if (ret) {
8900 btrfs_abort_transaction(trans, root, ret);
8901 goto out_fail;
8902 }
Chris Mason39279cc2007-06-12 06:35:45 -04008903
Miao Xie67de1172013-12-26 13:07:06 +08008904 if (old_inode->i_nlink == 1)
8905 BTRFS_I(old_inode)->dir_index = index;
8906
Li Zefan33345d012011-04-20 10:31:50 +08008907 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008908 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008909 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008910 btrfs_end_log_trans(root);
8911 }
Chris Mason39279cc2007-06-12 06:35:45 -04008912out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008913 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008914out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008915 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008916 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008917
Chris Mason39279cc2007-06-12 06:35:45 -04008918 return ret;
8919}
8920
Miklos Szeredi80ace852014-07-23 15:15:32 +02008921static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
8922 struct inode *new_dir, struct dentry *new_dentry,
8923 unsigned int flags)
8924{
8925 if (flags & ~RENAME_NOREPLACE)
8926 return -EINVAL;
8927
8928 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
8929}
8930
Miao Xie8ccf6f192012-10-25 09:28:04 +00008931static void btrfs_run_delalloc_work(struct btrfs_work *work)
8932{
8933 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008934 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008935
8936 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8937 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008938 inode = delalloc_work->inode;
8939 if (delalloc_work->wait) {
8940 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8941 } else {
8942 filemap_flush(inode->i_mapping);
8943 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8944 &BTRFS_I(inode)->runtime_flags))
8945 filemap_flush(inode->i_mapping);
8946 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008947
8948 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008949 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008950 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008951 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008952 complete(&delalloc_work->completion);
8953}
8954
8955struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8956 int wait, int delay_iput)
8957{
8958 struct btrfs_delalloc_work *work;
8959
8960 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8961 if (!work)
8962 return NULL;
8963
8964 init_completion(&work->completion);
8965 INIT_LIST_HEAD(&work->list);
8966 work->inode = inode;
8967 work->wait = wait;
8968 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08008969 WARN_ON_ONCE(!inode);
8970 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
8971 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008972
8973 return work;
8974}
8975
8976void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8977{
8978 wait_for_completion(&work->completion);
8979 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8980}
8981
Chris Masond352ac62008-09-29 15:18:18 -04008982/*
8983 * some fairly slow code that needs optimization. This walks the list
8984 * of all the inodes with pending delalloc and forces them to disk.
8985 */
Miao Xie6c255e62014-03-06 13:55:01 +08008986static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
8987 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04008988{
Chris Masonea8c2812008-08-04 23:17:27 -04008989 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008990 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008991 struct btrfs_delalloc_work *work, *next;
8992 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008993 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008994 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008995
Miao Xie8ccf6f192012-10-25 09:28:04 +00008996 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008997 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008998
Miao Xie573bfb72014-03-06 13:55:03 +08008999 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009000 spin_lock(&root->delalloc_lock);
9001 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009002 while (!list_empty(&splice)) {
9003 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009004 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009005
Miao Xieeb73c1b2013-05-15 07:48:22 +00009006 list_move_tail(&binode->delalloc_inodes,
9007 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009008 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009009 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009010 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009011 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009012 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009013 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009014
9015 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009016 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009017 if (delay_iput)
9018 btrfs_add_delayed_iput(inode);
9019 else
9020 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009021 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009022 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009023 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009024 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009025 btrfs_queue_work(root->fs_info->flush_workers,
9026 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009027 ret++;
9028 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009029 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009030 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009031 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009032 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009033 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009034
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009035out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009036 list_for_each_entry_safe(work, next, &works, list) {
9037 list_del_init(&work->list);
9038 btrfs_wait_and_free_delalloc_work(work);
9039 }
9040
Miao Xieeb73c1b2013-05-15 07:48:22 +00009041 if (!list_empty_careful(&splice)) {
9042 spin_lock(&root->delalloc_lock);
9043 list_splice_tail(&splice, &root->delalloc_inodes);
9044 spin_unlock(&root->delalloc_lock);
9045 }
Miao Xie573bfb72014-03-06 13:55:03 +08009046 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009047 return ret;
9048}
9049
9050int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9051{
9052 int ret;
9053
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009054 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009055 return -EROFS;
9056
Miao Xie6c255e62014-03-06 13:55:01 +08009057 ret = __start_delalloc_inodes(root, delay_iput, -1);
9058 if (ret > 0)
9059 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009060 /*
9061 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009062 * we have to make sure the IO is actually started and that
9063 * ordered extents get created before we return
9064 */
9065 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009066 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009067 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009068 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009069 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9070 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009071 }
9072 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009073 return ret;
9074}
9075
Miao Xie6c255e62014-03-06 13:55:01 +08009076int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9077 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009078{
9079 struct btrfs_root *root;
9080 struct list_head splice;
9081 int ret;
9082
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009083 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009084 return -EROFS;
9085
9086 INIT_LIST_HEAD(&splice);
9087
Miao Xie573bfb72014-03-06 13:55:03 +08009088 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009089 spin_lock(&fs_info->delalloc_root_lock);
9090 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009091 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009092 root = list_first_entry(&splice, struct btrfs_root,
9093 delalloc_root);
9094 root = btrfs_grab_fs_root(root);
9095 BUG_ON(!root);
9096 list_move_tail(&root->delalloc_root,
9097 &fs_info->delalloc_roots);
9098 spin_unlock(&fs_info->delalloc_root_lock);
9099
Miao Xie6c255e62014-03-06 13:55:01 +08009100 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009101 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009102 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009103 goto out;
9104
Miao Xie6c255e62014-03-06 13:55:01 +08009105 if (nr != -1) {
9106 nr -= ret;
9107 WARN_ON(nr < 0);
9108 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009109 spin_lock(&fs_info->delalloc_root_lock);
9110 }
9111 spin_unlock(&fs_info->delalloc_root_lock);
9112
Miao Xie6c255e62014-03-06 13:55:01 +08009113 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009114 atomic_inc(&fs_info->async_submit_draining);
9115 while (atomic_read(&fs_info->nr_async_submits) ||
9116 atomic_read(&fs_info->async_delalloc_pages)) {
9117 wait_event(fs_info->async_submit_wait,
9118 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9119 atomic_read(&fs_info->async_delalloc_pages) == 0));
9120 }
9121 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009122out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009123 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009124 spin_lock(&fs_info->delalloc_root_lock);
9125 list_splice_tail(&splice, &fs_info->delalloc_roots);
9126 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009127 }
Miao Xie573bfb72014-03-06 13:55:03 +08009128 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009129 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009130}
9131
Chris Mason39279cc2007-06-12 06:35:45 -04009132static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9133 const char *symname)
9134{
9135 struct btrfs_trans_handle *trans;
9136 struct btrfs_root *root = BTRFS_I(dir)->root;
9137 struct btrfs_path *path;
9138 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009139 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009140 int err;
9141 int drop_inode = 0;
9142 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309143 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009144 int name_len;
9145 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009146 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009147 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009148 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009149
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009150 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009151 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9152 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009153
Josef Bacik9ed74f22009-09-11 16:12:44 -04009154 /*
9155 * 2 items for inode item and ref
9156 * 2 items for dir items
9157 * 1 item for xattr if selinux is on
9158 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009159 trans = btrfs_start_transaction(root, 5);
9160 if (IS_ERR(trans))
9161 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009162
Li Zefan581bb052011-04-20 10:06:11 +08009163 err = btrfs_find_free_ino(root, &objectid);
9164 if (err)
9165 goto out_unlock;
9166
Josef Bacikaec74772008-07-24 12:12:38 -04009167 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009168 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04009169 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009170 if (IS_ERR(inode)) {
9171 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009172 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009173 }
Chris Mason39279cc2007-06-12 06:35:45 -04009174
Casey Schauflerad19db72011-12-15 10:09:07 -05009175 /*
9176 * If the active LSM wants to access the inode during
9177 * d_instantiate it needs these. Smack checks to see
9178 * if the filesystem supports xattrs by looking at the
9179 * ops vector.
9180 */
9181 inode->i_fop = &btrfs_file_operations;
9182 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009183 inode->i_mapping->a_ops = &btrfs_aops;
9184 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9185 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9186
9187 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9188 if (err)
9189 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009190
Josef Bacika1b075d2010-11-19 20:36:11 +00009191 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009192 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009193 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009194
9195 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009196 if (!path) {
9197 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009198 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009199 }
Li Zefan33345d012011-04-20 10:31:50 +08009200 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009201 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009202 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009203 datasize = btrfs_file_extent_calc_inline_size(name_len);
9204 err = btrfs_insert_empty_item(trans, root, path, &key,
9205 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009206 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009207 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009208 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009209 }
Chris Mason5f39d392007-10-15 16:14:19 -04009210 leaf = path->nodes[0];
9211 ei = btrfs_item_ptr(leaf, path->slots[0],
9212 struct btrfs_file_extent_item);
9213 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9214 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009215 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009216 btrfs_set_file_extent_encryption(leaf, ei, 0);
9217 btrfs_set_file_extent_compression(leaf, ei, 0);
9218 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9219 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9220
Chris Mason39279cc2007-06-12 06:35:45 -04009221 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009222 write_extent_buffer(leaf, symname, ptr, name_len);
9223 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009224 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009225
Chris Mason39279cc2007-06-12 06:35:45 -04009226 inode->i_op = &btrfs_symlink_inode_operations;
9227 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04009228 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04009229 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009230 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009231 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009232 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009233 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009234 goto out_unlock_inode;
9235 }
9236
9237 unlock_new_inode(inode);
9238 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009239
9240out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009241 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009242 if (drop_inode) {
9243 inode_dec_link_count(inode);
9244 iput(inode);
9245 }
Liu Bob53d3f52012-11-14 14:34:34 +00009246 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009247 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009248
9249out_unlock_inode:
9250 drop_inode = 1;
9251 unlock_new_inode(inode);
9252 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009253}
Chris Mason16432982008-04-10 10:23:21 -04009254
Josef Bacik0af3d002010-06-21 14:48:16 -04009255static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9256 u64 start, u64 num_bytes, u64 min_size,
9257 loff_t actual_len, u64 *alloc_hint,
9258 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009259{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009260 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9261 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009262 struct btrfs_root *root = BTRFS_I(inode)->root;
9263 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009264 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009265 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009266 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009267 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009268 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009269
Josef Bacik0af3d002010-06-21 14:48:16 -04009270 if (trans)
9271 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009272 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009273 if (own_trans) {
9274 trans = btrfs_start_transaction(root, 3);
9275 if (IS_ERR(trans)) {
9276 ret = PTR_ERR(trans);
9277 break;
9278 }
Yan Zhengd899e052008-10-30 14:25:28 -04009279 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009280
Chris Mason154ea282013-03-05 11:11:26 -05009281 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9282 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009283 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009284 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009285 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009286 if (own_trans)
9287 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009288 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009289 }
9290
Yan Zhengd899e052008-10-30 14:25:28 -04009291 ret = insert_reserved_file_extent(trans, inode,
9292 cur_offset, ins.objectid,
9293 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009294 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009295 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009296 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009297 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009298 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009299 btrfs_abort_transaction(trans, root, ret);
9300 if (own_trans)
9301 btrfs_end_transaction(trans, root);
9302 break;
9303 }
Chris Masona1ed8352009-09-11 12:27:37 -04009304 btrfs_drop_extent_cache(inode, cur_offset,
9305 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009306
Josef Bacik5dc562c2012-08-17 13:14:17 -04009307 em = alloc_extent_map();
9308 if (!em) {
9309 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9310 &BTRFS_I(inode)->runtime_flags);
9311 goto next;
9312 }
9313
9314 em->start = cur_offset;
9315 em->orig_start = cur_offset;
9316 em->len = ins.offset;
9317 em->block_start = ins.objectid;
9318 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009319 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009320 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009321 em->bdev = root->fs_info->fs_devices->latest_bdev;
9322 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9323 em->generation = trans->transid;
9324
9325 while (1) {
9326 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009327 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009328 write_unlock(&em_tree->lock);
9329 if (ret != -EEXIST)
9330 break;
9331 btrfs_drop_extent_cache(inode, cur_offset,
9332 cur_offset + ins.offset - 1,
9333 0);
9334 }
9335 free_extent_map(em);
9336next:
Yan Zhengd899e052008-10-30 14:25:28 -04009337 num_bytes -= ins.offset;
9338 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009339 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009340
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009341 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009342 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009343 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009344 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009345 (actual_len > inode->i_size) &&
9346 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009347 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009348 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009349 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009350 i_size = cur_offset;
9351 i_size_write(inode, i_size);
9352 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009353 }
9354
Yan Zhengd899e052008-10-30 14:25:28 -04009355 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009356
9357 if (ret) {
9358 btrfs_abort_transaction(trans, root, ret);
9359 if (own_trans)
9360 btrfs_end_transaction(trans, root);
9361 break;
9362 }
Yan Zhengd899e052008-10-30 14:25:28 -04009363
Josef Bacik0af3d002010-06-21 14:48:16 -04009364 if (own_trans)
9365 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009366 }
Yan Zhengd899e052008-10-30 14:25:28 -04009367 return ret;
9368}
9369
Josef Bacik0af3d002010-06-21 14:48:16 -04009370int btrfs_prealloc_file_range(struct inode *inode, int mode,
9371 u64 start, u64 num_bytes, u64 min_size,
9372 loff_t actual_len, u64 *alloc_hint)
9373{
9374 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9375 min_size, actual_len, alloc_hint,
9376 NULL);
9377}
9378
9379int btrfs_prealloc_file_range_trans(struct inode *inode,
9380 struct btrfs_trans_handle *trans, int mode,
9381 u64 start, u64 num_bytes, u64 min_size,
9382 loff_t actual_len, u64 *alloc_hint)
9383{
9384 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9385 min_size, actual_len, alloc_hint, trans);
9386}
9387
Chris Masone6dcd2d2008-07-17 12:53:50 -04009388static int btrfs_set_page_dirty(struct page *page)
9389{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009390 return __set_page_dirty_nobuffers(page);
9391}
9392
Al Viro10556cb2011-06-20 19:28:19 -04009393static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009394{
Li Zefanb83cc962010-12-20 16:04:08 +08009395 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009396 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009397
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009398 if (mask & MAY_WRITE &&
9399 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9400 if (btrfs_root_readonly(root))
9401 return -EROFS;
9402 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9403 return -EACCES;
9404 }
Al Viro2830ba72011-06-20 19:16:29 -04009405 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009406}
Chris Mason39279cc2007-06-12 06:35:45 -04009407
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009408static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9409{
9410 struct btrfs_trans_handle *trans;
9411 struct btrfs_root *root = BTRFS_I(dir)->root;
9412 struct inode *inode = NULL;
9413 u64 objectid;
9414 u64 index;
9415 int ret = 0;
9416
9417 /*
9418 * 5 units required for adding orphan entry
9419 */
9420 trans = btrfs_start_transaction(root, 5);
9421 if (IS_ERR(trans))
9422 return PTR_ERR(trans);
9423
9424 ret = btrfs_find_free_ino(root, &objectid);
9425 if (ret)
9426 goto out;
9427
9428 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9429 btrfs_ino(dir), objectid, mode, &index);
9430 if (IS_ERR(inode)) {
9431 ret = PTR_ERR(inode);
9432 inode = NULL;
9433 goto out;
9434 }
9435
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009436 inode->i_fop = &btrfs_file_operations;
9437 inode->i_op = &btrfs_file_inode_operations;
9438
9439 inode->i_mapping->a_ops = &btrfs_aops;
9440 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9441 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9442
Chris Masonb0d5d102014-09-08 13:08:51 -07009443 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9444 if (ret)
9445 goto out_inode;
9446
9447 ret = btrfs_update_inode(trans, root, inode);
9448 if (ret)
9449 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009450 ret = btrfs_orphan_add(trans, inode);
9451 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009452 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009453
Filipe Manana5762b5c2014-08-01 00:10:32 +01009454 /*
9455 * We set number of links to 0 in btrfs_new_inode(), and here we set
9456 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9457 * through:
9458 *
9459 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9460 */
9461 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009462 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009463 d_tmpfile(dentry, inode);
9464 mark_inode_dirty(inode);
9465
9466out:
9467 btrfs_end_transaction(trans, root);
9468 if (ret)
9469 iput(inode);
9470 btrfs_balance_delayed_items(root);
9471 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009472 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009473
9474out_inode:
9475 unlock_new_inode(inode);
9476 goto out;
9477
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009478}
9479
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009480static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009481 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009482 .lookup = btrfs_lookup,
9483 .create = btrfs_create,
9484 .unlink = btrfs_unlink,
9485 .link = btrfs_link,
9486 .mkdir = btrfs_mkdir,
9487 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009488 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009489 .symlink = btrfs_symlink,
9490 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009491 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009492 .setxattr = btrfs_setxattr,
9493 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009494 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009495 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009496 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009497 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009498 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009499 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009500 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009501};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009502static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009503 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009504 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009505 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009506 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009507 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009508};
Yan, Zheng76dda932009-09-21 16:00:26 -04009509
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009510static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009511 .llseek = generic_file_llseek,
9512 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009513 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009514 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009515#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009516 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009517#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009518 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009519 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009520};
9521
Chris Masond1310b22008-01-24 16:13:08 -05009522static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009523 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009524 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009525 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009526 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009527 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009528 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009529 .set_bit_hook = btrfs_set_bit_hook,
9530 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009531 .merge_extent_hook = btrfs_merge_extent_hook,
9532 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009533};
9534
Chris Mason35054392009-01-21 13:11:13 -05009535/*
9536 * btrfs doesn't support the bmap operation because swapfiles
9537 * use bmap to make a mapping of extents in the file. They assume
9538 * these extents won't change over the life of the file and they
9539 * use the bmap result to do IO directly to the drive.
9540 *
9541 * the btrfs bmap call would return logical addresses that aren't
9542 * suitable for IO and they also will change frequently as COW
9543 * operations happen. So, swapfile + btrfs == corruption.
9544 *
9545 * For now we're avoiding this by dropping bmap.
9546 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009547static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009548 .readpage = btrfs_readpage,
9549 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04009550 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009551 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009552 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009553 .invalidatepage = btrfs_invalidatepage,
9554 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009555 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009556 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009557};
9558
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009559static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009560 .readpage = btrfs_readpage,
9561 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009562 .invalidatepage = btrfs_invalidatepage,
9563 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009564};
9565
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009566static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009567 .getattr = btrfs_getattr,
9568 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009569 .setxattr = btrfs_setxattr,
9570 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009571 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009572 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009573 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009574 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009575 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009576 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009577 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009578};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009579static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009580 .getattr = btrfs_getattr,
9581 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009582 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009583 .setxattr = btrfs_setxattr,
9584 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009585 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009586 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009587 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009588 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009589 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009590};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009591static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009592 .readlink = generic_readlink,
9593 .follow_link = page_follow_link_light,
9594 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009595 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009596 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009597 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009598 .setxattr = btrfs_setxattr,
9599 .getxattr = btrfs_getxattr,
9600 .listxattr = btrfs_listxattr,
9601 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009602 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009603};
Yan, Zheng76dda932009-09-21 16:00:26 -04009604
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009605const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009606 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009607 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009608};