blob: b759585fbc6abf844791e675af4c981ff23d79ff [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070035#include <linux/aio.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050037#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040038#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040039#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020041#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050042#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000043#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050044#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040045#include <linux/posix_acl_xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Chris Mason39279cc2007-06-12 06:35:45 -040062
63struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080064 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040065 struct btrfs_root *root;
66};
67
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_dir_inode_operations;
69static const struct inode_operations btrfs_symlink_inode_operations;
70static const struct inode_operations btrfs_dir_ro_inode_operations;
71static const struct inode_operations btrfs_special_inode_operations;
72static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070073static const struct address_space_operations btrfs_aops;
74static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070075static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050076static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040077
78static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000079static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080struct kmem_cache *btrfs_trans_handle_cachep;
81struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040082struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050083struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040084
85#define S_SHIFT 12
86static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
87 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
88 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
89 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
90 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
91 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
92 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
93 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
94};
95
Eric Sandeen3972f262013-01-12 02:57:22 +000096static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050097static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040098static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050099static noinline int cow_file_range(struct inode *inode,
100 struct page *locked_page,
101 u64 start, u64 end, int *page_started,
102 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400103static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
104 u64 len, u64 orig_start,
105 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400106 u64 orig_block_len, u64 ram_bytes,
107 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400108
Eric Sandeen48a3b632013-04-25 20:41:01 +0000109static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400110
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000111static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500112 struct inode *inode, struct inode *dir,
113 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500114{
115 int err;
116
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000117 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500118 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500119 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500120 return err;
121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/*
Chris Masonc8b97812008-10-29 14:49:59 -0400124 * this does all the hard work for inserting an inline extent into
125 * the btree. The caller should have done a btrfs_drop_extents so that
126 * no overlapping inline items exist in the btree
127 */
Chris Mason40f76582014-05-21 13:35:51 -0700128static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000129 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400130 struct btrfs_root *root, struct inode *inode,
131 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000132 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400133 struct page **compressed_pages)
134{
Chris Masonc8b97812008-10-29 14:49:59 -0400135 struct extent_buffer *leaf;
136 struct page *page = NULL;
137 char *kaddr;
138 unsigned long ptr;
139 struct btrfs_file_extent_item *ei;
140 int err = 0;
141 int ret;
142 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400143 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400144
Li Zefanfe3f5662011-03-28 08:30:38 +0000145 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400146 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400147
Chris Masonc8b97812008-10-29 14:49:59 -0400148 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000149
150 if (!extent_inserted) {
151 struct btrfs_key key;
152 size_t datasize;
153
154 key.objectid = btrfs_ino(inode);
155 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200156 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000157
158 datasize = btrfs_file_extent_calc_inline_size(cur_size);
159 path->leave_spinning = 1;
160 ret = btrfs_insert_empty_item(trans, root, path, &key,
161 datasize);
162 if (ret) {
163 err = ret;
164 goto fail;
165 }
Chris Masonc8b97812008-10-29 14:49:59 -0400166 }
167 leaf = path->nodes[0];
168 ei = btrfs_item_ptr(leaf, path->slots[0],
169 struct btrfs_file_extent_item);
170 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
171 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
172 btrfs_set_file_extent_encryption(leaf, ei, 0);
173 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
174 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
175 ptr = btrfs_file_extent_inline_start(ei);
176
Li Zefan261507a02010-12-17 14:21:50 +0800177 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400178 struct page *cpage;
179 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500180 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400181 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500182 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400183 PAGE_CACHE_SIZE);
184
Cong Wang7ac687d2011-11-25 23:14:28 +0800185 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400186 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800187 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400188
189 i++;
190 ptr += cur_size;
191 compressed_size -= cur_size;
192 }
193 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800194 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400195 } else {
196 page = find_get_page(inode->i_mapping,
197 start >> PAGE_CACHE_SHIFT);
198 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800199 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400200 offset = start & (PAGE_CACHE_SIZE - 1);
201 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800202 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400203 page_cache_release(page);
204 }
205 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400207
Yan, Zhengc2167752009-11-12 09:34:21 +0000208 /*
209 * we're an inline extent, so nobody can
210 * extend the file past i_size without locking
211 * a page we already have locked.
212 *
213 * We must do any isize and inode updates
214 * before we unlock the pages. Otherwise we
215 * could end up racing with unlink.
216 */
Chris Masonc8b97812008-10-29 14:49:59 -0400217 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100218 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000219
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100220 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400221fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400222 return err;
223}
224
225
226/*
227 * conditionally insert an inline extent into the file. This
228 * does the checks required to make sure the data is small enough
229 * to fit as an inline extent.
230 */
Josef Bacik00361582013-08-14 14:02:47 -0400231static noinline int cow_file_range_inline(struct btrfs_root *root,
232 struct inode *inode, u64 start,
233 u64 end, size_t compressed_size,
234 int compress_type,
235 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400236{
Josef Bacik00361582013-08-14 14:02:47 -0400237 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400238 u64 isize = i_size_read(inode);
239 u64 actual_end = min(end + 1, isize);
240 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000241 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400242 u64 data_len = inline_len;
243 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000244 struct btrfs_path *path;
245 int extent_inserted = 0;
246 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400247
248 if (compressed_size)
249 data_len = compressed_size;
250
251 if (start > 0 ||
Wang Shilong354877b2014-07-17 11:44:11 +0800252 actual_end > PAGE_CACHE_SIZE ||
253 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400254 (!compressed_size &&
255 (actual_end & (root->sectorsize - 1)) == 0) ||
256 end + 1 < isize ||
257 data_len > root->fs_info->max_inline) {
258 return 1;
259 }
260
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000261 path = btrfs_alloc_path();
262 if (!path)
263 return -ENOMEM;
264
Josef Bacik00361582013-08-14 14:02:47 -0400265 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000266 if (IS_ERR(trans)) {
267 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400268 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000269 }
Josef Bacik00361582013-08-14 14:02:47 -0400270 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
271
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000272 if (compressed_size && compressed_pages)
273 extent_item_size = btrfs_file_extent_calc_inline_size(
274 compressed_size);
275 else
276 extent_item_size = btrfs_file_extent_calc_inline_size(
277 inline_len);
278
279 ret = __btrfs_drop_extents(trans, root, inode, path,
280 start, aligned_end, NULL,
281 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400282 if (ret) {
283 btrfs_abort_transaction(trans, root, ret);
284 goto out;
285 }
Chris Masonc8b97812008-10-29 14:49:59 -0400286
287 if (isize > actual_end)
288 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000289 ret = insert_inline_extent(trans, path, extent_inserted,
290 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400291 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000292 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400293 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100294 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400295 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400296 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400297 ret = 1;
298 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100299 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400300
Josef Bacikbdc20e62013-02-28 13:23:38 -0500301 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400302 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400303 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400304out:
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400306 btrfs_end_transaction(trans, root);
307 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400308}
309
Chris Mason771ed682008-11-06 22:02:51 -0500310struct async_extent {
311 u64 start;
312 u64 ram_size;
313 u64 compressed_size;
314 struct page **pages;
315 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800316 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500317 struct list_head list;
318};
319
320struct async_cow {
321 struct inode *inode;
322 struct btrfs_root *root;
323 struct page *locked_page;
324 u64 start;
325 u64 end;
326 struct list_head extents;
327 struct btrfs_work work;
328};
329
330static noinline int add_async_extent(struct async_cow *cow,
331 u64 start, u64 ram_size,
332 u64 compressed_size,
333 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800334 unsigned long nr_pages,
335 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500336{
337 struct async_extent *async_extent;
338
339 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100340 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500341 async_extent->start = start;
342 async_extent->ram_size = ram_size;
343 async_extent->compressed_size = compressed_size;
344 async_extent->pages = pages;
345 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800346 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500347 list_add_tail(&async_extent->list, &cow->extents);
348 return 0;
349}
350
Wang Shilongf79707b2014-07-17 11:44:09 +0800351static inline int inode_need_compress(struct inode *inode)
352{
353 struct btrfs_root *root = BTRFS_I(inode)->root;
354
355 /* force compress */
356 if (btrfs_test_opt(root, FORCE_COMPRESS))
357 return 1;
358 /* bad compression ratios */
359 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
360 return 0;
361 if (btrfs_test_opt(root, COMPRESS) ||
362 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
363 BTRFS_I(inode)->force_compress)
364 return 1;
365 return 0;
366}
367
Chris Masonc8b97812008-10-29 14:49:59 -0400368/*
Chris Mason771ed682008-11-06 22:02:51 -0500369 * we create compressed extents in two phases. The first
370 * phase compresses a range of pages that have already been
371 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400372 *
Chris Mason771ed682008-11-06 22:02:51 -0500373 * This is done inside an ordered work queue, and the compression
374 * is spread across many cpus. The actual IO submission is step
375 * two, and the ordered work queue takes care of making sure that
376 * happens in the same order things were put onto the queue by
377 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400378 *
Chris Mason771ed682008-11-06 22:02:51 -0500379 * If this code finds it can't get good compression, it puts an
380 * entry onto the work queue to write the uncompressed bytes. This
381 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300382 * are written in the same order that the flusher thread sent them
383 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400384 */
Chris Mason771ed682008-11-06 22:02:51 -0500385static noinline int compress_file_range(struct inode *inode,
386 struct page *locked_page,
387 u64 start, u64 end,
388 struct async_cow *async_cow,
389 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400390{
391 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400392 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400393 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400394 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500395 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400396 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400397 struct page **pages = NULL;
398 unsigned long nr_pages;
399 unsigned long nr_pages_ret = 0;
400 unsigned long total_compressed = 0;
401 unsigned long total_in = 0;
402 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500403 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400404 int i;
405 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800406 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400407 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400408
Liu Bo4cb13e52012-03-29 09:57:45 -0400409 /* if this is a small write inside eof, kick off a defrag */
410 if ((end - start + 1) < 16 * 1024 &&
411 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400412 btrfs_add_inode_defrag(NULL, inode);
413
Wang Shilong68bb4622014-04-01 18:01:42 +0800414 /*
415 * skip compression for a small file range(<=blocksize) that
416 * isn't an inline extent, since it dosen't save disk space at all.
417 */
418 if ((end - start + 1) <= blocksize &&
419 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
420 goto cleanup_and_bail_uncompressed;
421
Chris Mason42dc7ba2008-12-15 11:44:56 -0500422 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400423again:
424 will_compress = 0;
425 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
426 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
427
Chris Masonf03d9301f2009-02-04 09:31:06 -0500428 /*
429 * we don't want to send crud past the end of i_size through
430 * compression, that's just a waste of CPU time. So, if the
431 * end of the file is before the start of our current
432 * requested range of bytes, we bail out to the uncompressed
433 * cleanup code that can deal with all of this.
434 *
435 * It isn't really the fastest way to fix things, but this is a
436 * very uncommon corner.
437 */
438 if (actual_end <= start)
439 goto cleanup_and_bail_uncompressed;
440
Chris Masonc8b97812008-10-29 14:49:59 -0400441 total_compressed = actual_end - start;
442
443 /* we want to make sure that amount of ram required to uncompress
444 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500445 * of a compressed extent to 128k. This is a crucial number
446 * because it also controls how easily we can spread reads across
447 * cpus for decompression.
448 *
449 * We also want to make sure the amount of IO required to do
450 * a random read is reasonably small, so we limit the size of
451 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400452 */
453 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000454 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500455 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400456 total_in = 0;
457 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400458
Chris Mason771ed682008-11-06 22:02:51 -0500459 /*
460 * we do compression for mount -o compress and when the
461 * inode has not been flagged as nocompress. This flag can
462 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400463 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800464 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400465 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400466 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800467 if (!pages) {
468 /* just bail out to the uncompressed code */
469 goto cont;
470 }
Chris Mason179e29e2007-11-01 11:28:41 -0400471
Li Zefan261507a02010-12-17 14:21:50 +0800472 if (BTRFS_I(inode)->force_compress)
473 compress_type = BTRFS_I(inode)->force_compress;
474
Chris Mason4adaa612013-03-26 13:07:00 -0400475 /*
476 * we need to call clear_page_dirty_for_io on each
477 * page in the range. Otherwise applications with the file
478 * mmap'd can wander in and change the page contents while
479 * we are compressing them.
480 *
481 * If the compression fails for any reason, we set the pages
482 * dirty again later on.
483 */
484 extent_range_clear_dirty_for_io(inode, start, end);
485 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800486 ret = btrfs_compress_pages(compress_type,
487 inode->i_mapping, start,
488 total_compressed, pages,
489 nr_pages, &nr_pages_ret,
490 &total_in,
491 &total_compressed,
492 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400493
494 if (!ret) {
495 unsigned long offset = total_compressed &
496 (PAGE_CACHE_SIZE - 1);
497 struct page *page = pages[nr_pages_ret - 1];
498 char *kaddr;
499
500 /* zero the tail end of the last page, we might be
501 * sending it down to disk
502 */
503 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800504 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400505 memset(kaddr + offset, 0,
506 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800507 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400508 }
509 will_compress = 1;
510 }
511 }
Li Zefan560f7d72011-09-08 10:22:01 +0800512cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400513 if (start == 0) {
514 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500515 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400516 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500517 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400518 */
Josef Bacik00361582013-08-14 14:02:47 -0400519 ret = cow_file_range_inline(root, inode, start, end,
520 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400521 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500522 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400523 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000524 total_compressed,
525 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400526 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100527 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400528 unsigned long clear_flags = EXTENT_DELALLOC |
529 EXTENT_DEFRAG;
530 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
531
Chris Mason771ed682008-11-06 22:02:51 -0500532 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100533 * inline extent creation worked or returned error,
534 * we don't need to create any more async work items.
535 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500536 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400537 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400538 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400539 PAGE_CLEAR_DIRTY |
540 PAGE_SET_WRITEBACK |
541 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400542 goto free_pages_out;
543 }
544 }
545
546 if (will_compress) {
547 /*
548 * we aren't doing an inline extent round the compressed size
549 * up to a block size boundary so the allocator does sane
550 * things
551 */
Qu Wenruofda28322013-02-26 08:10:22 +0000552 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400553
554 /*
555 * one last check to make sure the compression is really a
556 * win, compare the page count read with the blocks on disk
557 */
Qu Wenruofda28322013-02-26 08:10:22 +0000558 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400559 if (total_compressed >= total_in) {
560 will_compress = 0;
561 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400562 num_bytes = total_in;
563 }
564 }
565 if (!will_compress && pages) {
566 /*
567 * the compression code ran but failed to make things smaller,
568 * free any pages it allocated and our page pointer array
569 */
570 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400571 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400572 page_cache_release(pages[i]);
573 }
574 kfree(pages);
575 pages = NULL;
576 total_compressed = 0;
577 nr_pages_ret = 0;
578
579 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500580 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
581 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500582 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500583 }
Chris Masonc8b97812008-10-29 14:49:59 -0400584 }
Chris Mason771ed682008-11-06 22:02:51 -0500585 if (will_compress) {
586 *num_added += 1;
587
588 /* the async work queues will take care of doing actual
589 * allocation on disk for these compressed pages,
590 * and will submit them to the elevator.
591 */
592 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800593 total_compressed, pages, nr_pages_ret,
594 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500595
Yan, Zheng24ae6362010-12-06 07:02:36 +0000596 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500597 start += num_bytes;
598 pages = NULL;
599 cond_resched();
600 goto again;
601 }
602 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500603cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500604 /*
605 * No compression, but we still need to write the pages in
606 * the file we've been given so far. redirty the locked
607 * page if it corresponds to our extent and set things up
608 * for the async work queue to run cow_file_range to do
609 * the normal delalloc dance
610 */
611 if (page_offset(locked_page) >= start &&
612 page_offset(locked_page) <= end) {
613 __set_page_dirty_nobuffers(locked_page);
614 /* unlocked later on in the async handlers */
615 }
Chris Mason4adaa612013-03-26 13:07:00 -0400616 if (redirty)
617 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800618 add_async_extent(async_cow, start, end - start + 1,
619 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500620 *num_added += 1;
621 }
622
623out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100624 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500625
626free_pages_out:
627 for (i = 0; i < nr_pages_ret; i++) {
628 WARN_ON(pages[i]->mapping);
629 page_cache_release(pages[i]);
630 }
Chris Masond3977122009-01-05 21:25:51 -0500631 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500632
633 goto out;
634}
635
Filipe Manana40ae8372014-10-06 22:14:24 +0100636static void free_async_extent_pages(struct async_extent *async_extent)
637{
638 int i;
639
640 if (!async_extent->pages)
641 return;
642
643 for (i = 0; i < async_extent->nr_pages; i++) {
644 WARN_ON(async_extent->pages[i]->mapping);
645 page_cache_release(async_extent->pages[i]);
646 }
647 kfree(async_extent->pages);
648 async_extent->nr_pages = 0;
649 async_extent->pages = NULL;
650}
651
Chris Mason771ed682008-11-06 22:02:51 -0500652/*
653 * phase two of compressed writeback. This is the ordered portion
654 * of the code, which only gets called in the order the work was
655 * queued. We walk all the async extents created by compress_file_range
656 * and send them down to the disk.
657 */
658static noinline int submit_compressed_extents(struct inode *inode,
659 struct async_cow *async_cow)
660{
661 struct async_extent *async_extent;
662 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500663 struct btrfs_key ins;
664 struct extent_map *em;
665 struct btrfs_root *root = BTRFS_I(inode)->root;
666 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
667 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500668 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500669
670 if (list_empty(&async_cow->extents))
671 return 0;
672
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500673again:
Chris Masond3977122009-01-05 21:25:51 -0500674 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500675 async_extent = list_entry(async_cow->extents.next,
676 struct async_extent, list);
677 list_del(&async_extent->list);
678
679 io_tree = &BTRFS_I(inode)->io_tree;
680
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500681retry:
Chris Mason771ed682008-11-06 22:02:51 -0500682 /* did the compression code fall back to uncompressed IO? */
683 if (!async_extent->pages) {
684 int page_started = 0;
685 unsigned long nr_written = 0;
686
687 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000688 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100689 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500690
691 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500692 ret = cow_file_range(inode, async_cow->locked_page,
693 async_extent->start,
694 async_extent->start +
695 async_extent->ram_size - 1,
696 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500697
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100698 /* JDM XXX */
699
Chris Mason771ed682008-11-06 22:02:51 -0500700 /*
701 * if page_started, cow_file_range inserted an
702 * inline extent and took care of all the unlocking
703 * and IO for us. Otherwise, we need to submit
704 * all those pages down to the drive.
705 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500706 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500707 extent_write_locked_range(io_tree,
708 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500709 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500710 async_extent->ram_size - 1,
711 btrfs_get_extent,
712 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500713 else if (ret)
714 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500715 kfree(async_extent);
716 cond_resched();
717 continue;
718 }
719
720 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100721 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500722
Josef Bacik00361582013-08-14 14:02:47 -0400723 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500724 async_extent->compressed_size,
725 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800726 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500727 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100728 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500729
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400730 if (ret == -ENOSPC) {
731 unlock_extent(io_tree, async_extent->start,
732 async_extent->start +
733 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800734
735 /*
736 * we need to redirty the pages if we decide to
737 * fallback to uncompressed IO, otherwise we
738 * will not submit these pages down to lower
739 * layers.
740 */
741 extent_range_redirty_for_io(inode,
742 async_extent->start,
743 async_extent->start +
744 async_extent->ram_size - 1);
745
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100746 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400747 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500748 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500749 }
750
Yan, Zhengc2167752009-11-12 09:34:21 +0000751 /*
752 * here we're doing allocation and writeback of the
753 * compressed pages
754 */
755 btrfs_drop_extent_cache(inode, async_extent->start,
756 async_extent->start +
757 async_extent->ram_size - 1, 0);
758
David Sterba172ddd62011-04-21 00:48:27 +0200759 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000760 if (!em) {
761 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500762 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000763 }
Chris Mason771ed682008-11-06 22:02:51 -0500764 em->start = async_extent->start;
765 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500766 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400767 em->mod_start = em->start;
768 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500769
770 em->block_start = ins.objectid;
771 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500772 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400773 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500774 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800775 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500776 set_bit(EXTENT_FLAG_PINNED, &em->flags);
777 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400778 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500779
Chris Masond3977122009-01-05 21:25:51 -0500780 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400781 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400782 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400783 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500784 if (ret != -EEXIST) {
785 free_extent_map(em);
786 break;
787 }
788 btrfs_drop_extent_cache(inode, async_extent->start,
789 async_extent->start +
790 async_extent->ram_size - 1, 0);
791 }
792
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500793 if (ret)
794 goto out_free_reserve;
795
Li Zefan261507a02010-12-17 14:21:50 +0800796 ret = btrfs_add_ordered_extent_compress(inode,
797 async_extent->start,
798 ins.objectid,
799 async_extent->ram_size,
800 ins.offset,
801 BTRFS_ORDERED_COMPRESSED,
802 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100803 if (ret) {
804 btrfs_drop_extent_cache(inode, async_extent->start,
805 async_extent->start +
806 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500807 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100808 }
Chris Mason771ed682008-11-06 22:02:51 -0500809
Chris Mason771ed682008-11-06 22:02:51 -0500810 /*
811 * clear dirty, set writeback and unlock the pages.
812 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400813 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400814 async_extent->start +
815 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400816 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
817 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400818 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500819 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500820 async_extent->start,
821 async_extent->ram_size,
822 ins.objectid,
823 ins.offset, async_extent->pages,
824 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100825 if (ret) {
826 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
827 struct page *p = async_extent->pages[0];
828 const u64 start = async_extent->start;
829 const u64 end = start + async_extent->ram_size - 1;
830
831 p->mapping = inode->i_mapping;
832 tree->ops->writepage_end_io_hook(p, start, end,
833 NULL, 0);
834 p->mapping = NULL;
835 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
836 PAGE_END_WRITEBACK |
837 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100838 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100839 }
Chris Mason771ed682008-11-06 22:02:51 -0500840 alloc_hint = ins.objectid + ins.offset;
841 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500842 if (ret)
843 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500844 cond_resched();
845 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100846 ret = 0;
847out:
848 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500849out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800850 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100851out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400852 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500853 async_extent->start +
854 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400855 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400856 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
857 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100858 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
859 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100860 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100861 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500862 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500863}
864
Josef Bacik4b46fce2010-05-23 11:00:55 -0400865static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
866 u64 num_bytes)
867{
868 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
869 struct extent_map *em;
870 u64 alloc_hint = 0;
871
872 read_lock(&em_tree->lock);
873 em = search_extent_mapping(em_tree, start, num_bytes);
874 if (em) {
875 /*
876 * if block start isn't an actual block number then find the
877 * first block in this inode and use that as a hint. If that
878 * block is also bogus then just don't worry about it.
879 */
880 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
881 free_extent_map(em);
882 em = search_extent_mapping(em_tree, 0, 0);
883 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
884 alloc_hint = em->block_start;
885 if (em)
886 free_extent_map(em);
887 } else {
888 alloc_hint = em->block_start;
889 free_extent_map(em);
890 }
891 }
892 read_unlock(&em_tree->lock);
893
894 return alloc_hint;
895}
896
Chris Mason771ed682008-11-06 22:02:51 -0500897/*
898 * when extent_io.c finds a delayed allocation range in the file,
899 * the call backs end up in this code. The basic idea is to
900 * allocate extents on disk for the range, and create ordered data structs
901 * in ram to track those extents.
902 *
903 * locked_page is the page that writepage had locked already. We use
904 * it to make sure we don't do extra locks or unlocks.
905 *
906 * *page_started is set to one if we unlock locked_page and do everything
907 * required to start IO on it. It may be clean and already done with
908 * IO when we return.
909 */
Josef Bacik00361582013-08-14 14:02:47 -0400910static noinline int cow_file_range(struct inode *inode,
911 struct page *locked_page,
912 u64 start, u64 end, int *page_started,
913 unsigned long *nr_written,
914 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500915{
Josef Bacik00361582013-08-14 14:02:47 -0400916 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500917 u64 alloc_hint = 0;
918 u64 num_bytes;
919 unsigned long ram_size;
920 u64 disk_num_bytes;
921 u64 cur_alloc_size;
922 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500923 struct btrfs_key ins;
924 struct extent_map *em;
925 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
926 int ret = 0;
927
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400928 if (btrfs_is_free_space_inode(inode)) {
929 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500930 ret = -EINVAL;
931 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400932 }
Chris Mason771ed682008-11-06 22:02:51 -0500933
Qu Wenruofda28322013-02-26 08:10:22 +0000934 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500935 num_bytes = max(blocksize, num_bytes);
936 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500937
Chris Mason4cb53002011-05-24 15:35:30 -0400938 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400939 if (num_bytes < 64 * 1024 &&
940 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400941 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400942
Chris Mason771ed682008-11-06 22:02:51 -0500943 if (start == 0) {
944 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400945 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
946 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500947 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400948 extent_clear_unlock_delalloc(inode, start, end, NULL,
949 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400950 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400951 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
952 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000953
Chris Mason771ed682008-11-06 22:02:51 -0500954 *nr_written = *nr_written +
955 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
956 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500957 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100958 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100959 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500960 }
961 }
Chris Masonc8b97812008-10-29 14:49:59 -0400962
963 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200964 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400965
Josef Bacik4b46fce2010-05-23 11:00:55 -0400966 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400967 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400968
Chris Masond3977122009-01-05 21:25:51 -0500969 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400970 unsigned long op;
971
Josef Bacik287a0ab2010-03-19 18:07:23 +0000972 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400973 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500974 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800975 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400976 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500978
David Sterba172ddd62011-04-21 00:48:27 +0200979 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000980 if (!em) {
981 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000982 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000983 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400984 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500985 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500986 ram_size = ins.offset;
987 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400988 em->mod_start = em->start;
989 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400990
Chris Masone6dcd2d2008-07-17 12:53:50 -0400991 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400992 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500993 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400994 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400995 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400996 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400997 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400998
Chris Masond3977122009-01-05 21:25:51 -0500999 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001000 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001001 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001002 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001003 if (ret != -EEXIST) {
1004 free_extent_map(em);
1005 break;
1006 }
1007 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001008 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001009 }
Liu Boace68ba2013-04-22 10:53:47 +00001010 if (ret)
1011 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001012
Chris Mason98d20f62008-04-14 09:46:10 -04001013 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001014 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001015 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001016 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001017 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001018
Yan Zheng17d217f2008-12-12 10:03:38 -05001019 if (root->root_key.objectid ==
1020 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1021 ret = btrfs_reloc_clone_csums(inode, start,
1022 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001023 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001024 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001025 }
1026
Chris Masond3977122009-01-05 21:25:51 -05001027 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001028 break;
Chris Masond3977122009-01-05 21:25:51 -05001029
Chris Masonc8b97812008-10-29 14:49:59 -04001030 /* we're not doing compressed IO, don't unlock the first
1031 * page (which the caller expects to stay locked), don't
1032 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001033 *
1034 * Do set the Private2 bit so we know this page was properly
1035 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001036 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001037 op = unlock ? PAGE_UNLOCK : 0;
1038 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001039
Josef Bacikc2790a22013-07-29 11:20:47 -04001040 extent_clear_unlock_delalloc(inode, start,
1041 start + ram_size - 1, locked_page,
1042 EXTENT_LOCKED | EXTENT_DELALLOC,
1043 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001044 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001045 num_bytes -= cur_alloc_size;
1046 alloc_hint = ins.objectid + ins.offset;
1047 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -04001048 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001049out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001050 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001051
Filipe Mananad9f85962014-08-25 10:43:00 +01001052out_drop_extent_cache:
1053 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001054out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001055 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001056out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001057 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001058 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1059 EXTENT_DELALLOC | EXTENT_DEFRAG,
1060 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1061 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001062 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001063}
Chris Masonc8b97812008-10-29 14:49:59 -04001064
Chris Mason771ed682008-11-06 22:02:51 -05001065/*
1066 * work queue call back to started compression on a file and pages
1067 */
1068static noinline void async_cow_start(struct btrfs_work *work)
1069{
1070 struct async_cow *async_cow;
1071 int num_added = 0;
1072 async_cow = container_of(work, struct async_cow, work);
1073
1074 compress_file_range(async_cow->inode, async_cow->locked_page,
1075 async_cow->start, async_cow->end, async_cow,
1076 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001077 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001078 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001079 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001080 }
Chris Mason771ed682008-11-06 22:02:51 -05001081}
1082
1083/*
1084 * work queue call back to submit previously compressed pages
1085 */
1086static noinline void async_cow_submit(struct btrfs_work *work)
1087{
1088 struct async_cow *async_cow;
1089 struct btrfs_root *root;
1090 unsigned long nr_pages;
1091
1092 async_cow = container_of(work, struct async_cow, work);
1093
1094 root = async_cow->root;
1095 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1096 PAGE_CACHE_SHIFT;
1097
Josef Bacik66657b32012-08-01 15:36:24 -04001098 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001099 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001100 waitqueue_active(&root->fs_info->async_submit_wait))
1101 wake_up(&root->fs_info->async_submit_wait);
1102
Chris Masond3977122009-01-05 21:25:51 -05001103 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001104 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001105}
Chris Masonc8b97812008-10-29 14:49:59 -04001106
Chris Mason771ed682008-11-06 22:02:51 -05001107static noinline void async_cow_free(struct btrfs_work *work)
1108{
1109 struct async_cow *async_cow;
1110 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001111 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001112 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001113 kfree(async_cow);
1114}
1115
1116static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1117 u64 start, u64 end, int *page_started,
1118 unsigned long *nr_written)
1119{
1120 struct async_cow *async_cow;
1121 struct btrfs_root *root = BTRFS_I(inode)->root;
1122 unsigned long nr_pages;
1123 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001124 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001125
Chris Masona3429ab2009-10-08 12:30:20 -04001126 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1127 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001128 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001129 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001130 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001131 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001132 async_cow->root = root;
1133 async_cow->locked_page = locked_page;
1134 async_cow->start = start;
1135
Wang Shilongf79707b2014-07-17 11:44:09 +08001136 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1137 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001138 cur_end = end;
1139 else
1140 cur_end = min(end, start + 512 * 1024 - 1);
1141
1142 async_cow->end = cur_end;
1143 INIT_LIST_HEAD(&async_cow->extents);
1144
Liu Bo9e0af232014-08-15 23:36:53 +08001145 btrfs_init_work(&async_cow->work,
1146 btrfs_delalloc_helper,
1147 async_cow_start, async_cow_submit,
1148 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001149
Chris Mason771ed682008-11-06 22:02:51 -05001150 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1151 PAGE_CACHE_SHIFT;
1152 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1153
Qu Wenruoafe3d242014-02-28 10:46:07 +08001154 btrfs_queue_work(root->fs_info->delalloc_workers,
1155 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001156
1157 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1158 wait_event(root->fs_info->async_submit_wait,
1159 (atomic_read(&root->fs_info->async_delalloc_pages) <
1160 limit));
1161 }
1162
Chris Masond3977122009-01-05 21:25:51 -05001163 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001164 atomic_read(&root->fs_info->async_delalloc_pages)) {
1165 wait_event(root->fs_info->async_submit_wait,
1166 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1167 0));
1168 }
1169
1170 *nr_written += nr_pages;
1171 start = cur_end + 1;
1172 }
1173 *page_started = 1;
1174 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001175}
1176
Chris Masond3977122009-01-05 21:25:51 -05001177static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001178 u64 bytenr, u64 num_bytes)
1179{
1180 int ret;
1181 struct btrfs_ordered_sum *sums;
1182 LIST_HEAD(list);
1183
Yan Zheng07d400a2009-01-06 11:42:00 -05001184 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001185 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001186 if (ret == 0 && list_empty(&list))
1187 return 0;
1188
1189 while (!list_empty(&list)) {
1190 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1191 list_del(&sums->list);
1192 kfree(sums);
1193 }
1194 return 1;
1195}
1196
Chris Masond352ac62008-09-29 15:18:18 -04001197/*
1198 * when nowcow writeback call back. This checks for snapshots or COW copies
1199 * of the extents that exist in the file, and COWs the file as required.
1200 *
1201 * If no cow copies or snapshots exist, we write directly to the existing
1202 * blocks on disk
1203 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001204static noinline int run_delalloc_nocow(struct inode *inode,
1205 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001206 u64 start, u64 end, int *page_started, int force,
1207 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001208{
Chris Masonbe20aa92007-12-17 20:14:01 -05001209 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001210 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001211 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001212 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001213 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001214 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001215 u64 cow_start;
1216 u64 cur_offset;
1217 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001218 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001219 u64 disk_bytenr;
1220 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001221 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001222 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001223 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001224 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001225 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001226 int nocow;
1227 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001228 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001229 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001230
1231 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001232 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001233 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1234 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001235 EXTENT_DO_ACCOUNTING |
1236 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001237 PAGE_CLEAR_DIRTY |
1238 PAGE_SET_WRITEBACK |
1239 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001240 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001241 }
Li Zefan82d59022011-04-20 10:33:24 +08001242
Liu Bo83eea1f2012-07-10 05:28:39 -06001243 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001244
1245 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001246 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001247 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001248 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001249
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001250 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001251 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1252 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001253 EXTENT_DO_ACCOUNTING |
1254 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001255 PAGE_CLEAR_DIRTY |
1256 PAGE_SET_WRITEBACK |
1257 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001258 btrfs_free_path(path);
1259 return PTR_ERR(trans);
1260 }
1261
Josef Bacik74b21072011-04-13 12:02:53 -04001262 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001263
Yan Zheng80ff3852008-10-30 14:20:02 -04001264 cow_start = (u64)-1;
1265 cur_offset = start;
1266 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001267 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001269 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001270 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001271 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1272 leaf = path->nodes[0];
1273 btrfs_item_key_to_cpu(leaf, &found_key,
1274 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001275 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001276 found_key.type == BTRFS_EXTENT_DATA_KEY)
1277 path->slots[0]--;
1278 }
1279 check_prev = 0;
1280next_slot:
1281 leaf = path->nodes[0];
1282 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1283 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001284 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001285 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 if (ret > 0)
1287 break;
1288 leaf = path->nodes[0];
1289 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001290
Yan Zheng80ff3852008-10-30 14:20:02 -04001291 nocow = 0;
1292 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001293 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001295
Li Zefan33345d012011-04-20 10:31:50 +08001296 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1298 found_key.offset > end)
1299 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001300
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 if (found_key.offset > cur_offset) {
1302 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001303 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001304 goto out_check;
1305 }
Chris Masonc31f8832008-01-08 15:46:31 -05001306
Yan Zheng80ff3852008-10-30 14:20:02 -04001307 fi = btrfs_item_ptr(leaf, path->slots[0],
1308 struct btrfs_file_extent_item);
1309 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001310
Josef Bacikcc95bef2013-04-04 14:31:27 -04001311 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001312 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1313 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001315 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 extent_end = found_key.offset +
1317 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001318 disk_num_bytes =
1319 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001320 if (extent_end <= start) {
1321 path->slots[0]++;
1322 goto next_slot;
1323 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001324 if (disk_bytenr == 0)
1325 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001326 if (btrfs_file_extent_compression(leaf, fi) ||
1327 btrfs_file_extent_encryption(leaf, fi) ||
1328 btrfs_file_extent_other_encoding(leaf, fi))
1329 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001330 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1331 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001332 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001333 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001334 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001335 found_key.offset -
1336 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001337 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001338 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001339 disk_bytenr += cur_offset - found_key.offset;
1340 num_bytes = min(end + 1, extent_end) - cur_offset;
1341 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001342 * if there are pending snapshots for this root,
1343 * we fall into common COW way.
1344 */
1345 if (!nolock) {
1346 err = btrfs_start_nocow_write(root);
1347 if (!err)
1348 goto out_check;
1349 }
1350 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001351 * force cow if csum exists in the range.
1352 * this ensure that csum for a given extent are
1353 * either valid or do not exist.
1354 */
1355 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1356 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 nocow = 1;
1358 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1359 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001360 btrfs_file_extent_inline_len(leaf,
1361 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001362 extent_end = ALIGN(extent_end, root->sectorsize);
1363 } else {
1364 BUG_ON(1);
1365 }
1366out_check:
1367 if (extent_end <= start) {
1368 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001369 if (!nolock && nocow)
1370 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001371 goto next_slot;
1372 }
1373 if (!nocow) {
1374 if (cow_start == (u64)-1)
1375 cow_start = cur_offset;
1376 cur_offset = extent_end;
1377 if (cur_offset > end)
1378 break;
1379 path->slots[0]++;
1380 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001381 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001382
David Sterbab3b4aa72011-04-21 01:20:15 +02001383 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001384 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001385 ret = cow_file_range(inode, locked_page,
1386 cow_start, found_key.offset - 1,
1387 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001388 if (ret) {
1389 if (!nolock && nocow)
1390 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001391 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001392 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001393 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001394 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001395
Yan Zhengd899e052008-10-30 14:25:28 -04001396 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1397 struct extent_map *em;
1398 struct extent_map_tree *em_tree;
1399 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001400 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001401 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001402 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001403 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001404 em->len = num_bytes;
1405 em->block_len = num_bytes;
1406 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001407 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001408 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001409 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001410 em->mod_start = em->start;
1411 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001412 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001413 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001414 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001415 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001416 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001417 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001418 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001419 if (ret != -EEXIST) {
1420 free_extent_map(em);
1421 break;
1422 }
1423 btrfs_drop_extent_cache(inode, em->start,
1424 em->start + em->len - 1, 0);
1425 }
1426 type = BTRFS_ORDERED_PREALLOC;
1427 } else {
1428 type = BTRFS_ORDERED_NOCOW;
1429 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001430
1431 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001432 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001433 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001434
Yan, Zhengefa56462010-05-16 10:49:59 -04001435 if (root->root_key.objectid ==
1436 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1437 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1438 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001439 if (ret) {
1440 if (!nolock && nocow)
1441 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001442 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001443 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001444 }
1445
Josef Bacikc2790a22013-07-29 11:20:47 -04001446 extent_clear_unlock_delalloc(inode, cur_offset,
1447 cur_offset + num_bytes - 1,
1448 locked_page, EXTENT_LOCKED |
1449 EXTENT_DELALLOC, PAGE_UNLOCK |
1450 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001451 if (!nolock && nocow)
1452 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001453 cur_offset = extent_end;
1454 if (cur_offset > end)
1455 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001456 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001457 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001458
Josef Bacik17ca04a2012-05-31 15:58:55 -04001459 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001460 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001461 cur_offset = end;
1462 }
1463
Yan Zheng80ff3852008-10-30 14:20:02 -04001464 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001465 ret = cow_file_range(inode, locked_page, cow_start, end,
1466 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001467 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001468 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001469 }
1470
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001471error:
Miao Xiea698d0752012-09-20 01:51:59 -06001472 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001473 if (!ret)
1474 ret = err;
1475
Josef Bacik17ca04a2012-05-31 15:58:55 -04001476 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001477 extent_clear_unlock_delalloc(inode, cur_offset, end,
1478 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001479 EXTENT_DELALLOC | EXTENT_DEFRAG |
1480 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1481 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001482 PAGE_SET_WRITEBACK |
1483 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001484 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001485 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001486}
1487
Wang Shilong47059d92014-07-03 18:22:07 +08001488static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1489{
1490
1491 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1492 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1493 return 0;
1494
1495 /*
1496 * @defrag_bytes is a hint value, no spinlock held here,
1497 * if is not zero, it means the file is defragging.
1498 * Force cow if given extent needs to be defragged.
1499 */
1500 if (BTRFS_I(inode)->defrag_bytes &&
1501 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1502 EXTENT_DEFRAG, 0, NULL))
1503 return 1;
1504
1505 return 0;
1506}
1507
Chris Masond352ac62008-09-29 15:18:18 -04001508/*
1509 * extent_io.c call back to do delayed allocation processing
1510 */
Chris Masonc8b97812008-10-29 14:49:59 -04001511static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001512 u64 start, u64 end, int *page_started,
1513 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001514{
Chris Masonbe20aa92007-12-17 20:14:01 -05001515 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001516 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001517
Wang Shilong47059d92014-07-03 18:22:07 +08001518 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001519 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001520 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001521 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001522 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001523 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001524 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001525 ret = cow_file_range(inode, locked_page, start, end,
1526 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001527 } else {
1528 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1529 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001530 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001531 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001532 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001533 return ret;
1534}
1535
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001536static void btrfs_split_extent_hook(struct inode *inode,
1537 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001538{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001539 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001540 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001541 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001542
Josef Bacik9e0baf62011-07-15 15:16:44 +00001543 spin_lock(&BTRFS_I(inode)->lock);
1544 BTRFS_I(inode)->outstanding_extents++;
1545 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001546}
1547
1548/*
1549 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1550 * extents so we can keep track of new extents that are just merged onto old
1551 * extents, such as when we are doing sequential writes, so we can properly
1552 * account for the metadata space we'll need.
1553 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001554static void btrfs_merge_extent_hook(struct inode *inode,
1555 struct extent_state *new,
1556 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001557{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001558 /* not delalloc, ignore it */
1559 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001560 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001561
Josef Bacik9e0baf62011-07-15 15:16:44 +00001562 spin_lock(&BTRFS_I(inode)->lock);
1563 BTRFS_I(inode)->outstanding_extents--;
1564 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001565}
1566
Miao Xieeb73c1b2013-05-15 07:48:22 +00001567static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1568 struct inode *inode)
1569{
1570 spin_lock(&root->delalloc_lock);
1571 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1572 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1573 &root->delalloc_inodes);
1574 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1575 &BTRFS_I(inode)->runtime_flags);
1576 root->nr_delalloc_inodes++;
1577 if (root->nr_delalloc_inodes == 1) {
1578 spin_lock(&root->fs_info->delalloc_root_lock);
1579 BUG_ON(!list_empty(&root->delalloc_root));
1580 list_add_tail(&root->delalloc_root,
1581 &root->fs_info->delalloc_roots);
1582 spin_unlock(&root->fs_info->delalloc_root_lock);
1583 }
1584 }
1585 spin_unlock(&root->delalloc_lock);
1586}
1587
1588static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1589 struct inode *inode)
1590{
1591 spin_lock(&root->delalloc_lock);
1592 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1593 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1594 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1595 &BTRFS_I(inode)->runtime_flags);
1596 root->nr_delalloc_inodes--;
1597 if (!root->nr_delalloc_inodes) {
1598 spin_lock(&root->fs_info->delalloc_root_lock);
1599 BUG_ON(list_empty(&root->delalloc_root));
1600 list_del_init(&root->delalloc_root);
1601 spin_unlock(&root->fs_info->delalloc_root_lock);
1602 }
1603 }
1604 spin_unlock(&root->delalloc_lock);
1605}
1606
Chris Masond352ac62008-09-29 15:18:18 -04001607/*
1608 * extent_io.c set_bit_hook, used to track delayed allocation
1609 * bytes in this file, and to maintain the list of inodes that
1610 * have pending delalloc work to be done.
1611 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001612static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001613 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001614{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001615
Wang Shilong47059d92014-07-03 18:22:07 +08001616 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1617 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001618 /*
1619 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001620 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001621 * bit, which is only set or cleared with irqs on
1622 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001623 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001624 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001625 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001626 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001627
Josef Bacik9e0baf62011-07-15 15:16:44 +00001628 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001629 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001630 } else {
1631 spin_lock(&BTRFS_I(inode)->lock);
1632 BTRFS_I(inode)->outstanding_extents++;
1633 spin_unlock(&BTRFS_I(inode)->lock);
1634 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001635
Miao Xie963d6782013-01-29 10:10:51 +00001636 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1637 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001638 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001639 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001640 if (*bits & EXTENT_DEFRAG)
1641 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001642 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001643 &BTRFS_I(inode)->runtime_flags))
1644 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001645 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001646 }
Chris Mason291d6732008-01-29 15:55:23 -05001647}
1648
Chris Masond352ac62008-09-29 15:18:18 -04001649/*
1650 * extent_io.c clear_bit_hook, see set_bit_hook for why
1651 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001652static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001653 struct extent_state *state,
1654 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001655{
Wang Shilong47059d92014-07-03 18:22:07 +08001656 u64 len = state->end + 1 - state->start;
1657
1658 spin_lock(&BTRFS_I(inode)->lock);
1659 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1660 BTRFS_I(inode)->defrag_bytes -= len;
1661 spin_unlock(&BTRFS_I(inode)->lock);
1662
Chris Mason75eff682008-12-15 15:54:40 -05001663 /*
1664 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001665 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001666 * bit, which is only set or cleared with irqs on
1667 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001668 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001669 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001670 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001671
Josef Bacik9e0baf62011-07-15 15:16:44 +00001672 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001673 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001674 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1675 spin_lock(&BTRFS_I(inode)->lock);
1676 BTRFS_I(inode)->outstanding_extents--;
1677 spin_unlock(&BTRFS_I(inode)->lock);
1678 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001679
Josef Bacikb6d08f02013-09-27 14:57:43 -04001680 /*
1681 * We don't reserve metadata space for space cache inodes so we
1682 * don't need to call dellalloc_release_metadata if there is an
1683 * error.
1684 */
1685 if (*bits & EXTENT_DO_ACCOUNTING &&
1686 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001687 btrfs_delalloc_release_metadata(inode, len);
1688
Josef Bacik0cb59c92010-07-02 12:14:14 -04001689 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001690 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001691 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001692
Miao Xie963d6782013-01-29 10:10:51 +00001693 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1694 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001695 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001696 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001697 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001698 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001699 &BTRFS_I(inode)->runtime_flags))
1700 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001701 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001702 }
Chris Mason291d6732008-01-29 15:55:23 -05001703}
1704
Chris Masond352ac62008-09-29 15:18:18 -04001705/*
1706 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1707 * we don't create bios that span stripes or chunks
1708 */
David Woodhouse64a16702009-07-15 23:29:37 +01001709int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001710 size_t size, struct bio *bio,
1711 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001712{
1713 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001714 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001715 u64 length = 0;
1716 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001717 int ret;
1718
Chris Mason771ed682008-11-06 22:02:51 -05001719 if (bio_flags & EXTENT_BIO_COMPRESSED)
1720 return 0;
1721
Kent Overstreet4f024f32013-10-11 15:44:27 -07001722 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001723 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001724 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001725 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001726 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001727 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001728 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001729 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001730 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001731}
1732
Chris Masond352ac62008-09-29 15:18:18 -04001733/*
1734 * in order to insert checksums into the metadata in large chunks,
1735 * we wait until bio submission time. All the pages in the bio are
1736 * checksummed and sums are attached onto the ordered extent record.
1737 *
1738 * At IO completion time the cums attached on the ordered extent record
1739 * are inserted into the btree
1740 */
Chris Masond3977122009-01-05 21:25:51 -05001741static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1742 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001743 unsigned long bio_flags,
1744 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001745{
Chris Mason065631f2008-02-20 12:07:25 -05001746 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001747 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001748
Chris Masond20f7042008-12-08 16:58:54 -05001749 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001750 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001751 return 0;
1752}
Chris Masone0156402008-04-16 11:15:20 -04001753
Chris Mason4a69a412008-11-06 22:03:00 -05001754/*
1755 * in order to insert checksums into the metadata in large chunks,
1756 * we wait until bio submission time. All the pages in the bio are
1757 * checksummed and sums are attached onto the ordered extent record.
1758 *
1759 * At IO completion time the cums attached on the ordered extent record
1760 * are inserted into the btree
1761 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001762static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001763 int mirror_num, unsigned long bio_flags,
1764 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001765{
1766 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001767 int ret;
1768
1769 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1770 if (ret)
1771 bio_endio(bio, ret);
1772 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001773}
1774
Chris Masond352ac62008-09-29 15:18:18 -04001775/*
Chris Masoncad321a2008-12-17 14:51:42 -05001776 * extent_io.c submission hook. This does the right thing for csum calculation
1777 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001778 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001779static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001780 int mirror_num, unsigned long bio_flags,
1781 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001782{
1783 struct btrfs_root *root = BTRFS_I(inode)->root;
1784 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001785 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001786 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001787 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001788
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001789 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001790
Liu Bo83eea1f2012-07-10 05:28:39 -06001791 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001792 metadata = 2;
1793
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001794 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001795 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1796 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001797 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001798
Chris Masond20f7042008-12-08 16:58:54 -05001799 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001800 ret = btrfs_submit_compressed_read(inode, bio,
1801 mirror_num,
1802 bio_flags);
1803 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001804 } else if (!skip_sum) {
1805 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1806 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001807 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001808 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001809 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001810 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001811 /* csum items have already been cloned */
1812 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1813 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001814 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001815 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001816 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001817 bio_flags, bio_offset,
1818 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001819 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001820 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001821 } else if (!skip_sum) {
1822 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1823 if (ret)
1824 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001825 }
1826
Chris Mason0b86a832008-03-24 15:01:56 -04001827mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001828 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1829
1830out:
1831 if (ret < 0)
1832 bio_endio(bio, ret);
1833 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001834}
Chris Mason6885f302008-02-20 16:11:05 -05001835
Chris Masond352ac62008-09-29 15:18:18 -04001836/*
1837 * given a list of ordered sums record them in the inode. This happens
1838 * at IO completion time based on sums calculated at bio submission time.
1839 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001840static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001841 struct inode *inode, u64 file_offset,
1842 struct list_head *list)
1843{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001844 struct btrfs_ordered_sum *sum;
1845
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001846 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001847 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001848 btrfs_csum_file_blocks(trans,
1849 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001850 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001851 }
1852 return 0;
1853}
1854
Josef Bacik2ac55d42010-02-03 19:33:23 +00001855int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1856 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001857{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001858 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001859 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001860 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001861}
1862
Chris Masond352ac62008-09-29 15:18:18 -04001863/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001864struct btrfs_writepage_fixup {
1865 struct page *page;
1866 struct btrfs_work work;
1867};
1868
Christoph Hellwigb2950862008-12-02 09:54:17 -05001869static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001870{
1871 struct btrfs_writepage_fixup *fixup;
1872 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001873 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001874 struct page *page;
1875 struct inode *inode;
1876 u64 page_start;
1877 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001878 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001879
1880 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1881 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001882again:
Chris Mason247e7432008-07-17 12:53:51 -04001883 lock_page(page);
1884 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1885 ClearPageChecked(page);
1886 goto out_page;
1887 }
1888
1889 inode = page->mapping->host;
1890 page_start = page_offset(page);
1891 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1892
Josef Bacik2ac55d42010-02-03 19:33:23 +00001893 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001894 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001895
1896 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001897 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001898 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001899
1900 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1901 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001902 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1903 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001904 unlock_page(page);
1905 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001906 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001907 goto again;
1908 }
Chris Mason247e7432008-07-17 12:53:51 -04001909
Jeff Mahoney87826df2012-02-15 16:23:57 +01001910 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1911 if (ret) {
1912 mapping_set_error(page->mapping, ret);
1913 end_extent_writepage(page, ret, page_start, page_end);
1914 ClearPageChecked(page);
1915 goto out;
1916 }
1917
Josef Bacik2ac55d42010-02-03 19:33:23 +00001918 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001919 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001920 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001921out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001922 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1923 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001924out_page:
1925 unlock_page(page);
1926 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001927 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001928}
1929
1930/*
1931 * There are a few paths in the higher layers of the kernel that directly
1932 * set the page dirty bit without asking the filesystem if it is a
1933 * good idea. This causes problems because we want to make sure COW
1934 * properly happens and the data=ordered rules are followed.
1935 *
Chris Masonc8b97812008-10-29 14:49:59 -04001936 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001937 * hasn't been properly setup for IO. We kick off an async process
1938 * to fix it up. The async helper will wait for ordered extents, set
1939 * the delalloc bit and make it safe to write the page.
1940 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001941static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001942{
1943 struct inode *inode = page->mapping->host;
1944 struct btrfs_writepage_fixup *fixup;
1945 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001946
Chris Mason8b62b722009-09-02 16:53:46 -04001947 /* this page is properly in the ordered list */
1948 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001949 return 0;
1950
1951 if (PageChecked(page))
1952 return -EAGAIN;
1953
1954 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1955 if (!fixup)
1956 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001957
Chris Mason247e7432008-07-17 12:53:51 -04001958 SetPageChecked(page);
1959 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08001960 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
1961 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04001962 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08001963 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001964 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001965}
1966
Yan Zhengd899e052008-10-30 14:25:28 -04001967static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1968 struct inode *inode, u64 file_pos,
1969 u64 disk_bytenr, u64 disk_num_bytes,
1970 u64 num_bytes, u64 ram_bytes,
1971 u8 compression, u8 encryption,
1972 u16 other_encoding, int extent_type)
1973{
1974 struct btrfs_root *root = BTRFS_I(inode)->root;
1975 struct btrfs_file_extent_item *fi;
1976 struct btrfs_path *path;
1977 struct extent_buffer *leaf;
1978 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001979 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001980 int ret;
1981
1982 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001983 if (!path)
1984 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001985
Chris Masona1ed8352009-09-11 12:27:37 -04001986 /*
1987 * we may be replacing one extent in the tree with another.
1988 * The new extent is pinned in the extent map, and we don't want
1989 * to drop it from the cache until it is completely in the btree.
1990 *
1991 * So, tell btrfs_drop_extents to leave this extent in the cache.
1992 * the caller is expected to unpin it and allow it to be merged
1993 * with the others.
1994 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001995 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1996 file_pos + num_bytes, NULL, 0,
1997 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001998 if (ret)
1999 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002000
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002001 if (!extent_inserted) {
2002 ins.objectid = btrfs_ino(inode);
2003 ins.offset = file_pos;
2004 ins.type = BTRFS_EXTENT_DATA_KEY;
2005
2006 path->leave_spinning = 1;
2007 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2008 sizeof(*fi));
2009 if (ret)
2010 goto out;
2011 }
Yan Zhengd899e052008-10-30 14:25:28 -04002012 leaf = path->nodes[0];
2013 fi = btrfs_item_ptr(leaf, path->slots[0],
2014 struct btrfs_file_extent_item);
2015 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2016 btrfs_set_file_extent_type(leaf, fi, extent_type);
2017 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2018 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2019 btrfs_set_file_extent_offset(leaf, fi, 0);
2020 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2021 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2022 btrfs_set_file_extent_compression(leaf, fi, compression);
2023 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2024 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002025
Yan Zhengd899e052008-10-30 14:25:28 -04002026 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002027 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002028
2029 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002030
2031 ins.objectid = disk_bytenr;
2032 ins.offset = disk_num_bytes;
2033 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002034 ret = btrfs_alloc_reserved_file_extent(trans, root,
2035 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002036 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002037out:
Yan Zhengd899e052008-10-30 14:25:28 -04002038 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002039
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002040 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002041}
2042
Liu Bo38c227d2013-01-29 03:18:40 +00002043/* snapshot-aware defrag */
2044struct sa_defrag_extent_backref {
2045 struct rb_node node;
2046 struct old_sa_defrag_extent *old;
2047 u64 root_id;
2048 u64 inum;
2049 u64 file_pos;
2050 u64 extent_offset;
2051 u64 num_bytes;
2052 u64 generation;
2053};
2054
2055struct old_sa_defrag_extent {
2056 struct list_head list;
2057 struct new_sa_defrag_extent *new;
2058
2059 u64 extent_offset;
2060 u64 bytenr;
2061 u64 offset;
2062 u64 len;
2063 int count;
2064};
2065
2066struct new_sa_defrag_extent {
2067 struct rb_root root;
2068 struct list_head head;
2069 struct btrfs_path *path;
2070 struct inode *inode;
2071 u64 file_pos;
2072 u64 len;
2073 u64 bytenr;
2074 u64 disk_len;
2075 u8 compress_type;
2076};
2077
2078static int backref_comp(struct sa_defrag_extent_backref *b1,
2079 struct sa_defrag_extent_backref *b2)
2080{
2081 if (b1->root_id < b2->root_id)
2082 return -1;
2083 else if (b1->root_id > b2->root_id)
2084 return 1;
2085
2086 if (b1->inum < b2->inum)
2087 return -1;
2088 else if (b1->inum > b2->inum)
2089 return 1;
2090
2091 if (b1->file_pos < b2->file_pos)
2092 return -1;
2093 else if (b1->file_pos > b2->file_pos)
2094 return 1;
2095
2096 /*
2097 * [------------------------------] ===> (a range of space)
2098 * |<--->| |<---->| =============> (fs/file tree A)
2099 * |<---------------------------->| ===> (fs/file tree B)
2100 *
2101 * A range of space can refer to two file extents in one tree while
2102 * refer to only one file extent in another tree.
2103 *
2104 * So we may process a disk offset more than one time(two extents in A)
2105 * and locate at the same extent(one extent in B), then insert two same
2106 * backrefs(both refer to the extent in B).
2107 */
2108 return 0;
2109}
2110
2111static void backref_insert(struct rb_root *root,
2112 struct sa_defrag_extent_backref *backref)
2113{
2114 struct rb_node **p = &root->rb_node;
2115 struct rb_node *parent = NULL;
2116 struct sa_defrag_extent_backref *entry;
2117 int ret;
2118
2119 while (*p) {
2120 parent = *p;
2121 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2122
2123 ret = backref_comp(backref, entry);
2124 if (ret < 0)
2125 p = &(*p)->rb_left;
2126 else
2127 p = &(*p)->rb_right;
2128 }
2129
2130 rb_link_node(&backref->node, parent, p);
2131 rb_insert_color(&backref->node, root);
2132}
2133
2134/*
2135 * Note the backref might has changed, and in this case we just return 0.
2136 */
2137static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2138 void *ctx)
2139{
2140 struct btrfs_file_extent_item *extent;
2141 struct btrfs_fs_info *fs_info;
2142 struct old_sa_defrag_extent *old = ctx;
2143 struct new_sa_defrag_extent *new = old->new;
2144 struct btrfs_path *path = new->path;
2145 struct btrfs_key key;
2146 struct btrfs_root *root;
2147 struct sa_defrag_extent_backref *backref;
2148 struct extent_buffer *leaf;
2149 struct inode *inode = new->inode;
2150 int slot;
2151 int ret;
2152 u64 extent_offset;
2153 u64 num_bytes;
2154
2155 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2156 inum == btrfs_ino(inode))
2157 return 0;
2158
2159 key.objectid = root_id;
2160 key.type = BTRFS_ROOT_ITEM_KEY;
2161 key.offset = (u64)-1;
2162
2163 fs_info = BTRFS_I(inode)->root->fs_info;
2164 root = btrfs_read_fs_root_no_name(fs_info, &key);
2165 if (IS_ERR(root)) {
2166 if (PTR_ERR(root) == -ENOENT)
2167 return 0;
2168 WARN_ON(1);
2169 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2170 inum, offset, root_id);
2171 return PTR_ERR(root);
2172 }
2173
2174 key.objectid = inum;
2175 key.type = BTRFS_EXTENT_DATA_KEY;
2176 if (offset > (u64)-1 << 32)
2177 key.offset = 0;
2178 else
2179 key.offset = offset;
2180
2181 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302182 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002183 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002184 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002185
2186 while (1) {
2187 cond_resched();
2188
2189 leaf = path->nodes[0];
2190 slot = path->slots[0];
2191
2192 if (slot >= btrfs_header_nritems(leaf)) {
2193 ret = btrfs_next_leaf(root, path);
2194 if (ret < 0) {
2195 goto out;
2196 } else if (ret > 0) {
2197 ret = 0;
2198 goto out;
2199 }
2200 continue;
2201 }
2202
2203 path->slots[0]++;
2204
2205 btrfs_item_key_to_cpu(leaf, &key, slot);
2206
2207 if (key.objectid > inum)
2208 goto out;
2209
2210 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2211 continue;
2212
2213 extent = btrfs_item_ptr(leaf, slot,
2214 struct btrfs_file_extent_item);
2215
2216 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2217 continue;
2218
Liu Boe68afa42013-07-01 22:13:26 +08002219 /*
2220 * 'offset' refers to the exact key.offset,
2221 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2222 * (key.offset - extent_offset).
2223 */
2224 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002225 continue;
2226
Liu Boe68afa42013-07-01 22:13:26 +08002227 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002228 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002229
Liu Bo38c227d2013-01-29 03:18:40 +00002230 if (extent_offset >= old->extent_offset + old->offset +
2231 old->len || extent_offset + num_bytes <=
2232 old->extent_offset + old->offset)
2233 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002234 break;
2235 }
2236
2237 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2238 if (!backref) {
2239 ret = -ENOENT;
2240 goto out;
2241 }
2242
2243 backref->root_id = root_id;
2244 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002245 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002246 backref->num_bytes = num_bytes;
2247 backref->extent_offset = extent_offset;
2248 backref->generation = btrfs_file_extent_generation(leaf, extent);
2249 backref->old = old;
2250 backref_insert(&new->root, backref);
2251 old->count++;
2252out:
2253 btrfs_release_path(path);
2254 WARN_ON(ret);
2255 return ret;
2256}
2257
2258static noinline bool record_extent_backrefs(struct btrfs_path *path,
2259 struct new_sa_defrag_extent *new)
2260{
2261 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2262 struct old_sa_defrag_extent *old, *tmp;
2263 int ret;
2264
2265 new->path = path;
2266
2267 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002268 ret = iterate_inodes_from_logical(old->bytenr +
2269 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002270 path, record_one_backref,
2271 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002272 if (ret < 0 && ret != -ENOENT)
2273 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002274
2275 /* no backref to be processed for this extent */
2276 if (!old->count) {
2277 list_del(&old->list);
2278 kfree(old);
2279 }
2280 }
2281
2282 if (list_empty(&new->head))
2283 return false;
2284
2285 return true;
2286}
2287
2288static int relink_is_mergable(struct extent_buffer *leaf,
2289 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002290 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002291{
Liu Bo116e0022013-08-02 16:30:40 +08002292 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002293 return 0;
2294
2295 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2296 return 0;
2297
Liu Bo116e0022013-08-02 16:30:40 +08002298 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2299 return 0;
2300
2301 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002302 btrfs_file_extent_other_encoding(leaf, fi))
2303 return 0;
2304
2305 return 1;
2306}
2307
2308/*
2309 * Note the backref might has changed, and in this case we just return 0.
2310 */
2311static noinline int relink_extent_backref(struct btrfs_path *path,
2312 struct sa_defrag_extent_backref *prev,
2313 struct sa_defrag_extent_backref *backref)
2314{
2315 struct btrfs_file_extent_item *extent;
2316 struct btrfs_file_extent_item *item;
2317 struct btrfs_ordered_extent *ordered;
2318 struct btrfs_trans_handle *trans;
2319 struct btrfs_fs_info *fs_info;
2320 struct btrfs_root *root;
2321 struct btrfs_key key;
2322 struct extent_buffer *leaf;
2323 struct old_sa_defrag_extent *old = backref->old;
2324 struct new_sa_defrag_extent *new = old->new;
2325 struct inode *src_inode = new->inode;
2326 struct inode *inode;
2327 struct extent_state *cached = NULL;
2328 int ret = 0;
2329 u64 start;
2330 u64 len;
2331 u64 lock_start;
2332 u64 lock_end;
2333 bool merge = false;
2334 int index;
2335
2336 if (prev && prev->root_id == backref->root_id &&
2337 prev->inum == backref->inum &&
2338 prev->file_pos + prev->num_bytes == backref->file_pos)
2339 merge = true;
2340
2341 /* step 1: get root */
2342 key.objectid = backref->root_id;
2343 key.type = BTRFS_ROOT_ITEM_KEY;
2344 key.offset = (u64)-1;
2345
2346 fs_info = BTRFS_I(src_inode)->root->fs_info;
2347 index = srcu_read_lock(&fs_info->subvol_srcu);
2348
2349 root = btrfs_read_fs_root_no_name(fs_info, &key);
2350 if (IS_ERR(root)) {
2351 srcu_read_unlock(&fs_info->subvol_srcu, index);
2352 if (PTR_ERR(root) == -ENOENT)
2353 return 0;
2354 return PTR_ERR(root);
2355 }
Liu Bo38c227d2013-01-29 03:18:40 +00002356
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002357 if (btrfs_root_readonly(root)) {
2358 srcu_read_unlock(&fs_info->subvol_srcu, index);
2359 return 0;
2360 }
2361
Liu Bo38c227d2013-01-29 03:18:40 +00002362 /* step 2: get inode */
2363 key.objectid = backref->inum;
2364 key.type = BTRFS_INODE_ITEM_KEY;
2365 key.offset = 0;
2366
2367 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2368 if (IS_ERR(inode)) {
2369 srcu_read_unlock(&fs_info->subvol_srcu, index);
2370 return 0;
2371 }
2372
2373 srcu_read_unlock(&fs_info->subvol_srcu, index);
2374
2375 /* step 3: relink backref */
2376 lock_start = backref->file_pos;
2377 lock_end = backref->file_pos + backref->num_bytes - 1;
2378 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2379 0, &cached);
2380
2381 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2382 if (ordered) {
2383 btrfs_put_ordered_extent(ordered);
2384 goto out_unlock;
2385 }
2386
2387 trans = btrfs_join_transaction(root);
2388 if (IS_ERR(trans)) {
2389 ret = PTR_ERR(trans);
2390 goto out_unlock;
2391 }
2392
2393 key.objectid = backref->inum;
2394 key.type = BTRFS_EXTENT_DATA_KEY;
2395 key.offset = backref->file_pos;
2396
2397 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2398 if (ret < 0) {
2399 goto out_free_path;
2400 } else if (ret > 0) {
2401 ret = 0;
2402 goto out_free_path;
2403 }
2404
2405 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2406 struct btrfs_file_extent_item);
2407
2408 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2409 backref->generation)
2410 goto out_free_path;
2411
2412 btrfs_release_path(path);
2413
2414 start = backref->file_pos;
2415 if (backref->extent_offset < old->extent_offset + old->offset)
2416 start += old->extent_offset + old->offset -
2417 backref->extent_offset;
2418
2419 len = min(backref->extent_offset + backref->num_bytes,
2420 old->extent_offset + old->offset + old->len);
2421 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2422
2423 ret = btrfs_drop_extents(trans, root, inode, start,
2424 start + len, 1);
2425 if (ret)
2426 goto out_free_path;
2427again:
2428 key.objectid = btrfs_ino(inode);
2429 key.type = BTRFS_EXTENT_DATA_KEY;
2430 key.offset = start;
2431
Liu Boa09a0a72013-03-11 09:20:58 +00002432 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002433 if (merge) {
2434 struct btrfs_file_extent_item *fi;
2435 u64 extent_len;
2436 struct btrfs_key found_key;
2437
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002438 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002439 if (ret < 0)
2440 goto out_free_path;
2441
2442 path->slots[0]--;
2443 leaf = path->nodes[0];
2444 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2445
2446 fi = btrfs_item_ptr(leaf, path->slots[0],
2447 struct btrfs_file_extent_item);
2448 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2449
Liu Bo116e0022013-08-02 16:30:40 +08002450 if (extent_len + found_key.offset == start &&
2451 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002452 btrfs_set_file_extent_num_bytes(leaf, fi,
2453 extent_len + len);
2454 btrfs_mark_buffer_dirty(leaf);
2455 inode_add_bytes(inode, len);
2456
2457 ret = 1;
2458 goto out_free_path;
2459 } else {
2460 merge = false;
2461 btrfs_release_path(path);
2462 goto again;
2463 }
2464 }
2465
2466 ret = btrfs_insert_empty_item(trans, root, path, &key,
2467 sizeof(*extent));
2468 if (ret) {
2469 btrfs_abort_transaction(trans, root, ret);
2470 goto out_free_path;
2471 }
2472
2473 leaf = path->nodes[0];
2474 item = btrfs_item_ptr(leaf, path->slots[0],
2475 struct btrfs_file_extent_item);
2476 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2477 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2478 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2479 btrfs_set_file_extent_num_bytes(leaf, item, len);
2480 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2481 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2482 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2483 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2484 btrfs_set_file_extent_encryption(leaf, item, 0);
2485 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2486
2487 btrfs_mark_buffer_dirty(leaf);
2488 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002489 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002490
2491 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2492 new->disk_len, 0,
2493 backref->root_id, backref->inum,
2494 new->file_pos, 0); /* start - extent_offset */
2495 if (ret) {
2496 btrfs_abort_transaction(trans, root, ret);
2497 goto out_free_path;
2498 }
2499
2500 ret = 1;
2501out_free_path:
2502 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002503 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002504 btrfs_end_transaction(trans, root);
2505out_unlock:
2506 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2507 &cached, GFP_NOFS);
2508 iput(inode);
2509 return ret;
2510}
2511
Liu Bo6f519562013-10-29 10:45:05 +08002512static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2513{
2514 struct old_sa_defrag_extent *old, *tmp;
2515
2516 if (!new)
2517 return;
2518
2519 list_for_each_entry_safe(old, tmp, &new->head, list) {
2520 list_del(&old->list);
2521 kfree(old);
2522 }
2523 kfree(new);
2524}
2525
Liu Bo38c227d2013-01-29 03:18:40 +00002526static void relink_file_extents(struct new_sa_defrag_extent *new)
2527{
2528 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002529 struct sa_defrag_extent_backref *backref;
2530 struct sa_defrag_extent_backref *prev = NULL;
2531 struct inode *inode;
2532 struct btrfs_root *root;
2533 struct rb_node *node;
2534 int ret;
2535
2536 inode = new->inode;
2537 root = BTRFS_I(inode)->root;
2538
2539 path = btrfs_alloc_path();
2540 if (!path)
2541 return;
2542
2543 if (!record_extent_backrefs(path, new)) {
2544 btrfs_free_path(path);
2545 goto out;
2546 }
2547 btrfs_release_path(path);
2548
2549 while (1) {
2550 node = rb_first(&new->root);
2551 if (!node)
2552 break;
2553 rb_erase(node, &new->root);
2554
2555 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2556
2557 ret = relink_extent_backref(path, prev, backref);
2558 WARN_ON(ret < 0);
2559
2560 kfree(prev);
2561
2562 if (ret == 1)
2563 prev = backref;
2564 else
2565 prev = NULL;
2566 cond_resched();
2567 }
2568 kfree(prev);
2569
2570 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002571out:
Liu Bo6f519562013-10-29 10:45:05 +08002572 free_sa_defrag_extent(new);
2573
Liu Bo38c227d2013-01-29 03:18:40 +00002574 atomic_dec(&root->fs_info->defrag_running);
2575 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002576}
2577
2578static struct new_sa_defrag_extent *
2579record_old_file_extents(struct inode *inode,
2580 struct btrfs_ordered_extent *ordered)
2581{
2582 struct btrfs_root *root = BTRFS_I(inode)->root;
2583 struct btrfs_path *path;
2584 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002585 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002586 struct new_sa_defrag_extent *new;
2587 int ret;
2588
2589 new = kmalloc(sizeof(*new), GFP_NOFS);
2590 if (!new)
2591 return NULL;
2592
2593 new->inode = inode;
2594 new->file_pos = ordered->file_offset;
2595 new->len = ordered->len;
2596 new->bytenr = ordered->start;
2597 new->disk_len = ordered->disk_len;
2598 new->compress_type = ordered->compress_type;
2599 new->root = RB_ROOT;
2600 INIT_LIST_HEAD(&new->head);
2601
2602 path = btrfs_alloc_path();
2603 if (!path)
2604 goto out_kfree;
2605
2606 key.objectid = btrfs_ino(inode);
2607 key.type = BTRFS_EXTENT_DATA_KEY;
2608 key.offset = new->file_pos;
2609
2610 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2611 if (ret < 0)
2612 goto out_free_path;
2613 if (ret > 0 && path->slots[0] > 0)
2614 path->slots[0]--;
2615
2616 /* find out all the old extents for the file range */
2617 while (1) {
2618 struct btrfs_file_extent_item *extent;
2619 struct extent_buffer *l;
2620 int slot;
2621 u64 num_bytes;
2622 u64 offset;
2623 u64 end;
2624 u64 disk_bytenr;
2625 u64 extent_offset;
2626
2627 l = path->nodes[0];
2628 slot = path->slots[0];
2629
2630 if (slot >= btrfs_header_nritems(l)) {
2631 ret = btrfs_next_leaf(root, path);
2632 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002633 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002634 else if (ret > 0)
2635 break;
2636 continue;
2637 }
2638
2639 btrfs_item_key_to_cpu(l, &key, slot);
2640
2641 if (key.objectid != btrfs_ino(inode))
2642 break;
2643 if (key.type != BTRFS_EXTENT_DATA_KEY)
2644 break;
2645 if (key.offset >= new->file_pos + new->len)
2646 break;
2647
2648 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2649
2650 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2651 if (key.offset + num_bytes < new->file_pos)
2652 goto next;
2653
2654 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2655 if (!disk_bytenr)
2656 goto next;
2657
2658 extent_offset = btrfs_file_extent_offset(l, extent);
2659
2660 old = kmalloc(sizeof(*old), GFP_NOFS);
2661 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002662 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002663
2664 offset = max(new->file_pos, key.offset);
2665 end = min(new->file_pos + new->len, key.offset + num_bytes);
2666
2667 old->bytenr = disk_bytenr;
2668 old->extent_offset = extent_offset;
2669 old->offset = offset - key.offset;
2670 old->len = end - offset;
2671 old->new = new;
2672 old->count = 0;
2673 list_add_tail(&old->list, &new->head);
2674next:
2675 path->slots[0]++;
2676 cond_resched();
2677 }
2678
2679 btrfs_free_path(path);
2680 atomic_inc(&root->fs_info->defrag_running);
2681
2682 return new;
2683
Liu Bo38c227d2013-01-29 03:18:40 +00002684out_free_path:
2685 btrfs_free_path(path);
2686out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002687 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002688 return NULL;
2689}
2690
Miao Xiee570fd22014-06-19 10:42:50 +08002691static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2692 u64 start, u64 len)
2693{
2694 struct btrfs_block_group_cache *cache;
2695
2696 cache = btrfs_lookup_block_group(root->fs_info, start);
2697 ASSERT(cache);
2698
2699 spin_lock(&cache->lock);
2700 cache->delalloc_bytes -= len;
2701 spin_unlock(&cache->lock);
2702
2703 btrfs_put_block_group(cache);
2704}
2705
Chris Masond352ac62008-09-29 15:18:18 -04002706/* as ordered data IO finishes, this gets called so we can finish
2707 * an ordered extent if the range of bytes in the file it covers are
2708 * fully written.
2709 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002710static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002711{
Josef Bacik5fd02042012-05-02 14:00:54 -04002712 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002713 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002714 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002715 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002716 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002717 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002718 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002719 int ret = 0;
2720 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002721 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002722 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002723
Liu Bo83eea1f2012-07-10 05:28:39 -06002724 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002725
Josef Bacik5fd02042012-05-02 14:00:54 -04002726 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2727 ret = -EIO;
2728 goto out;
2729 }
2730
Miao Xief6124962014-09-12 18:44:04 +08002731 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2732 ordered_extent->file_offset +
2733 ordered_extent->len - 1);
2734
Josef Bacik77cef2e2013-08-29 13:57:21 -04002735 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2736 truncated = true;
2737 logical_len = ordered_extent->truncated_len;
2738 /* Truncated the entire extent, don't bother adding */
2739 if (!logical_len)
2740 goto out;
2741 }
2742
Yan, Zhengc2167752009-11-12 09:34:21 +00002743 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002744 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002745 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2746 if (nolock)
2747 trans = btrfs_join_transaction_nolock(root);
2748 else
2749 trans = btrfs_join_transaction(root);
2750 if (IS_ERR(trans)) {
2751 ret = PTR_ERR(trans);
2752 trans = NULL;
2753 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002754 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002755 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2756 ret = btrfs_update_inode_fallback(trans, root, inode);
2757 if (ret) /* -ENOMEM or corruption */
2758 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002759 goto out;
2760 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002761
Josef Bacik2ac55d42010-02-03 19:33:23 +00002762 lock_extent_bits(io_tree, ordered_extent->file_offset,
2763 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002764 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002765
Liu Bo38c227d2013-01-29 03:18:40 +00002766 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2767 ordered_extent->file_offset + ordered_extent->len - 1,
2768 EXTENT_DEFRAG, 1, cached_state);
2769 if (ret) {
2770 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002771 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002772 /* the inode is shared */
2773 new = record_old_file_extents(inode, ordered_extent);
2774
2775 clear_extent_bit(io_tree, ordered_extent->file_offset,
2776 ordered_extent->file_offset + ordered_extent->len - 1,
2777 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2778 }
2779
Josef Bacik0cb59c92010-07-02 12:14:14 -04002780 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002781 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002782 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002783 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002784 if (IS_ERR(trans)) {
2785 ret = PTR_ERR(trans);
2786 trans = NULL;
2787 goto out_unlock;
2788 }
Chris Masona79b7d42014-05-22 16:18:52 -07002789
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002790 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002791
Chris Masonc8b97812008-10-29 14:49:59 -04002792 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002793 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002794 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002795 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002796 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002797 ordered_extent->file_offset,
2798 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002799 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002800 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002801 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002802 ret = insert_reserved_file_extent(trans, inode,
2803 ordered_extent->file_offset,
2804 ordered_extent->start,
2805 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002806 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002807 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002808 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002809 if (!ret)
2810 btrfs_release_delalloc_bytes(root,
2811 ordered_extent->start,
2812 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002813 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002814 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2815 ordered_extent->file_offset, ordered_extent->len,
2816 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002817 if (ret < 0) {
2818 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002819 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002820 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002821
Chris Masone6dcd2d2008-07-17 12:53:50 -04002822 add_pending_csums(trans, inode, ordered_extent->file_offset,
2823 &ordered_extent->list);
2824
Josef Bacik6c760c02012-11-09 10:53:21 -05002825 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2826 ret = btrfs_update_inode_fallback(trans, root, inode);
2827 if (ret) { /* -ENOMEM or corruption */
2828 btrfs_abort_transaction(trans, root, ret);
2829 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002830 }
2831 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002832out_unlock:
2833 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2834 ordered_extent->file_offset +
2835 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002836out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002837 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002838 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002839 if (trans)
2840 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002841
Josef Bacik77cef2e2013-08-29 13:57:21 -04002842 if (ret || truncated) {
2843 u64 start, end;
2844
2845 if (truncated)
2846 start = ordered_extent->file_offset + logical_len;
2847 else
2848 start = ordered_extent->file_offset;
2849 end = ordered_extent->file_offset + ordered_extent->len - 1;
2850 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2851
2852 /* Drop the cache for the part of the extent we didn't write. */
2853 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002854
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002855 /*
2856 * If the ordered extent had an IOERR or something else went
2857 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002858 * back to the allocator. We only free the extent in the
2859 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002860 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002861 if ((ret || !logical_len) &&
2862 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002863 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2864 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002865 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002866 }
2867
2868
Josef Bacik5fd02042012-05-02 14:00:54 -04002869 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002870 * This needs to be done to make sure anybody waiting knows we are done
2871 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002872 */
2873 btrfs_remove_ordered_extent(inode, ordered_extent);
2874
Liu Bo38c227d2013-01-29 03:18:40 +00002875 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002876 if (new) {
2877 if (ret) {
2878 free_sa_defrag_extent(new);
2879 atomic_dec(&root->fs_info->defrag_running);
2880 } else {
2881 relink_file_extents(new);
2882 }
2883 }
Liu Bo38c227d2013-01-29 03:18:40 +00002884
Chris Masone6dcd2d2008-07-17 12:53:50 -04002885 /* once for us */
2886 btrfs_put_ordered_extent(ordered_extent);
2887 /* once for the tree */
2888 btrfs_put_ordered_extent(ordered_extent);
2889
Josef Bacik5fd02042012-05-02 14:00:54 -04002890 return ret;
2891}
2892
2893static void finish_ordered_fn(struct btrfs_work *work)
2894{
2895 struct btrfs_ordered_extent *ordered_extent;
2896 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2897 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002898}
2899
Christoph Hellwigb2950862008-12-02 09:54:17 -05002900static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002901 struct extent_state *state, int uptodate)
2902{
Josef Bacik5fd02042012-05-02 14:00:54 -04002903 struct inode *inode = page->mapping->host;
2904 struct btrfs_root *root = BTRFS_I(inode)->root;
2905 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002906 struct btrfs_workqueue *wq;
2907 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002908
liubo1abe9b82011-03-24 11:18:59 +00002909 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2910
Chris Mason8b62b722009-09-02 16:53:46 -04002911 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002912 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2913 end - start + 1, uptodate))
2914 return 0;
2915
Liu Bo9e0af232014-08-15 23:36:53 +08002916 if (btrfs_is_free_space_inode(inode)) {
2917 wq = root->fs_info->endio_freespace_worker;
2918 func = btrfs_freespace_write_helper;
2919 } else {
2920 wq = root->fs_info->endio_write_workers;
2921 func = btrfs_endio_write_helper;
2922 }
Josef Bacik5fd02042012-05-02 14:00:54 -04002923
Liu Bo9e0af232014-08-15 23:36:53 +08002924 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
2925 NULL);
2926 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002927
2928 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002929}
2930
Miao Xiedc380ae2014-09-12 18:43:55 +08002931static int __readpage_endio_check(struct inode *inode,
2932 struct btrfs_io_bio *io_bio,
2933 int icsum, struct page *page,
2934 int pgoff, u64 start, size_t len)
2935{
2936 char *kaddr;
2937 u32 csum_expected;
2938 u32 csum = ~(u32)0;
2939 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2940 DEFAULT_RATELIMIT_BURST);
2941
2942 csum_expected = *(((u32 *)io_bio->csum) + icsum);
2943
2944 kaddr = kmap_atomic(page);
2945 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
2946 btrfs_csum_final(csum, (char *)&csum);
2947 if (csum != csum_expected)
2948 goto zeroit;
2949
2950 kunmap_atomic(kaddr);
2951 return 0;
2952zeroit:
2953 if (__ratelimit(&_rs))
2954 btrfs_info(BTRFS_I(inode)->root->fs_info,
2955 "csum failed ino %llu off %llu csum %u expected csum %u",
2956 btrfs_ino(inode), start, csum, csum_expected);
2957 memset(kaddr + pgoff, 1, len);
2958 flush_dcache_page(page);
2959 kunmap_atomic(kaddr);
2960 if (csum_expected == 0)
2961 return 0;
2962 return -EIO;
2963}
2964
Chris Masond352ac62008-09-29 15:18:18 -04002965/*
Chris Masond352ac62008-09-29 15:18:18 -04002966 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002967 * if there's a match, we allow the bio to finish. If not, the code in
2968 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002969 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002970static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2971 u64 phy_offset, struct page *page,
2972 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002973{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002974 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002975 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002976 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002977 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002978
Chris Masond20f7042008-12-08 16:58:54 -05002979 if (PageChecked(page)) {
2980 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002981 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002982 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002983
2984 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002985 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002986
Yan Zheng17d217f2008-12-12 10:03:38 -05002987 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002988 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002989 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2990 GFP_NOFS);
2991 return 0;
2992 }
2993
Miao Xiefacc8a222013-07-25 19:22:34 +08002994 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08002995 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
2996 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002997}
Chris Masonb888db2b2007-08-27 16:49:44 -04002998
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002999struct delayed_iput {
3000 struct list_head list;
3001 struct inode *inode;
3002};
3003
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003004/* JDM: If this is fs-wide, why can't we add a pointer to
3005 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003006void btrfs_add_delayed_iput(struct inode *inode)
3007{
3008 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3009 struct delayed_iput *delayed;
3010
3011 if (atomic_add_unless(&inode->i_count, -1, 1))
3012 return;
3013
3014 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3015 delayed->inode = inode;
3016
3017 spin_lock(&fs_info->delayed_iput_lock);
3018 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3019 spin_unlock(&fs_info->delayed_iput_lock);
3020}
3021
3022void btrfs_run_delayed_iputs(struct btrfs_root *root)
3023{
3024 LIST_HEAD(list);
3025 struct btrfs_fs_info *fs_info = root->fs_info;
3026 struct delayed_iput *delayed;
3027 int empty;
3028
3029 spin_lock(&fs_info->delayed_iput_lock);
3030 empty = list_empty(&fs_info->delayed_iputs);
3031 spin_unlock(&fs_info->delayed_iput_lock);
3032 if (empty)
3033 return;
3034
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003035 spin_lock(&fs_info->delayed_iput_lock);
3036 list_splice_init(&fs_info->delayed_iputs, &list);
3037 spin_unlock(&fs_info->delayed_iput_lock);
3038
3039 while (!list_empty(&list)) {
3040 delayed = list_entry(list.next, struct delayed_iput, list);
3041 list_del(&delayed->list);
3042 iput(delayed->inode);
3043 kfree(delayed);
3044 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003045}
3046
Yan, Zhengd68fc572010-05-16 10:49:58 -04003047/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003048 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003049 * files in the subvolume, it removes orphan item and frees block_rsv
3050 * structure.
3051 */
3052void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3053 struct btrfs_root *root)
3054{
Josef Bacik90290e12011-12-02 15:44:12 -05003055 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003056 int ret;
3057
Josef Bacik8a35d952012-05-23 14:26:42 -04003058 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003059 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3060 return;
3061
Josef Bacik90290e12011-12-02 15:44:12 -05003062 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003063 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003064 spin_unlock(&root->orphan_lock);
3065 return;
3066 }
3067
3068 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3069 spin_unlock(&root->orphan_lock);
3070 return;
3071 }
3072
3073 block_rsv = root->orphan_block_rsv;
3074 root->orphan_block_rsv = NULL;
3075 spin_unlock(&root->orphan_lock);
3076
Miao Xie27cdeb72014-04-02 19:51:05 +08003077 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003078 btrfs_root_refs(&root->root_item) > 0) {
3079 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3080 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003081 if (ret)
3082 btrfs_abort_transaction(trans, root, ret);
3083 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003084 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3085 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003086 }
3087
Josef Bacik90290e12011-12-02 15:44:12 -05003088 if (block_rsv) {
3089 WARN_ON(block_rsv->size > 0);
3090 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003091 }
3092}
3093
3094/*
Josef Bacik7b128762008-07-24 12:17:14 -04003095 * This creates an orphan entry for the given inode in case something goes
3096 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003097 *
3098 * NOTE: caller of this function should reserve 5 units of metadata for
3099 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003100 */
3101int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3102{
3103 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003104 struct btrfs_block_rsv *block_rsv = NULL;
3105 int reserve = 0;
3106 int insert = 0;
3107 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003108
Yan, Zhengd68fc572010-05-16 10:49:58 -04003109 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003110 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003111 if (!block_rsv)
3112 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003113 }
3114
Yan, Zhengd68fc572010-05-16 10:49:58 -04003115 spin_lock(&root->orphan_lock);
3116 if (!root->orphan_block_rsv) {
3117 root->orphan_block_rsv = block_rsv;
3118 } else if (block_rsv) {
3119 btrfs_free_block_rsv(root, block_rsv);
3120 block_rsv = NULL;
3121 }
Josef Bacik7b128762008-07-24 12:17:14 -04003122
Josef Bacik8a35d952012-05-23 14:26:42 -04003123 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3124 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003125#if 0
3126 /*
3127 * For proper ENOSPC handling, we should do orphan
3128 * cleanup when mounting. But this introduces backward
3129 * compatibility issue.
3130 */
3131 if (!xchg(&root->orphan_item_inserted, 1))
3132 insert = 2;
3133 else
3134 insert = 1;
3135#endif
3136 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003137 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003138 }
Josef Bacik7b128762008-07-24 12:17:14 -04003139
Josef Bacik72ac3c02012-05-23 14:13:11 -04003140 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3141 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003142 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003143 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003144
Yan, Zhengd68fc572010-05-16 10:49:58 -04003145 /* grab metadata reservation from transaction handle */
3146 if (reserve) {
3147 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003148 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003149 }
3150
3151 /* insert an orphan item to track this unlinked/truncated file */
3152 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003153 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003154 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003155 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003156 if (reserve) {
3157 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3158 &BTRFS_I(inode)->runtime_flags);
3159 btrfs_orphan_release_metadata(inode);
3160 }
3161 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003162 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3163 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003164 btrfs_abort_transaction(trans, root, ret);
3165 return ret;
3166 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003167 }
3168 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003169 }
3170
3171 /* insert an orphan item to track subvolume contains orphan files */
3172 if (insert >= 2) {
3173 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3174 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003175 if (ret && ret != -EEXIST) {
3176 btrfs_abort_transaction(trans, root, ret);
3177 return ret;
3178 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003179 }
3180 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003181}
3182
3183/*
3184 * We have done the truncate/delete so we can go ahead and remove the orphan
3185 * item for this particular inode.
3186 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003187static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3188 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003189{
3190 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003191 int delete_item = 0;
3192 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003193 int ret = 0;
3194
Yan, Zhengd68fc572010-05-16 10:49:58 -04003195 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003196 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3197 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003198 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003199
Josef Bacik72ac3c02012-05-23 14:13:11 -04003200 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3201 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003202 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003203 spin_unlock(&root->orphan_lock);
3204
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003205 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003206 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003207 if (trans)
3208 ret = btrfs_del_orphan_item(trans, root,
3209 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003210 }
Josef Bacik7b128762008-07-24 12:17:14 -04003211
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003212 if (release_rsv)
3213 btrfs_orphan_release_metadata(inode);
3214
Josef Bacik4ef31a42013-08-13 14:10:08 -04003215 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003216}
3217
3218/*
3219 * this cleans up any orphans that may be left on the list from the last use
3220 * of this root.
3221 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003222int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003223{
3224 struct btrfs_path *path;
3225 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003226 struct btrfs_key key, found_key;
3227 struct btrfs_trans_handle *trans;
3228 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003229 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003230 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3231
Yan, Zhengd68fc572010-05-16 10:49:58 -04003232 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003233 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003234
3235 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003236 if (!path) {
3237 ret = -ENOMEM;
3238 goto out;
3239 }
Josef Bacik7b128762008-07-24 12:17:14 -04003240 path->reada = -1;
3241
3242 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003243 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003244 key.offset = (u64)-1;
3245
Josef Bacik7b128762008-07-24 12:17:14 -04003246 while (1) {
3247 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003248 if (ret < 0)
3249 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003250
3251 /*
3252 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003253 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003254 * find the key and see if we have stuff that matches
3255 */
3256 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003257 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003258 if (path->slots[0] == 0)
3259 break;
3260 path->slots[0]--;
3261 }
3262
3263 /* pull out the item */
3264 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003265 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3266
3267 /* make sure the item matches what we want */
3268 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3269 break;
David Sterba962a2982014-06-04 18:41:45 +02003270 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003271 break;
3272
3273 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003274 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003275
3276 /*
3277 * this is where we are basically btrfs_lookup, without the
3278 * crossing root thing. we store the inode number in the
3279 * offset of the orphan item.
3280 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003281
3282 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003283 btrfs_err(root->fs_info,
3284 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003285 ret = -EINVAL;
3286 goto out;
3287 }
3288
3289 last_objectid = found_key.offset;
3290
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003291 found_key.objectid = found_key.offset;
3292 found_key.type = BTRFS_INODE_ITEM_KEY;
3293 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003294 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303295 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003296 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003297 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003298
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003299 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3300 struct btrfs_root *dead_root;
3301 struct btrfs_fs_info *fs_info = root->fs_info;
3302 int is_dead_root = 0;
3303
3304 /*
3305 * this is an orphan in the tree root. Currently these
3306 * could come from 2 sources:
3307 * a) a snapshot deletion in progress
3308 * b) a free space cache inode
3309 * We need to distinguish those two, as the snapshot
3310 * orphan must not get deleted.
3311 * find_dead_roots already ran before us, so if this
3312 * is a snapshot deletion, we should find the root
3313 * in the dead_roots list
3314 */
3315 spin_lock(&fs_info->trans_lock);
3316 list_for_each_entry(dead_root, &fs_info->dead_roots,
3317 root_list) {
3318 if (dead_root->root_key.objectid ==
3319 found_key.objectid) {
3320 is_dead_root = 1;
3321 break;
3322 }
3323 }
3324 spin_unlock(&fs_info->trans_lock);
3325 if (is_dead_root) {
3326 /* prevent this orphan from being found again */
3327 key.offset = found_key.objectid - 1;
3328 continue;
3329 }
3330 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003331 /*
3332 * Inode is already gone but the orphan item is still there,
3333 * kill the orphan item.
3334 */
3335 if (ret == -ESTALE) {
3336 trans = btrfs_start_transaction(root, 1);
3337 if (IS_ERR(trans)) {
3338 ret = PTR_ERR(trans);
3339 goto out;
3340 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003341 btrfs_debug(root->fs_info, "auto deleting %Lu",
3342 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003343 ret = btrfs_del_orphan_item(trans, root,
3344 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003345 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003346 if (ret)
3347 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003348 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003349 }
Josef Bacik7b128762008-07-24 12:17:14 -04003350
Josef Bacik7b128762008-07-24 12:17:14 -04003351 /*
3352 * add this inode to the orphan list so btrfs_orphan_del does
3353 * the proper thing when we hit it
3354 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003355 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3356 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003357 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003358
Josef Bacik7b128762008-07-24 12:17:14 -04003359 /* if we have links, this was a truncate, lets do that */
3360 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303361 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003362 iput(inode);
3363 continue;
3364 }
Josef Bacik7b128762008-07-24 12:17:14 -04003365 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003366
3367 /* 1 for the orphan item deletion. */
3368 trans = btrfs_start_transaction(root, 1);
3369 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003370 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003371 ret = PTR_ERR(trans);
3372 goto out;
3373 }
3374 ret = btrfs_orphan_add(trans, inode);
3375 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003376 if (ret) {
3377 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003378 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003379 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003380
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003381 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003382 if (ret)
3383 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003384 } else {
3385 nr_unlink++;
3386 }
3387
3388 /* this will do delete_inode and everything for us */
3389 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003390 if (ret)
3391 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003392 }
Miao Xie3254c872011-11-10 20:45:05 -05003393 /* release the path since we're done with it */
3394 btrfs_release_path(path);
3395
Yan, Zhengd68fc572010-05-16 10:49:58 -04003396 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3397
3398 if (root->orphan_block_rsv)
3399 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3400 (u64)-1);
3401
Miao Xie27cdeb72014-04-02 19:51:05 +08003402 if (root->orphan_block_rsv ||
3403 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003404 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003405 if (!IS_ERR(trans))
3406 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003407 }
Josef Bacik7b128762008-07-24 12:17:14 -04003408
3409 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003410 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003411 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003412 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003413
3414out:
3415 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003416 btrfs_crit(root->fs_info,
3417 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003418 btrfs_free_path(path);
3419 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003420}
3421
Chris Masond352ac62008-09-29 15:18:18 -04003422/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003423 * very simple check to peek ahead in the leaf looking for xattrs. If we
3424 * don't find any xattrs, we know there can't be any acls.
3425 *
3426 * slot is the slot the inode is in, objectid is the objectid of the inode
3427 */
3428static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003429 int slot, u64 objectid,
3430 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003431{
3432 u32 nritems = btrfs_header_nritems(leaf);
3433 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003434 static u64 xattr_access = 0;
3435 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003436 int scanned = 0;
3437
Josef Bacikf23b5a52013-06-19 10:16:26 -04003438 if (!xattr_access) {
3439 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3440 strlen(POSIX_ACL_XATTR_ACCESS));
3441 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3442 strlen(POSIX_ACL_XATTR_DEFAULT));
3443 }
3444
Chris Mason46a53cc2009-04-27 11:47:50 -04003445 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003446 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003447 while (slot < nritems) {
3448 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3449
3450 /* we found a different objectid, there must not be acls */
3451 if (found_key.objectid != objectid)
3452 return 0;
3453
3454 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003455 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003456 if (*first_xattr_slot == -1)
3457 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003458 if (found_key.offset == xattr_access ||
3459 found_key.offset == xattr_default)
3460 return 1;
3461 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003462
3463 /*
3464 * we found a key greater than an xattr key, there can't
3465 * be any acls later on
3466 */
3467 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3468 return 0;
3469
3470 slot++;
3471 scanned++;
3472
3473 /*
3474 * it goes inode, inode backrefs, xattrs, extents,
3475 * so if there are a ton of hard links to an inode there can
3476 * be a lot of backrefs. Don't waste time searching too hard,
3477 * this is just an optimization
3478 */
3479 if (scanned >= 8)
3480 break;
3481 }
3482 /* we hit the end of the leaf before we found an xattr or
3483 * something larger than an xattr. We have to assume the inode
3484 * has acls
3485 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003486 if (*first_xattr_slot == -1)
3487 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003488 return 1;
3489}
3490
3491/*
Chris Masond352ac62008-09-29 15:18:18 -04003492 * read an inode from the btree into the in-memory inode
3493 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003494static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003495{
3496 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003497 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003498 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003499 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003500 struct btrfs_root *root = BTRFS_I(inode)->root;
3501 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003502 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003503 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003504 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003505 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003506 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003507 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003508
3509 ret = btrfs_fill_inode(inode, &rdev);
3510 if (!ret)
3511 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003512
3513 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003514 if (!path)
3515 goto make_bad;
3516
Chris Mason39279cc2007-06-12 06:35:45 -04003517 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003518
Chris Mason39279cc2007-06-12 06:35:45 -04003519 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003520 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003521 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003522
Chris Mason5f39d392007-10-15 16:14:19 -04003523 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003524
3525 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003526 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003527
Chris Mason5f39d392007-10-15 16:14:19 -04003528 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3529 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003530 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003531 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003532 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3533 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003534 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003535
3536 tspec = btrfs_inode_atime(inode_item);
3537 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3538 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3539
3540 tspec = btrfs_inode_mtime(inode_item);
3541 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3542 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3543
3544 tspec = btrfs_inode_ctime(inode_item);
3545 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3546 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3547
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003548 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003549 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003550 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3551
3552 /*
3553 * If we were modified in the current generation and evicted from memory
3554 * and then re-read we need to do a full sync since we don't have any
3555 * idea about which extents were modified before we were evicted from
3556 * cache.
3557 */
3558 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3559 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3560 &BTRFS_I(inode)->runtime_flags);
3561
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003562 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003563 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003564 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003565 rdev = btrfs_inode_rdev(leaf, inode_item);
3566
Josef Bacikaec74772008-07-24 12:12:38 -04003567 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003568 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003569
3570cache_index:
3571 path->slots[0]++;
3572 if (inode->i_nlink != 1 ||
3573 path->slots[0] >= btrfs_header_nritems(leaf))
3574 goto cache_acl;
3575
3576 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3577 if (location.objectid != btrfs_ino(inode))
3578 goto cache_acl;
3579
3580 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3581 if (location.type == BTRFS_INODE_REF_KEY) {
3582 struct btrfs_inode_ref *ref;
3583
3584 ref = (struct btrfs_inode_ref *)ptr;
3585 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3586 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3587 struct btrfs_inode_extref *extref;
3588
3589 extref = (struct btrfs_inode_extref *)ptr;
3590 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3591 extref);
3592 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003593cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003594 /*
3595 * try to precache a NULL acl entry for files that don't have
3596 * any xattrs or acls
3597 */
Li Zefan33345d012011-04-20 10:31:50 +08003598 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003599 btrfs_ino(inode), &first_xattr_slot);
3600 if (first_xattr_slot != -1) {
3601 path->slots[0] = first_xattr_slot;
3602 ret = btrfs_load_inode_props(inode, path);
3603 if (ret)
3604 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003605 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003606 btrfs_ino(inode),
3607 root->root_key.objectid, ret);
3608 }
3609 btrfs_free_path(path);
3610
Al Viro72c04902009-06-24 16:58:48 -04003611 if (!maybe_acls)
3612 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003613
Chris Mason39279cc2007-06-12 06:35:45 -04003614 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003615 case S_IFREG:
3616 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003617 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003618 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003619 inode->i_fop = &btrfs_file_operations;
3620 inode->i_op = &btrfs_file_inode_operations;
3621 break;
3622 case S_IFDIR:
3623 inode->i_fop = &btrfs_dir_file_operations;
3624 if (root == root->fs_info->tree_root)
3625 inode->i_op = &btrfs_dir_ro_inode_operations;
3626 else
3627 inode->i_op = &btrfs_dir_inode_operations;
3628 break;
3629 case S_IFLNK:
3630 inode->i_op = &btrfs_symlink_inode_operations;
3631 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003632 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003633 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003634 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003635 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003636 init_special_inode(inode, inode->i_mode, rdev);
3637 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003638 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003639
3640 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003641 return;
3642
3643make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003644 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003645 make_bad_inode(inode);
3646}
3647
Chris Masond352ac62008-09-29 15:18:18 -04003648/*
3649 * given a leaf and an inode, copy the inode fields into the leaf
3650 */
Chris Masone02119d2008-09-05 16:13:11 -04003651static void fill_inode_item(struct btrfs_trans_handle *trans,
3652 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003653 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003654 struct inode *inode)
3655{
Liu Bo51fab692012-12-27 09:01:21 +00003656 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003657
Liu Bo51fab692012-12-27 09:01:21 +00003658 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003659
Liu Bo51fab692012-12-27 09:01:21 +00003660 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3661 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3662 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3663 &token);
3664 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3665 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003666
Liu Bo51fab692012-12-27 09:01:21 +00003667 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3668 inode->i_atime.tv_sec, &token);
3669 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3670 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003671
Liu Bo51fab692012-12-27 09:01:21 +00003672 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3673 inode->i_mtime.tv_sec, &token);
3674 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3675 inode->i_mtime.tv_nsec, &token);
3676
3677 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3678 inode->i_ctime.tv_sec, &token);
3679 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3680 inode->i_ctime.tv_nsec, &token);
3681
3682 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3683 &token);
3684 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3685 &token);
3686 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3687 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3688 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3689 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3690 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003691}
3692
Chris Masond352ac62008-09-29 15:18:18 -04003693/*
3694 * copy everything in the in-memory inode into the btree.
3695 */
Chris Mason21151332011-11-10 20:39:08 -05003696static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003697 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003698{
3699 struct btrfs_inode_item *inode_item;
3700 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003701 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003702 int ret;
3703
3704 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003705 if (!path)
3706 return -ENOMEM;
3707
Chris Masonb9473432009-03-13 11:00:37 -04003708 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003709 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3710 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003711 if (ret) {
3712 if (ret > 0)
3713 ret = -ENOENT;
3714 goto failed;
3715 }
3716
Chris Mason5f39d392007-10-15 16:14:19 -04003717 leaf = path->nodes[0];
3718 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003719 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003720
Chris Masone02119d2008-09-05 16:13:11 -04003721 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003722 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003723 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003724 ret = 0;
3725failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003726 btrfs_free_path(path);
3727 return ret;
3728}
3729
Chris Masond352ac62008-09-29 15:18:18 -04003730/*
Chris Mason21151332011-11-10 20:39:08 -05003731 * copy everything in the in-memory inode into the btree.
3732 */
3733noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3734 struct btrfs_root *root, struct inode *inode)
3735{
3736 int ret;
3737
3738 /*
3739 * If the inode is a free space inode, we can deadlock during commit
3740 * if we put it into the delayed code.
3741 *
3742 * The data relocation inode should also be directly updated
3743 * without delay
3744 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003745 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003746 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3747 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003748 btrfs_update_root_times(trans, root);
3749
Chris Mason21151332011-11-10 20:39:08 -05003750 ret = btrfs_delayed_update_inode(trans, root, inode);
3751 if (!ret)
3752 btrfs_set_inode_last_trans(trans, inode);
3753 return ret;
3754 }
3755
3756 return btrfs_update_inode_item(trans, root, inode);
3757}
3758
Josef Bacikbe6aef62012-10-22 15:43:12 -04003759noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3760 struct btrfs_root *root,
3761 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003762{
3763 int ret;
3764
3765 ret = btrfs_update_inode(trans, root, inode);
3766 if (ret == -ENOSPC)
3767 return btrfs_update_inode_item(trans, root, inode);
3768 return ret;
3769}
3770
3771/*
Chris Masond352ac62008-09-29 15:18:18 -04003772 * unlink helper that gets used here in inode.c and in the tree logging
3773 * recovery code. It remove a link in a directory with a given name, and
3774 * also drops the back refs in the inode to the directory
3775 */
Al Viro92986792011-03-04 17:14:37 +00003776static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3777 struct btrfs_root *root,
3778 struct inode *dir, struct inode *inode,
3779 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003780{
3781 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003782 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003783 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003784 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003785 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003786 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003787 u64 ino = btrfs_ino(inode);
3788 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003789
3790 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003791 if (!path) {
3792 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003793 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003794 }
3795
Chris Masonb9473432009-03-13 11:00:37 -04003796 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003797 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003798 name, name_len, -1);
3799 if (IS_ERR(di)) {
3800 ret = PTR_ERR(di);
3801 goto err;
3802 }
3803 if (!di) {
3804 ret = -ENOENT;
3805 goto err;
3806 }
Chris Mason5f39d392007-10-15 16:14:19 -04003807 leaf = path->nodes[0];
3808 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003809 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003810 if (ret)
3811 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003812 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003813
Miao Xie67de1172013-12-26 13:07:06 +08003814 /*
3815 * If we don't have dir index, we have to get it by looking up
3816 * the inode ref, since we get the inode ref, remove it directly,
3817 * it is unnecessary to do delayed deletion.
3818 *
3819 * But if we have dir index, needn't search inode ref to get it.
3820 * Since the inode ref is close to the inode item, it is better
3821 * that we delay to delete it, and just do this deletion when
3822 * we update the inode item.
3823 */
3824 if (BTRFS_I(inode)->dir_index) {
3825 ret = btrfs_delayed_delete_inode_ref(inode);
3826 if (!ret) {
3827 index = BTRFS_I(inode)->dir_index;
3828 goto skip_backref;
3829 }
3830 }
3831
Li Zefan33345d012011-04-20 10:31:50 +08003832 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3833 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003834 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003835 btrfs_info(root->fs_info,
3836 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003837 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003838 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003839 goto err;
3840 }
Miao Xie67de1172013-12-26 13:07:06 +08003841skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003842 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003843 if (ret) {
3844 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003845 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003846 }
Chris Mason39279cc2007-06-12 06:35:45 -04003847
Chris Masone02119d2008-09-05 16:13:11 -04003848 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003849 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003850 if (ret != 0 && ret != -ENOENT) {
3851 btrfs_abort_transaction(trans, root, ret);
3852 goto err;
3853 }
Chris Masone02119d2008-09-05 16:13:11 -04003854
3855 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3856 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003857 if (ret == -ENOENT)
3858 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003859 else if (ret)
3860 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003861err:
3862 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003863 if (ret)
3864 goto out;
3865
3866 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003867 inode_inc_iversion(inode);
3868 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003869 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003870 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003871out:
Chris Mason39279cc2007-06-12 06:35:45 -04003872 return ret;
3873}
3874
Al Viro92986792011-03-04 17:14:37 +00003875int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3876 struct btrfs_root *root,
3877 struct inode *dir, struct inode *inode,
3878 const char *name, int name_len)
3879{
3880 int ret;
3881 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3882 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003883 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003884 ret = btrfs_update_inode(trans, root, inode);
3885 }
3886 return ret;
3887}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003888
3889/*
3890 * helper to start transaction for unlink and rmdir.
3891 *
Josef Bacikd52be812013-05-29 14:54:47 -04003892 * unlink and rmdir are special in btrfs, they do not always free space, so
3893 * if we cannot make our reservations the normal way try and see if there is
3894 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3895 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003896 */
Josef Bacikd52be812013-05-29 14:54:47 -04003897static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003898{
3899 struct btrfs_trans_handle *trans;
3900 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003901 int ret;
3902
Josef Bacike70bea52011-10-11 14:18:24 -04003903 /*
3904 * 1 for the possible orphan item
3905 * 1 for the dir item
3906 * 1 for the dir index
3907 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003908 * 1 for the inode
3909 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003910 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003911 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3912 return trans;
3913
Josef Bacikd52be812013-05-29 14:54:47 -04003914 if (PTR_ERR(trans) == -ENOSPC) {
3915 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003916
Josef Bacikd52be812013-05-29 14:54:47 -04003917 trans = btrfs_start_transaction(root, 0);
3918 if (IS_ERR(trans))
3919 return trans;
3920 ret = btrfs_cond_migrate_bytes(root->fs_info,
3921 &root->fs_info->trans_block_rsv,
3922 num_bytes, 5);
3923 if (ret) {
3924 btrfs_end_transaction(trans, root);
3925 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003926 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003927 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003928 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003929 }
Josef Bacikd52be812013-05-29 14:54:47 -04003930 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003931}
3932
Chris Mason39279cc2007-06-12 06:35:45 -04003933static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3934{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003935 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003936 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003937 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003938 int ret;
3939
Josef Bacikd52be812013-05-29 14:54:47 -04003940 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003941 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003942 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003943
Chris Mason12fcfd22009-03-24 10:24:20 -04003944 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3945
Chris Masone02119d2008-09-05 16:13:11 -04003946 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3947 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003948 if (ret)
3949 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003950
Yan, Zhenga22285a2010-05-16 10:48:46 -04003951 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003952 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003953 if (ret)
3954 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003955 }
Josef Bacik7b128762008-07-24 12:17:14 -04003956
Tsutomu Itohb5324022011-07-19 07:27:20 +00003957out:
Josef Bacikd52be812013-05-29 14:54:47 -04003958 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003959 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003960 return ret;
3961}
3962
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003963int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3964 struct btrfs_root *root,
3965 struct inode *dir, u64 objectid,
3966 const char *name, int name_len)
3967{
3968 struct btrfs_path *path;
3969 struct extent_buffer *leaf;
3970 struct btrfs_dir_item *di;
3971 struct btrfs_key key;
3972 u64 index;
3973 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003974 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003975
3976 path = btrfs_alloc_path();
3977 if (!path)
3978 return -ENOMEM;
3979
Li Zefan33345d012011-04-20 10:31:50 +08003980 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003981 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003982 if (IS_ERR_OR_NULL(di)) {
3983 if (!di)
3984 ret = -ENOENT;
3985 else
3986 ret = PTR_ERR(di);
3987 goto out;
3988 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003989
3990 leaf = path->nodes[0];
3991 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3992 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3993 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003994 if (ret) {
3995 btrfs_abort_transaction(trans, root, ret);
3996 goto out;
3997 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003998 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003999
4000 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4001 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004002 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004003 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004004 if (ret != -ENOENT) {
4005 btrfs_abort_transaction(trans, root, ret);
4006 goto out;
4007 }
Li Zefan33345d012011-04-20 10:31:50 +08004008 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004009 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004010 if (IS_ERR_OR_NULL(di)) {
4011 if (!di)
4012 ret = -ENOENT;
4013 else
4014 ret = PTR_ERR(di);
4015 btrfs_abort_transaction(trans, root, ret);
4016 goto out;
4017 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004018
4019 leaf = path->nodes[0];
4020 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004021 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004022 index = key.offset;
4023 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004024 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004025
Miao Xie16cdcec2011-04-22 18:12:22 +08004026 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004027 if (ret) {
4028 btrfs_abort_transaction(trans, root, ret);
4029 goto out;
4030 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004031
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004032 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004033 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004034 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004035 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004036 if (ret)
4037 btrfs_abort_transaction(trans, root, ret);
4038out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004039 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004040 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004041}
4042
Chris Mason39279cc2007-06-12 06:35:45 -04004043static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4044{
4045 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004046 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004047 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004048 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004049
David Sterbab3ae2442012-09-13 16:04:34 -06004050 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004051 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004052 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4053 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004054
Josef Bacikd52be812013-05-29 14:54:47 -04004055 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004056 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004057 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004058
Li Zefan33345d012011-04-20 10:31:50 +08004059 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004060 err = btrfs_unlink_subvol(trans, root, dir,
4061 BTRFS_I(inode)->location.objectid,
4062 dentry->d_name.name,
4063 dentry->d_name.len);
4064 goto out;
4065 }
4066
Josef Bacik7b128762008-07-24 12:17:14 -04004067 err = btrfs_orphan_add(trans, inode);
4068 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004069 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004070
Chris Mason39279cc2007-06-12 06:35:45 -04004071 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004072 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4073 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004074 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004075 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004076out:
Josef Bacikd52be812013-05-29 14:54:47 -04004077 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004078 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004079
Chris Mason39279cc2007-06-12 06:35:45 -04004080 return err;
4081}
4082
Chris Mason323ac952008-10-01 19:05:46 -04004083/*
Chris Mason39279cc2007-06-12 06:35:45 -04004084 * this can truncate away extent items, csum items and directory items.
4085 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004086 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004087 *
4088 * csum items that cross the new i_size are truncated to the new size
4089 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004090 *
4091 * min_type is the minimum key type to truncate down to. If set to 0, this
4092 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004093 */
Yan, Zheng80825102009-11-12 09:35:36 +00004094int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4095 struct btrfs_root *root,
4096 struct inode *inode,
4097 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004098{
Chris Mason39279cc2007-06-12 06:35:45 -04004099 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004100 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004101 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004102 struct btrfs_key key;
4103 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004104 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004105 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004106 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004107 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004108 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004109 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004110 int found_extent;
4111 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004112 int pending_del_nr = 0;
4113 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004114 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004115 int ret;
4116 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004117 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004118
4119 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004120
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004121 path = btrfs_alloc_path();
4122 if (!path)
4123 return -ENOMEM;
4124 path->reada = -1;
4125
Josef Bacik5dc562c2012-08-17 13:14:17 -04004126 /*
4127 * We want to drop from the next block forward in case this new size is
4128 * not block aligned since we will be keeping the last block of the
4129 * extent just the way it is.
4130 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004131 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4132 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004133 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4134 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004135
Miao Xie16cdcec2011-04-22 18:12:22 +08004136 /*
4137 * This function is also used to drop the items in the log tree before
4138 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4139 * it is used to drop the loged items. So we shouldn't kill the delayed
4140 * items.
4141 */
4142 if (min_type == 0 && root == BTRFS_I(inode)->root)
4143 btrfs_kill_delayed_inode_items(inode);
4144
Li Zefan33345d012011-04-20 10:31:50 +08004145 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004146 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004147 key.type = (u8)-1;
4148
Chris Mason85e21ba2008-01-29 15:11:36 -05004149search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004150 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004151 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004152 if (ret < 0) {
4153 err = ret;
4154 goto out;
4155 }
Chris Masond3977122009-01-05 21:25:51 -05004156
Chris Mason85e21ba2008-01-29 15:11:36 -05004157 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004158 /* there are no items in the tree for us to truncate, we're
4159 * done
4160 */
Yan, Zheng80825102009-11-12 09:35:36 +00004161 if (path->slots[0] == 0)
4162 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004163 path->slots[0]--;
4164 }
4165
Chris Masond3977122009-01-05 21:25:51 -05004166 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004167 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004168 leaf = path->nodes[0];
4169 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004170 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004171
Li Zefan33345d012011-04-20 10:31:50 +08004172 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004173 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004174
Chris Mason85e21ba2008-01-29 15:11:36 -05004175 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004176 break;
4177
Chris Mason5f39d392007-10-15 16:14:19 -04004178 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004179 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004180 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004181 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004182 extent_type = btrfs_file_extent_type(leaf, fi);
4183 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004184 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004185 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004186 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004187 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004188 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004189 }
Yan008630c2007-11-07 13:31:09 -05004190 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004191 }
Yan, Zheng80825102009-11-12 09:35:36 +00004192 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004193 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004194 } else {
4195 if (item_end < new_size)
4196 break;
4197 if (found_key.offset >= new_size)
4198 del_item = 1;
4199 else
4200 del_item = 0;
4201 }
Chris Mason39279cc2007-06-12 06:35:45 -04004202 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004203 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004204 if (found_type != BTRFS_EXTENT_DATA_KEY)
4205 goto delete;
4206
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004207 if (del_item)
4208 last_size = found_key.offset;
4209 else
4210 last_size = new_size;
4211
Chris Mason179e29e2007-11-01 11:28:41 -04004212 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004213 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004214 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004215 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004216 u64 orig_num_bytes =
4217 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004218 extent_num_bytes = ALIGN(new_size -
4219 found_key.offset,
4220 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004221 btrfs_set_file_extent_num_bytes(leaf, fi,
4222 extent_num_bytes);
4223 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004224 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004225 if (test_bit(BTRFS_ROOT_REF_COWS,
4226 &root->state) &&
4227 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004228 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004229 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004230 } else {
Chris Masondb945352007-10-15 16:15:53 -04004231 extent_num_bytes =
4232 btrfs_file_extent_disk_num_bytes(leaf,
4233 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004234 extent_offset = found_key.offset -
4235 btrfs_file_extent_offset(leaf, fi);
4236
Chris Mason39279cc2007-06-12 06:35:45 -04004237 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004238 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004239 if (extent_start != 0) {
4240 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004241 if (test_bit(BTRFS_ROOT_REF_COWS,
4242 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004243 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004244 }
4245 }
Chris Mason90692182008-02-08 13:49:28 -05004246 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004247 /*
4248 * we can't truncate inline items that have had
4249 * special encodings
4250 */
4251 if (!del_item &&
4252 btrfs_file_extent_compression(leaf, fi) == 0 &&
4253 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4254 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004255 u32 size = new_size - found_key.offset;
4256
Miao Xie27cdeb72014-04-02 19:51:05 +08004257 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004258 inode_sub_bytes(inode, item_end + 1 -
4259 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004260
4261 /*
4262 * update the ram bytes to properly reflect
4263 * the new size of our item
4264 */
4265 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004266 size =
4267 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004268 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004269 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4270 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004271 inode_sub_bytes(inode, item_end + 1 -
4272 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004273 }
Chris Mason39279cc2007-06-12 06:35:45 -04004274 }
Chris Mason179e29e2007-11-01 11:28:41 -04004275delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004276 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004277 if (!pending_del_nr) {
4278 /* no pending yet, add ourselves */
4279 pending_del_slot = path->slots[0];
4280 pending_del_nr = 1;
4281 } else if (pending_del_nr &&
4282 path->slots[0] + 1 == pending_del_slot) {
4283 /* hop on the pending chunk */
4284 pending_del_nr++;
4285 pending_del_slot = path->slots[0];
4286 } else {
Chris Masond3977122009-01-05 21:25:51 -05004287 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004288 }
Chris Mason39279cc2007-06-12 06:35:45 -04004289 } else {
4290 break;
4291 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004292 if (found_extent &&
4293 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4294 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004295 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004296 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004297 extent_num_bytes, 0,
4298 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004299 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004300 BUG_ON(ret);
4301 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004302
Yan, Zheng80825102009-11-12 09:35:36 +00004303 if (found_type == BTRFS_INODE_ITEM_KEY)
4304 break;
4305
4306 if (path->slots[0] == 0 ||
4307 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004308 if (pending_del_nr) {
4309 ret = btrfs_del_items(trans, root, path,
4310 pending_del_slot,
4311 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004312 if (ret) {
4313 btrfs_abort_transaction(trans,
4314 root, ret);
4315 goto error;
4316 }
Yan, Zheng80825102009-11-12 09:35:36 +00004317 pending_del_nr = 0;
4318 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004319 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004320 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004321 } else {
4322 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004323 }
Chris Mason39279cc2007-06-12 06:35:45 -04004324 }
Yan, Zheng80825102009-11-12 09:35:36 +00004325out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004326 if (pending_del_nr) {
4327 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4328 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004329 if (ret)
4330 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004331 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004332error:
Filipe Mananadac57052014-08-29 20:54:26 +01004333 if (last_size != (u64)-1 &&
4334 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004335 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004336 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004337 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004338}
4339
Chris Masona52d9a82007-08-27 16:49:44 -04004340/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004341 * btrfs_truncate_page - read, zero a chunk and write a page
4342 * @inode - inode that we're zeroing
4343 * @from - the offset to start zeroing
4344 * @len - the length to zero, 0 to zero the entire range respective to the
4345 * offset
4346 * @front - zero up to the offset instead of from the offset on
4347 *
4348 * This will find the page for the "from" offset and cow the page and zero the
4349 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004350 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004351int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4352 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004353{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004354 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004355 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004356 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4357 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004358 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004359 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004360 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004361 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4362 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4363 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004364 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004365 int ret = 0;
4366 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004367 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004368
Josef Bacik2aaa6652012-08-29 14:27:18 -04004369 if ((offset & (blocksize - 1)) == 0 &&
4370 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004371 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004372 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004373 if (ret)
4374 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004375
Chris Mason211c17f2008-05-15 09:13:45 -04004376again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004377 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004378 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004379 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004380 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004381 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004382 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004383
4384 page_start = page_offset(page);
4385 page_end = page_start + PAGE_CACHE_SIZE - 1;
4386
Chris Masona52d9a82007-08-27 16:49:44 -04004387 if (!PageUptodate(page)) {
4388 ret = btrfs_readpage(NULL, page);
4389 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004390 if (page->mapping != mapping) {
4391 unlock_page(page);
4392 page_cache_release(page);
4393 goto again;
4394 }
Chris Masona52d9a82007-08-27 16:49:44 -04004395 if (!PageUptodate(page)) {
4396 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004397 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004398 }
4399 }
Chris Mason211c17f2008-05-15 09:13:45 -04004400 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004401
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004402 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004403 set_page_extent_mapped(page);
4404
4405 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4406 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004407 unlock_extent_cached(io_tree, page_start, page_end,
4408 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004409 unlock_page(page);
4410 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004411 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004412 btrfs_put_ordered_extent(ordered);
4413 goto again;
4414 }
4415
Josef Bacik2ac55d42010-02-03 19:33:23 +00004416 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004417 EXTENT_DIRTY | EXTENT_DELALLOC |
4418 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004419 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004420
Josef Bacik2ac55d42010-02-03 19:33:23 +00004421 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4422 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004423 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004424 unlock_extent_cached(io_tree, page_start, page_end,
4425 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004426 goto out_unlock;
4427 }
4428
Chris Masone6dcd2d2008-07-17 12:53:50 -04004429 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004430 if (!len)
4431 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004432 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004433 if (front)
4434 memset(kaddr, 0, offset);
4435 else
4436 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004437 flush_dcache_page(page);
4438 kunmap(page);
4439 }
Chris Mason247e7432008-07-17 12:53:51 -04004440 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004441 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004442 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4443 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004444
Chris Mason89642222008-07-24 09:41:53 -04004445out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004446 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004447 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004448 unlock_page(page);
4449 page_cache_release(page);
4450out:
4451 return ret;
4452}
4453
Josef Bacik16e75492013-10-22 12:18:51 -04004454static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4455 u64 offset, u64 len)
4456{
4457 struct btrfs_trans_handle *trans;
4458 int ret;
4459
4460 /*
4461 * Still need to make sure the inode looks like it's been updated so
4462 * that any holes get logged if we fsync.
4463 */
4464 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4465 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4466 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4467 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4468 return 0;
4469 }
4470
4471 /*
4472 * 1 - for the one we're dropping
4473 * 1 - for the one we're adding
4474 * 1 - for updating the inode.
4475 */
4476 trans = btrfs_start_transaction(root, 3);
4477 if (IS_ERR(trans))
4478 return PTR_ERR(trans);
4479
4480 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4481 if (ret) {
4482 btrfs_abort_transaction(trans, root, ret);
4483 btrfs_end_transaction(trans, root);
4484 return ret;
4485 }
4486
4487 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4488 0, 0, len, 0, len, 0, 0, 0);
4489 if (ret)
4490 btrfs_abort_transaction(trans, root, ret);
4491 else
4492 btrfs_update_inode(trans, root, inode);
4493 btrfs_end_transaction(trans, root);
4494 return ret;
4495}
4496
Josef Bacik695a0d02011-03-04 15:46:53 -05004497/*
4498 * This function puts in dummy file extents for the area we're creating a hole
4499 * for. So if we are truncating this file to a larger size we need to insert
4500 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4501 * the range between oldsize and size
4502 */
Josef Bacika41ad392011-01-31 15:30:16 -05004503int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004504{
Yan Zheng9036c102008-10-30 14:19:41 -04004505 struct btrfs_root *root = BTRFS_I(inode)->root;
4506 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004507 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004508 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004509 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004510 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4511 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004512 u64 last_byte;
4513 u64 cur_offset;
4514 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004515 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004516
Josef Bacika71754f2013-06-17 17:14:39 -04004517 /*
4518 * If our size started in the middle of a page we need to zero out the
4519 * rest of the page before we expand the i_size, otherwise we could
4520 * expose stale data.
4521 */
4522 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4523 if (err)
4524 return err;
4525
Yan Zheng9036c102008-10-30 14:19:41 -04004526 if (size <= hole_start)
4527 return 0;
4528
Yan Zheng9036c102008-10-30 14:19:41 -04004529 while (1) {
4530 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004531
Josef Bacik2ac55d42010-02-03 19:33:23 +00004532 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004533 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004534 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4535 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004536 if (!ordered)
4537 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004538 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4539 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004540 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004541 btrfs_put_ordered_extent(ordered);
4542 }
4543
Yan Zheng9036c102008-10-30 14:19:41 -04004544 cur_offset = hole_start;
4545 while (1) {
4546 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4547 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004548 if (IS_ERR(em)) {
4549 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004550 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004551 break;
4552 }
Yan Zheng9036c102008-10-30 14:19:41 -04004553 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004554 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004555 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004556 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004557 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004558
Josef Bacik16e75492013-10-22 12:18:51 -04004559 err = maybe_insert_hole(root, inode, cur_offset,
4560 hole_size);
4561 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004562 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004563 btrfs_drop_extent_cache(inode, cur_offset,
4564 cur_offset + hole_size - 1, 0);
4565 hole_em = alloc_extent_map();
4566 if (!hole_em) {
4567 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4568 &BTRFS_I(inode)->runtime_flags);
4569 goto next;
4570 }
4571 hole_em->start = cur_offset;
4572 hole_em->len = hole_size;
4573 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004574
Josef Bacik5dc562c2012-08-17 13:14:17 -04004575 hole_em->block_start = EXTENT_MAP_HOLE;
4576 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004577 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004578 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004579 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4580 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004581 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004582
4583 while (1) {
4584 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004585 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004586 write_unlock(&em_tree->lock);
4587 if (err != -EEXIST)
4588 break;
4589 btrfs_drop_extent_cache(inode, cur_offset,
4590 cur_offset +
4591 hole_size - 1, 0);
4592 }
4593 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004594 }
Josef Bacik16e75492013-10-22 12:18:51 -04004595next:
Yan Zheng9036c102008-10-30 14:19:41 -04004596 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004597 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004598 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004599 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004600 break;
4601 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004602 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004603 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4604 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004605 return err;
4606}
4607
Eric Sandeen3972f262013-01-12 02:57:22 +00004608static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004609{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004610 struct btrfs_root *root = BTRFS_I(inode)->root;
4611 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004612 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004613 loff_t newsize = attr->ia_size;
4614 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004615 int ret;
4616
Eric Sandeen3972f262013-01-12 02:57:22 +00004617 /*
4618 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4619 * special case where we need to update the times despite not having
4620 * these flags set. For all other operations the VFS set these flags
4621 * explicitly if it wants a timestamp update.
4622 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004623 if (newsize != oldsize) {
4624 inode_inc_iversion(inode);
4625 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4626 inode->i_ctime = inode->i_mtime =
4627 current_fs_time(inode->i_sb);
4628 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004629
Josef Bacika41ad392011-01-31 15:30:16 -05004630 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004631 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004632 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004633 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004634 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004635
Miao Xief4a2f4c2011-12-14 20:12:01 -05004636 trans = btrfs_start_transaction(root, 1);
4637 if (IS_ERR(trans))
4638 return PTR_ERR(trans);
4639
4640 i_size_write(inode, newsize);
4641 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4642 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004643 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004644 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004645
Josef Bacika41ad392011-01-31 15:30:16 -05004646 /*
4647 * We're truncating a file that used to have good data down to
4648 * zero. Make sure it gets into the ordered flush list so that
4649 * any new writes get down to disk quickly.
4650 */
4651 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004652 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4653 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004654
Josef Bacikf3fe8202013-01-07 17:03:21 -05004655 /*
4656 * 1 for the orphan item we're going to add
4657 * 1 for the orphan item deletion.
4658 */
4659 trans = btrfs_start_transaction(root, 2);
4660 if (IS_ERR(trans))
4661 return PTR_ERR(trans);
4662
4663 /*
4664 * We need to do this in case we fail at _any_ point during the
4665 * actual truncate. Once we do the truncate_setsize we could
4666 * invalidate pages which forces any outstanding ordered io to
4667 * be instantly completed which will give us extents that need
4668 * to be truncated. If we fail to get an orphan inode down we
4669 * could have left over extents that were never meant to live,
4670 * so we need to garuntee from this point on that everything
4671 * will be consistent.
4672 */
4673 ret = btrfs_orphan_add(trans, inode);
4674 btrfs_end_transaction(trans, root);
4675 if (ret)
4676 return ret;
4677
Josef Bacika41ad392011-01-31 15:30:16 -05004678 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4679 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004680
4681 /* Disable nonlocked read DIO to avoid the end less truncate */
4682 btrfs_inode_block_unlocked_dio(inode);
4683 inode_dio_wait(inode);
4684 btrfs_inode_resume_unlocked_dio(inode);
4685
Josef Bacika41ad392011-01-31 15:30:16 -05004686 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004687 if (ret && inode->i_nlink) {
4688 int err;
4689
4690 /*
4691 * failed to truncate, disk_i_size is only adjusted down
4692 * as we remove extents, so it should represent the true
4693 * size of the inode, so reset the in memory size and
4694 * delete our orphan entry.
4695 */
4696 trans = btrfs_join_transaction(root);
4697 if (IS_ERR(trans)) {
4698 btrfs_orphan_del(NULL, inode);
4699 return ret;
4700 }
4701 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4702 err = btrfs_orphan_del(trans, inode);
4703 if (err)
4704 btrfs_abort_transaction(trans, root, err);
4705 btrfs_end_transaction(trans, root);
4706 }
Yan, Zheng80825102009-11-12 09:35:36 +00004707 }
4708
Josef Bacika41ad392011-01-31 15:30:16 -05004709 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004710}
4711
Chris Mason39279cc2007-06-12 06:35:45 -04004712static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4713{
4714 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004715 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004716 int err;
4717
Li Zefanb83cc962010-12-20 16:04:08 +08004718 if (btrfs_root_readonly(root))
4719 return -EROFS;
4720
Chris Mason39279cc2007-06-12 06:35:45 -04004721 err = inode_change_ok(inode, attr);
4722 if (err)
4723 return err;
4724
Chris Mason5a3f23d2009-03-31 13:27:11 -04004725 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004726 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004727 if (err)
4728 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004729 }
Yan Zheng9036c102008-10-30 14:19:41 -04004730
Christoph Hellwig10257742010-06-04 11:30:02 +02004731 if (attr->ia_valid) {
4732 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004733 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004734 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004735
Josef Bacik22c44fe2011-11-30 10:45:38 -05004736 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004737 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004738 }
4739
Chris Mason39279cc2007-06-12 06:35:45 -04004740 return err;
4741}
Chris Mason61295eb2008-01-14 16:24:38 -05004742
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004743/*
4744 * While truncating the inode pages during eviction, we get the VFS calling
4745 * btrfs_invalidatepage() against each page of the inode. This is slow because
4746 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4747 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4748 * extent_state structures over and over, wasting lots of time.
4749 *
4750 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4751 * those expensive operations on a per page basis and do only the ordered io
4752 * finishing, while we release here the extent_map and extent_state structures,
4753 * without the excessive merging and splitting.
4754 */
4755static void evict_inode_truncate_pages(struct inode *inode)
4756{
4757 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4758 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4759 struct rb_node *node;
4760
4761 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004762 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004763
4764 write_lock(&map_tree->lock);
4765 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4766 struct extent_map *em;
4767
4768 node = rb_first(&map_tree->map);
4769 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004770 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4771 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004772 remove_extent_mapping(map_tree, em);
4773 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004774 if (need_resched()) {
4775 write_unlock(&map_tree->lock);
4776 cond_resched();
4777 write_lock(&map_tree->lock);
4778 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004779 }
4780 write_unlock(&map_tree->lock);
4781
4782 spin_lock(&io_tree->lock);
4783 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4784 struct extent_state *state;
4785 struct extent_state *cached_state = NULL;
4786
4787 node = rb_first(&io_tree->state);
4788 state = rb_entry(node, struct extent_state, rb_node);
4789 atomic_inc(&state->refs);
4790 spin_unlock(&io_tree->lock);
4791
4792 lock_extent_bits(io_tree, state->start, state->end,
4793 0, &cached_state);
4794 clear_extent_bit(io_tree, state->start, state->end,
4795 EXTENT_LOCKED | EXTENT_DIRTY |
4796 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4797 EXTENT_DEFRAG, 1, 1,
4798 &cached_state, GFP_NOFS);
4799 free_extent_state(state);
4800
Filipe Manana7064dd52014-08-08 02:47:05 +01004801 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004802 spin_lock(&io_tree->lock);
4803 }
4804 spin_unlock(&io_tree->lock);
4805}
4806
Al Virobd555972010-06-07 11:35:40 -04004807void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004808{
4809 struct btrfs_trans_handle *trans;
4810 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004811 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004812 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004813 int ret;
4814
liubo1abe9b82011-03-24 11:18:59 +00004815 trace_btrfs_inode_evict(inode);
4816
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004817 evict_inode_truncate_pages(inode);
4818
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004819 if (inode->i_nlink &&
4820 ((btrfs_root_refs(&root->root_item) != 0 &&
4821 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4822 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004823 goto no_delete;
4824
Chris Mason39279cc2007-06-12 06:35:45 -04004825 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004826 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004827 goto no_delete;
4828 }
Al Virobd555972010-06-07 11:35:40 -04004829 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004830 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004831
Miao Xief6124962014-09-12 18:44:04 +08004832 btrfs_free_io_failure_record(inode, 0, (u64)-1);
4833
Yan, Zhengc71bf092009-11-12 09:34:40 +00004834 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004835 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004836 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004837 goto no_delete;
4838 }
4839
Yan, Zheng76dda932009-09-21 16:00:26 -04004840 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004841 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4842 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004843 goto no_delete;
4844 }
4845
Miao Xie0e8c36a2012-12-19 06:59:51 +00004846 ret = btrfs_commit_inode_delayed_inode(inode);
4847 if (ret) {
4848 btrfs_orphan_del(NULL, inode);
4849 goto no_delete;
4850 }
4851
Miao Xie66d8f3d2012-09-06 04:02:28 -06004852 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004853 if (!rsv) {
4854 btrfs_orphan_del(NULL, inode);
4855 goto no_delete;
4856 }
Josef Bacik4a338542011-08-29 11:01:31 -04004857 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004858 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004859 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004860
Chris Masondbe674a2008-07-17 12:54:05 -04004861 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004862
Josef Bacik4289a662011-08-05 13:22:24 -04004863 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004864 * This is a bit simpler than btrfs_truncate since we've already
4865 * reserved our space for our orphan item in the unlink, so we just
4866 * need to reserve some slack space in case we add bytes and update
4867 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004868 */
Yan, Zheng80825102009-11-12 09:35:36 +00004869 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004870 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4871 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004872
Josef Bacik726c35f2011-09-26 15:46:06 -04004873 /*
4874 * Try and steal from the global reserve since we will
4875 * likely not use this space anyway, we want to try as
4876 * hard as possible to get this to work.
4877 */
4878 if (ret)
4879 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4880
Yan, Zhengd68fc572010-05-16 10:49:58 -04004881 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004882 btrfs_warn(root->fs_info,
4883 "Could not get space for a delete, will truncate on mount %d",
4884 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004885 btrfs_orphan_del(NULL, inode);
4886 btrfs_free_block_rsv(root, rsv);
4887 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004888 }
4889
Miao Xie0e8c36a2012-12-19 06:59:51 +00004890 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004891 if (IS_ERR(trans)) {
4892 btrfs_orphan_del(NULL, inode);
4893 btrfs_free_block_rsv(root, rsv);
4894 goto no_delete;
4895 }
4896
4897 trans->block_rsv = rsv;
4898
Yan, Zhengd68fc572010-05-16 10:49:58 -04004899 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004900 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004901 break;
4902
Miao Xie8407aa42012-09-07 01:43:32 -06004903 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004904 btrfs_end_transaction(trans, root);
4905 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004906 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004907 }
4908
Josef Bacik4289a662011-08-05 13:22:24 -04004909 btrfs_free_block_rsv(root, rsv);
4910
Josef Bacik4ef31a42013-08-13 14:10:08 -04004911 /*
4912 * Errors here aren't a big deal, it just means we leave orphan items
4913 * in the tree. They will be cleaned up on the next mount.
4914 */
Yan, Zheng80825102009-11-12 09:35:36 +00004915 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004916 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004917 btrfs_orphan_del(trans, inode);
4918 } else {
4919 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004920 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004921
Josef Bacik4289a662011-08-05 13:22:24 -04004922 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004923 if (!(root == root->fs_info->tree_root ||
4924 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004925 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004926
Chris Mason54aa1f42007-06-22 14:16:25 -04004927 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004928 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004929no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004930 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004931 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004932 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004933}
4934
4935/*
4936 * this returns the key found in the dir entry in the location pointer.
4937 * If no dir entries were found, location->objectid is 0.
4938 */
4939static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4940 struct btrfs_key *location)
4941{
4942 const char *name = dentry->d_name.name;
4943 int namelen = dentry->d_name.len;
4944 struct btrfs_dir_item *di;
4945 struct btrfs_path *path;
4946 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004947 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004948
4949 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004950 if (!path)
4951 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004952
Li Zefan33345d012011-04-20 10:31:50 +08004953 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004954 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004955 if (IS_ERR(di))
4956 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004957
David Sterbac7040052011-04-19 18:00:01 +02004958 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004959 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004960
Chris Mason5f39d392007-10-15 16:14:19 -04004961 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004962out:
Chris Mason39279cc2007-06-12 06:35:45 -04004963 btrfs_free_path(path);
4964 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004965out_err:
4966 location->objectid = 0;
4967 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004968}
4969
4970/*
4971 * when we hit a tree root in a directory, the btrfs part of the inode
4972 * needs to be changed to reflect the root directory of the tree root. This
4973 * is kind of like crossing a mount point.
4974 */
4975static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004976 struct inode *dir,
4977 struct dentry *dentry,
4978 struct btrfs_key *location,
4979 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004980{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004981 struct btrfs_path *path;
4982 struct btrfs_root *new_root;
4983 struct btrfs_root_ref *ref;
4984 struct extent_buffer *leaf;
4985 int ret;
4986 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004987
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004988 path = btrfs_alloc_path();
4989 if (!path) {
4990 err = -ENOMEM;
4991 goto out;
4992 }
Chris Mason39279cc2007-06-12 06:35:45 -04004993
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004994 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004995 ret = btrfs_find_item(root->fs_info->tree_root, path,
4996 BTRFS_I(dir)->root->root_key.objectid,
4997 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004998 if (ret) {
4999 if (ret < 0)
5000 err = ret;
5001 goto out;
5002 }
Chris Mason39279cc2007-06-12 06:35:45 -04005003
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005004 leaf = path->nodes[0];
5005 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005006 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005007 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5008 goto out;
5009
5010 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5011 (unsigned long)(ref + 1),
5012 dentry->d_name.len);
5013 if (ret)
5014 goto out;
5015
David Sterbab3b4aa72011-04-21 01:20:15 +02005016 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005017
5018 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5019 if (IS_ERR(new_root)) {
5020 err = PTR_ERR(new_root);
5021 goto out;
5022 }
5023
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005024 *sub_root = new_root;
5025 location->objectid = btrfs_root_dirid(&new_root->root_item);
5026 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005027 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005028 err = 0;
5029out:
5030 btrfs_free_path(path);
5031 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005032}
5033
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005034static void inode_tree_add(struct inode *inode)
5035{
5036 struct btrfs_root *root = BTRFS_I(inode)->root;
5037 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005038 struct rb_node **p;
5039 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005040 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005041 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005042
Al Viro1d3382cb2010-10-23 15:19:20 -04005043 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005044 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005045 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005046 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005047 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005048 while (*p) {
5049 parent = *p;
5050 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5051
Li Zefan33345d012011-04-20 10:31:50 +08005052 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005053 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005054 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005055 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005056 else {
5057 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005058 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005059 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005060 RB_CLEAR_NODE(parent);
5061 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005062 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005063 }
5064 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005065 rb_link_node(new, parent, p);
5066 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005067 spin_unlock(&root->inode_lock);
5068}
5069
5070static void inode_tree_del(struct inode *inode)
5071{
5072 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005073 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005074
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005075 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005076 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005077 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005078 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005079 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005080 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005081 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005082
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005083 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005084 synchronize_srcu(&root->fs_info->subvol_srcu);
5085 spin_lock(&root->inode_lock);
5086 empty = RB_EMPTY_ROOT(&root->inode_tree);
5087 spin_unlock(&root->inode_lock);
5088 if (empty)
5089 btrfs_add_dead_root(root);
5090 }
5091}
5092
Jeff Mahoney143bede2012-03-01 14:56:26 +01005093void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005094{
5095 struct rb_node *node;
5096 struct rb_node *prev;
5097 struct btrfs_inode *entry;
5098 struct inode *inode;
5099 u64 objectid = 0;
5100
Liu Bo7813b3d2014-02-10 17:37:25 +08005101 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5102 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005103
5104 spin_lock(&root->inode_lock);
5105again:
5106 node = root->inode_tree.rb_node;
5107 prev = NULL;
5108 while (node) {
5109 prev = node;
5110 entry = rb_entry(node, struct btrfs_inode, rb_node);
5111
Li Zefan33345d012011-04-20 10:31:50 +08005112 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005113 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005114 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005115 node = node->rb_right;
5116 else
5117 break;
5118 }
5119 if (!node) {
5120 while (prev) {
5121 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005122 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005123 node = prev;
5124 break;
5125 }
5126 prev = rb_next(prev);
5127 }
5128 }
5129 while (node) {
5130 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005131 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005132 inode = igrab(&entry->vfs_inode);
5133 if (inode) {
5134 spin_unlock(&root->inode_lock);
5135 if (atomic_read(&inode->i_count) > 1)
5136 d_prune_aliases(inode);
5137 /*
Al Viro45321ac2010-06-07 13:43:19 -04005138 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005139 * the inode cache when its usage count
5140 * hits zero.
5141 */
5142 iput(inode);
5143 cond_resched();
5144 spin_lock(&root->inode_lock);
5145 goto again;
5146 }
5147
5148 if (cond_resched_lock(&root->inode_lock))
5149 goto again;
5150
5151 node = rb_next(node);
5152 }
5153 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005154}
5155
Chris Masone02119d2008-09-05 16:13:11 -04005156static int btrfs_init_locked_inode(struct inode *inode, void *p)
5157{
5158 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005159 inode->i_ino = args->location->objectid;
5160 memcpy(&BTRFS_I(inode)->location, args->location,
5161 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005162 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005163 return 0;
5164}
5165
5166static int btrfs_find_actor(struct inode *inode, void *opaque)
5167{
5168 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005169 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005170 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005171}
5172
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005173static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005174 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005175 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005176{
5177 struct inode *inode;
5178 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005179 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005180
Chris Mason90d3e592014-01-09 17:28:00 -08005181 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005182 args.root = root;
5183
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005184 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005185 btrfs_init_locked_inode,
5186 (void *)&args);
5187 return inode;
5188}
5189
Balaji Rao1a54ef82008-07-21 02:01:04 +05305190/* Get an inode object given its location and corresponding root.
5191 * Returns in *is_new if the inode was read from disk
5192 */
5193struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005194 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305195{
5196 struct inode *inode;
5197
Chris Mason90d3e592014-01-09 17:28:00 -08005198 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305199 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005200 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305201
5202 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305203 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005204 if (!is_bad_inode(inode)) {
5205 inode_tree_add(inode);
5206 unlock_new_inode(inode);
5207 if (new)
5208 *new = 1;
5209 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005210 unlock_new_inode(inode);
5211 iput(inode);
5212 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005213 }
5214 }
5215
Balaji Rao1a54ef82008-07-21 02:01:04 +05305216 return inode;
5217}
5218
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005219static struct inode *new_simple_dir(struct super_block *s,
5220 struct btrfs_key *key,
5221 struct btrfs_root *root)
5222{
5223 struct inode *inode = new_inode(s);
5224
5225 if (!inode)
5226 return ERR_PTR(-ENOMEM);
5227
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005228 BTRFS_I(inode)->root = root;
5229 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005230 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005231
5232 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005233 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005234 inode->i_fop = &simple_dir_operations;
5235 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5236 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5237
5238 return inode;
5239}
5240
Chris Mason3de45862008-11-17 21:02:50 -05005241struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005242{
Chris Masond3977122009-01-05 21:25:51 -05005243 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005244 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005245 struct btrfs_root *sub_root = root;
5246 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005247 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005248 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005249
5250 if (dentry->d_name.len > BTRFS_NAME_LEN)
5251 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005252
Jeff Layton39e3c952012-11-28 11:30:53 -05005253 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005254 if (ret < 0)
5255 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005256
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005257 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005258 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005259
5260 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005261 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005262 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005263 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005264
5265 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5266
Yan, Zheng76dda932009-09-21 16:00:26 -04005267 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005268 ret = fixup_tree_root_location(root, dir, dentry,
5269 &location, &sub_root);
5270 if (ret < 0) {
5271 if (ret != -ENOENT)
5272 inode = ERR_PTR(ret);
5273 else
5274 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5275 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005276 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005277 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005278 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5279
Julia Lawall34d19ba2011-01-24 19:55:19 +00005280 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005281 down_read(&root->fs_info->cleanup_work_sem);
5282 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005283 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005284 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005285 if (ret) {
5286 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005287 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005288 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005289 }
5290
Chris Mason3de45862008-11-17 21:02:50 -05005291 return inode;
5292}
5293
Nick Pigginfe15ce42011-01-07 17:49:23 +11005294static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005295{
5296 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005297 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005298
Li Zefan848cce02012-02-21 17:04:28 +08005299 if (!inode && !IS_ROOT(dentry))
5300 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005301
Li Zefan848cce02012-02-21 17:04:28 +08005302 if (inode) {
5303 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005304 if (btrfs_root_refs(&root->root_item) == 0)
5305 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005306
5307 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5308 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005309 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005310 return 0;
5311}
5312
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005313static void btrfs_dentry_release(struct dentry *dentry)
5314{
Daeseok Youn944a4512014-04-14 15:37:02 +09005315 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005316}
5317
Chris Mason3de45862008-11-17 21:02:50 -05005318static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005319 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005320{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005321 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005322
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005323 inode = btrfs_lookup_dentry(dir, dentry);
5324 if (IS_ERR(inode)) {
5325 if (PTR_ERR(inode) == -ENOENT)
5326 inode = NULL;
5327 else
5328 return ERR_CAST(inode);
5329 }
5330
Josef Bacik3a0dfa62014-02-14 13:43:48 -05005331 return d_materialise_unique(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005332}
5333
Miao Xie16cdcec2011-04-22 18:12:22 +08005334unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005335 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5336};
5337
Al Viro9cdda8d2013-05-22 16:48:09 -04005338static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005339{
Al Viro9cdda8d2013-05-22 16:48:09 -04005340 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005341 struct btrfs_root *root = BTRFS_I(inode)->root;
5342 struct btrfs_item *item;
5343 struct btrfs_dir_item *di;
5344 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005345 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005346 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005347 struct list_head ins_list;
5348 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005349 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005350 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005351 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005352 unsigned char d_type;
5353 int over = 0;
5354 u32 di_cur;
5355 u32 di_total;
5356 u32 di_len;
5357 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005358 char tmp_name[32];
5359 char *name_ptr;
5360 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005361 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005362
5363 /* FIXME, use a real flag for deciding about the key type */
5364 if (root->fs_info->tree_root == root)
5365 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005366
Al Viro9cdda8d2013-05-22 16:48:09 -04005367 if (!dir_emit_dots(file, ctx))
5368 return 0;
5369
David Woodhouse49593bf2008-08-17 17:08:36 +01005370 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005371 if (!path)
5372 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005373
Josef Bacik026fd312011-05-13 10:32:11 -04005374 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005375
Miao Xie16cdcec2011-04-22 18:12:22 +08005376 if (key_type == BTRFS_DIR_INDEX_KEY) {
5377 INIT_LIST_HEAD(&ins_list);
5378 INIT_LIST_HEAD(&del_list);
5379 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5380 }
5381
David Sterba962a2982014-06-04 18:41:45 +02005382 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005383 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005384 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005385
Chris Mason39279cc2007-06-12 06:35:45 -04005386 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5387 if (ret < 0)
5388 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005389
5390 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005391 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005392 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005393 if (slot >= btrfs_header_nritems(leaf)) {
5394 ret = btrfs_next_leaf(root, path);
5395 if (ret < 0)
5396 goto err;
5397 else if (ret > 0)
5398 break;
5399 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005400 }
Chris Mason3de45862008-11-17 21:02:50 -05005401
Ross Kirkdd3cc162013-09-16 15:58:09 +01005402 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005403 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5404
5405 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005406 break;
David Sterba962a2982014-06-04 18:41:45 +02005407 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005408 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005409 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005410 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005411 if (key_type == BTRFS_DIR_INDEX_KEY &&
5412 btrfs_should_delete_dir_index(&del_list,
5413 found_key.offset))
5414 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005415
Al Viro9cdda8d2013-05-22 16:48:09 -04005416 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005417 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005418
Chris Mason39279cc2007-06-12 06:35:45 -04005419 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5420 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005421 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005422
5423 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005424 struct btrfs_key location;
5425
Josef Bacik22a94d42011-03-16 16:47:17 -04005426 if (verify_dir_item(root, leaf, di))
5427 break;
5428
Chris Mason5f39d392007-10-15 16:14:19 -04005429 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005430 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005431 name_ptr = tmp_name;
5432 } else {
5433 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005434 if (!name_ptr) {
5435 ret = -ENOMEM;
5436 goto err;
5437 }
Chris Mason5f39d392007-10-15 16:14:19 -04005438 }
5439 read_extent_buffer(leaf, name_ptr,
5440 (unsigned long)(di + 1), name_len);
5441
5442 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5443 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005444
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005445
Chris Mason3de45862008-11-17 21:02:50 -05005446 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005447 * skip it.
5448 *
5449 * In contrast to old kernels, we insert the snapshot's
5450 * dir item and dir index after it has been created, so
5451 * we won't find a reference to our own snapshot. We
5452 * still keep the following code for backward
5453 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005454 */
5455 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5456 location.objectid == root->root_key.objectid) {
5457 over = 0;
5458 goto skip;
5459 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005460 over = !dir_emit(ctx, name_ptr, name_len,
5461 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005462
Chris Mason3de45862008-11-17 21:02:50 -05005463skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005464 if (name_ptr != tmp_name)
5465 kfree(name_ptr);
5466
Chris Mason39279cc2007-06-12 06:35:45 -04005467 if (over)
5468 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005469 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005470 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005471 di_cur += di_len;
5472 di = (struct btrfs_dir_item *)((char *)di + di_len);
5473 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005474next:
5475 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005476 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005477
Miao Xie16cdcec2011-04-22 18:12:22 +08005478 if (key_type == BTRFS_DIR_INDEX_KEY) {
5479 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005480 ctx->pos++;
5481 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005482 if (ret)
5483 goto nopos;
5484 }
5485
David Woodhouse49593bf2008-08-17 17:08:36 +01005486 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005487 ctx->pos++;
5488
5489 /*
5490 * Stop new entries from being returned after we return the last
5491 * entry.
5492 *
5493 * New directory entries are assigned a strictly increasing
5494 * offset. This means that new entries created during readdir
5495 * are *guaranteed* to be seen in the future by that readdir.
5496 * This has broken buggy programs which operate on names as
5497 * they're returned by readdir. Until we re-use freed offsets
5498 * we have this hack to stop new entries from being returned
5499 * under the assumption that they'll never reach this huge
5500 * offset.
5501 *
5502 * This is being careful not to overflow 32bit loff_t unless the
5503 * last entry requires it because doing so has broken 32bit apps
5504 * in the past.
5505 */
5506 if (key_type == BTRFS_DIR_INDEX_KEY) {
5507 if (ctx->pos >= INT_MAX)
5508 ctx->pos = LLONG_MAX;
5509 else
5510 ctx->pos = INT_MAX;
5511 }
Chris Mason39279cc2007-06-12 06:35:45 -04005512nopos:
5513 ret = 0;
5514err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005515 if (key_type == BTRFS_DIR_INDEX_KEY)
5516 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005517 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005518 return ret;
5519}
5520
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005521int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005522{
5523 struct btrfs_root *root = BTRFS_I(inode)->root;
5524 struct btrfs_trans_handle *trans;
5525 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005526 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005527
Josef Bacik72ac3c02012-05-23 14:13:11 -04005528 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005529 return 0;
5530
Liu Bo83eea1f2012-07-10 05:28:39 -06005531 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005532 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005533
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005534 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005535 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005536 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005537 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005538 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005539 if (IS_ERR(trans))
5540 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005541 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005542 }
5543 return ret;
5544}
5545
5546/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005547 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005548 * inode changes. But, it is most likely to find the inode in cache.
5549 * FIXME, needs more benchmarking...there are no reasons other than performance
5550 * to keep or drop this code.
5551 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005552static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005553{
5554 struct btrfs_root *root = BTRFS_I(inode)->root;
5555 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005556 int ret;
5557
Josef Bacik72ac3c02012-05-23 14:13:11 -04005558 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005559 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005560
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005561 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005562 if (IS_ERR(trans))
5563 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005564
5565 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005566 if (ret && ret == -ENOSPC) {
5567 /* whoops, lets try again with the full transaction */
5568 btrfs_end_transaction(trans, root);
5569 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005570 if (IS_ERR(trans))
5571 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005572
Chris Mason94b60442010-05-26 11:02:00 -04005573 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005574 }
Chris Mason39279cc2007-06-12 06:35:45 -04005575 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005576 if (BTRFS_I(inode)->delayed_node)
5577 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005578
5579 return ret;
5580}
5581
5582/*
5583 * This is a copy of file_update_time. We need this so we can return error on
5584 * ENOSPC for updating the inode in the case of file write and mmap writes.
5585 */
Josef Bacike41f9412012-03-26 09:46:47 -04005586static int btrfs_update_time(struct inode *inode, struct timespec *now,
5587 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005588{
Alexander Block2bc5565282012-06-15 09:49:33 +02005589 struct btrfs_root *root = BTRFS_I(inode)->root;
5590
5591 if (btrfs_root_readonly(root))
5592 return -EROFS;
5593
Josef Bacike41f9412012-03-26 09:46:47 -04005594 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005595 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005596 if (flags & S_CTIME)
5597 inode->i_ctime = *now;
5598 if (flags & S_MTIME)
5599 inode->i_mtime = *now;
5600 if (flags & S_ATIME)
5601 inode->i_atime = *now;
5602 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005603}
5604
Chris Masond352ac62008-09-29 15:18:18 -04005605/*
5606 * find the highest existing sequence number in a directory
5607 * and then set the in-memory index_cnt variable to reflect
5608 * free sequence numbers
5609 */
Josef Bacikaec74772008-07-24 12:12:38 -04005610static int btrfs_set_inode_index_count(struct inode *inode)
5611{
5612 struct btrfs_root *root = BTRFS_I(inode)->root;
5613 struct btrfs_key key, found_key;
5614 struct btrfs_path *path;
5615 struct extent_buffer *leaf;
5616 int ret;
5617
Li Zefan33345d012011-04-20 10:31:50 +08005618 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005619 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005620 key.offset = (u64)-1;
5621
5622 path = btrfs_alloc_path();
5623 if (!path)
5624 return -ENOMEM;
5625
5626 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5627 if (ret < 0)
5628 goto out;
5629 /* FIXME: we should be able to handle this */
5630 if (ret == 0)
5631 goto out;
5632 ret = 0;
5633
5634 /*
5635 * MAGIC NUMBER EXPLANATION:
5636 * since we search a directory based on f_pos we have to start at 2
5637 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5638 * else has to start at 2
5639 */
5640 if (path->slots[0] == 0) {
5641 BTRFS_I(inode)->index_cnt = 2;
5642 goto out;
5643 }
5644
5645 path->slots[0]--;
5646
5647 leaf = path->nodes[0];
5648 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5649
Li Zefan33345d012011-04-20 10:31:50 +08005650 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005651 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005652 BTRFS_I(inode)->index_cnt = 2;
5653 goto out;
5654 }
5655
5656 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5657out:
5658 btrfs_free_path(path);
5659 return ret;
5660}
5661
Chris Masond352ac62008-09-29 15:18:18 -04005662/*
5663 * helper to find a free sequence number in a given directory. This current
5664 * code is very simple, later versions will do smarter things in the btree
5665 */
Chris Mason3de45862008-11-17 21:02:50 -05005666int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005667{
5668 int ret = 0;
5669
5670 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005671 ret = btrfs_inode_delayed_dir_index_count(dir);
5672 if (ret) {
5673 ret = btrfs_set_inode_index_count(dir);
5674 if (ret)
5675 return ret;
5676 }
Josef Bacikaec74772008-07-24 12:12:38 -04005677 }
5678
Chris Mason00e4e6b2008-08-05 11:18:09 -04005679 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005680 BTRFS_I(dir)->index_cnt++;
5681
5682 return ret;
5683}
5684
Chris Masonb0d5d102014-09-08 13:08:51 -07005685static int btrfs_insert_inode_locked(struct inode *inode)
5686{
5687 struct btrfs_iget_args args;
5688 args.location = &BTRFS_I(inode)->location;
5689 args.root = BTRFS_I(inode)->root;
5690
5691 return insert_inode_locked4(inode,
5692 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5693 btrfs_find_actor, &args);
5694}
5695
Chris Mason39279cc2007-06-12 06:35:45 -04005696static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5697 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005698 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005699 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005700 u64 ref_objectid, u64 objectid,
5701 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005702{
5703 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005704 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005705 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005706 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005707 struct btrfs_inode_ref *ref;
5708 struct btrfs_key key[2];
5709 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005710 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005711 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005712 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005713
Chris Mason5f39d392007-10-15 16:14:19 -04005714 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005715 if (!path)
5716 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005717
Chris Mason39279cc2007-06-12 06:35:45 -04005718 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005719 if (!inode) {
5720 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005721 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005722 }
Chris Mason39279cc2007-06-12 06:35:45 -04005723
Li Zefan581bb052011-04-20 10:06:11 +08005724 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005725 * O_TMPFILE, set link count to 0, so that after this point,
5726 * we fill in an inode item with the correct link count.
5727 */
5728 if (!name)
5729 set_nlink(inode, 0);
5730
5731 /*
Li Zefan581bb052011-04-20 10:06:11 +08005732 * we have to initialize this early, so we can reclaim the inode
5733 * number if we fail afterwards in this function.
5734 */
5735 inode->i_ino = objectid;
5736
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005737 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005738 trace_btrfs_inode_request(dir);
5739
Chris Mason3de45862008-11-17 21:02:50 -05005740 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005741 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005742 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005743 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005744 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005745 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005746 } else if (dir) {
5747 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005748 }
5749 /*
5750 * index_cnt is ignored for everything but a dir,
5751 * btrfs_get_inode_index_count has an explanation for the magic
5752 * number
5753 */
5754 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005755 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005756 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005757 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005758 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005759
Josef Bacik5dc562c2012-08-17 13:14:17 -04005760 /*
5761 * We could have gotten an inode number from somebody who was fsynced
5762 * and then removed in this same transaction, so let's just set full
5763 * sync since it will be a full sync anyway and this will blow away the
5764 * old info in the log.
5765 */
5766 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5767
Chris Mason9c583092008-01-29 15:15:18 -05005768 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005769 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005770 key[0].offset = 0;
5771
Chris Mason9c583092008-01-29 15:15:18 -05005772 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005773
5774 if (name) {
5775 /*
5776 * Start new inodes with an inode_ref. This is slightly more
5777 * efficient for small numbers of hard links since they will
5778 * be packed into one item. Extended refs will kick in if we
5779 * add more hard links than can fit in the ref item.
5780 */
5781 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005782 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005783 key[1].offset = ref_objectid;
5784
5785 sizes[1] = name_len + sizeof(*ref);
5786 }
Chris Mason9c583092008-01-29 15:15:18 -05005787
Chris Masonb0d5d102014-09-08 13:08:51 -07005788 location = &BTRFS_I(inode)->location;
5789 location->objectid = objectid;
5790 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005791 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005792
5793 ret = btrfs_insert_inode_locked(inode);
5794 if (ret < 0)
5795 goto fail;
5796
Chris Masonb9473432009-03-13 11:00:37 -04005797 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005798 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005799 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005800 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005801
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005802 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005803 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005804 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005805 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5806 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005807 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5808 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005809 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005810
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005811 if (name) {
5812 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5813 struct btrfs_inode_ref);
5814 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5815 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5816 ptr = (unsigned long)(ref + 1);
5817 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5818 }
Chris Mason9c583092008-01-29 15:15:18 -05005819
Chris Mason5f39d392007-10-15 16:14:19 -04005820 btrfs_mark_buffer_dirty(path->nodes[0]);
5821 btrfs_free_path(path);
5822
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005823 btrfs_inherit_iflags(inode, dir);
5824
Al Viro569254b2011-07-24 17:08:40 -04005825 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005826 if (btrfs_test_opt(root, NODATASUM))
5827 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005828 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005829 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5830 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005831 }
5832
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005833 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005834
5835 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005836 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005837
Alexander Block8ea05e32012-07-25 17:35:53 +02005838 btrfs_update_root_times(trans, root);
5839
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005840 ret = btrfs_inode_inherit_props(trans, inode, dir);
5841 if (ret)
5842 btrfs_err(root->fs_info,
5843 "error inheriting props for ino %llu (root %llu): %d",
5844 btrfs_ino(inode), root->root_key.objectid, ret);
5845
Chris Mason39279cc2007-06-12 06:35:45 -04005846 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005847
5848fail_unlock:
5849 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005850fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005851 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005852 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005853 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005854 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005855 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005856}
5857
5858static inline u8 btrfs_inode_type(struct inode *inode)
5859{
5860 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5861}
5862
Chris Masond352ac62008-09-29 15:18:18 -04005863/*
5864 * utility function to add 'inode' into 'parent_inode' with
5865 * a give name and a given sequence number.
5866 * if 'add_backref' is true, also insert a backref from the
5867 * inode to the parent directory.
5868 */
Chris Masone02119d2008-09-05 16:13:11 -04005869int btrfs_add_link(struct btrfs_trans_handle *trans,
5870 struct inode *parent_inode, struct inode *inode,
5871 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005872{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005873 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005874 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005875 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005876 u64 ino = btrfs_ino(inode);
5877 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005878
Li Zefan33345d012011-04-20 10:31:50 +08005879 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005880 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5881 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005882 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02005883 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005884 key.offset = 0;
5885 }
Chris Mason39279cc2007-06-12 06:35:45 -04005886
Li Zefan33345d012011-04-20 10:31:50 +08005887 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005888 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5889 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005890 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005891 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005892 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5893 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005894 }
5895
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005896 /* Nothing to clean up yet */
5897 if (ret)
5898 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005899
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005900 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5901 parent_inode, &key,
5902 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005903 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005904 goto fail_dir_item;
5905 else if (ret) {
5906 btrfs_abort_transaction(trans, root, ret);
5907 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005908 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005909
5910 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5911 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005912 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005913 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5914 ret = btrfs_update_inode(trans, root, parent_inode);
5915 if (ret)
5916 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005917 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005918
5919fail_dir_item:
5920 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5921 u64 local_index;
5922 int err;
5923 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5924 key.objectid, root->root_key.objectid,
5925 parent_ino, &local_index, name, name_len);
5926
5927 } else if (add_backref) {
5928 u64 local_index;
5929 int err;
5930
5931 err = btrfs_del_inode_ref(trans, root, name, name_len,
5932 ino, parent_ino, &local_index);
5933 }
5934 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005935}
5936
5937static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005938 struct inode *dir, struct dentry *dentry,
5939 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005940{
Josef Bacika1b075d2010-11-19 20:36:11 +00005941 int err = btrfs_add_link(trans, dir, inode,
5942 dentry->d_name.name, dentry->d_name.len,
5943 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005944 if (err > 0)
5945 err = -EEXIST;
5946 return err;
5947}
5948
Josef Bacik618e21d2007-07-11 10:18:17 -04005949static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005950 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005951{
5952 struct btrfs_trans_handle *trans;
5953 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005954 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005955 int err;
5956 int drop_inode = 0;
5957 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005958 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005959
5960 if (!new_valid_dev(rdev))
5961 return -EINVAL;
5962
Josef Bacik9ed74f22009-09-11 16:12:44 -04005963 /*
5964 * 2 for inode item and ref
5965 * 2 for dir items
5966 * 1 for xattr if selinux is on
5967 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005968 trans = btrfs_start_transaction(root, 5);
5969 if (IS_ERR(trans))
5970 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005971
Li Zefan581bb052011-04-20 10:06:11 +08005972 err = btrfs_find_free_ino(root, &objectid);
5973 if (err)
5974 goto out_unlock;
5975
Josef Bacikaec74772008-07-24 12:12:38 -04005976 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005977 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005978 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005979 if (IS_ERR(inode)) {
5980 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005981 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005982 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005983
Casey Schauflerad19db72011-12-15 10:09:07 -05005984 /*
5985 * If the active LSM wants to access the inode during
5986 * d_instantiate it needs these. Smack checks to see
5987 * if the filesystem supports xattrs by looking at the
5988 * ops vector.
5989 */
Casey Schauflerad19db72011-12-15 10:09:07 -05005990 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07005991 init_special_inode(inode, inode->i_mode, rdev);
5992
5993 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04005994 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07005995 goto out_unlock_inode;
5996
5997 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
5998 if (err) {
5999 goto out_unlock_inode;
6000 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006001 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006002 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006003 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006004 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006005
Josef Bacik618e21d2007-07-11 10:18:17 -04006006out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006007 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006008 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006009 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006010 if (drop_inode) {
6011 inode_dec_link_count(inode);
6012 iput(inode);
6013 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006014 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006015
6016out_unlock_inode:
6017 drop_inode = 1;
6018 unlock_new_inode(inode);
6019 goto out_unlock;
6020
Josef Bacik618e21d2007-07-11 10:18:17 -04006021}
6022
Chris Mason39279cc2007-06-12 06:35:45 -04006023static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006024 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006025{
6026 struct btrfs_trans_handle *trans;
6027 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006028 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006029 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006030 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006031 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006032 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006033
Josef Bacik9ed74f22009-09-11 16:12:44 -04006034 /*
6035 * 2 for inode item and ref
6036 * 2 for dir items
6037 * 1 for xattr if selinux is on
6038 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006039 trans = btrfs_start_transaction(root, 5);
6040 if (IS_ERR(trans))
6041 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006042
Li Zefan581bb052011-04-20 10:06:11 +08006043 err = btrfs_find_free_ino(root, &objectid);
6044 if (err)
6045 goto out_unlock;
6046
Josef Bacikaec74772008-07-24 12:12:38 -04006047 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006048 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006049 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006050 if (IS_ERR(inode)) {
6051 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006052 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006053 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006054 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006055 /*
6056 * If the active LSM wants to access the inode during
6057 * d_instantiate it needs these. Smack checks to see
6058 * if the filesystem supports xattrs by looking at the
6059 * ops vector.
6060 */
6061 inode->i_fop = &btrfs_file_operations;
6062 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006063 inode->i_mapping->a_ops = &btrfs_aops;
6064 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
6065
6066 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6067 if (err)
6068 goto out_unlock_inode;
6069
6070 err = btrfs_update_inode(trans, root, inode);
6071 if (err)
6072 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006073
Josef Bacika1b075d2010-11-19 20:36:11 +00006074 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006075 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006076 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006077
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006078 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006079 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006080 d_instantiate(dentry, inode);
6081
Chris Mason39279cc2007-06-12 06:35:45 -04006082out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006083 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006084 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006085 inode_dec_link_count(inode);
6086 iput(inode);
6087 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006088 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006089 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006090 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006091
6092out_unlock_inode:
6093 unlock_new_inode(inode);
6094 goto out_unlock;
6095
Chris Mason39279cc2007-06-12 06:35:45 -04006096}
6097
6098static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6099 struct dentry *dentry)
6100{
6101 struct btrfs_trans_handle *trans;
6102 struct btrfs_root *root = BTRFS_I(dir)->root;
6103 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006104 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006105 int err;
6106 int drop_inode = 0;
6107
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006108 /* do not allow sys_link's with other subvols of the same device */
6109 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006110 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006111
Mark Fashehf1863732012-08-08 11:32:27 -07006112 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006113 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006114
Chris Mason3de45862008-11-17 21:02:50 -05006115 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006116 if (err)
6117 goto fail;
6118
Yan, Zhenga22285a2010-05-16 10:48:46 -04006119 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006120 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006121 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006122 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006123 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006124 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006125 if (IS_ERR(trans)) {
6126 err = PTR_ERR(trans);
6127 goto fail;
6128 }
Chris Mason5f39d392007-10-15 16:14:19 -04006129
Miao Xie67de1172013-12-26 13:07:06 +08006130 /* There are several dir indexes for this inode, clear the cache. */
6131 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006132 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006133 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006134 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006135 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006136 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006137
Josef Bacika1b075d2010-11-19 20:36:11 +00006138 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006139
Yan, Zhenga5719522009-09-24 09:17:31 -04006140 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006141 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006142 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006143 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006144 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006145 if (err)
6146 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006147 if (inode->i_nlink == 1) {
6148 /*
6149 * If new hard link count is 1, it's a file created
6150 * with open(2) O_TMPFILE flag.
6151 */
6152 err = btrfs_orphan_del(trans, inode);
6153 if (err)
6154 goto fail;
6155 }
Al Viro08c422c2011-12-23 07:58:13 -05006156 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006157 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006158 }
Chris Mason39279cc2007-06-12 06:35:45 -04006159
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006160 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006161 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006162fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006163 if (drop_inode) {
6164 inode_dec_link_count(inode);
6165 iput(inode);
6166 }
Liu Bob53d3f52012-11-14 14:34:34 +00006167 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006168 return err;
6169}
6170
Al Viro18bb1db2011-07-26 01:41:39 -04006171static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006172{
Chris Masonb9d86662008-05-02 16:13:49 -04006173 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006174 struct btrfs_trans_handle *trans;
6175 struct btrfs_root *root = BTRFS_I(dir)->root;
6176 int err = 0;
6177 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006178 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006179 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006180
Josef Bacik9ed74f22009-09-11 16:12:44 -04006181 /*
6182 * 2 items for inode and ref
6183 * 2 items for dir items
6184 * 1 for xattr if selinux is on
6185 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006186 trans = btrfs_start_transaction(root, 5);
6187 if (IS_ERR(trans))
6188 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006189
Li Zefan581bb052011-04-20 10:06:11 +08006190 err = btrfs_find_free_ino(root, &objectid);
6191 if (err)
6192 goto out_fail;
6193
Josef Bacikaec74772008-07-24 12:12:38 -04006194 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006195 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006196 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006197 if (IS_ERR(inode)) {
6198 err = PTR_ERR(inode);
6199 goto out_fail;
6200 }
Chris Mason5f39d392007-10-15 16:14:19 -04006201
Chris Mason39279cc2007-06-12 06:35:45 -04006202 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006203 /* these must be set before we unlock the inode */
6204 inode->i_op = &btrfs_dir_inode_operations;
6205 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006206
Eric Paris2a7dba32011-02-01 11:05:39 -05006207 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006208 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006209 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006210
Chris Masondbe674a2008-07-17 12:54:05 -04006211 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006212 err = btrfs_update_inode(trans, root, inode);
6213 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006214 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006215
Josef Bacika1b075d2010-11-19 20:36:11 +00006216 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6217 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006218 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006219 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006220
Chris Mason39279cc2007-06-12 06:35:45 -04006221 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006222 /*
6223 * mkdir is special. We're unlocking after we call d_instantiate
6224 * to avoid a race with nfsd calling d_instantiate.
6225 */
6226 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006227 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006228
6229out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006230 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006231 if (drop_on_err)
6232 iput(inode);
Josef Bacikc581afc2014-02-06 16:06:06 -05006233 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006234 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006235 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006236
6237out_fail_inode:
6238 unlock_new_inode(inode);
6239 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006240}
6241
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006242/* Find next extent map of a given extent map, caller needs to ensure locks */
6243static struct extent_map *next_extent_map(struct extent_map *em)
6244{
6245 struct rb_node *next;
6246
6247 next = rb_next(&em->rb_node);
6248 if (!next)
6249 return NULL;
6250 return container_of(next, struct extent_map, rb_node);
6251}
6252
6253static struct extent_map *prev_extent_map(struct extent_map *em)
6254{
6255 struct rb_node *prev;
6256
6257 prev = rb_prev(&em->rb_node);
6258 if (!prev)
6259 return NULL;
6260 return container_of(prev, struct extent_map, rb_node);
6261}
6262
Chris Masond352ac62008-09-29 15:18:18 -04006263/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006264 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006265 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006266 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006267 */
Chris Mason3b951512008-04-17 11:29:12 -04006268static int merge_extent_mapping(struct extent_map_tree *em_tree,
6269 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006270 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006271 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006272{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006273 struct extent_map *prev;
6274 struct extent_map *next;
6275 u64 start;
6276 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006277 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006278
Chris Masone6dcd2d2008-07-17 12:53:50 -04006279 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006280
6281 if (existing->start > map_start) {
6282 next = existing;
6283 prev = prev_extent_map(next);
6284 } else {
6285 prev = existing;
6286 next = next_extent_map(prev);
6287 }
6288
6289 start = prev ? extent_map_end(prev) : em->start;
6290 start = max_t(u64, start, em->start);
6291 end = next ? next->start : extent_map_end(em);
6292 end = min_t(u64, end, extent_map_end(em));
6293 start_diff = start - em->start;
6294 em->start = start;
6295 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006296 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6297 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006298 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006299 em->block_len -= start_diff;
6300 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006301 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006302}
6303
Chris Masonc8b97812008-10-29 14:49:59 -04006304static noinline int uncompress_inline(struct btrfs_path *path,
6305 struct inode *inode, struct page *page,
6306 size_t pg_offset, u64 extent_offset,
6307 struct btrfs_file_extent_item *item)
6308{
6309 int ret;
6310 struct extent_buffer *leaf = path->nodes[0];
6311 char *tmp;
6312 size_t max_size;
6313 unsigned long inline_size;
6314 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006315 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006316
6317 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006318 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006319 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6320 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006321 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006322 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006323 if (!tmp)
6324 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006325 ptr = btrfs_file_extent_inline_start(item);
6326
6327 read_extent_buffer(leaf, tmp, ptr, inline_size);
6328
Chris Mason5b050f02008-11-11 09:34:41 -05006329 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006330 ret = btrfs_decompress(compress_type, tmp, page,
6331 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006332 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006333 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006334}
6335
Chris Masond352ac62008-09-29 15:18:18 -04006336/*
6337 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006338 * the ugly parts come from merging extents from the disk with the in-ram
6339 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006340 * where the in-ram extents might be locked pending data=ordered completion.
6341 *
6342 * This also copies inline extents directly into the page.
6343 */
Chris Masond3977122009-01-05 21:25:51 -05006344
Chris Masona52d9a82007-08-27 16:49:44 -04006345struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006346 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006347 int create)
6348{
6349 int ret;
6350 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006351 u64 extent_start = 0;
6352 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006353 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006354 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006355 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006356 struct btrfs_root *root = BTRFS_I(inode)->root;
6357 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006358 struct extent_buffer *leaf;
6359 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006360 struct extent_map *em = NULL;
6361 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006362 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006363 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006364 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006365
Chris Masona52d9a82007-08-27 16:49:44 -04006366again:
Chris Mason890871b2009-09-02 16:24:52 -04006367 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006368 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006369 if (em)
6370 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006371 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006372
Chris Masona52d9a82007-08-27 16:49:44 -04006373 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006374 if (em->start > start || em->start + em->len <= start)
6375 free_extent_map(em);
6376 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006377 free_extent_map(em);
6378 else
6379 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006380 }
David Sterba172ddd62011-04-21 00:48:27 +02006381 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006382 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006383 err = -ENOMEM;
6384 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006385 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006386 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006387 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006388 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006389 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006390 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006391
6392 if (!path) {
6393 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006394 if (!path) {
6395 err = -ENOMEM;
6396 goto out;
6397 }
6398 /*
6399 * Chances are we'll be called again, so go ahead and do
6400 * readahead
6401 */
6402 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006403 }
6404
Chris Mason179e29e2007-11-01 11:28:41 -04006405 ret = btrfs_lookup_file_extent(trans, root, path,
6406 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006407 if (ret < 0) {
6408 err = ret;
6409 goto out;
6410 }
6411
6412 if (ret != 0) {
6413 if (path->slots[0] == 0)
6414 goto not_found;
6415 path->slots[0]--;
6416 }
6417
Chris Mason5f39d392007-10-15 16:14:19 -04006418 leaf = path->nodes[0];
6419 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006420 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006421 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006422 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006423 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006424 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006425 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006426 /*
6427 * If we backup past the first extent we want to move forward
6428 * and see if there is an extent in front of us, otherwise we'll
6429 * say there is a hole for our whole search range which can
6430 * cause problems.
6431 */
6432 extent_end = start;
6433 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006434 }
6435
Chris Mason5f39d392007-10-15 16:14:19 -04006436 found_type = btrfs_file_extent_type(leaf, item);
6437 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006438 if (found_type == BTRFS_FILE_EXTENT_REG ||
6439 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006440 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006441 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006442 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6443 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006444 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006445 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006446 }
Josef Bacik25a50342013-10-14 12:08:38 -04006447next:
Yan Zheng9036c102008-10-30 14:19:41 -04006448 if (start >= extent_end) {
6449 path->slots[0]++;
6450 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6451 ret = btrfs_next_leaf(root, path);
6452 if (ret < 0) {
6453 err = ret;
6454 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006455 }
Yan Zheng9036c102008-10-30 14:19:41 -04006456 if (ret > 0)
6457 goto not_found;
6458 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006459 }
Yan Zheng9036c102008-10-30 14:19:41 -04006460 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6461 if (found_key.objectid != objectid ||
6462 found_key.type != BTRFS_EXTENT_DATA_KEY)
6463 goto not_found;
6464 if (start + len <= found_key.offset)
6465 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006466 if (start > found_key.offset)
6467 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006468 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006469 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006470 em->len = found_key.offset - start;
6471 goto not_found_em;
6472 }
6473
Filipe Manana7ffbb592014-06-09 03:48:05 +01006474 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6475
Yan Zhengd899e052008-10-30 14:25:28 -04006476 if (found_type == BTRFS_FILE_EXTENT_REG ||
6477 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006478 goto insert;
6479 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006480 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006481 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006482 size_t size;
6483 size_t extent_offset;
6484 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006485
Filipe Manana7ffbb592014-06-09 03:48:05 +01006486 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006487 goto out;
Yan689f9342007-10-29 11:41:07 -04006488
Chris Mason514ac8a2014-01-03 21:07:00 -08006489 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006490 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006491 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006492 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006493 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006494 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006495 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006496 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006497 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006498 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006499 if (btrfs_file_extent_compression(leaf, item) !=
6500 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006501 ret = uncompress_inline(path, inode, page,
6502 pg_offset,
6503 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006504 if (ret) {
6505 err = ret;
6506 goto out;
6507 }
Chris Masonc8b97812008-10-29 14:49:59 -04006508 } else {
6509 map = kmap(page);
6510 read_extent_buffer(leaf, map + pg_offset, ptr,
6511 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006512 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6513 memset(map + pg_offset + copy_size, 0,
6514 PAGE_CACHE_SIZE - pg_offset -
6515 copy_size);
6516 }
Chris Masonc8b97812008-10-29 14:49:59 -04006517 kunmap(page);
6518 }
Chris Mason179e29e2007-11-01 11:28:41 -04006519 flush_dcache_page(page);
6520 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006521 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006522 if (!trans) {
6523 kunmap(page);
6524 free_extent_map(em);
6525 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006526
David Sterbab3b4aa72011-04-21 01:20:15 +02006527 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006528 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006529
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006530 if (IS_ERR(trans))
6531 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006532 goto again;
6533 }
Chris Masonc8b97812008-10-29 14:49:59 -04006534 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006535 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006536 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006537 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006538 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006539 }
Chris Masond1310b22008-01-24 16:13:08 -05006540 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006541 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006542 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006543 }
6544not_found:
6545 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006546 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006547 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006548not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006549 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006550 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006551insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006552 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006553 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006554 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006555 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006556 err = -EIO;
6557 goto out;
6558 }
Chris Masond1310b22008-01-24 16:13:08 -05006559
6560 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006561 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006562 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006563 /* it is possible that someone inserted the extent into the tree
6564 * while we had the lock dropped. It is also possible that
6565 * an overlapping map exists in the tree
6566 */
Chris Masona52d9a82007-08-27 16:49:44 -04006567 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006568 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006569
6570 ret = 0;
6571
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006572 existing = search_extent_mapping(em_tree, start, len);
6573 /*
6574 * existing will always be non-NULL, since there must be
6575 * extent causing the -EEXIST.
6576 */
6577 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006578 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006579 /*
6580 * The existing extent map is the one nearest to
6581 * the [start, start + len) range which overlaps
6582 */
6583 err = merge_extent_mapping(em_tree, existing,
6584 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006585 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006586 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006587 free_extent_map(em);
6588 em = NULL;
6589 }
6590 } else {
6591 free_extent_map(em);
6592 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006593 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006594 }
Chris Masona52d9a82007-08-27 16:49:44 -04006595 }
Chris Mason890871b2009-09-02 16:24:52 -04006596 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006597out:
liubo1abe9b82011-03-24 11:18:59 +00006598
Steven Rostedt4cd85872013-11-14 22:57:29 -05006599 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006600
Chris Masonf4219502008-07-22 11:18:09 -04006601 if (path)
6602 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006603 if (trans) {
6604 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006605 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006606 err = ret;
6607 }
Chris Masona52d9a82007-08-27 16:49:44 -04006608 if (err) {
6609 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006610 return ERR_PTR(err);
6611 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006612 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006613 return em;
6614}
6615
Chris Masonec29ed52011-02-23 16:23:20 -05006616struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6617 size_t pg_offset, u64 start, u64 len,
6618 int create)
6619{
6620 struct extent_map *em;
6621 struct extent_map *hole_em = NULL;
6622 u64 range_start = start;
6623 u64 end;
6624 u64 found;
6625 u64 found_end;
6626 int err = 0;
6627
6628 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6629 if (IS_ERR(em))
6630 return em;
6631 if (em) {
6632 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006633 * if our em maps to
6634 * - a hole or
6635 * - a pre-alloc extent,
6636 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006637 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006638 if (em->block_start != EXTENT_MAP_HOLE &&
6639 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006640 return em;
6641 else
6642 hole_em = em;
6643 }
6644
6645 /* check to see if we've wrapped (len == -1 or similar) */
6646 end = start + len;
6647 if (end < start)
6648 end = (u64)-1;
6649 else
6650 end -= 1;
6651
6652 em = NULL;
6653
6654 /* ok, we didn't find anything, lets look for delalloc */
6655 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6656 end, len, EXTENT_DELALLOC, 1);
6657 found_end = range_start + found;
6658 if (found_end < range_start)
6659 found_end = (u64)-1;
6660
6661 /*
6662 * we didn't find anything useful, return
6663 * the original results from get_extent()
6664 */
6665 if (range_start > end || found_end <= start) {
6666 em = hole_em;
6667 hole_em = NULL;
6668 goto out;
6669 }
6670
6671 /* adjust the range_start to make sure it doesn't
6672 * go backwards from the start they passed in
6673 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306674 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006675 found = found_end - range_start;
6676
6677 if (found > 0) {
6678 u64 hole_start = start;
6679 u64 hole_len = len;
6680
David Sterba172ddd62011-04-21 00:48:27 +02006681 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006682 if (!em) {
6683 err = -ENOMEM;
6684 goto out;
6685 }
6686 /*
6687 * when btrfs_get_extent can't find anything it
6688 * returns one huge hole
6689 *
6690 * make sure what it found really fits our range, and
6691 * adjust to make sure it is based on the start from
6692 * the caller
6693 */
6694 if (hole_em) {
6695 u64 calc_end = extent_map_end(hole_em);
6696
6697 if (calc_end <= start || (hole_em->start > end)) {
6698 free_extent_map(hole_em);
6699 hole_em = NULL;
6700 } else {
6701 hole_start = max(hole_em->start, start);
6702 hole_len = calc_end - hole_start;
6703 }
6704 }
6705 em->bdev = NULL;
6706 if (hole_em && range_start > hole_start) {
6707 /* our hole starts before our delalloc, so we
6708 * have to return just the parts of the hole
6709 * that go until the delalloc starts
6710 */
6711 em->len = min(hole_len,
6712 range_start - hole_start);
6713 em->start = hole_start;
6714 em->orig_start = hole_start;
6715 /*
6716 * don't adjust block start at all,
6717 * it is fixed at EXTENT_MAP_HOLE
6718 */
6719 em->block_start = hole_em->block_start;
6720 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006721 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6722 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006723 } else {
6724 em->start = range_start;
6725 em->len = found;
6726 em->orig_start = range_start;
6727 em->block_start = EXTENT_MAP_DELALLOC;
6728 em->block_len = found;
6729 }
6730 } else if (hole_em) {
6731 return hole_em;
6732 }
6733out:
6734
6735 free_extent_map(hole_em);
6736 if (err) {
6737 free_extent_map(em);
6738 return ERR_PTR(err);
6739 }
6740 return em;
6741}
6742
Josef Bacik4b46fce2010-05-23 11:00:55 -04006743static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6744 u64 start, u64 len)
6745{
6746 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006747 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006748 struct btrfs_key ins;
6749 u64 alloc_hint;
6750 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006751
Josef Bacik4b46fce2010-05-23 11:00:55 -04006752 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006753 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08006754 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006755 if (ret)
6756 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006757
Josef Bacik70c8a912012-10-11 16:54:30 -04006758 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006759 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006760 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08006761 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006762 return em;
6763 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006764
6765 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6766 ins.offset, ins.offset, 0);
6767 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08006768 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006769 free_extent_map(em);
6770 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006771 }
Josef Bacik00361582013-08-14 14:02:47 -04006772
Josef Bacik4b46fce2010-05-23 11:00:55 -04006773 return em;
6774}
6775
Chris Mason46bfbb52010-05-26 11:04:10 -04006776/*
6777 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6778 * block must be cow'd
6779 */
Josef Bacik00361582013-08-14 14:02:47 -04006780noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006781 u64 *orig_start, u64 *orig_block_len,
6782 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006783{
Josef Bacik00361582013-08-14 14:02:47 -04006784 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006785 struct btrfs_path *path;
6786 int ret;
6787 struct extent_buffer *leaf;
6788 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006789 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006790 struct btrfs_file_extent_item *fi;
6791 struct btrfs_key key;
6792 u64 disk_bytenr;
6793 u64 backref_offset;
6794 u64 extent_end;
6795 u64 num_bytes;
6796 int slot;
6797 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006798 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006799
Chris Mason46bfbb52010-05-26 11:04:10 -04006800 path = btrfs_alloc_path();
6801 if (!path)
6802 return -ENOMEM;
6803
Josef Bacik00361582013-08-14 14:02:47 -04006804 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006805 offset, 0);
6806 if (ret < 0)
6807 goto out;
6808
6809 slot = path->slots[0];
6810 if (ret == 1) {
6811 if (slot == 0) {
6812 /* can't find the item, must cow */
6813 ret = 0;
6814 goto out;
6815 }
6816 slot--;
6817 }
6818 ret = 0;
6819 leaf = path->nodes[0];
6820 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006821 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006822 key.type != BTRFS_EXTENT_DATA_KEY) {
6823 /* not our file or wrong item type, must cow */
6824 goto out;
6825 }
6826
6827 if (key.offset > offset) {
6828 /* Wrong offset, must cow */
6829 goto out;
6830 }
6831
6832 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6833 found_type = btrfs_file_extent_type(leaf, fi);
6834 if (found_type != BTRFS_FILE_EXTENT_REG &&
6835 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6836 /* not a regular extent, must cow */
6837 goto out;
6838 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006839
6840 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6841 goto out;
6842
Miao Xiee77751a2013-12-27 21:11:50 +08006843 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6844 if (extent_end <= offset)
6845 goto out;
6846
Chris Mason46bfbb52010-05-26 11:04:10 -04006847 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006848 if (disk_bytenr == 0)
6849 goto out;
6850
6851 if (btrfs_file_extent_compression(leaf, fi) ||
6852 btrfs_file_extent_encryption(leaf, fi) ||
6853 btrfs_file_extent_other_encoding(leaf, fi))
6854 goto out;
6855
Chris Mason46bfbb52010-05-26 11:04:10 -04006856 backref_offset = btrfs_file_extent_offset(leaf, fi);
6857
Josef Bacik7ee9e442013-06-21 16:37:03 -04006858 if (orig_start) {
6859 *orig_start = key.offset - backref_offset;
6860 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6861 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6862 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006863
Chris Mason46bfbb52010-05-26 11:04:10 -04006864 if (btrfs_extent_readonly(root, disk_bytenr))
6865 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006866
6867 num_bytes = min(offset + *len, extent_end) - offset;
6868 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6869 u64 range_end;
6870
6871 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6872 ret = test_range_bit(io_tree, offset, range_end,
6873 EXTENT_DELALLOC, 0, NULL);
6874 if (ret) {
6875 ret = -EAGAIN;
6876 goto out;
6877 }
6878 }
6879
Josef Bacik1bda19e2013-10-18 12:10:36 -04006880 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006881
6882 /*
6883 * look for other files referencing this extent, if we
6884 * find any we must cow
6885 */
Josef Bacik00361582013-08-14 14:02:47 -04006886 trans = btrfs_join_transaction(root);
6887 if (IS_ERR(trans)) {
6888 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006889 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006890 }
6891
6892 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6893 key.offset - backref_offset, disk_bytenr);
6894 btrfs_end_transaction(trans, root);
6895 if (ret) {
6896 ret = 0;
6897 goto out;
6898 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006899
6900 /*
6901 * adjust disk_bytenr and num_bytes to cover just the bytes
6902 * in this extent we are about to write. If there
6903 * are any csums in that range we have to cow in order
6904 * to keep the csums correct
6905 */
6906 disk_bytenr += backref_offset;
6907 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006908 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6909 goto out;
6910 /*
6911 * all of the above have passed, it is safe to overwrite this extent
6912 * without cow
6913 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006914 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006915 ret = 1;
6916out:
6917 btrfs_free_path(path);
6918 return ret;
6919}
6920
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006921bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
6922{
6923 struct radix_tree_root *root = &inode->i_mapping->page_tree;
6924 int found = false;
6925 void **pagep = NULL;
6926 struct page *page = NULL;
6927 int start_idx;
6928 int end_idx;
6929
6930 start_idx = start >> PAGE_CACHE_SHIFT;
6931
6932 /*
6933 * end is the last byte in the last page. end == start is legal
6934 */
6935 end_idx = end >> PAGE_CACHE_SHIFT;
6936
6937 rcu_read_lock();
6938
6939 /* Most of the code in this while loop is lifted from
6940 * find_get_page. It's been modified to begin searching from a
6941 * page and return just the first page found in that range. If the
6942 * found idx is less than or equal to the end idx then we know that
6943 * a page exists. If no pages are found or if those pages are
6944 * outside of the range then we're fine (yay!) */
6945 while (page == NULL &&
6946 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
6947 page = radix_tree_deref_slot(pagep);
6948 if (unlikely(!page))
6949 break;
6950
6951 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01006952 if (radix_tree_deref_retry(page)) {
6953 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006954 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01006955 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006956 /*
6957 * Otherwise, shmem/tmpfs must be storing a swap entry
6958 * here as an exceptional entry: so return it without
6959 * attempting to raise page count.
6960 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01006961 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006962 break; /* TODO: Is this relevant for this use case? */
6963 }
6964
Filipe Manana91405152014-06-05 13:22:24 +01006965 if (!page_cache_get_speculative(page)) {
6966 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006967 continue;
Filipe Manana91405152014-06-05 13:22:24 +01006968 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006969
6970 /*
6971 * Has the page moved?
6972 * This is part of the lockless pagecache protocol. See
6973 * include/linux/pagemap.h for details.
6974 */
6975 if (unlikely(page != *pagep)) {
6976 page_cache_release(page);
6977 page = NULL;
6978 }
6979 }
6980
6981 if (page) {
6982 if (page->index <= end_idx)
6983 found = true;
6984 page_cache_release(page);
6985 }
6986
6987 rcu_read_unlock();
6988 return found;
6989}
6990
Josef Bacikeb838e72012-07-31 16:28:48 -04006991static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6992 struct extent_state **cached_state, int writing)
6993{
6994 struct btrfs_ordered_extent *ordered;
6995 int ret = 0;
6996
6997 while (1) {
6998 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6999 0, cached_state);
7000 /*
7001 * We're concerned with the entire range that we're going to be
7002 * doing DIO to, so we need to make sure theres no ordered
7003 * extents in this range.
7004 */
7005 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7006 lockend - lockstart + 1);
7007
7008 /*
7009 * We need to make sure there are no buffered pages in this
7010 * range either, we could have raced between the invalidate in
7011 * generic_file_direct_write and locking the extent. The
7012 * invalidate needs to happen so that reads after a write do not
7013 * get stale data.
7014 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007015 if (!ordered &&
7016 (!writing ||
7017 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007018 break;
7019
7020 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7021 cached_state, GFP_NOFS);
7022
7023 if (ordered) {
7024 btrfs_start_ordered_extent(inode, ordered, 1);
7025 btrfs_put_ordered_extent(ordered);
7026 } else {
7027 /* Screw you mmap */
7028 ret = filemap_write_and_wait_range(inode->i_mapping,
7029 lockstart,
7030 lockend);
7031 if (ret)
7032 break;
7033
7034 /*
7035 * If we found a page that couldn't be invalidated just
7036 * fall back to buffered.
7037 */
7038 ret = invalidate_inode_pages2_range(inode->i_mapping,
7039 lockstart >> PAGE_CACHE_SHIFT,
7040 lockend >> PAGE_CACHE_SHIFT);
7041 if (ret)
7042 break;
7043 }
7044
7045 cond_resched();
7046 }
7047
7048 return ret;
7049}
7050
Josef Bacik69ffb542012-09-11 15:40:07 -04007051static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7052 u64 len, u64 orig_start,
7053 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007054 u64 orig_block_len, u64 ram_bytes,
7055 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007056{
7057 struct extent_map_tree *em_tree;
7058 struct extent_map *em;
7059 struct btrfs_root *root = BTRFS_I(inode)->root;
7060 int ret;
7061
7062 em_tree = &BTRFS_I(inode)->extent_tree;
7063 em = alloc_extent_map();
7064 if (!em)
7065 return ERR_PTR(-ENOMEM);
7066
7067 em->start = start;
7068 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007069 em->mod_start = start;
7070 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007071 em->len = len;
7072 em->block_len = block_len;
7073 em->block_start = block_start;
7074 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007075 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007076 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007077 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007078 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7079 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007080 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007081
7082 do {
7083 btrfs_drop_extent_cache(inode, em->start,
7084 em->start + em->len - 1, 0);
7085 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007086 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007087 write_unlock(&em_tree->lock);
7088 } while (ret == -EEXIST);
7089
7090 if (ret) {
7091 free_extent_map(em);
7092 return ERR_PTR(ret);
7093 }
7094
7095 return em;
7096}
7097
7098
Josef Bacik4b46fce2010-05-23 11:00:55 -04007099static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7100 struct buffer_head *bh_result, int create)
7101{
7102 struct extent_map *em;
7103 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007104 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007105 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007106 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007107 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007108 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007109 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007110
Miao Xie172a5042013-02-21 02:48:22 -07007111 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04007112 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007113 else
Josef Bacikc3298612012-08-03 16:49:19 -04007114 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007115
Josef Bacikc3298612012-08-03 16:49:19 -04007116 lockstart = start;
7117 lockend = start + len - 1;
7118
Josef Bacikeb838e72012-07-31 16:28:48 -04007119 /*
7120 * If this errors out it's because we couldn't invalidate pagecache for
7121 * this range and we need to fallback to buffered.
7122 */
7123 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7124 return -ENOTBLK;
7125
Josef Bacik4b46fce2010-05-23 11:00:55 -04007126 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007127 if (IS_ERR(em)) {
7128 ret = PTR_ERR(em);
7129 goto unlock_err;
7130 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007131
7132 /*
7133 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7134 * io. INLINE is special, and we could probably kludge it in here, but
7135 * it's still buffered so for safety lets just fall back to the generic
7136 * buffered path.
7137 *
7138 * For COMPRESSED we _have_ to read the entire extent in so we can
7139 * decompress it, so there will be buffering required no matter what we
7140 * do, so go ahead and fallback to buffered.
7141 *
7142 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7143 * to buffered IO. Don't blame me, this is the price we pay for using
7144 * the generic code.
7145 */
7146 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7147 em->block_start == EXTENT_MAP_INLINE) {
7148 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007149 ret = -ENOTBLK;
7150 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007151 }
7152
7153 /* Just a good old fashioned hole, return */
7154 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7155 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7156 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007157 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007158 }
7159
7160 /*
7161 * We don't allocate a new extent in the following cases
7162 *
7163 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7164 * existing extent.
7165 * 2) The extent is marked as PREALLOC. We're good to go here and can
7166 * just use the extent.
7167 *
7168 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007169 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007170 len = min(len, em->len - (start - em->start));
7171 lockstart = start + len;
7172 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007173 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007174
7175 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7176 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7177 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007178 int type;
7179 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04007180 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007181
7182 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7183 type = BTRFS_ORDERED_PREALLOC;
7184 else
7185 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007186 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007187 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007188
Josef Bacik00361582013-08-14 14:02:47 -04007189 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007190 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007191 if (type == BTRFS_ORDERED_PREALLOC) {
7192 free_extent_map(em);
7193 em = create_pinned_em(inode, start, len,
7194 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007195 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007196 orig_block_len,
7197 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007198 if (IS_ERR(em)) {
7199 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007200 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007201 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007202 }
7203
Chris Mason46bfbb52010-05-26 11:04:10 -04007204 ret = btrfs_add_ordered_extent_dio(inode, start,
7205 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007206 if (ret) {
7207 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007208 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007209 }
7210 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007211 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007212 }
Josef Bacik00361582013-08-14 14:02:47 -04007213
Chris Mason46bfbb52010-05-26 11:04:10 -04007214 /*
7215 * this will cow the extent, reset the len in case we changed
7216 * it above
7217 */
7218 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007219 free_extent_map(em);
7220 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007221 if (IS_ERR(em)) {
7222 ret = PTR_ERR(em);
7223 goto unlock_err;
7224 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007225 len = min(len, em->len - (start - em->start));
7226unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007227 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7228 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007229 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007230 bh_result->b_bdev = em->bdev;
7231 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007232 if (create) {
7233 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7234 set_buffer_new(bh_result);
7235
7236 /*
7237 * Need to update the i_size under the extent lock so buffered
7238 * readers will get the updated i_size when we unlock.
7239 */
7240 if (start + len > i_size_read(inode))
7241 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007242
Miao Xie172a5042013-02-21 02:48:22 -07007243 spin_lock(&BTRFS_I(inode)->lock);
7244 BTRFS_I(inode)->outstanding_extents++;
7245 spin_unlock(&BTRFS_I(inode)->lock);
7246
Miao Xie09348562013-02-07 10:12:07 +00007247 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7248 lockstart + len - 1, EXTENT_DELALLOC, NULL,
7249 &cached_state, GFP_NOFS);
7250 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04007251 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007252
Josef Bacikeb838e72012-07-31 16:28:48 -04007253 /*
7254 * In the case of write we need to clear and unlock the entire range,
7255 * in the case of read we need to unlock only the end area that we
7256 * aren't using if there is any left over space.
7257 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007258 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007259 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7260 lockend, unlock_bits, 1, 0,
7261 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007262 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007263 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007264 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007265
Josef Bacik4b46fce2010-05-23 11:00:55 -04007266 free_extent_map(em);
7267
7268 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007269
7270unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007271 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7272 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7273 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007274}
7275
Miao Xie8b110e32014-09-12 18:44:03 +08007276static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7277 int rw, int mirror_num)
7278{
7279 struct btrfs_root *root = BTRFS_I(inode)->root;
7280 int ret;
7281
7282 BUG_ON(rw & REQ_WRITE);
7283
7284 bio_get(bio);
7285
7286 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7287 BTRFS_WQ_ENDIO_DIO_REPAIR);
7288 if (ret)
7289 goto err;
7290
7291 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7292err:
7293 bio_put(bio);
7294 return ret;
7295}
7296
7297static int btrfs_check_dio_repairable(struct inode *inode,
7298 struct bio *failed_bio,
7299 struct io_failure_record *failrec,
7300 int failed_mirror)
7301{
7302 int num_copies;
7303
7304 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7305 failrec->logical, failrec->len);
7306 if (num_copies == 1) {
7307 /*
7308 * we only have a single copy of the data, so don't bother with
7309 * all the retry and error correction code that follows. no
7310 * matter what the error is, it is very likely to persist.
7311 */
7312 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7313 num_copies, failrec->this_mirror, failed_mirror);
7314 return 0;
7315 }
7316
7317 failrec->failed_mirror = failed_mirror;
7318 failrec->this_mirror++;
7319 if (failrec->this_mirror == failed_mirror)
7320 failrec->this_mirror++;
7321
7322 if (failrec->this_mirror > num_copies) {
7323 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7324 num_copies, failrec->this_mirror, failed_mirror);
7325 return 0;
7326 }
7327
7328 return 1;
7329}
7330
7331static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7332 struct page *page, u64 start, u64 end,
7333 int failed_mirror, bio_end_io_t *repair_endio,
7334 void *repair_arg)
7335{
7336 struct io_failure_record *failrec;
7337 struct bio *bio;
7338 int isector;
7339 int read_mode;
7340 int ret;
7341
7342 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7343
7344 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7345 if (ret)
7346 return ret;
7347
7348 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7349 failed_mirror);
7350 if (!ret) {
7351 free_io_failure(inode, failrec);
7352 return -EIO;
7353 }
7354
7355 if (failed_bio->bi_vcnt > 1)
7356 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7357 else
7358 read_mode = READ_SYNC;
7359
7360 isector = start - btrfs_io_bio(failed_bio)->logical;
7361 isector >>= inode->i_sb->s_blocksize_bits;
7362 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7363 0, isector, repair_endio, repair_arg);
7364 if (!bio) {
7365 free_io_failure(inode, failrec);
7366 return -EIO;
7367 }
7368
7369 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7370 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7371 read_mode, failrec->this_mirror, failrec->in_validation);
7372
7373 ret = submit_dio_repair_bio(inode, bio, read_mode,
7374 failrec->this_mirror);
7375 if (ret) {
7376 free_io_failure(inode, failrec);
7377 bio_put(bio);
7378 }
7379
7380 return ret;
7381}
7382
7383struct btrfs_retry_complete {
7384 struct completion done;
7385 struct inode *inode;
7386 u64 start;
7387 int uptodate;
7388};
7389
7390static void btrfs_retry_endio_nocsum(struct bio *bio, int err)
7391{
7392 struct btrfs_retry_complete *done = bio->bi_private;
7393 struct bio_vec *bvec;
7394 int i;
7395
7396 if (err)
7397 goto end;
7398
7399 done->uptodate = 1;
7400 bio_for_each_segment_all(bvec, bio, i)
7401 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7402end:
7403 complete(&done->done);
7404 bio_put(bio);
7405}
7406
7407static int __btrfs_correct_data_nocsum(struct inode *inode,
7408 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007409{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007410 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007411 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007412 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007413 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007414 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007415
Miao Xiec1dc0892014-09-12 18:43:56 +08007416 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007417 done.inode = inode;
7418
7419 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7420try_again:
7421 done.uptodate = 0;
7422 done.start = start;
7423 init_completion(&done.done);
7424
7425 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7426 start + bvec->bv_len - 1,
7427 io_bio->mirror_num,
7428 btrfs_retry_endio_nocsum, &done);
7429 if (ret)
7430 return ret;
7431
7432 wait_for_completion(&done.done);
7433
7434 if (!done.uptodate) {
7435 /* We might have another mirror, so try again */
7436 goto try_again;
7437 }
7438
7439 start += bvec->bv_len;
7440 }
7441
7442 return 0;
7443}
7444
7445static void btrfs_retry_endio(struct bio *bio, int err)
7446{
7447 struct btrfs_retry_complete *done = bio->bi_private;
7448 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7449 struct bio_vec *bvec;
7450 int uptodate;
7451 int ret;
7452 int i;
7453
7454 if (err)
7455 goto end;
7456
7457 uptodate = 1;
7458 bio_for_each_segment_all(bvec, bio, i) {
7459 ret = __readpage_endio_check(done->inode, io_bio, i,
7460 bvec->bv_page, 0,
7461 done->start, bvec->bv_len);
7462 if (!ret)
7463 clean_io_failure(done->inode, done->start,
7464 bvec->bv_page, 0);
7465 else
7466 uptodate = 0;
7467 }
7468
7469 done->uptodate = uptodate;
7470end:
7471 complete(&done->done);
7472 bio_put(bio);
7473}
7474
7475static int __btrfs_subio_endio_read(struct inode *inode,
7476 struct btrfs_io_bio *io_bio, int err)
7477{
7478 struct bio_vec *bvec;
7479 struct btrfs_retry_complete done;
7480 u64 start;
7481 u64 offset = 0;
7482 int i;
7483 int ret;
7484
7485 err = 0;
7486 start = io_bio->logical;
7487 done.inode = inode;
7488
Miao Xiec1dc0892014-09-12 18:43:56 +08007489 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007490 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7491 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007492 if (likely(!ret))
7493 goto next;
7494try_again:
7495 done.uptodate = 0;
7496 done.start = start;
7497 init_completion(&done.done);
7498
7499 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7500 start + bvec->bv_len - 1,
7501 io_bio->mirror_num,
7502 btrfs_retry_endio, &done);
7503 if (ret) {
7504 err = ret;
7505 goto next;
7506 }
7507
7508 wait_for_completion(&done.done);
7509
7510 if (!done.uptodate) {
7511 /* We might have another mirror, so try again */
7512 goto try_again;
7513 }
7514next:
7515 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007516 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007517 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007518
7519 return err;
7520}
7521
Miao Xie8b110e32014-09-12 18:44:03 +08007522static int btrfs_subio_endio_read(struct inode *inode,
7523 struct btrfs_io_bio *io_bio, int err)
7524{
7525 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7526
7527 if (skip_csum) {
7528 if (unlikely(err))
7529 return __btrfs_correct_data_nocsum(inode, io_bio);
7530 else
7531 return 0;
7532 } else {
7533 return __btrfs_subio_endio_read(inode, io_bio, err);
7534 }
7535}
7536
Miao Xiec1dc0892014-09-12 18:43:56 +08007537static void btrfs_endio_direct_read(struct bio *bio, int err)
7538{
7539 struct btrfs_dio_private *dip = bio->bi_private;
7540 struct inode *inode = dip->inode;
7541 struct bio *dio_bio;
7542 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7543
Miao Xie8b110e32014-09-12 18:44:03 +08007544 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7545 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007546
Josef Bacik4b46fce2010-05-23 11:00:55 -04007547 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007548 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007549 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007550
Josef Bacik4b46fce2010-05-23 11:00:55 -04007551 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007552
7553 /* If we had a csum failure make sure to clear the uptodate flag */
7554 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007555 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7556 dio_end_io(dio_bio, err);
Miao Xie23ea8e52014-09-12 18:43:54 +08007557
7558 if (io_bio->end_io)
7559 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007560 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007561}
7562
7563static void btrfs_endio_direct_write(struct bio *bio, int err)
7564{
7565 struct btrfs_dio_private *dip = bio->bi_private;
7566 struct inode *inode = dip->inode;
7567 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007568 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007569 u64 ordered_offset = dip->logical_offset;
7570 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007571 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007572 int ret;
7573
7574 if (err)
7575 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007576again:
7577 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7578 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007579 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007580 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007581 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007582
Liu Bo9e0af232014-08-15 23:36:53 +08007583 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7584 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007585 btrfs_queue_work(root->fs_info->endio_write_workers,
7586 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007587out_test:
7588 /*
7589 * our bio might span multiple ordered extents. If we haven't
7590 * completed the accounting for the whole dio, go back and try again
7591 */
7592 if (ordered_offset < dip->logical_offset + dip->bytes) {
7593 ordered_bytes = dip->logical_offset + dip->bytes -
7594 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007595 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007596 goto again;
7597 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007598out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007599 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007600
Josef Bacik4b46fce2010-05-23 11:00:55 -04007601 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007602
7603 /* If we had an error make sure to clear the uptodate flag */
7604 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007605 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7606 dio_end_io(dio_bio, err);
7607 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007608}
7609
Chris Masoneaf25d92010-05-25 09:48:28 -04007610static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7611 struct bio *bio, int mirror_num,
7612 unsigned long bio_flags, u64 offset)
7613{
7614 int ret;
7615 struct btrfs_root *root = BTRFS_I(inode)->root;
7616 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007617 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007618 return 0;
7619}
7620
Miao Xiee65e1532010-11-22 03:04:43 +00007621static void btrfs_end_dio_bio(struct bio *bio, int err)
7622{
7623 struct btrfs_dio_private *dip = bio->bi_private;
7624
Miao Xie8b110e32014-09-12 18:44:03 +08007625 if (err)
7626 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
7627 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7628 btrfs_ino(dip->inode), bio->bi_rw,
7629 (unsigned long long)bio->bi_iter.bi_sector,
7630 bio->bi_iter.bi_size, err);
7631
7632 if (dip->subio_endio)
7633 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007634
7635 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007636 dip->errors = 1;
7637
7638 /*
7639 * before atomic variable goto zero, we must make sure
7640 * dip->errors is perceived to be set.
7641 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007642 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007643 }
7644
7645 /* if there are more bios still pending for this dio, just exit */
7646 if (!atomic_dec_and_test(&dip->pending_bios))
7647 goto out;
7648
Chris Mason9be33952013-05-17 18:30:14 -04007649 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007650 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007651 } else {
7652 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007653 bio_endio(dip->orig_bio, 0);
7654 }
7655out:
7656 bio_put(bio);
7657}
7658
7659static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7660 u64 first_sector, gfp_t gfp_flags)
7661{
7662 int nr_vecs = bio_get_nr_vecs(bdev);
7663 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7664}
7665
Miao Xiec1dc0892014-09-12 18:43:56 +08007666static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
7667 struct inode *inode,
7668 struct btrfs_dio_private *dip,
7669 struct bio *bio,
7670 u64 file_offset)
7671{
7672 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7673 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
7674 int ret;
7675
7676 /*
7677 * We load all the csum data we need when we submit
7678 * the first bio to reduce the csum tree search and
7679 * contention.
7680 */
7681 if (dip->logical_offset == file_offset) {
7682 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
7683 file_offset);
7684 if (ret)
7685 return ret;
7686 }
7687
7688 if (bio == dip->orig_bio)
7689 return 0;
7690
7691 file_offset -= dip->logical_offset;
7692 file_offset >>= inode->i_sb->s_blocksize_bits;
7693 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
7694
7695 return 0;
7696}
7697
Miao Xiee65e1532010-11-22 03:04:43 +00007698static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7699 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007700 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007701{
Miao Xiefacc8a222013-07-25 19:22:34 +08007702 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007703 int write = rw & REQ_WRITE;
7704 struct btrfs_root *root = BTRFS_I(inode)->root;
7705 int ret;
7706
Josef Bacikb812ce22012-11-16 13:56:32 -05007707 if (async_submit)
7708 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7709
Miao Xiee65e1532010-11-22 03:04:43 +00007710 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007711
7712 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02007713 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7714 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007715 if (ret)
7716 goto err;
7717 }
Miao Xiee65e1532010-11-22 03:04:43 +00007718
Josef Bacik1ae39932011-04-06 14:41:34 -04007719 if (skip_sum)
7720 goto map;
7721
7722 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007723 ret = btrfs_wq_submit_bio(root->fs_info,
7724 inode, rw, bio, 0, 0,
7725 file_offset,
7726 __btrfs_submit_bio_start_direct_io,
7727 __btrfs_submit_bio_done);
7728 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007729 } else if (write) {
7730 /*
7731 * If we aren't doing async submit, calculate the csum of the
7732 * bio now.
7733 */
7734 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7735 if (ret)
7736 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007737 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08007738 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
7739 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007740 if (ret)
7741 goto err;
7742 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007743map:
7744 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007745err:
7746 bio_put(bio);
7747 return ret;
7748}
7749
7750static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7751 int skip_sum)
7752{
7753 struct inode *inode = dip->inode;
7754 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007755 struct bio *bio;
7756 struct bio *orig_bio = dip->orig_bio;
7757 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007758 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007759 u64 file_offset = dip->logical_offset;
7760 u64 submit_len = 0;
7761 u64 map_length;
7762 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08007763 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04007764 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007765
Kent Overstreet4f024f32013-10-11 15:44:27 -07007766 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007767 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007768 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007769 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007770 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08007771
Kent Overstreet4f024f32013-10-11 15:44:27 -07007772 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007773 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007774 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04007775 goto submit;
7776 }
7777
David Woodhouse53b381b2013-01-29 18:40:14 -05007778 /* async crcs make it difficult to collect full stripe writes. */
7779 if (btrfs_get_alloc_profile(root, 1) &
7780 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7781 async_submit = 0;
7782 else
7783 async_submit = 1;
7784
Josef Bacik02f57c72011-04-06 14:25:44 -04007785 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7786 if (!bio)
7787 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08007788
Josef Bacik02f57c72011-04-06 14:25:44 -04007789 bio->bi_private = dip;
7790 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007791 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04007792 atomic_inc(&dip->pending_bios);
7793
Miao Xiee65e1532010-11-22 03:04:43 +00007794 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02007795 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00007796 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02007797 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00007798 /*
7799 * inc the count before we submit the bio so
7800 * we know the end IO handler won't happen before
7801 * we inc the count. Otherwise, the dip might get freed
7802 * before we're done setting it up
7803 */
7804 atomic_inc(&dip->pending_bios);
7805 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7806 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007807 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007808 if (ret) {
7809 bio_put(bio);
7810 atomic_dec(&dip->pending_bios);
7811 goto out_err;
7812 }
7813
Miao Xiee65e1532010-11-22 03:04:43 +00007814 start_sector += submit_len >> 9;
7815 file_offset += submit_len;
7816
7817 submit_len = 0;
7818 nr_pages = 0;
7819
7820 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7821 start_sector, GFP_NOFS);
7822 if (!bio)
7823 goto out_err;
7824 bio->bi_private = dip;
7825 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007826 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007827
Kent Overstreet4f024f32013-10-11 15:44:27 -07007828 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007829 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007830 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007831 &map_length, NULL, 0);
7832 if (ret) {
7833 bio_put(bio);
7834 goto out_err;
7835 }
7836 } else {
7837 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307838 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007839 bvec++;
7840 }
7841 }
7842
Josef Bacik02f57c72011-04-06 14:25:44 -04007843submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007844 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007845 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007846 if (!ret)
7847 return 0;
7848
7849 bio_put(bio);
7850out_err:
7851 dip->errors = 1;
7852 /*
7853 * before atomic variable goto zero, we must
7854 * make sure dip->errors is perceived to be set.
7855 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007856 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007857 if (atomic_dec_and_test(&dip->pending_bios))
7858 bio_io_error(dip->orig_bio);
7859
7860 /* bio_end_io() will handle error, so we needn't return it */
7861 return 0;
7862}
7863
Chris Mason9be33952013-05-17 18:30:14 -04007864static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7865 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007866{
7867 struct btrfs_root *root = BTRFS_I(inode)->root;
7868 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007869 struct bio *io_bio;
Miao Xie23ea8e52014-09-12 18:43:54 +08007870 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007871 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007872 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007873 int ret = 0;
7874
7875 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7876
Chris Mason9be33952013-05-17 18:30:14 -04007877 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007878 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007879 ret = -ENOMEM;
7880 goto free_ordered;
7881 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007882
Miao Xiec1dc0892014-09-12 18:43:56 +08007883 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007884 if (!dip) {
7885 ret = -ENOMEM;
7886 goto free_io_bio;
7887 }
7888
7889 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007890 dip->inode = inode;
7891 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007892 dip->bytes = dio_bio->bi_iter.bi_size;
7893 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04007894 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04007895 dip->orig_bio = io_bio;
7896 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007897 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08007898 btrfs_bio = btrfs_io_bio(io_bio);
7899 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007900
Miao Xiec1dc0892014-09-12 18:43:56 +08007901 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04007902 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08007903 } else {
Chris Mason9be33952013-05-17 18:30:14 -04007904 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08007905 dip->subio_endio = btrfs_subio_endio_read;
7906 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007907
Miao Xiee65e1532010-11-22 03:04:43 +00007908 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7909 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007910 return;
Chris Mason9be33952013-05-17 18:30:14 -04007911
Miao Xie23ea8e52014-09-12 18:43:54 +08007912 if (btrfs_bio->end_io)
7913 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04007914free_io_bio:
7915 bio_put(io_bio);
7916
Josef Bacik4b46fce2010-05-23 11:00:55 -04007917free_ordered:
7918 /*
7919 * If this is a write, we need to clean up the reserved space and kill
7920 * the ordered extent.
7921 */
7922 if (write) {
7923 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007924 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007925 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7926 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7927 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08007928 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007929 btrfs_put_ordered_extent(ordered);
7930 btrfs_put_ordered_extent(ordered);
7931 }
Chris Mason9be33952013-05-17 18:30:14 -04007932 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007933}
7934
Chris Mason5a5f79b2010-05-26 21:33:37 -04007935static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04007936 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04007937{
7938 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007939 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007940 unsigned blocksize_mask = root->sectorsize - 1;
7941 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007942
7943 if (offset & blocksize_mask)
7944 goto out;
7945
Al Viro28060d52014-03-22 05:15:17 -04007946 if (iov_iter_alignment(iter) & blocksize_mask)
7947 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007948
Al Viro28060d52014-03-22 05:15:17 -04007949 /* If this is a write we don't need to check anymore */
7950 if (rw & WRITE)
7951 return 0;
7952 /*
7953 * Check to make sure we don't have duplicate iov_base's in this
7954 * iovec, if so return EINVAL, otherwise we'll get csum errors
7955 * when reading back.
7956 */
7957 for (seg = 0; seg < iter->nr_segs; seg++) {
7958 for (i = seg + 1; i < iter->nr_segs; i++) {
7959 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00007960 goto out;
7961 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007962 }
7963 retval = 0;
7964out:
7965 return retval;
7966}
Josef Bacikeb838e72012-07-31 16:28:48 -04007967
Chris Mason16432982008-04-10 10:23:21 -04007968static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05007969 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04007970{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007971 struct file *file = iocb->ki_filp;
7972 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007973 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007974 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007975 bool wakeup = true;
7976 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007977 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007978
Al Viro28060d52014-03-22 05:15:17 -04007979 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007980 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007981
Miao Xie38851cc2013-02-08 07:04:11 +00007982 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007983 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00007984
Josef Bacik0e267c42013-07-02 10:38:02 -04007985 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08007986 * The generic stuff only does filemap_write_and_wait_range, which
7987 * isn't enough if we've written compressed pages to this area, so
7988 * we need to flush the dirty pages again to make absolutely sure
7989 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04007990 */
Al Viroa6cbcd42014-03-04 22:38:00 -05007991 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08007992 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7993 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08007994 filemap_fdatawrite_range(inode->i_mapping, offset,
7995 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04007996
Miao Xie09348562013-02-07 10:12:07 +00007997 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007998 /*
7999 * If the write DIO is beyond the EOF, we need update
8000 * the isize, but it is protected by i_mutex. So we can
8001 * not unlock the i_mutex at this case.
8002 */
8003 if (offset + count <= inode->i_size) {
8004 mutex_unlock(&inode->i_mutex);
8005 relock = true;
8006 }
Miao Xie09348562013-02-07 10:12:07 +00008007 ret = btrfs_delalloc_reserve_space(inode, count);
8008 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008009 goto out;
David Sterbaee39b432014-09-30 01:33:33 +02008010 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8011 &BTRFS_I(inode)->runtime_flags)) {
Miao Xie38851cc2013-02-08 07:04:11 +00008012 inode_dio_done(inode);
8013 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8014 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008015 }
8016
8017 ret = __blockdev_direct_IO(rw, iocb, inode,
8018 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05008019 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00008020 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00008021 if (rw & WRITE) {
8022 if (ret < 0 && ret != -EIOCBQUEUED)
8023 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07008024 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00008025 btrfs_delalloc_release_space(inode,
8026 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07008027 else
8028 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00008029 }
Miao Xie38851cc2013-02-08 07:04:11 +00008030out:
Miao Xie2e60a512013-02-08 07:01:08 +00008031 if (wakeup)
8032 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008033 if (relock)
8034 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008035
8036 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008037}
8038
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008039#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8040
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008041static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8042 __u64 start, __u64 len)
8043{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008044 int ret;
8045
8046 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8047 if (ret)
8048 return ret;
8049
Chris Masonec29ed52011-02-23 16:23:20 -05008050 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008051}
8052
Chris Mason9ebefb182007-06-15 13:50:00 -04008053int btrfs_readpage(struct file *file, struct page *page)
8054{
Chris Masond1310b22008-01-24 16:13:08 -05008055 struct extent_io_tree *tree;
8056 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008057 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008058}
Chris Mason1832a6d2007-12-21 16:27:21 -05008059
Chris Mason39279cc2007-06-12 06:35:45 -04008060static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8061{
Chris Masond1310b22008-01-24 16:13:08 -05008062 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04008063
8064
8065 if (current->flags & PF_MEMALLOC) {
8066 redirty_page_for_writepage(wbc, page);
8067 unlock_page(page);
8068 return 0;
8069 }
Chris Masond1310b22008-01-24 16:13:08 -05008070 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008071 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8072}
Chris Mason39279cc2007-06-12 06:35:45 -04008073
Eric Sandeen48a3b632013-04-25 20:41:01 +00008074static int btrfs_writepages(struct address_space *mapping,
8075 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008076{
Chris Masond1310b22008-01-24 16:13:08 -05008077 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008078
Chris Masond1310b22008-01-24 16:13:08 -05008079 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008080 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8081}
8082
Chris Mason3ab2fb52007-11-08 10:59:22 -05008083static int
8084btrfs_readpages(struct file *file, struct address_space *mapping,
8085 struct list_head *pages, unsigned nr_pages)
8086{
Chris Masond1310b22008-01-24 16:13:08 -05008087 struct extent_io_tree *tree;
8088 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008089 return extent_readpages(tree, mapping, pages, nr_pages,
8090 btrfs_get_extent);
8091}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008092static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008093{
Chris Masond1310b22008-01-24 16:13:08 -05008094 struct extent_io_tree *tree;
8095 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008096 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008097
Chris Masond1310b22008-01-24 16:13:08 -05008098 tree = &BTRFS_I(page->mapping->host)->io_tree;
8099 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008100 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008101 if (ret == 1) {
8102 ClearPagePrivate(page);
8103 set_page_private(page, 0);
8104 page_cache_release(page);
8105 }
8106 return ret;
8107}
Chris Mason39279cc2007-06-12 06:35:45 -04008108
Chris Masone6dcd2d2008-07-17 12:53:50 -04008109static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8110{
Chris Mason98509cf2008-09-11 15:51:43 -04008111 if (PageWriteback(page) || PageDirty(page))
8112 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008113 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008114}
8115
Lukas Czernerd47992f2013-05-21 23:17:23 -04008116static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8117 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008118{
Josef Bacik5fd02042012-05-02 14:00:54 -04008119 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008120 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008121 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008122 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008123 u64 page_start = page_offset(page);
8124 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008125 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008126
Chris Mason8b62b722009-09-02 16:53:46 -04008127 /*
8128 * we have the page locked, so new writeback can't start,
8129 * and the dirty bit won't be cleared while we are here.
8130 *
8131 * Wait for IO on this page so that we can safely clear
8132 * the PagePrivate2 bit and do ordered accounting
8133 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008134 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008135
Josef Bacik5fd02042012-05-02 14:00:54 -04008136 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008137 if (offset) {
8138 btrfs_releasepage(page, GFP_NOFS);
8139 return;
8140 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008141
8142 if (!inode_evicting)
8143 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8144 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008145 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008146 /*
8147 * IO on this page will never be started, so we need
8148 * to account for any ordered extents now
8149 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008150 if (!inode_evicting)
8151 clear_extent_bit(tree, page_start, page_end,
8152 EXTENT_DIRTY | EXTENT_DELALLOC |
8153 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8154 EXTENT_DEFRAG, 1, 0, &cached_state,
8155 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008156 /*
8157 * whoever cleared the private bit is responsible
8158 * for the finish_ordered_io
8159 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008160 if (TestClearPagePrivate2(page)) {
8161 struct btrfs_ordered_inode_tree *tree;
8162 u64 new_len;
8163
8164 tree = &BTRFS_I(inode)->ordered_tree;
8165
8166 spin_lock_irq(&tree->lock);
8167 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8168 new_len = page_start - ordered->file_offset;
8169 if (new_len < ordered->truncated_len)
8170 ordered->truncated_len = new_len;
8171 spin_unlock_irq(&tree->lock);
8172
8173 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8174 page_start,
8175 PAGE_CACHE_SIZE, 1))
8176 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008177 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008178 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008179 if (!inode_evicting) {
8180 cached_state = NULL;
8181 lock_extent_bits(tree, page_start, page_end, 0,
8182 &cached_state);
8183 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008184 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008185
8186 if (!inode_evicting) {
8187 clear_extent_bit(tree, page_start, page_end,
8188 EXTENT_LOCKED | EXTENT_DIRTY |
8189 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8190 EXTENT_DEFRAG, 1, 1,
8191 &cached_state, GFP_NOFS);
8192
8193 __btrfs_releasepage(page, GFP_NOFS);
8194 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008195
Chris Mason4a096752008-07-21 10:29:44 -04008196 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008197 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008198 ClearPagePrivate(page);
8199 set_page_private(page, 0);
8200 page_cache_release(page);
8201 }
Chris Mason39279cc2007-06-12 06:35:45 -04008202}
8203
Chris Mason9ebefb182007-06-15 13:50:00 -04008204/*
8205 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8206 * called from a page fault handler when a page is first dirtied. Hence we must
8207 * be careful to check for EOF conditions here. We set the page up correctly
8208 * for a written page which means we get ENOSPC checking when writing into
8209 * holes and correct delalloc and unwritten extent mapping on filesystems that
8210 * support these features.
8211 *
8212 * We are not allowed to take the i_mutex here so we have to play games to
8213 * protect against truncate races as the page could now be beyond EOF. Because
8214 * vmtruncate() writes the inode size before removing pages, once we have the
8215 * page lock we can determine safely if the page is beyond EOF. If it is not
8216 * beyond EOF, then the page is guaranteed safe against truncation until we
8217 * unlock the page.
8218 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008219int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008220{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008221 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008222 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008223 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008224 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8225 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008226 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008227 char *kaddr;
8228 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008229 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008230 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008231 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008232 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008233 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008234
Jan Karab2b5ef52012-06-12 16:20:45 +02008235 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04008236 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008237 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008238 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008239 reserved = 1;
8240 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008241 if (ret) {
8242 if (ret == -ENOMEM)
8243 ret = VM_FAULT_OOM;
8244 else /* -ENOSPC, -EIO, etc */
8245 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008246 if (reserved)
8247 goto out;
8248 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008249 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008250
Nick Piggin56a76f82009-03-31 15:23:23 -07008251 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008252again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008253 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008254 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008255 page_start = page_offset(page);
8256 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04008257
Chris Mason9ebefb182007-06-15 13:50:00 -04008258 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008259 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008260 /* page got truncated out from underneath us */
8261 goto out_unlock;
8262 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008263 wait_on_page_writeback(page);
8264
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008265 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008266 set_page_extent_mapped(page);
8267
Chris Masoneb84ae02008-07-17 13:53:27 -04008268 /*
8269 * we can't set the delalloc bits if there are pending ordered
8270 * extents. Drop our locks and wait for them to finish
8271 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008272 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8273 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008274 unlock_extent_cached(io_tree, page_start, page_end,
8275 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008276 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008277 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008278 btrfs_put_ordered_extent(ordered);
8279 goto again;
8280 }
8281
Josef Bacikfbf19082009-10-01 17:10:23 -04008282 /*
8283 * XXX - page_mkwrite gets called every time the page is dirtied, even
8284 * if it was already dirty, so for space accounting reasons we need to
8285 * clear any delalloc bits for the range we are fixing to save. There
8286 * is probably a better way to do this, but for now keep consistent with
8287 * prepare_pages in the normal write path.
8288 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008289 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008290 EXTENT_DIRTY | EXTENT_DELALLOC |
8291 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008292 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008293
Josef Bacik2ac55d42010-02-03 19:33:23 +00008294 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8295 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008296 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008297 unlock_extent_cached(io_tree, page_start, page_end,
8298 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008299 ret = VM_FAULT_SIGBUS;
8300 goto out_unlock;
8301 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008302 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008303
8304 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008305 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008306 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008307 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008308 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008309
Chris Masone6dcd2d2008-07-17 12:53:50 -04008310 if (zero_start != PAGE_CACHE_SIZE) {
8311 kaddr = kmap(page);
8312 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8313 flush_dcache_page(page);
8314 kunmap(page);
8315 }
Chris Mason247e7432008-07-17 12:53:51 -04008316 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008317 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008318 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008319
Chris Mason257c62e2009-10-13 13:21:08 -04008320 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8321 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008322 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008323
Josef Bacik2ac55d42010-02-03 19:33:23 +00008324 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008325
8326out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008327 if (!ret) {
8328 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008329 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008330 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008331 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008332out:
Josef Bacikec39e182012-01-12 19:10:12 -05008333 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008334out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008335 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008336 return ret;
8337}
8338
Josef Bacika41ad392011-01-31 15:30:16 -05008339static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008340{
8341 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008342 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008343 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008344 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008345 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008346 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008347 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008348
Josef Bacik0ef8b722013-10-25 16:13:35 -04008349 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8350 (u64)-1);
8351 if (ret)
8352 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008353
Josef Bacikfcb80c22011-05-03 10:40:22 -04008354 /*
8355 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8356 * 3 things going on here
8357 *
8358 * 1) We need to reserve space for our orphan item and the space to
8359 * delete our orphan item. Lord knows we don't want to have a dangling
8360 * orphan item because we didn't reserve space to remove it.
8361 *
8362 * 2) We need to reserve space to update our inode.
8363 *
8364 * 3) We need to have something to cache all the space that is going to
8365 * be free'd up by the truncate operation, but also have some slack
8366 * space reserved in case it uses space during the truncate (thank you
8367 * very much snapshotting).
8368 *
8369 * And we need these to all be seperate. The fact is we can use alot of
8370 * space doing the truncate, and we have no earthly idea how much space
8371 * we will use, so we need the truncate reservation to be seperate so it
8372 * doesn't end up using space reserved for updating the inode or
8373 * removing the orphan item. We also need to be able to stop the
8374 * transaction and start a new one, which means we need to be able to
8375 * update the inode several times, and we have no idea of knowing how
8376 * many times that will be, so we can't just reserve 1 item for the
8377 * entirety of the opration, so that has to be done seperately as well.
8378 * Then there is the orphan item, which does indeed need to be held on
8379 * to for the whole operation, and we need nobody to touch this reserved
8380 * space except the orphan code.
8381 *
8382 * So that leaves us with
8383 *
8384 * 1) root->orphan_block_rsv - for the orphan deletion.
8385 * 2) rsv - for the truncate reservation, which we will steal from the
8386 * transaction reservation.
8387 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8388 * updating the inode.
8389 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008390 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008391 if (!rsv)
8392 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008393 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008394 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008395
Josef Bacik907cbce2011-08-08 13:46:15 -04008396 /*
Josef Bacik07127182011-08-19 10:29:59 -04008397 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008398 * 1 for updating the inode.
8399 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008400 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008401 if (IS_ERR(trans)) {
8402 err = PTR_ERR(trans);
8403 goto out;
8404 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008405
Josef Bacik907cbce2011-08-08 13:46:15 -04008406 /* Migrate the slack space for the truncate to our reserve */
8407 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8408 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008409 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008410
Chris Mason5a3f23d2009-03-31 13:27:11 -04008411 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008412 * So if we truncate and then write and fsync we normally would just
8413 * write the extents that changed, which is a problem if we need to
8414 * first truncate that entire inode. So set this flag so we write out
8415 * all of the extents in the inode to the sync log so we're completely
8416 * safe.
8417 */
8418 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008419 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008420
Yan, Zheng80825102009-11-12 09:35:36 +00008421 while (1) {
8422 ret = btrfs_truncate_inode_items(trans, root, inode,
8423 inode->i_size,
8424 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008425 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05008426 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008427 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008428 }
Chris Mason39279cc2007-06-12 06:35:45 -04008429
Josef Bacikfcb80c22011-05-03 10:40:22 -04008430 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008431 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008432 if (ret) {
8433 err = ret;
8434 break;
8435 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008436
Yan, Zheng80825102009-11-12 09:35:36 +00008437 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008438 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008439
8440 trans = btrfs_start_transaction(root, 2);
8441 if (IS_ERR(trans)) {
8442 ret = err = PTR_ERR(trans);
8443 trans = NULL;
8444 break;
8445 }
8446
8447 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8448 rsv, min_size);
8449 BUG_ON(ret); /* shouldn't happen */
8450 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008451 }
8452
8453 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008454 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008455 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008456 if (ret)
8457 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008458 }
8459
Chris Mason917c16b2011-11-08 14:49:59 -05008460 if (trans) {
8461 trans->block_rsv = &root->fs_info->trans_block_rsv;
8462 ret = btrfs_update_inode(trans, root, inode);
8463 if (ret && !err)
8464 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008465
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008466 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008467 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008468 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008469
8470out:
8471 btrfs_free_block_rsv(root, rsv);
8472
Josef Bacik3893e332011-01-31 16:03:11 -05008473 if (ret && !err)
8474 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008475
Josef Bacik3893e332011-01-31 16:03:11 -05008476 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008477}
8478
Sven Wegener3b963622008-06-09 21:57:42 -04008479/*
Chris Masond352ac62008-09-29 15:18:18 -04008480 * create a new subvolume directory/inode (helper for the ioctl).
8481 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008482int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008483 struct btrfs_root *new_root,
8484 struct btrfs_root *parent_root,
8485 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008486{
Chris Mason39279cc2007-06-12 06:35:45 -04008487 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008488 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008489 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008490
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008491 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8492 new_dirid, new_dirid,
8493 S_IFDIR | (~current_umask() & S_IRWXUGO),
8494 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008495 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008496 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008497 inode->i_op = &btrfs_dir_inode_operations;
8498 inode->i_fop = &btrfs_dir_file_operations;
8499
Miklos Szeredibfe86842011-10-28 14:13:29 +02008500 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008501 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008502 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008503
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008504 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8505 if (err)
8506 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008507 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008508 new_root->root_key.objectid, err);
8509
Yan, Zheng76dda932009-09-21 16:00:26 -04008510 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008511
Yan, Zheng76dda932009-09-21 16:00:26 -04008512 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008513 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008514}
8515
Chris Mason39279cc2007-06-12 06:35:45 -04008516struct inode *btrfs_alloc_inode(struct super_block *sb)
8517{
8518 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008519 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008520
8521 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8522 if (!ei)
8523 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008524
8525 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008526 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008527 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008528 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008529 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008530 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008531 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008532 ei->disk_i_size = 0;
8533 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008534 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008535 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008536 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008537 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008538 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008539
Josef Bacik9e0baf62011-07-15 15:16:44 +00008540 spin_lock_init(&ei->lock);
8541 ei->outstanding_extents = 0;
8542 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008543
Josef Bacik72ac3c02012-05-23 14:13:11 -04008544 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008545 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008546
Miao Xie16cdcec2011-04-22 18:12:22 +08008547 ei->delayed_node = NULL;
8548
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008549 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008550 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008551 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8552 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008553 ei->io_tree.track_uptodate = 1;
8554 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008555 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008556 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008557 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008558 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008559 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008560 RB_CLEAR_NODE(&ei->rb_node);
8561
8562 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008563}
8564
Josef Bacikaaedb552013-10-11 14:44:09 -04008565#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8566void btrfs_test_destroy_inode(struct inode *inode)
8567{
8568 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8569 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8570}
8571#endif
8572
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008573static void btrfs_i_callback(struct rcu_head *head)
8574{
8575 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008576 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8577}
8578
Chris Mason39279cc2007-06-12 06:35:45 -04008579void btrfs_destroy_inode(struct inode *inode)
8580{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008581 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008582 struct btrfs_root *root = BTRFS_I(inode)->root;
8583
Al Virob3d9b7a2012-06-09 13:51:19 -04008584 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008585 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008586 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8587 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008588 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8589 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008590 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008591
Chris Mason5a3f23d2009-03-31 13:27:11 -04008592 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008593 * This can happen where we create an inode, but somebody else also
8594 * created the same inode and we need to destroy the one we already
8595 * created.
8596 */
8597 if (!root)
8598 goto free;
8599
Josef Bacik8a35d952012-05-23 14:26:42 -04008600 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8601 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008602 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008603 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008604 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008605 }
Josef Bacik7b128762008-07-24 12:17:14 -04008606
Chris Masond3977122009-01-05 21:25:51 -05008607 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008608 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8609 if (!ordered)
8610 break;
8611 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008612 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008613 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008614 btrfs_remove_ordered_extent(inode, ordered);
8615 btrfs_put_ordered_extent(ordered);
8616 btrfs_put_ordered_extent(ordered);
8617 }
8618 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008619 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008620 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008621free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008622 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008623}
8624
Al Viro45321ac2010-06-07 13:43:19 -04008625int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008626{
8627 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008628
Naohiro Aota6379ef92013-06-06 09:56:34 +00008629 if (root == NULL)
8630 return 1;
8631
Liu Bofa6ac872013-02-20 14:10:23 +00008632 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008633 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008634 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008635 else
Al Viro45321ac2010-06-07 13:43:19 -04008636 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008637}
8638
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008639static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008640{
8641 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8642
8643 inode_init_once(&ei->vfs_inode);
8644}
8645
8646void btrfs_destroy_cachep(void)
8647{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008648 /*
8649 * Make sure all delayed rcu free inodes are flushed before we
8650 * destroy cache.
8651 */
8652 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008653 if (btrfs_inode_cachep)
8654 kmem_cache_destroy(btrfs_inode_cachep);
8655 if (btrfs_trans_handle_cachep)
8656 kmem_cache_destroy(btrfs_trans_handle_cachep);
8657 if (btrfs_transaction_cachep)
8658 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008659 if (btrfs_path_cachep)
8660 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008661 if (btrfs_free_space_cachep)
8662 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008663 if (btrfs_delalloc_work_cachep)
8664 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008665}
8666
8667int btrfs_init_cachep(void)
8668{
David Sterba837e1972012-09-07 03:00:48 -06008669 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008670 sizeof(struct btrfs_inode), 0,
8671 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008672 if (!btrfs_inode_cachep)
8673 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008674
David Sterba837e1972012-09-07 03:00:48 -06008675 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008676 sizeof(struct btrfs_trans_handle), 0,
8677 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008678 if (!btrfs_trans_handle_cachep)
8679 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008680
David Sterba837e1972012-09-07 03:00:48 -06008681 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008682 sizeof(struct btrfs_transaction), 0,
8683 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008684 if (!btrfs_transaction_cachep)
8685 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008686
David Sterba837e1972012-09-07 03:00:48 -06008687 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008688 sizeof(struct btrfs_path), 0,
8689 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008690 if (!btrfs_path_cachep)
8691 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008692
David Sterba837e1972012-09-07 03:00:48 -06008693 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008694 sizeof(struct btrfs_free_space), 0,
8695 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8696 if (!btrfs_free_space_cachep)
8697 goto fail;
8698
Miao Xie8ccf6f192012-10-25 09:28:04 +00008699 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8700 sizeof(struct btrfs_delalloc_work), 0,
8701 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8702 NULL);
8703 if (!btrfs_delalloc_work_cachep)
8704 goto fail;
8705
Chris Mason39279cc2007-06-12 06:35:45 -04008706 return 0;
8707fail:
8708 btrfs_destroy_cachep();
8709 return -ENOMEM;
8710}
8711
8712static int btrfs_getattr(struct vfsmount *mnt,
8713 struct dentry *dentry, struct kstat *stat)
8714{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008715 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008716 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008717 u32 blocksize = inode->i_sb->s_blocksize;
8718
Chris Mason39279cc2007-06-12 06:35:45 -04008719 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008720 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008721 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008722
8723 spin_lock(&BTRFS_I(inode)->lock);
8724 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8725 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008726 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008727 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008728 return 0;
8729}
8730
Chris Masond3977122009-01-05 21:25:51 -05008731static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8732 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008733{
8734 struct btrfs_trans_handle *trans;
8735 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008736 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008737 struct inode *new_inode = new_dentry->d_inode;
8738 struct inode *old_inode = old_dentry->d_inode;
8739 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008740 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008741 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008742 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008743 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008744
Li Zefan33345d012011-04-20 10:31:50 +08008745 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008746 return -EPERM;
8747
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008748 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008749 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008750 return -EXDEV;
8751
Li Zefan33345d012011-04-20 10:31:50 +08008752 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8753 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008754 return -ENOTEMPTY;
8755
Chris Mason39279cc2007-06-12 06:35:45 -04008756 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008757 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008758 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008759
8760
8761 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008762 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008763 new_dentry->d_name.name,
8764 new_dentry->d_name.len);
8765
8766 if (ret) {
8767 if (ret == -EEXIST) {
8768 /* we shouldn't get
8769 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308770 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008771 return ret;
8772 }
8773 } else {
8774 /* maybe -EOVERFLOW */
8775 return ret;
8776 }
8777 }
8778 ret = 0;
8779
Chris Mason5a3f23d2009-03-31 13:27:11 -04008780 /*
Chris Mason8d875f92014-08-12 10:47:42 -07008781 * we're using rename to replace one file with another. Start IO on it
8782 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04008783 */
Chris Mason8d875f92014-08-12 10:47:42 -07008784 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04008785 filemap_flush(old_inode->i_mapping);
8786
Yan, Zheng76dda932009-09-21 16:00:26 -04008787 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008788 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008789 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008790 /*
8791 * We want to reserve the absolute worst case amount of items. So if
8792 * both inodes are subvols and we need to unlink them then that would
8793 * require 4 item modifications, but if they are both normal inodes it
8794 * would require 5 item modifications, so we'll assume their normal
8795 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8796 * should cover the worst case number of items we'll modify.
8797 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008798 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008799 if (IS_ERR(trans)) {
8800 ret = PTR_ERR(trans);
8801 goto out_notrans;
8802 }
Chris Mason5f39d392007-10-15 16:14:19 -04008803
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008804 if (dest != root)
8805 btrfs_record_root_in_trans(trans, dest);
8806
Yan, Zhenga5719522009-09-24 09:17:31 -04008807 ret = btrfs_set_inode_index(new_dir, &index);
8808 if (ret)
8809 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008810
Miao Xie67de1172013-12-26 13:07:06 +08008811 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008812 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008813 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08008814 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008815 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008816 ret = btrfs_insert_inode_ref(trans, dest,
8817 new_dentry->d_name.name,
8818 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008819 old_ino,
8820 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008821 if (ret)
8822 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008823 /*
8824 * this is an ugly little race, but the rename is required
8825 * to make sure that if we crash, the inode is either at the
8826 * old name or the new one. pinning the log transaction lets
8827 * us make sure we don't allow a log commit to come in after
8828 * we unlink the name but before we add the new name back in.
8829 */
8830 btrfs_pin_log_trans(root);
8831 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04008832
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008833 inode_inc_iversion(old_dir);
8834 inode_inc_iversion(new_dir);
8835 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008836 old_dir->i_ctime = old_dir->i_mtime = ctime;
8837 new_dir->i_ctime = new_dir->i_mtime = ctime;
8838 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008839
Chris Mason12fcfd22009-03-24 10:24:20 -04008840 if (old_dentry->d_parent != new_dentry->d_parent)
8841 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8842
Li Zefan33345d012011-04-20 10:31:50 +08008843 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008844 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8845 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8846 old_dentry->d_name.name,
8847 old_dentry->d_name.len);
8848 } else {
Al Viro92986792011-03-04 17:14:37 +00008849 ret = __btrfs_unlink_inode(trans, root, old_dir,
8850 old_dentry->d_inode,
8851 old_dentry->d_name.name,
8852 old_dentry->d_name.len);
8853 if (!ret)
8854 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008855 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008856 if (ret) {
8857 btrfs_abort_transaction(trans, root, ret);
8858 goto out_fail;
8859 }
Chris Mason39279cc2007-06-12 06:35:45 -04008860
8861 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008862 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008863 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008864 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008865 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8866 root_objectid = BTRFS_I(new_inode)->location.objectid;
8867 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8868 root_objectid,
8869 new_dentry->d_name.name,
8870 new_dentry->d_name.len);
8871 BUG_ON(new_inode->i_nlink == 0);
8872 } else {
8873 ret = btrfs_unlink_inode(trans, dest, new_dir,
8874 new_dentry->d_inode,
8875 new_dentry->d_name.name,
8876 new_dentry->d_name.len);
8877 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008878 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008879 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008880 if (ret) {
8881 btrfs_abort_transaction(trans, root, ret);
8882 goto out_fail;
8883 }
Chris Mason39279cc2007-06-12 06:35:45 -04008884 }
Josef Bacikaec74772008-07-24 12:12:38 -04008885
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008886 ret = btrfs_add_link(trans, new_dir, old_inode,
8887 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008888 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008889 if (ret) {
8890 btrfs_abort_transaction(trans, root, ret);
8891 goto out_fail;
8892 }
Chris Mason39279cc2007-06-12 06:35:45 -04008893
Miao Xie67de1172013-12-26 13:07:06 +08008894 if (old_inode->i_nlink == 1)
8895 BTRFS_I(old_inode)->dir_index = index;
8896
Li Zefan33345d012011-04-20 10:31:50 +08008897 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008898 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008899 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008900 btrfs_end_log_trans(root);
8901 }
Chris Mason39279cc2007-06-12 06:35:45 -04008902out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008903 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008904out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008905 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008906 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008907
Chris Mason39279cc2007-06-12 06:35:45 -04008908 return ret;
8909}
8910
Miklos Szeredi80ace852014-07-23 15:15:32 +02008911static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
8912 struct inode *new_dir, struct dentry *new_dentry,
8913 unsigned int flags)
8914{
8915 if (flags & ~RENAME_NOREPLACE)
8916 return -EINVAL;
8917
8918 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
8919}
8920
Miao Xie8ccf6f192012-10-25 09:28:04 +00008921static void btrfs_run_delalloc_work(struct btrfs_work *work)
8922{
8923 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008924 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008925
8926 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8927 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008928 inode = delalloc_work->inode;
8929 if (delalloc_work->wait) {
8930 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8931 } else {
8932 filemap_flush(inode->i_mapping);
8933 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8934 &BTRFS_I(inode)->runtime_flags))
8935 filemap_flush(inode->i_mapping);
8936 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008937
8938 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008939 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008940 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008941 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008942 complete(&delalloc_work->completion);
8943}
8944
8945struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8946 int wait, int delay_iput)
8947{
8948 struct btrfs_delalloc_work *work;
8949
8950 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8951 if (!work)
8952 return NULL;
8953
8954 init_completion(&work->completion);
8955 INIT_LIST_HEAD(&work->list);
8956 work->inode = inode;
8957 work->wait = wait;
8958 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08008959 WARN_ON_ONCE(!inode);
8960 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
8961 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008962
8963 return work;
8964}
8965
8966void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8967{
8968 wait_for_completion(&work->completion);
8969 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8970}
8971
Chris Masond352ac62008-09-29 15:18:18 -04008972/*
8973 * some fairly slow code that needs optimization. This walks the list
8974 * of all the inodes with pending delalloc and forces them to disk.
8975 */
Miao Xie6c255e62014-03-06 13:55:01 +08008976static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
8977 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04008978{
Chris Masonea8c2812008-08-04 23:17:27 -04008979 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008980 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008981 struct btrfs_delalloc_work *work, *next;
8982 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008983 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008984 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008985
Miao Xie8ccf6f192012-10-25 09:28:04 +00008986 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008987 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008988
Miao Xie573bfb72014-03-06 13:55:03 +08008989 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008990 spin_lock(&root->delalloc_lock);
8991 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008992 while (!list_empty(&splice)) {
8993 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008994 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008995
Miao Xieeb73c1b2013-05-15 07:48:22 +00008996 list_move_tail(&binode->delalloc_inodes,
8997 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008998 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008999 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009000 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009001 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009002 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009003 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009004
9005 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009006 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009007 if (delay_iput)
9008 btrfs_add_delayed_iput(inode);
9009 else
9010 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009011 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009012 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009013 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009014 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009015 btrfs_queue_work(root->fs_info->flush_workers,
9016 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009017 ret++;
9018 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009019 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009020 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009021 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009022 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009023 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009024
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009025out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009026 list_for_each_entry_safe(work, next, &works, list) {
9027 list_del_init(&work->list);
9028 btrfs_wait_and_free_delalloc_work(work);
9029 }
9030
Miao Xieeb73c1b2013-05-15 07:48:22 +00009031 if (!list_empty_careful(&splice)) {
9032 spin_lock(&root->delalloc_lock);
9033 list_splice_tail(&splice, &root->delalloc_inodes);
9034 spin_unlock(&root->delalloc_lock);
9035 }
Miao Xie573bfb72014-03-06 13:55:03 +08009036 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009037 return ret;
9038}
9039
9040int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9041{
9042 int ret;
9043
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009044 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009045 return -EROFS;
9046
Miao Xie6c255e62014-03-06 13:55:01 +08009047 ret = __start_delalloc_inodes(root, delay_iput, -1);
9048 if (ret > 0)
9049 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009050 /*
9051 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009052 * we have to make sure the IO is actually started and that
9053 * ordered extents get created before we return
9054 */
9055 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009056 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009057 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009058 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009059 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9060 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009061 }
9062 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009063 return ret;
9064}
9065
Miao Xie6c255e62014-03-06 13:55:01 +08009066int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9067 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009068{
9069 struct btrfs_root *root;
9070 struct list_head splice;
9071 int ret;
9072
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009073 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009074 return -EROFS;
9075
9076 INIT_LIST_HEAD(&splice);
9077
Miao Xie573bfb72014-03-06 13:55:03 +08009078 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009079 spin_lock(&fs_info->delalloc_root_lock);
9080 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009081 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009082 root = list_first_entry(&splice, struct btrfs_root,
9083 delalloc_root);
9084 root = btrfs_grab_fs_root(root);
9085 BUG_ON(!root);
9086 list_move_tail(&root->delalloc_root,
9087 &fs_info->delalloc_roots);
9088 spin_unlock(&fs_info->delalloc_root_lock);
9089
Miao Xie6c255e62014-03-06 13:55:01 +08009090 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009091 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009092 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009093 goto out;
9094
Miao Xie6c255e62014-03-06 13:55:01 +08009095 if (nr != -1) {
9096 nr -= ret;
9097 WARN_ON(nr < 0);
9098 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009099 spin_lock(&fs_info->delalloc_root_lock);
9100 }
9101 spin_unlock(&fs_info->delalloc_root_lock);
9102
Miao Xie6c255e62014-03-06 13:55:01 +08009103 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009104 atomic_inc(&fs_info->async_submit_draining);
9105 while (atomic_read(&fs_info->nr_async_submits) ||
9106 atomic_read(&fs_info->async_delalloc_pages)) {
9107 wait_event(fs_info->async_submit_wait,
9108 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9109 atomic_read(&fs_info->async_delalloc_pages) == 0));
9110 }
9111 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009112out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009113 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009114 spin_lock(&fs_info->delalloc_root_lock);
9115 list_splice_tail(&splice, &fs_info->delalloc_roots);
9116 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009117 }
Miao Xie573bfb72014-03-06 13:55:03 +08009118 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009119 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009120}
9121
Chris Mason39279cc2007-06-12 06:35:45 -04009122static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9123 const char *symname)
9124{
9125 struct btrfs_trans_handle *trans;
9126 struct btrfs_root *root = BTRFS_I(dir)->root;
9127 struct btrfs_path *path;
9128 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009129 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009130 int err;
9131 int drop_inode = 0;
9132 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309133 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009134 int name_len;
9135 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009136 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009137 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009138 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009139
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009140 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009141 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9142 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009143
Josef Bacik9ed74f22009-09-11 16:12:44 -04009144 /*
9145 * 2 items for inode item and ref
9146 * 2 items for dir items
9147 * 1 item for xattr if selinux is on
9148 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009149 trans = btrfs_start_transaction(root, 5);
9150 if (IS_ERR(trans))
9151 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009152
Li Zefan581bb052011-04-20 10:06:11 +08009153 err = btrfs_find_free_ino(root, &objectid);
9154 if (err)
9155 goto out_unlock;
9156
Josef Bacikaec74772008-07-24 12:12:38 -04009157 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009158 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04009159 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009160 if (IS_ERR(inode)) {
9161 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009162 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009163 }
Chris Mason39279cc2007-06-12 06:35:45 -04009164
Casey Schauflerad19db72011-12-15 10:09:07 -05009165 /*
9166 * If the active LSM wants to access the inode during
9167 * d_instantiate it needs these. Smack checks to see
9168 * if the filesystem supports xattrs by looking at the
9169 * ops vector.
9170 */
9171 inode->i_fop = &btrfs_file_operations;
9172 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009173 inode->i_mapping->a_ops = &btrfs_aops;
9174 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9175 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9176
9177 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9178 if (err)
9179 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009180
Josef Bacika1b075d2010-11-19 20:36:11 +00009181 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009182 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009183 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009184
9185 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009186 if (!path) {
9187 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009188 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009189 }
Li Zefan33345d012011-04-20 10:31:50 +08009190 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009191 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009192 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009193 datasize = btrfs_file_extent_calc_inline_size(name_len);
9194 err = btrfs_insert_empty_item(trans, root, path, &key,
9195 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009196 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009197 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009198 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009199 }
Chris Mason5f39d392007-10-15 16:14:19 -04009200 leaf = path->nodes[0];
9201 ei = btrfs_item_ptr(leaf, path->slots[0],
9202 struct btrfs_file_extent_item);
9203 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9204 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009205 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009206 btrfs_set_file_extent_encryption(leaf, ei, 0);
9207 btrfs_set_file_extent_compression(leaf, ei, 0);
9208 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9209 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9210
Chris Mason39279cc2007-06-12 06:35:45 -04009211 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009212 write_extent_buffer(leaf, symname, ptr, name_len);
9213 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009214 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009215
Chris Mason39279cc2007-06-12 06:35:45 -04009216 inode->i_op = &btrfs_symlink_inode_operations;
9217 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04009218 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04009219 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009220 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009221 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009222 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009223 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009224 goto out_unlock_inode;
9225 }
9226
9227 unlock_new_inode(inode);
9228 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009229
9230out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009231 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009232 if (drop_inode) {
9233 inode_dec_link_count(inode);
9234 iput(inode);
9235 }
Liu Bob53d3f52012-11-14 14:34:34 +00009236 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009237 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009238
9239out_unlock_inode:
9240 drop_inode = 1;
9241 unlock_new_inode(inode);
9242 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009243}
Chris Mason16432982008-04-10 10:23:21 -04009244
Josef Bacik0af3d002010-06-21 14:48:16 -04009245static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9246 u64 start, u64 num_bytes, u64 min_size,
9247 loff_t actual_len, u64 *alloc_hint,
9248 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009249{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009250 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9251 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009252 struct btrfs_root *root = BTRFS_I(inode)->root;
9253 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009254 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009255 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009256 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009257 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009258 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009259
Josef Bacik0af3d002010-06-21 14:48:16 -04009260 if (trans)
9261 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009262 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009263 if (own_trans) {
9264 trans = btrfs_start_transaction(root, 3);
9265 if (IS_ERR(trans)) {
9266 ret = PTR_ERR(trans);
9267 break;
9268 }
Yan Zhengd899e052008-10-30 14:25:28 -04009269 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009270
Chris Mason154ea282013-03-05 11:11:26 -05009271 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9272 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009273 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009274 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009275 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009276 if (own_trans)
9277 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009278 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009279 }
9280
Yan Zhengd899e052008-10-30 14:25:28 -04009281 ret = insert_reserved_file_extent(trans, inode,
9282 cur_offset, ins.objectid,
9283 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009284 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009285 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009286 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009287 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009288 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009289 btrfs_abort_transaction(trans, root, ret);
9290 if (own_trans)
9291 btrfs_end_transaction(trans, root);
9292 break;
9293 }
Chris Masona1ed8352009-09-11 12:27:37 -04009294 btrfs_drop_extent_cache(inode, cur_offset,
9295 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009296
Josef Bacik5dc562c2012-08-17 13:14:17 -04009297 em = alloc_extent_map();
9298 if (!em) {
9299 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9300 &BTRFS_I(inode)->runtime_flags);
9301 goto next;
9302 }
9303
9304 em->start = cur_offset;
9305 em->orig_start = cur_offset;
9306 em->len = ins.offset;
9307 em->block_start = ins.objectid;
9308 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009309 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009310 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009311 em->bdev = root->fs_info->fs_devices->latest_bdev;
9312 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9313 em->generation = trans->transid;
9314
9315 while (1) {
9316 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009317 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009318 write_unlock(&em_tree->lock);
9319 if (ret != -EEXIST)
9320 break;
9321 btrfs_drop_extent_cache(inode, cur_offset,
9322 cur_offset + ins.offset - 1,
9323 0);
9324 }
9325 free_extent_map(em);
9326next:
Yan Zhengd899e052008-10-30 14:25:28 -04009327 num_bytes -= ins.offset;
9328 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009329 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009330
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009331 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009332 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009333 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009334 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009335 (actual_len > inode->i_size) &&
9336 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009337 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009338 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009339 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009340 i_size = cur_offset;
9341 i_size_write(inode, i_size);
9342 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009343 }
9344
Yan Zhengd899e052008-10-30 14:25:28 -04009345 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009346
9347 if (ret) {
9348 btrfs_abort_transaction(trans, root, ret);
9349 if (own_trans)
9350 btrfs_end_transaction(trans, root);
9351 break;
9352 }
Yan Zhengd899e052008-10-30 14:25:28 -04009353
Josef Bacik0af3d002010-06-21 14:48:16 -04009354 if (own_trans)
9355 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009356 }
Yan Zhengd899e052008-10-30 14:25:28 -04009357 return ret;
9358}
9359
Josef Bacik0af3d002010-06-21 14:48:16 -04009360int btrfs_prealloc_file_range(struct inode *inode, int mode,
9361 u64 start, u64 num_bytes, u64 min_size,
9362 loff_t actual_len, u64 *alloc_hint)
9363{
9364 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9365 min_size, actual_len, alloc_hint,
9366 NULL);
9367}
9368
9369int btrfs_prealloc_file_range_trans(struct inode *inode,
9370 struct btrfs_trans_handle *trans, 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, trans);
9376}
9377
Chris Masone6dcd2d2008-07-17 12:53:50 -04009378static int btrfs_set_page_dirty(struct page *page)
9379{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009380 return __set_page_dirty_nobuffers(page);
9381}
9382
Al Viro10556cb2011-06-20 19:28:19 -04009383static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009384{
Li Zefanb83cc962010-12-20 16:04:08 +08009385 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009386 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009387
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009388 if (mask & MAY_WRITE &&
9389 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9390 if (btrfs_root_readonly(root))
9391 return -EROFS;
9392 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9393 return -EACCES;
9394 }
Al Viro2830ba72011-06-20 19:16:29 -04009395 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009396}
Chris Mason39279cc2007-06-12 06:35:45 -04009397
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009398static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9399{
9400 struct btrfs_trans_handle *trans;
9401 struct btrfs_root *root = BTRFS_I(dir)->root;
9402 struct inode *inode = NULL;
9403 u64 objectid;
9404 u64 index;
9405 int ret = 0;
9406
9407 /*
9408 * 5 units required for adding orphan entry
9409 */
9410 trans = btrfs_start_transaction(root, 5);
9411 if (IS_ERR(trans))
9412 return PTR_ERR(trans);
9413
9414 ret = btrfs_find_free_ino(root, &objectid);
9415 if (ret)
9416 goto out;
9417
9418 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9419 btrfs_ino(dir), objectid, mode, &index);
9420 if (IS_ERR(inode)) {
9421 ret = PTR_ERR(inode);
9422 inode = NULL;
9423 goto out;
9424 }
9425
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009426 inode->i_fop = &btrfs_file_operations;
9427 inode->i_op = &btrfs_file_inode_operations;
9428
9429 inode->i_mapping->a_ops = &btrfs_aops;
9430 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9431 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9432
Chris Masonb0d5d102014-09-08 13:08:51 -07009433 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9434 if (ret)
9435 goto out_inode;
9436
9437 ret = btrfs_update_inode(trans, root, inode);
9438 if (ret)
9439 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009440 ret = btrfs_orphan_add(trans, inode);
9441 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009442 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009443
Filipe Manana5762b5c2014-08-01 00:10:32 +01009444 /*
9445 * We set number of links to 0 in btrfs_new_inode(), and here we set
9446 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9447 * through:
9448 *
9449 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9450 */
9451 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009452 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009453 d_tmpfile(dentry, inode);
9454 mark_inode_dirty(inode);
9455
9456out:
9457 btrfs_end_transaction(trans, root);
9458 if (ret)
9459 iput(inode);
9460 btrfs_balance_delayed_items(root);
9461 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009462 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009463
9464out_inode:
9465 unlock_new_inode(inode);
9466 goto out;
9467
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009468}
9469
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009470static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009471 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009472 .lookup = btrfs_lookup,
9473 .create = btrfs_create,
9474 .unlink = btrfs_unlink,
9475 .link = btrfs_link,
9476 .mkdir = btrfs_mkdir,
9477 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009478 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009479 .symlink = btrfs_symlink,
9480 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009481 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009482 .setxattr = btrfs_setxattr,
9483 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009484 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009485 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009486 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009487 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009488 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009489 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009490 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009491};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009492static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009493 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009494 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009495 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009496 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009497 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009498};
Yan, Zheng76dda932009-09-21 16:00:26 -04009499
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009500static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009501 .llseek = generic_file_llseek,
9502 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009503 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009504 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009505#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009506 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009507#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009508 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009509 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009510};
9511
Chris Masond1310b22008-01-24 16:13:08 -05009512static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009513 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009514 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009515 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009516 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009517 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009518 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009519 .set_bit_hook = btrfs_set_bit_hook,
9520 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009521 .merge_extent_hook = btrfs_merge_extent_hook,
9522 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009523};
9524
Chris Mason35054392009-01-21 13:11:13 -05009525/*
9526 * btrfs doesn't support the bmap operation because swapfiles
9527 * use bmap to make a mapping of extents in the file. They assume
9528 * these extents won't change over the life of the file and they
9529 * use the bmap result to do IO directly to the drive.
9530 *
9531 * the btrfs bmap call would return logical addresses that aren't
9532 * suitable for IO and they also will change frequently as COW
9533 * operations happen. So, swapfile + btrfs == corruption.
9534 *
9535 * For now we're avoiding this by dropping bmap.
9536 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009537static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009538 .readpage = btrfs_readpage,
9539 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04009540 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009541 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009542 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009543 .invalidatepage = btrfs_invalidatepage,
9544 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009545 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009546 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009547};
9548
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009549static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009550 .readpage = btrfs_readpage,
9551 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009552 .invalidatepage = btrfs_invalidatepage,
9553 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009554};
9555
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009556static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009557 .getattr = btrfs_getattr,
9558 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009559 .setxattr = btrfs_setxattr,
9560 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009561 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009562 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009563 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009564 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009565 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009566 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009567 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009568};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009569static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009570 .getattr = btrfs_getattr,
9571 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009572 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009573 .setxattr = btrfs_setxattr,
9574 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009575 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009576 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009577 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009578 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009579 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009580};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009581static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009582 .readlink = generic_readlink,
9583 .follow_link = page_follow_link_light,
9584 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009585 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009586 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009587 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009588 .setxattr = btrfs_setxattr,
9589 .getxattr = btrfs_getxattr,
9590 .listxattr = btrfs_listxattr,
9591 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009592 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009593};
Yan, Zheng76dda932009-09-21 16:00:26 -04009594
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009595const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009596 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009597 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009598};