blob: a9f69a0d4b08ba88f11736d1f8052a5aed2a555b [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>
Guenter Roeck4a3d1ca2015-03-13 01:58:46 -070046#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040052#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040053#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040054#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020055#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040056#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050057#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050058#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080059#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000060#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040061#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000062#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080063#include "qgroup.h"
Chris Mason39279cc2007-06-12 06:35:45 -040064
65struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080066 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040067 struct btrfs_root *root;
68};
69
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070070static const struct inode_operations btrfs_dir_inode_operations;
71static const struct inode_operations btrfs_symlink_inode_operations;
72static const struct inode_operations btrfs_dir_ro_inode_operations;
73static const struct inode_operations btrfs_special_inode_operations;
74static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070075static const struct address_space_operations btrfs_aops;
76static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070077static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050078static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040079
80static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000081static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040082struct kmem_cache *btrfs_trans_handle_cachep;
83struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040084struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050085struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87#define S_SHIFT 12
88static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
89 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
90 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
91 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
92 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
93 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
94 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
95 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
96};
97
Eric Sandeen3972f262013-01-12 02:57:22 +000098static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050099static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400100static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500101static noinline int cow_file_range(struct inode *inode,
102 struct page *locked_page,
103 u64 start, u64 end, int *page_started,
104 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400105static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
106 u64 len, u64 orig_start,
107 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400108 u64 orig_block_len, u64 ram_bytes,
109 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400110
Eric Sandeen48a3b632013-04-25 20:41:01 +0000111static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400112
Josef Bacik6a3891c2015-03-16 17:38:52 -0400113#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
114void btrfs_test_inode_set_ops(struct inode *inode)
115{
116 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
117}
118#endif
119
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000120static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500121 struct inode *inode, struct inode *dir,
122 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500123{
124 int err;
125
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000126 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500127 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500128 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500129 return err;
130}
131
Chris Masond352ac62008-09-29 15:18:18 -0400132/*
Chris Masonc8b97812008-10-29 14:49:59 -0400133 * this does all the hard work for inserting an inline extent into
134 * the btree. The caller should have done a btrfs_drop_extents so that
135 * no overlapping inline items exist in the btree
136 */
Chris Mason40f76582014-05-21 13:35:51 -0700137static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000138 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400139 struct btrfs_root *root, struct inode *inode,
140 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000141 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400142 struct page **compressed_pages)
143{
Chris Masonc8b97812008-10-29 14:49:59 -0400144 struct extent_buffer *leaf;
145 struct page *page = NULL;
146 char *kaddr;
147 unsigned long ptr;
148 struct btrfs_file_extent_item *ei;
149 int err = 0;
150 int ret;
151 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400152 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400153
Li Zefanfe3f5662011-03-28 08:30:38 +0000154 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400155 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400156
Chris Masonc8b97812008-10-29 14:49:59 -0400157 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000158
159 if (!extent_inserted) {
160 struct btrfs_key key;
161 size_t datasize;
162
163 key.objectid = btrfs_ino(inode);
164 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200165 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000166
167 datasize = btrfs_file_extent_calc_inline_size(cur_size);
168 path->leave_spinning = 1;
169 ret = btrfs_insert_empty_item(trans, root, path, &key,
170 datasize);
171 if (ret) {
172 err = ret;
173 goto fail;
174 }
Chris Masonc8b97812008-10-29 14:49:59 -0400175 }
176 leaf = path->nodes[0];
177 ei = btrfs_item_ptr(leaf, path->slots[0],
178 struct btrfs_file_extent_item);
179 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
180 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
181 btrfs_set_file_extent_encryption(leaf, ei, 0);
182 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
183 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
184 ptr = btrfs_file_extent_inline_start(ei);
185
Li Zefan261507a02010-12-17 14:21:50 +0800186 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400187 struct page *cpage;
188 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500189 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400190 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500191 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400192 PAGE_CACHE_SIZE);
193
Cong Wang7ac687d2011-11-25 23:14:28 +0800194 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400195 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800196 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400197
198 i++;
199 ptr += cur_size;
200 compressed_size -= cur_size;
201 }
202 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800203 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400204 } else {
205 page = find_get_page(inode->i_mapping,
206 start >> PAGE_CACHE_SHIFT);
207 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800208 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400209 offset = start & (PAGE_CACHE_SIZE - 1);
210 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800211 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400212 page_cache_release(page);
213 }
214 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000215 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400216
Yan, Zhengc2167752009-11-12 09:34:21 +0000217 /*
218 * we're an inline extent, so nobody can
219 * extend the file past i_size without locking
220 * a page we already have locked.
221 *
222 * We must do any isize and inode updates
223 * before we unlock the pages. Otherwise we
224 * could end up racing with unlink.
225 */
Chris Masonc8b97812008-10-29 14:49:59 -0400226 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100227 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000228
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100229 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400230fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400231 return err;
232}
233
234
235/*
236 * conditionally insert an inline extent into the file. This
237 * does the checks required to make sure the data is small enough
238 * to fit as an inline extent.
239 */
Josef Bacik00361582013-08-14 14:02:47 -0400240static noinline int cow_file_range_inline(struct btrfs_root *root,
241 struct inode *inode, u64 start,
242 u64 end, size_t compressed_size,
243 int compress_type,
244 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400245{
Josef Bacik00361582013-08-14 14:02:47 -0400246 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400247 u64 isize = i_size_read(inode);
248 u64 actual_end = min(end + 1, isize);
249 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000250 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400251 u64 data_len = inline_len;
252 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000253 struct btrfs_path *path;
254 int extent_inserted = 0;
255 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400256
257 if (compressed_size)
258 data_len = compressed_size;
259
260 if (start > 0 ||
Wang Shilong354877b2014-07-17 11:44:11 +0800261 actual_end > PAGE_CACHE_SIZE ||
262 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400263 (!compressed_size &&
264 (actual_end & (root->sectorsize - 1)) == 0) ||
265 end + 1 < isize ||
266 data_len > root->fs_info->max_inline) {
267 return 1;
268 }
269
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000270 path = btrfs_alloc_path();
271 if (!path)
272 return -ENOMEM;
273
Josef Bacik00361582013-08-14 14:02:47 -0400274 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000275 if (IS_ERR(trans)) {
276 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400277 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000278 }
Josef Bacik00361582013-08-14 14:02:47 -0400279 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
280
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000281 if (compressed_size && compressed_pages)
282 extent_item_size = btrfs_file_extent_calc_inline_size(
283 compressed_size);
284 else
285 extent_item_size = btrfs_file_extent_calc_inline_size(
286 inline_len);
287
288 ret = __btrfs_drop_extents(trans, root, inode, path,
289 start, aligned_end, NULL,
290 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400291 if (ret) {
292 btrfs_abort_transaction(trans, root, ret);
293 goto out;
294 }
Chris Masonc8b97812008-10-29 14:49:59 -0400295
296 if (isize > actual_end)
297 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000298 ret = insert_inline_extent(trans, path, extent_inserted,
299 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400300 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000301 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400302 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100303 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400304 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400305 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400306 ret = 1;
307 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100308 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400309
Josef Bacikbdc20e62013-02-28 13:23:38 -0500310 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400311 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400312 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400313out:
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000314 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400315 btrfs_end_transaction(trans, root);
316 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400317}
318
Chris Mason771ed682008-11-06 22:02:51 -0500319struct async_extent {
320 u64 start;
321 u64 ram_size;
322 u64 compressed_size;
323 struct page **pages;
324 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800325 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500326 struct list_head list;
327};
328
329struct async_cow {
330 struct inode *inode;
331 struct btrfs_root *root;
332 struct page *locked_page;
333 u64 start;
334 u64 end;
335 struct list_head extents;
336 struct btrfs_work work;
337};
338
339static noinline int add_async_extent(struct async_cow *cow,
340 u64 start, u64 ram_size,
341 u64 compressed_size,
342 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800343 unsigned long nr_pages,
344 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500345{
346 struct async_extent *async_extent;
347
348 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100349 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500350 async_extent->start = start;
351 async_extent->ram_size = ram_size;
352 async_extent->compressed_size = compressed_size;
353 async_extent->pages = pages;
354 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800355 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500356 list_add_tail(&async_extent->list, &cow->extents);
357 return 0;
358}
359
Wang Shilongf79707b2014-07-17 11:44:09 +0800360static inline int inode_need_compress(struct inode *inode)
361{
362 struct btrfs_root *root = BTRFS_I(inode)->root;
363
364 /* force compress */
365 if (btrfs_test_opt(root, FORCE_COMPRESS))
366 return 1;
367 /* bad compression ratios */
368 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
369 return 0;
370 if (btrfs_test_opt(root, COMPRESS) ||
371 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
372 BTRFS_I(inode)->force_compress)
373 return 1;
374 return 0;
375}
376
Chris Masonc8b97812008-10-29 14:49:59 -0400377/*
Chris Mason771ed682008-11-06 22:02:51 -0500378 * we create compressed extents in two phases. The first
379 * phase compresses a range of pages that have already been
380 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400381 *
Chris Mason771ed682008-11-06 22:02:51 -0500382 * This is done inside an ordered work queue, and the compression
383 * is spread across many cpus. The actual IO submission is step
384 * two, and the ordered work queue takes care of making sure that
385 * happens in the same order things were put onto the queue by
386 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400387 *
Chris Mason771ed682008-11-06 22:02:51 -0500388 * If this code finds it can't get good compression, it puts an
389 * entry onto the work queue to write the uncompressed bytes. This
390 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300391 * are written in the same order that the flusher thread sent them
392 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400393 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100394static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500395 struct page *locked_page,
396 u64 start, u64 end,
397 struct async_cow *async_cow,
398 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400399{
400 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400401 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400402 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400403 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500404 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400405 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400406 struct page **pages = NULL;
407 unsigned long nr_pages;
408 unsigned long nr_pages_ret = 0;
409 unsigned long total_compressed = 0;
410 unsigned long total_in = 0;
411 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500412 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400413 int i;
414 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800415 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400416 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400417
Liu Bo4cb13e52012-03-29 09:57:45 -0400418 /* if this is a small write inside eof, kick off a defrag */
419 if ((end - start + 1) < 16 * 1024 &&
420 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400421 btrfs_add_inode_defrag(NULL, inode);
422
Chris Mason42dc7ba2008-12-15 11:44:56 -0500423 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400424again:
425 will_compress = 0;
426 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
427 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
428
Chris Masonf03d9301f2009-02-04 09:31:06 -0500429 /*
430 * we don't want to send crud past the end of i_size through
431 * compression, that's just a waste of CPU time. So, if the
432 * end of the file is before the start of our current
433 * requested range of bytes, we bail out to the uncompressed
434 * cleanup code that can deal with all of this.
435 *
436 * It isn't really the fastest way to fix things, but this is a
437 * very uncommon corner.
438 */
439 if (actual_end <= start)
440 goto cleanup_and_bail_uncompressed;
441
Chris Masonc8b97812008-10-29 14:49:59 -0400442 total_compressed = actual_end - start;
443
Shilong Wang4bcbb332014-10-07 18:44:35 -0400444 /*
445 * skip compression for a small file range(<=blocksize) that
446 * isn't an inline extent, since it dosen't save disk space at all.
447 */
448 if (total_compressed <= blocksize &&
449 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
450 goto cleanup_and_bail_uncompressed;
451
Chris Masonc8b97812008-10-29 14:49:59 -0400452 /* we want to make sure that amount of ram required to uncompress
453 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500454 * of a compressed extent to 128k. This is a crucial number
455 * because it also controls how easily we can spread reads across
456 * cpus for decompression.
457 *
458 * We also want to make sure the amount of IO required to do
459 * a random read is reasonably small, so we limit the size of
460 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400461 */
462 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000463 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500464 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400465 total_in = 0;
466 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400467
Chris Mason771ed682008-11-06 22:02:51 -0500468 /*
469 * we do compression for mount -o compress and when the
470 * inode has not been flagged as nocompress. This flag can
471 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400472 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800473 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400474 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100475 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800476 if (!pages) {
477 /* just bail out to the uncompressed code */
478 goto cont;
479 }
Chris Mason179e29e2007-11-01 11:28:41 -0400480
Li Zefan261507a02010-12-17 14:21:50 +0800481 if (BTRFS_I(inode)->force_compress)
482 compress_type = BTRFS_I(inode)->force_compress;
483
Chris Mason4adaa612013-03-26 13:07:00 -0400484 /*
485 * we need to call clear_page_dirty_for_io on each
486 * page in the range. Otherwise applications with the file
487 * mmap'd can wander in and change the page contents while
488 * we are compressing them.
489 *
490 * If the compression fails for any reason, we set the pages
491 * dirty again later on.
492 */
493 extent_range_clear_dirty_for_io(inode, start, end);
494 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800495 ret = btrfs_compress_pages(compress_type,
496 inode->i_mapping, start,
497 total_compressed, pages,
498 nr_pages, &nr_pages_ret,
499 &total_in,
500 &total_compressed,
501 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400502
503 if (!ret) {
504 unsigned long offset = total_compressed &
505 (PAGE_CACHE_SIZE - 1);
506 struct page *page = pages[nr_pages_ret - 1];
507 char *kaddr;
508
509 /* zero the tail end of the last page, we might be
510 * sending it down to disk
511 */
512 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800513 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400514 memset(kaddr + offset, 0,
515 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800516 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400517 }
518 will_compress = 1;
519 }
520 }
Li Zefan560f7d72011-09-08 10:22:01 +0800521cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400522 if (start == 0) {
523 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500524 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400525 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500526 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400527 */
Josef Bacik00361582013-08-14 14:02:47 -0400528 ret = cow_file_range_inline(root, inode, start, end,
529 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400530 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500531 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400532 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000533 total_compressed,
534 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400535 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100536 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400537 unsigned long clear_flags = EXTENT_DELALLOC |
538 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100539 unsigned long page_error_op;
540
Josef Bacik151a41b2013-07-29 13:22:24 -0400541 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100542 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400543
Chris Mason771ed682008-11-06 22:02:51 -0500544 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100545 * inline extent creation worked or returned error,
546 * we don't need to create any more async work items.
547 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500548 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400549 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400550 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400551 PAGE_CLEAR_DIRTY |
552 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100553 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400554 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400555 goto free_pages_out;
556 }
557 }
558
559 if (will_compress) {
560 /*
561 * we aren't doing an inline extent round the compressed size
562 * up to a block size boundary so the allocator does sane
563 * things
564 */
Qu Wenruofda28322013-02-26 08:10:22 +0000565 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400566
567 /*
568 * one last check to make sure the compression is really a
569 * win, compare the page count read with the blocks on disk
570 */
Qu Wenruofda28322013-02-26 08:10:22 +0000571 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400572 if (total_compressed >= total_in) {
573 will_compress = 0;
574 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400575 num_bytes = total_in;
576 }
577 }
578 if (!will_compress && pages) {
579 /*
580 * the compression code ran but failed to make things smaller,
581 * free any pages it allocated and our page pointer array
582 */
583 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400584 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400585 page_cache_release(pages[i]);
586 }
587 kfree(pages);
588 pages = NULL;
589 total_compressed = 0;
590 nr_pages_ret = 0;
591
592 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500593 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
594 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500595 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500596 }
Chris Masonc8b97812008-10-29 14:49:59 -0400597 }
Chris Mason771ed682008-11-06 22:02:51 -0500598 if (will_compress) {
599 *num_added += 1;
600
601 /* the async work queues will take care of doing actual
602 * allocation on disk for these compressed pages,
603 * and will submit them to the elevator.
604 */
605 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800606 total_compressed, pages, nr_pages_ret,
607 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500608
Yan, Zheng24ae6362010-12-06 07:02:36 +0000609 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500610 start += num_bytes;
611 pages = NULL;
612 cond_resched();
613 goto again;
614 }
615 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500616cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500617 /*
618 * No compression, but we still need to write the pages in
619 * the file we've been given so far. redirty the locked
620 * page if it corresponds to our extent and set things up
621 * for the async work queue to run cow_file_range to do
622 * the normal delalloc dance
623 */
624 if (page_offset(locked_page) >= start &&
625 page_offset(locked_page) <= end) {
626 __set_page_dirty_nobuffers(locked_page);
627 /* unlocked later on in the async handlers */
628 }
Chris Mason4adaa612013-03-26 13:07:00 -0400629 if (redirty)
630 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800631 add_async_extent(async_cow, start, end - start + 1,
632 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500633 *num_added += 1;
634 }
635
Filipe Mananac44f6492014-10-09 21:15:44 +0100636 return;
Chris Mason771ed682008-11-06 22:02:51 -0500637
638free_pages_out:
639 for (i = 0; i < nr_pages_ret; i++) {
640 WARN_ON(pages[i]->mapping);
641 page_cache_release(pages[i]);
642 }
Chris Masond3977122009-01-05 21:25:51 -0500643 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500644}
Chris Mason771ed682008-11-06 22:02:51 -0500645
Filipe Manana40ae8372014-10-06 22:14:24 +0100646static void free_async_extent_pages(struct async_extent *async_extent)
647{
648 int i;
649
650 if (!async_extent->pages)
651 return;
652
653 for (i = 0; i < async_extent->nr_pages; i++) {
654 WARN_ON(async_extent->pages[i]->mapping);
655 page_cache_release(async_extent->pages[i]);
656 }
657 kfree(async_extent->pages);
658 async_extent->nr_pages = 0;
659 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500660}
661
662/*
663 * phase two of compressed writeback. This is the ordered portion
664 * of the code, which only gets called in the order the work was
665 * queued. We walk all the async extents created by compress_file_range
666 * and send them down to the disk.
667 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100668static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500669 struct async_cow *async_cow)
670{
671 struct async_extent *async_extent;
672 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500673 struct btrfs_key ins;
674 struct extent_map *em;
675 struct btrfs_root *root = BTRFS_I(inode)->root;
676 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
677 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500678 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500679
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500680again:
Chris Masond3977122009-01-05 21:25:51 -0500681 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500682 async_extent = list_entry(async_cow->extents.next,
683 struct async_extent, list);
684 list_del(&async_extent->list);
685
686 io_tree = &BTRFS_I(inode)->io_tree;
687
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500688retry:
Chris Mason771ed682008-11-06 22:02:51 -0500689 /* did the compression code fall back to uncompressed IO? */
690 if (!async_extent->pages) {
691 int page_started = 0;
692 unsigned long nr_written = 0;
693
694 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000695 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100696 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500697
698 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500699 ret = cow_file_range(inode, async_cow->locked_page,
700 async_extent->start,
701 async_extent->start +
702 async_extent->ram_size - 1,
703 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500704
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100705 /* JDM XXX */
706
Chris Mason771ed682008-11-06 22:02:51 -0500707 /*
708 * if page_started, cow_file_range inserted an
709 * inline extent and took care of all the unlocking
710 * and IO for us. Otherwise, we need to submit
711 * all those pages down to the drive.
712 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500713 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500714 extent_write_locked_range(io_tree,
715 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500716 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500717 async_extent->ram_size - 1,
718 btrfs_get_extent,
719 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500720 else if (ret)
721 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500722 kfree(async_extent);
723 cond_resched();
724 continue;
725 }
726
727 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100728 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500729
Josef Bacik00361582013-08-14 14:02:47 -0400730 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500731 async_extent->compressed_size,
732 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800733 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500734 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100735 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500736
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400737 if (ret == -ENOSPC) {
738 unlock_extent(io_tree, async_extent->start,
739 async_extent->start +
740 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800741
742 /*
743 * we need to redirty the pages if we decide to
744 * fallback to uncompressed IO, otherwise we
745 * will not submit these pages down to lower
746 * layers.
747 */
748 extent_range_redirty_for_io(inode,
749 async_extent->start,
750 async_extent->start +
751 async_extent->ram_size - 1);
752
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100753 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400754 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500755 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500756 }
Dongsheng Yang31193212014-12-12 16:44:35 +0800757 btrfs_qgroup_update_reserved_bytes(root->fs_info,
758 root->root_key.objectid,
759 ins.offset, 1);
Yan, Zhengc2167752009-11-12 09:34:21 +0000760 /*
761 * here we're doing allocation and writeback of the
762 * compressed pages
763 */
764 btrfs_drop_extent_cache(inode, async_extent->start,
765 async_extent->start +
766 async_extent->ram_size - 1, 0);
767
David Sterba172ddd62011-04-21 00:48:27 +0200768 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000769 if (!em) {
770 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500771 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000772 }
Chris Mason771ed682008-11-06 22:02:51 -0500773 em->start = async_extent->start;
774 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500775 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400776 em->mod_start = em->start;
777 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500778
779 em->block_start = ins.objectid;
780 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500781 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400782 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500783 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800784 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500785 set_bit(EXTENT_FLAG_PINNED, &em->flags);
786 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400787 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500788
Chris Masond3977122009-01-05 21:25:51 -0500789 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400790 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400791 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400792 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500793 if (ret != -EEXIST) {
794 free_extent_map(em);
795 break;
796 }
797 btrfs_drop_extent_cache(inode, async_extent->start,
798 async_extent->start +
799 async_extent->ram_size - 1, 0);
800 }
801
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500802 if (ret)
803 goto out_free_reserve;
804
Li Zefan261507a02010-12-17 14:21:50 +0800805 ret = btrfs_add_ordered_extent_compress(inode,
806 async_extent->start,
807 ins.objectid,
808 async_extent->ram_size,
809 ins.offset,
810 BTRFS_ORDERED_COMPRESSED,
811 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100812 if (ret) {
813 btrfs_drop_extent_cache(inode, async_extent->start,
814 async_extent->start +
815 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500816 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100817 }
Chris Mason771ed682008-11-06 22:02:51 -0500818
Chris Mason771ed682008-11-06 22:02:51 -0500819 /*
820 * clear dirty, set writeback and unlock the pages.
821 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400822 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400823 async_extent->start +
824 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400825 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
826 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400827 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500828 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500829 async_extent->start,
830 async_extent->ram_size,
831 ins.objectid,
832 ins.offset, async_extent->pages,
833 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100834 if (ret) {
835 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
836 struct page *p = async_extent->pages[0];
837 const u64 start = async_extent->start;
838 const u64 end = start + async_extent->ram_size - 1;
839
840 p->mapping = inode->i_mapping;
841 tree->ops->writepage_end_io_hook(p, start, end,
842 NULL, 0);
843 p->mapping = NULL;
844 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
845 PAGE_END_WRITEBACK |
846 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100847 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100848 }
Chris Mason771ed682008-11-06 22:02:51 -0500849 alloc_hint = ins.objectid + ins.offset;
850 kfree(async_extent);
851 cond_resched();
852 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100853 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500854out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800855 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100856out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400857 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500858 async_extent->start +
859 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400860 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400861 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
862 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100863 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
864 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100865 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100866 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500867 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500868}
869
Josef Bacik4b46fce2010-05-23 11:00:55 -0400870static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
871 u64 num_bytes)
872{
873 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
874 struct extent_map *em;
875 u64 alloc_hint = 0;
876
877 read_lock(&em_tree->lock);
878 em = search_extent_mapping(em_tree, start, num_bytes);
879 if (em) {
880 /*
881 * if block start isn't an actual block number then find the
882 * first block in this inode and use that as a hint. If that
883 * block is also bogus then just don't worry about it.
884 */
885 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
886 free_extent_map(em);
887 em = search_extent_mapping(em_tree, 0, 0);
888 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
889 alloc_hint = em->block_start;
890 if (em)
891 free_extent_map(em);
892 } else {
893 alloc_hint = em->block_start;
894 free_extent_map(em);
895 }
896 }
897 read_unlock(&em_tree->lock);
898
899 return alloc_hint;
900}
901
Chris Mason771ed682008-11-06 22:02:51 -0500902/*
903 * when extent_io.c finds a delayed allocation range in the file,
904 * the call backs end up in this code. The basic idea is to
905 * allocate extents on disk for the range, and create ordered data structs
906 * in ram to track those extents.
907 *
908 * locked_page is the page that writepage had locked already. We use
909 * it to make sure we don't do extra locks or unlocks.
910 *
911 * *page_started is set to one if we unlock locked_page and do everything
912 * required to start IO on it. It may be clean and already done with
913 * IO when we return.
914 */
Josef Bacik00361582013-08-14 14:02:47 -0400915static noinline int cow_file_range(struct inode *inode,
916 struct page *locked_page,
917 u64 start, u64 end, int *page_started,
918 unsigned long *nr_written,
919 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500920{
Josef Bacik00361582013-08-14 14:02:47 -0400921 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500922 u64 alloc_hint = 0;
923 u64 num_bytes;
924 unsigned long ram_size;
925 u64 disk_num_bytes;
926 u64 cur_alloc_size;
927 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500928 struct btrfs_key ins;
929 struct extent_map *em;
930 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
931 int ret = 0;
932
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400933 if (btrfs_is_free_space_inode(inode)) {
934 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500935 ret = -EINVAL;
936 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400937 }
Chris Mason771ed682008-11-06 22:02:51 -0500938
Qu Wenruofda28322013-02-26 08:10:22 +0000939 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500940 num_bytes = max(blocksize, num_bytes);
941 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500942
Chris Mason4cb53002011-05-24 15:35:30 -0400943 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400944 if (num_bytes < 64 * 1024 &&
945 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400946 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400947
Chris Mason771ed682008-11-06 22:02:51 -0500948 if (start == 0) {
949 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400950 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
951 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500952 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400953 extent_clear_unlock_delalloc(inode, start, end, NULL,
954 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400955 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400956 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
957 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000958
Chris Mason771ed682008-11-06 22:02:51 -0500959 *nr_written = *nr_written +
960 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
961 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500962 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100963 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100964 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500965 }
966 }
Chris Masonc8b97812008-10-29 14:49:59 -0400967
968 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200969 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400970
Josef Bacik4b46fce2010-05-23 11:00:55 -0400971 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400972 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400973
Chris Masond3977122009-01-05 21:25:51 -0500974 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400975 unsigned long op;
976
Josef Bacik287a0ab2010-03-19 18:07:23 +0000977 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400978 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500979 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800980 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400981 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100982 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500983
Dongsheng Yang31193212014-12-12 16:44:35 +0800984 btrfs_qgroup_update_reserved_bytes(root->fs_info,
985 root->root_key.objectid,
986 ins.offset, 1);
987
David Sterba172ddd62011-04-21 00:48:27 +0200988 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000989 if (!em) {
990 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000991 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000992 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400993 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500994 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500995 ram_size = ins.offset;
996 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400997 em->mod_start = em->start;
998 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400999
Chris Masone6dcd2d2008-07-17 12:53:50 -04001000 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001001 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001002 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001003 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001004 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001005 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001006 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001007
Chris Masond3977122009-01-05 21:25:51 -05001008 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001009 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001010 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001011 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001012 if (ret != -EEXIST) {
1013 free_extent_map(em);
1014 break;
1015 }
1016 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001017 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001018 }
Liu Boace68ba2013-04-22 10:53:47 +00001019 if (ret)
1020 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001021
Chris Mason98d20f62008-04-14 09:46:10 -04001022 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001023 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001024 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001025 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001026 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001027
Yan Zheng17d217f2008-12-12 10:03:38 -05001028 if (root->root_key.objectid ==
1029 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1030 ret = btrfs_reloc_clone_csums(inode, start,
1031 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001032 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001033 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001034 }
1035
Chris Masond3977122009-01-05 21:25:51 -05001036 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001037 break;
Chris Masond3977122009-01-05 21:25:51 -05001038
Chris Masonc8b97812008-10-29 14:49:59 -04001039 /* we're not doing compressed IO, don't unlock the first
1040 * page (which the caller expects to stay locked), don't
1041 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001042 *
1043 * Do set the Private2 bit so we know this page was properly
1044 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001045 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001046 op = unlock ? PAGE_UNLOCK : 0;
1047 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001048
Josef Bacikc2790a22013-07-29 11:20:47 -04001049 extent_clear_unlock_delalloc(inode, start,
1050 start + ram_size - 1, locked_page,
1051 EXTENT_LOCKED | EXTENT_DELALLOC,
1052 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001053 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001054 num_bytes -= cur_alloc_size;
1055 alloc_hint = ins.objectid + ins.offset;
1056 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -04001057 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001058out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001059 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001060
Filipe Mananad9f85962014-08-25 10:43:00 +01001061out_drop_extent_cache:
1062 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001063out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001064 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001065out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001066 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001067 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1068 EXTENT_DELALLOC | EXTENT_DEFRAG,
1069 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1070 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001071 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001072}
Chris Masonc8b97812008-10-29 14:49:59 -04001073
Chris Mason771ed682008-11-06 22:02:51 -05001074/*
1075 * work queue call back to started compression on a file and pages
1076 */
1077static noinline void async_cow_start(struct btrfs_work *work)
1078{
1079 struct async_cow *async_cow;
1080 int num_added = 0;
1081 async_cow = container_of(work, struct async_cow, work);
1082
1083 compress_file_range(async_cow->inode, async_cow->locked_page,
1084 async_cow->start, async_cow->end, async_cow,
1085 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001086 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001087 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001088 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001089 }
Chris Mason771ed682008-11-06 22:02:51 -05001090}
1091
1092/*
1093 * work queue call back to submit previously compressed pages
1094 */
1095static noinline void async_cow_submit(struct btrfs_work *work)
1096{
1097 struct async_cow *async_cow;
1098 struct btrfs_root *root;
1099 unsigned long nr_pages;
1100
1101 async_cow = container_of(work, struct async_cow, work);
1102
1103 root = async_cow->root;
1104 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1105 PAGE_CACHE_SHIFT;
1106
Josef Bacik66657b32012-08-01 15:36:24 -04001107 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001108 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001109 waitqueue_active(&root->fs_info->async_submit_wait))
1110 wake_up(&root->fs_info->async_submit_wait);
1111
Chris Masond3977122009-01-05 21:25:51 -05001112 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001113 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001114}
Chris Masonc8b97812008-10-29 14:49:59 -04001115
Chris Mason771ed682008-11-06 22:02:51 -05001116static noinline void async_cow_free(struct btrfs_work *work)
1117{
1118 struct async_cow *async_cow;
1119 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001120 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001121 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001122 kfree(async_cow);
1123}
1124
1125static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1126 u64 start, u64 end, int *page_started,
1127 unsigned long *nr_written)
1128{
1129 struct async_cow *async_cow;
1130 struct btrfs_root *root = BTRFS_I(inode)->root;
1131 unsigned long nr_pages;
1132 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001133 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001134
Chris Masona3429ab2009-10-08 12:30:20 -04001135 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1136 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001137 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001138 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001139 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001140 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001141 async_cow->root = root;
1142 async_cow->locked_page = locked_page;
1143 async_cow->start = start;
1144
Wang Shilongf79707b2014-07-17 11:44:09 +08001145 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1146 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001147 cur_end = end;
1148 else
1149 cur_end = min(end, start + 512 * 1024 - 1);
1150
1151 async_cow->end = cur_end;
1152 INIT_LIST_HEAD(&async_cow->extents);
1153
Liu Bo9e0af232014-08-15 23:36:53 +08001154 btrfs_init_work(&async_cow->work,
1155 btrfs_delalloc_helper,
1156 async_cow_start, async_cow_submit,
1157 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001158
Chris Mason771ed682008-11-06 22:02:51 -05001159 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1160 PAGE_CACHE_SHIFT;
1161 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1162
Qu Wenruoafe3d242014-02-28 10:46:07 +08001163 btrfs_queue_work(root->fs_info->delalloc_workers,
1164 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001165
1166 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1167 wait_event(root->fs_info->async_submit_wait,
1168 (atomic_read(&root->fs_info->async_delalloc_pages) <
1169 limit));
1170 }
1171
Chris Masond3977122009-01-05 21:25:51 -05001172 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001173 atomic_read(&root->fs_info->async_delalloc_pages)) {
1174 wait_event(root->fs_info->async_submit_wait,
1175 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1176 0));
1177 }
1178
1179 *nr_written += nr_pages;
1180 start = cur_end + 1;
1181 }
1182 *page_started = 1;
1183 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001184}
1185
Chris Masond3977122009-01-05 21:25:51 -05001186static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001187 u64 bytenr, u64 num_bytes)
1188{
1189 int ret;
1190 struct btrfs_ordered_sum *sums;
1191 LIST_HEAD(list);
1192
Yan Zheng07d400a2009-01-06 11:42:00 -05001193 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001194 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001195 if (ret == 0 && list_empty(&list))
1196 return 0;
1197
1198 while (!list_empty(&list)) {
1199 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1200 list_del(&sums->list);
1201 kfree(sums);
1202 }
1203 return 1;
1204}
1205
Chris Masond352ac62008-09-29 15:18:18 -04001206/*
1207 * when nowcow writeback call back. This checks for snapshots or COW copies
1208 * of the extents that exist in the file, and COWs the file as required.
1209 *
1210 * If no cow copies or snapshots exist, we write directly to the existing
1211 * blocks on disk
1212 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001213static noinline int run_delalloc_nocow(struct inode *inode,
1214 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001215 u64 start, u64 end, int *page_started, int force,
1216 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001217{
Chris Masonbe20aa92007-12-17 20:14:01 -05001218 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001219 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001220 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001221 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001222 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001223 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001224 u64 cow_start;
1225 u64 cur_offset;
1226 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001227 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 u64 disk_bytenr;
1229 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001230 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001231 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001232 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001233 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001234 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001235 int nocow;
1236 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001237 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001238 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001239
1240 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001241 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001242 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1243 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001244 EXTENT_DO_ACCOUNTING |
1245 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001246 PAGE_CLEAR_DIRTY |
1247 PAGE_SET_WRITEBACK |
1248 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001249 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001250 }
Li Zefan82d59022011-04-20 10:33:24 +08001251
Liu Bo83eea1f2012-07-10 05:28:39 -06001252 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001253
1254 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001255 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001256 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001257 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001258
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001259 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001260 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1261 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001262 EXTENT_DO_ACCOUNTING |
1263 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001264 PAGE_CLEAR_DIRTY |
1265 PAGE_SET_WRITEBACK |
1266 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001267 btrfs_free_path(path);
1268 return PTR_ERR(trans);
1269 }
1270
Josef Bacik74b21072011-04-13 12:02:53 -04001271 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001272
Yan Zheng80ff3852008-10-30 14:20:02 -04001273 cow_start = (u64)-1;
1274 cur_offset = start;
1275 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001276 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001277 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001278 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001279 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1281 leaf = path->nodes[0];
1282 btrfs_item_key_to_cpu(leaf, &found_key,
1283 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001284 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 found_key.type == BTRFS_EXTENT_DATA_KEY)
1286 path->slots[0]--;
1287 }
1288 check_prev = 0;
1289next_slot:
1290 leaf = path->nodes[0];
1291 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1292 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001293 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001294 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 if (ret > 0)
1296 break;
1297 leaf = path->nodes[0];
1298 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001299
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 nocow = 0;
1301 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001302 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001304
Li Zefan33345d012011-04-20 10:31:50 +08001305 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001306 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1307 found_key.offset > end)
1308 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001309
Yan Zheng80ff3852008-10-30 14:20:02 -04001310 if (found_key.offset > cur_offset) {
1311 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001312 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 goto out_check;
1314 }
Chris Masonc31f8832008-01-08 15:46:31 -05001315
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 fi = btrfs_item_ptr(leaf, path->slots[0],
1317 struct btrfs_file_extent_item);
1318 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001319
Josef Bacikcc95bef2013-04-04 14:31:27 -04001320 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001321 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1322 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001323 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001324 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 extent_end = found_key.offset +
1326 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001327 disk_num_bytes =
1328 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 if (extent_end <= start) {
1330 path->slots[0]++;
1331 goto next_slot;
1332 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001333 if (disk_bytenr == 0)
1334 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001335 if (btrfs_file_extent_compression(leaf, fi) ||
1336 btrfs_file_extent_encryption(leaf, fi) ||
1337 btrfs_file_extent_other_encoding(leaf, fi))
1338 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001339 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1340 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001341 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001342 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001343 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001344 found_key.offset -
1345 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001346 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001347 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001348 disk_bytenr += cur_offset - found_key.offset;
1349 num_bytes = min(end + 1, extent_end) - cur_offset;
1350 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001351 * if there are pending snapshots for this root,
1352 * we fall into common COW way.
1353 */
1354 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001355 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001356 if (!err)
1357 goto out_check;
1358 }
1359 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001360 * force cow if csum exists in the range.
1361 * this ensure that csum for a given extent are
1362 * either valid or do not exist.
1363 */
1364 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1365 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001366 nocow = 1;
1367 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1368 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001369 btrfs_file_extent_inline_len(leaf,
1370 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001371 extent_end = ALIGN(extent_end, root->sectorsize);
1372 } else {
1373 BUG_ON(1);
1374 }
1375out_check:
1376 if (extent_end <= start) {
1377 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001378 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001379 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001380 goto next_slot;
1381 }
1382 if (!nocow) {
1383 if (cow_start == (u64)-1)
1384 cow_start = cur_offset;
1385 cur_offset = extent_end;
1386 if (cur_offset > end)
1387 break;
1388 path->slots[0]++;
1389 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001390 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001391
David Sterbab3b4aa72011-04-21 01:20:15 +02001392 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001393 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001394 ret = cow_file_range(inode, locked_page,
1395 cow_start, found_key.offset - 1,
1396 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001397 if (ret) {
1398 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001399 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001400 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001401 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001402 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001403 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001404
Yan Zhengd899e052008-10-30 14:25:28 -04001405 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1406 struct extent_map *em;
1407 struct extent_map_tree *em_tree;
1408 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001409 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001410 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001411 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001412 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001413 em->len = num_bytes;
1414 em->block_len = num_bytes;
1415 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001416 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001417 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001418 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001419 em->mod_start = em->start;
1420 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001421 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001422 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001423 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001424 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001425 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001426 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001427 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001428 if (ret != -EEXIST) {
1429 free_extent_map(em);
1430 break;
1431 }
1432 btrfs_drop_extent_cache(inode, em->start,
1433 em->start + em->len - 1, 0);
1434 }
1435 type = BTRFS_ORDERED_PREALLOC;
1436 } else {
1437 type = BTRFS_ORDERED_NOCOW;
1438 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001439
1440 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001441 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001442 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001443
Yan, Zhengefa56462010-05-16 10:49:59 -04001444 if (root->root_key.objectid ==
1445 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1446 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1447 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001448 if (ret) {
1449 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001450 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001451 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001452 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001453 }
1454
Josef Bacikc2790a22013-07-29 11:20:47 -04001455 extent_clear_unlock_delalloc(inode, cur_offset,
1456 cur_offset + num_bytes - 1,
1457 locked_page, EXTENT_LOCKED |
1458 EXTENT_DELALLOC, PAGE_UNLOCK |
1459 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001460 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001461 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001462 cur_offset = extent_end;
1463 if (cur_offset > end)
1464 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001465 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001466 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001467
Josef Bacik17ca04a2012-05-31 15:58:55 -04001468 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001469 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001470 cur_offset = end;
1471 }
1472
Yan Zheng80ff3852008-10-30 14:20:02 -04001473 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001474 ret = cow_file_range(inode, locked_page, cow_start, end,
1475 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001476 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001477 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001478 }
1479
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001480error:
Miao Xiea698d0752012-09-20 01:51:59 -06001481 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001482 if (!ret)
1483 ret = err;
1484
Josef Bacik17ca04a2012-05-31 15:58:55 -04001485 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001486 extent_clear_unlock_delalloc(inode, cur_offset, end,
1487 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001488 EXTENT_DELALLOC | EXTENT_DEFRAG |
1489 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1490 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001491 PAGE_SET_WRITEBACK |
1492 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001493 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001494 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001495}
1496
Wang Shilong47059d92014-07-03 18:22:07 +08001497static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1498{
1499
1500 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1501 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1502 return 0;
1503
1504 /*
1505 * @defrag_bytes is a hint value, no spinlock held here,
1506 * if is not zero, it means the file is defragging.
1507 * Force cow if given extent needs to be defragged.
1508 */
1509 if (BTRFS_I(inode)->defrag_bytes &&
1510 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1511 EXTENT_DEFRAG, 0, NULL))
1512 return 1;
1513
1514 return 0;
1515}
1516
Chris Masond352ac62008-09-29 15:18:18 -04001517/*
1518 * extent_io.c call back to do delayed allocation processing
1519 */
Chris Masonc8b97812008-10-29 14:49:59 -04001520static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001521 u64 start, u64 end, int *page_started,
1522 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001523{
Chris Masonbe20aa92007-12-17 20:14:01 -05001524 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001525 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001526
Wang Shilong47059d92014-07-03 18:22:07 +08001527 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001528 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001529 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001530 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001531 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001532 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001533 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001534 ret = cow_file_range(inode, locked_page, start, end,
1535 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001536 } else {
1537 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1538 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001539 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001540 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001541 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001542 return ret;
1543}
1544
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001545static void btrfs_split_extent_hook(struct inode *inode,
1546 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001547{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001548 u64 size;
1549
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001550 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001551 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001552 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001553
Josef Bacikdcab6a32015-02-11 15:08:59 -05001554 size = orig->end - orig->start + 1;
1555 if (size > BTRFS_MAX_EXTENT_SIZE) {
1556 u64 num_extents;
1557 u64 new_size;
1558
1559 /*
Josef Bacikba117212015-03-13 15:01:24 -04001560 * See the explanation in btrfs_merge_extent_hook, the same
1561 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001562 */
1563 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001564 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001565 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001566 new_size = split - orig->start;
1567 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1568 BTRFS_MAX_EXTENT_SIZE);
1569 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1570 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001571 return;
1572 }
1573
Josef Bacik9e0baf62011-07-15 15:16:44 +00001574 spin_lock(&BTRFS_I(inode)->lock);
1575 BTRFS_I(inode)->outstanding_extents++;
1576 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001577}
1578
1579/*
1580 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1581 * extents so we can keep track of new extents that are just merged onto old
1582 * extents, such as when we are doing sequential writes, so we can properly
1583 * account for the metadata space we'll need.
1584 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001585static void btrfs_merge_extent_hook(struct inode *inode,
1586 struct extent_state *new,
1587 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001588{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001589 u64 new_size, old_size;
1590 u64 num_extents;
1591
Josef Bacik9ed74f22009-09-11 16:12:44 -04001592 /* not delalloc, ignore it */
1593 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001594 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001595
Josef Bacik8461a3d2015-03-13 15:12:08 -04001596 if (new->start > other->start)
1597 new_size = new->end - other->start + 1;
1598 else
1599 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001600
1601 /* we're not bigger than the max, unreserve the space and go */
1602 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1603 spin_lock(&BTRFS_I(inode)->lock);
1604 BTRFS_I(inode)->outstanding_extents--;
1605 spin_unlock(&BTRFS_I(inode)->lock);
1606 return;
1607 }
1608
1609 /*
Josef Bacikba117212015-03-13 15:01:24 -04001610 * We have to add up either side to figure out how many extents were
1611 * accounted for before we merged into one big extent. If the number of
1612 * extents we accounted for is <= the amount we need for the new range
1613 * then we can return, otherwise drop. Think of it like this
1614 *
1615 * [ 4k][MAX_SIZE]
1616 *
1617 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1618 * need 2 outstanding extents, on one side we have 1 and the other side
1619 * we have 1 so they are == and we can return. But in this case
1620 *
1621 * [MAX_SIZE+4k][MAX_SIZE+4k]
1622 *
1623 * Each range on their own accounts for 2 extents, but merged together
1624 * they are only 3 extents worth of accounting, so we need to drop in
1625 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001626 */
Josef Bacikba117212015-03-13 15:01:24 -04001627 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001628 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1629 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001630 old_size = new->end - new->start + 1;
1631 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1632 BTRFS_MAX_EXTENT_SIZE);
1633
Josef Bacikdcab6a32015-02-11 15:08:59 -05001634 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001635 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001636 return;
1637
Josef Bacik9e0baf62011-07-15 15:16:44 +00001638 spin_lock(&BTRFS_I(inode)->lock);
1639 BTRFS_I(inode)->outstanding_extents--;
1640 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001641}
1642
Miao Xieeb73c1b2013-05-15 07:48:22 +00001643static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1644 struct inode *inode)
1645{
1646 spin_lock(&root->delalloc_lock);
1647 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1648 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1649 &root->delalloc_inodes);
1650 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1651 &BTRFS_I(inode)->runtime_flags);
1652 root->nr_delalloc_inodes++;
1653 if (root->nr_delalloc_inodes == 1) {
1654 spin_lock(&root->fs_info->delalloc_root_lock);
1655 BUG_ON(!list_empty(&root->delalloc_root));
1656 list_add_tail(&root->delalloc_root,
1657 &root->fs_info->delalloc_roots);
1658 spin_unlock(&root->fs_info->delalloc_root_lock);
1659 }
1660 }
1661 spin_unlock(&root->delalloc_lock);
1662}
1663
1664static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1665 struct inode *inode)
1666{
1667 spin_lock(&root->delalloc_lock);
1668 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1669 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1670 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1671 &BTRFS_I(inode)->runtime_flags);
1672 root->nr_delalloc_inodes--;
1673 if (!root->nr_delalloc_inodes) {
1674 spin_lock(&root->fs_info->delalloc_root_lock);
1675 BUG_ON(list_empty(&root->delalloc_root));
1676 list_del_init(&root->delalloc_root);
1677 spin_unlock(&root->fs_info->delalloc_root_lock);
1678 }
1679 }
1680 spin_unlock(&root->delalloc_lock);
1681}
1682
Chris Masond352ac62008-09-29 15:18:18 -04001683/*
1684 * extent_io.c set_bit_hook, used to track delayed allocation
1685 * bytes in this file, and to maintain the list of inodes that
1686 * have pending delalloc work to be done.
1687 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001688static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001689 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001690{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001691
Wang Shilong47059d92014-07-03 18:22:07 +08001692 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1693 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001694 /*
1695 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001696 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001697 * bit, which is only set or cleared with irqs on
1698 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001699 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001700 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001701 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001702 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001703
Josef Bacik9e0baf62011-07-15 15:16:44 +00001704 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001705 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001706 } else {
1707 spin_lock(&BTRFS_I(inode)->lock);
1708 BTRFS_I(inode)->outstanding_extents++;
1709 spin_unlock(&BTRFS_I(inode)->lock);
1710 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001711
Josef Bacik6a3891c2015-03-16 17:38:52 -04001712 /* For sanity tests */
1713 if (btrfs_test_is_dummy_root(root))
1714 return;
1715
Miao Xie963d6782013-01-29 10:10:51 +00001716 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1717 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001718 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001719 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001720 if (*bits & EXTENT_DEFRAG)
1721 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001722 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001723 &BTRFS_I(inode)->runtime_flags))
1724 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001725 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001726 }
Chris Mason291d6732008-01-29 15:55:23 -05001727}
1728
Chris Masond352ac62008-09-29 15:18:18 -04001729/*
1730 * extent_io.c clear_bit_hook, see set_bit_hook for why
1731 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001732static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001733 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001734 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001735{
Wang Shilong47059d92014-07-03 18:22:07 +08001736 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001737 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1738 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001739
1740 spin_lock(&BTRFS_I(inode)->lock);
1741 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1742 BTRFS_I(inode)->defrag_bytes -= len;
1743 spin_unlock(&BTRFS_I(inode)->lock);
1744
Chris Mason75eff682008-12-15 15:54:40 -05001745 /*
1746 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001747 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001748 * bit, which is only set or cleared with irqs on
1749 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001750 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001751 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001752 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001753
Josef Bacik9e0baf62011-07-15 15:16:44 +00001754 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001755 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001756 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1757 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001758 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001759 spin_unlock(&BTRFS_I(inode)->lock);
1760 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001761
Josef Bacikb6d08f02013-09-27 14:57:43 -04001762 /*
1763 * We don't reserve metadata space for space cache inodes so we
1764 * don't need to call dellalloc_release_metadata if there is an
1765 * error.
1766 */
1767 if (*bits & EXTENT_DO_ACCOUNTING &&
1768 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001769 btrfs_delalloc_release_metadata(inode, len);
1770
Josef Bacik6a3891c2015-03-16 17:38:52 -04001771 /* For sanity tests. */
1772 if (btrfs_test_is_dummy_root(root))
1773 return;
1774
Josef Bacik0cb59c92010-07-02 12:14:14 -04001775 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001776 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001777 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001778
Miao Xie963d6782013-01-29 10:10:51 +00001779 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1780 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001781 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001782 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001783 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001784 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001785 &BTRFS_I(inode)->runtime_flags))
1786 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001787 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001788 }
Chris Mason291d6732008-01-29 15:55:23 -05001789}
1790
Chris Masond352ac62008-09-29 15:18:18 -04001791/*
1792 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1793 * we don't create bios that span stripes or chunks
1794 */
David Woodhouse64a16702009-07-15 23:29:37 +01001795int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001796 size_t size, struct bio *bio,
1797 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001798{
1799 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001800 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001801 u64 length = 0;
1802 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001803 int ret;
1804
Chris Mason771ed682008-11-06 22:02:51 -05001805 if (bio_flags & EXTENT_BIO_COMPRESSED)
1806 return 0;
1807
Kent Overstreet4f024f32013-10-11 15:44:27 -07001808 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001809 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001810 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001811 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001812 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001813 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001814 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001815 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001816 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001817}
1818
Chris Masond352ac62008-09-29 15:18:18 -04001819/*
1820 * in order to insert checksums into the metadata in large chunks,
1821 * we wait until bio submission time. All the pages in the bio are
1822 * checksummed and sums are attached onto the ordered extent record.
1823 *
1824 * At IO completion time the cums attached on the ordered extent record
1825 * are inserted into the btree
1826 */
Chris Masond3977122009-01-05 21:25:51 -05001827static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1828 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001829 unsigned long bio_flags,
1830 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001831{
Chris Mason065631f2008-02-20 12:07:25 -05001832 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001833 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001834
Chris Masond20f7042008-12-08 16:58:54 -05001835 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001836 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001837 return 0;
1838}
Chris Masone0156402008-04-16 11:15:20 -04001839
Chris Mason4a69a412008-11-06 22:03:00 -05001840/*
1841 * in order to insert checksums into the metadata in large chunks,
1842 * we wait until bio submission time. All the pages in the bio are
1843 * checksummed and sums are attached onto the ordered extent record.
1844 *
1845 * At IO completion time the cums attached on the ordered extent record
1846 * are inserted into the btree
1847 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001848static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001849 int mirror_num, unsigned long bio_flags,
1850 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001851{
1852 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001853 int ret;
1854
1855 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1856 if (ret)
1857 bio_endio(bio, ret);
1858 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001859}
1860
Chris Masond352ac62008-09-29 15:18:18 -04001861/*
Chris Masoncad321a2008-12-17 14:51:42 -05001862 * extent_io.c submission hook. This does the right thing for csum calculation
1863 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001864 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001865static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001866 int mirror_num, unsigned long bio_flags,
1867 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001868{
1869 struct btrfs_root *root = BTRFS_I(inode)->root;
1870 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001871 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001872 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001873 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001874
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001875 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001876
Liu Bo83eea1f2012-07-10 05:28:39 -06001877 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001878 metadata = 2;
1879
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001880 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001881 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1882 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001883 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001884
Chris Masond20f7042008-12-08 16:58:54 -05001885 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001886 ret = btrfs_submit_compressed_read(inode, bio,
1887 mirror_num,
1888 bio_flags);
1889 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001890 } else if (!skip_sum) {
1891 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1892 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001893 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001894 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001895 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001896 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001897 /* csum items have already been cloned */
1898 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1899 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001900 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001901 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001902 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001903 bio_flags, bio_offset,
1904 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001905 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001906 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001907 } else if (!skip_sum) {
1908 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1909 if (ret)
1910 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001911 }
1912
Chris Mason0b86a832008-03-24 15:01:56 -04001913mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001914 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1915
1916out:
1917 if (ret < 0)
1918 bio_endio(bio, ret);
1919 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001920}
Chris Mason6885f302008-02-20 16:11:05 -05001921
Chris Masond352ac62008-09-29 15:18:18 -04001922/*
1923 * given a list of ordered sums record them in the inode. This happens
1924 * at IO completion time based on sums calculated at bio submission time.
1925 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001926static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001927 struct inode *inode, u64 file_offset,
1928 struct list_head *list)
1929{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001930 struct btrfs_ordered_sum *sum;
1931
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001932 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001933 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001934 btrfs_csum_file_blocks(trans,
1935 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001936 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001937 }
1938 return 0;
1939}
1940
Josef Bacik2ac55d42010-02-03 19:33:23 +00001941int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1942 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001943{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001944 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001945 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001946 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001947}
1948
Chris Masond352ac62008-09-29 15:18:18 -04001949/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001950struct btrfs_writepage_fixup {
1951 struct page *page;
1952 struct btrfs_work work;
1953};
1954
Christoph Hellwigb2950862008-12-02 09:54:17 -05001955static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001956{
1957 struct btrfs_writepage_fixup *fixup;
1958 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001959 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001960 struct page *page;
1961 struct inode *inode;
1962 u64 page_start;
1963 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001964 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001965
1966 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1967 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001968again:
Chris Mason247e7432008-07-17 12:53:51 -04001969 lock_page(page);
1970 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1971 ClearPageChecked(page);
1972 goto out_page;
1973 }
1974
1975 inode = page->mapping->host;
1976 page_start = page_offset(page);
1977 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1978
Josef Bacik2ac55d42010-02-03 19:33:23 +00001979 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001980 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001981
1982 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001983 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001984 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001985
1986 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1987 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001988 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1989 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001990 unlock_page(page);
1991 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001992 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001993 goto again;
1994 }
Chris Mason247e7432008-07-17 12:53:51 -04001995
Jeff Mahoney87826df2012-02-15 16:23:57 +01001996 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1997 if (ret) {
1998 mapping_set_error(page->mapping, ret);
1999 end_extent_writepage(page, ret, page_start, page_end);
2000 ClearPageChecked(page);
2001 goto out;
2002 }
2003
Josef Bacik2ac55d42010-02-03 19:33:23 +00002004 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002005 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002006 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002007out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002008 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2009 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002010out_page:
2011 unlock_page(page);
2012 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002013 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002014}
2015
2016/*
2017 * There are a few paths in the higher layers of the kernel that directly
2018 * set the page dirty bit without asking the filesystem if it is a
2019 * good idea. This causes problems because we want to make sure COW
2020 * properly happens and the data=ordered rules are followed.
2021 *
Chris Masonc8b97812008-10-29 14:49:59 -04002022 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002023 * hasn't been properly setup for IO. We kick off an async process
2024 * to fix it up. The async helper will wait for ordered extents, set
2025 * the delalloc bit and make it safe to write the page.
2026 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002027static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002028{
2029 struct inode *inode = page->mapping->host;
2030 struct btrfs_writepage_fixup *fixup;
2031 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002032
Chris Mason8b62b722009-09-02 16:53:46 -04002033 /* this page is properly in the ordered list */
2034 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002035 return 0;
2036
2037 if (PageChecked(page))
2038 return -EAGAIN;
2039
2040 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2041 if (!fixup)
2042 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002043
Chris Mason247e7432008-07-17 12:53:51 -04002044 SetPageChecked(page);
2045 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002046 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2047 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002048 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002049 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002050 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002051}
2052
Yan Zhengd899e052008-10-30 14:25:28 -04002053static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2054 struct inode *inode, u64 file_pos,
2055 u64 disk_bytenr, u64 disk_num_bytes,
2056 u64 num_bytes, u64 ram_bytes,
2057 u8 compression, u8 encryption,
2058 u16 other_encoding, int extent_type)
2059{
2060 struct btrfs_root *root = BTRFS_I(inode)->root;
2061 struct btrfs_file_extent_item *fi;
2062 struct btrfs_path *path;
2063 struct extent_buffer *leaf;
2064 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002065 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002066 int ret;
2067
2068 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002069 if (!path)
2070 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002071
Chris Masona1ed8352009-09-11 12:27:37 -04002072 /*
2073 * we may be replacing one extent in the tree with another.
2074 * The new extent is pinned in the extent map, and we don't want
2075 * to drop it from the cache until it is completely in the btree.
2076 *
2077 * So, tell btrfs_drop_extents to leave this extent in the cache.
2078 * the caller is expected to unpin it and allow it to be merged
2079 * with the others.
2080 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002081 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2082 file_pos + num_bytes, NULL, 0,
2083 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002084 if (ret)
2085 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002086
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002087 if (!extent_inserted) {
2088 ins.objectid = btrfs_ino(inode);
2089 ins.offset = file_pos;
2090 ins.type = BTRFS_EXTENT_DATA_KEY;
2091
2092 path->leave_spinning = 1;
2093 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2094 sizeof(*fi));
2095 if (ret)
2096 goto out;
2097 }
Yan Zhengd899e052008-10-30 14:25:28 -04002098 leaf = path->nodes[0];
2099 fi = btrfs_item_ptr(leaf, path->slots[0],
2100 struct btrfs_file_extent_item);
2101 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2102 btrfs_set_file_extent_type(leaf, fi, extent_type);
2103 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2104 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2105 btrfs_set_file_extent_offset(leaf, fi, 0);
2106 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2107 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2108 btrfs_set_file_extent_compression(leaf, fi, compression);
2109 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2110 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002111
Yan Zhengd899e052008-10-30 14:25:28 -04002112 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002113 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002114
2115 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002116
2117 ins.objectid = disk_bytenr;
2118 ins.offset = disk_num_bytes;
2119 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002120 ret = btrfs_alloc_reserved_file_extent(trans, root,
2121 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002122 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002123out:
Yan Zhengd899e052008-10-30 14:25:28 -04002124 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002125
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002126 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002127}
2128
Liu Bo38c227d2013-01-29 03:18:40 +00002129/* snapshot-aware defrag */
2130struct sa_defrag_extent_backref {
2131 struct rb_node node;
2132 struct old_sa_defrag_extent *old;
2133 u64 root_id;
2134 u64 inum;
2135 u64 file_pos;
2136 u64 extent_offset;
2137 u64 num_bytes;
2138 u64 generation;
2139};
2140
2141struct old_sa_defrag_extent {
2142 struct list_head list;
2143 struct new_sa_defrag_extent *new;
2144
2145 u64 extent_offset;
2146 u64 bytenr;
2147 u64 offset;
2148 u64 len;
2149 int count;
2150};
2151
2152struct new_sa_defrag_extent {
2153 struct rb_root root;
2154 struct list_head head;
2155 struct btrfs_path *path;
2156 struct inode *inode;
2157 u64 file_pos;
2158 u64 len;
2159 u64 bytenr;
2160 u64 disk_len;
2161 u8 compress_type;
2162};
2163
2164static int backref_comp(struct sa_defrag_extent_backref *b1,
2165 struct sa_defrag_extent_backref *b2)
2166{
2167 if (b1->root_id < b2->root_id)
2168 return -1;
2169 else if (b1->root_id > b2->root_id)
2170 return 1;
2171
2172 if (b1->inum < b2->inum)
2173 return -1;
2174 else if (b1->inum > b2->inum)
2175 return 1;
2176
2177 if (b1->file_pos < b2->file_pos)
2178 return -1;
2179 else if (b1->file_pos > b2->file_pos)
2180 return 1;
2181
2182 /*
2183 * [------------------------------] ===> (a range of space)
2184 * |<--->| |<---->| =============> (fs/file tree A)
2185 * |<---------------------------->| ===> (fs/file tree B)
2186 *
2187 * A range of space can refer to two file extents in one tree while
2188 * refer to only one file extent in another tree.
2189 *
2190 * So we may process a disk offset more than one time(two extents in A)
2191 * and locate at the same extent(one extent in B), then insert two same
2192 * backrefs(both refer to the extent in B).
2193 */
2194 return 0;
2195}
2196
2197static void backref_insert(struct rb_root *root,
2198 struct sa_defrag_extent_backref *backref)
2199{
2200 struct rb_node **p = &root->rb_node;
2201 struct rb_node *parent = NULL;
2202 struct sa_defrag_extent_backref *entry;
2203 int ret;
2204
2205 while (*p) {
2206 parent = *p;
2207 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2208
2209 ret = backref_comp(backref, entry);
2210 if (ret < 0)
2211 p = &(*p)->rb_left;
2212 else
2213 p = &(*p)->rb_right;
2214 }
2215
2216 rb_link_node(&backref->node, parent, p);
2217 rb_insert_color(&backref->node, root);
2218}
2219
2220/*
2221 * Note the backref might has changed, and in this case we just return 0.
2222 */
2223static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2224 void *ctx)
2225{
2226 struct btrfs_file_extent_item *extent;
2227 struct btrfs_fs_info *fs_info;
2228 struct old_sa_defrag_extent *old = ctx;
2229 struct new_sa_defrag_extent *new = old->new;
2230 struct btrfs_path *path = new->path;
2231 struct btrfs_key key;
2232 struct btrfs_root *root;
2233 struct sa_defrag_extent_backref *backref;
2234 struct extent_buffer *leaf;
2235 struct inode *inode = new->inode;
2236 int slot;
2237 int ret;
2238 u64 extent_offset;
2239 u64 num_bytes;
2240
2241 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2242 inum == btrfs_ino(inode))
2243 return 0;
2244
2245 key.objectid = root_id;
2246 key.type = BTRFS_ROOT_ITEM_KEY;
2247 key.offset = (u64)-1;
2248
2249 fs_info = BTRFS_I(inode)->root->fs_info;
2250 root = btrfs_read_fs_root_no_name(fs_info, &key);
2251 if (IS_ERR(root)) {
2252 if (PTR_ERR(root) == -ENOENT)
2253 return 0;
2254 WARN_ON(1);
2255 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2256 inum, offset, root_id);
2257 return PTR_ERR(root);
2258 }
2259
2260 key.objectid = inum;
2261 key.type = BTRFS_EXTENT_DATA_KEY;
2262 if (offset > (u64)-1 << 32)
2263 key.offset = 0;
2264 else
2265 key.offset = offset;
2266
2267 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302268 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002269 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002270 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002271
2272 while (1) {
2273 cond_resched();
2274
2275 leaf = path->nodes[0];
2276 slot = path->slots[0];
2277
2278 if (slot >= btrfs_header_nritems(leaf)) {
2279 ret = btrfs_next_leaf(root, path);
2280 if (ret < 0) {
2281 goto out;
2282 } else if (ret > 0) {
2283 ret = 0;
2284 goto out;
2285 }
2286 continue;
2287 }
2288
2289 path->slots[0]++;
2290
2291 btrfs_item_key_to_cpu(leaf, &key, slot);
2292
2293 if (key.objectid > inum)
2294 goto out;
2295
2296 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2297 continue;
2298
2299 extent = btrfs_item_ptr(leaf, slot,
2300 struct btrfs_file_extent_item);
2301
2302 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2303 continue;
2304
Liu Boe68afa42013-07-01 22:13:26 +08002305 /*
2306 * 'offset' refers to the exact key.offset,
2307 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2308 * (key.offset - extent_offset).
2309 */
2310 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002311 continue;
2312
Liu Boe68afa42013-07-01 22:13:26 +08002313 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002314 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002315
Liu Bo38c227d2013-01-29 03:18:40 +00002316 if (extent_offset >= old->extent_offset + old->offset +
2317 old->len || extent_offset + num_bytes <=
2318 old->extent_offset + old->offset)
2319 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002320 break;
2321 }
2322
2323 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2324 if (!backref) {
2325 ret = -ENOENT;
2326 goto out;
2327 }
2328
2329 backref->root_id = root_id;
2330 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002331 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002332 backref->num_bytes = num_bytes;
2333 backref->extent_offset = extent_offset;
2334 backref->generation = btrfs_file_extent_generation(leaf, extent);
2335 backref->old = old;
2336 backref_insert(&new->root, backref);
2337 old->count++;
2338out:
2339 btrfs_release_path(path);
2340 WARN_ON(ret);
2341 return ret;
2342}
2343
2344static noinline bool record_extent_backrefs(struct btrfs_path *path,
2345 struct new_sa_defrag_extent *new)
2346{
2347 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2348 struct old_sa_defrag_extent *old, *tmp;
2349 int ret;
2350
2351 new->path = path;
2352
2353 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002354 ret = iterate_inodes_from_logical(old->bytenr +
2355 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002356 path, record_one_backref,
2357 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002358 if (ret < 0 && ret != -ENOENT)
2359 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002360
2361 /* no backref to be processed for this extent */
2362 if (!old->count) {
2363 list_del(&old->list);
2364 kfree(old);
2365 }
2366 }
2367
2368 if (list_empty(&new->head))
2369 return false;
2370
2371 return true;
2372}
2373
2374static int relink_is_mergable(struct extent_buffer *leaf,
2375 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002376 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002377{
Liu Bo116e0022013-08-02 16:30:40 +08002378 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002379 return 0;
2380
2381 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2382 return 0;
2383
Liu Bo116e0022013-08-02 16:30:40 +08002384 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2385 return 0;
2386
2387 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002388 btrfs_file_extent_other_encoding(leaf, fi))
2389 return 0;
2390
2391 return 1;
2392}
2393
2394/*
2395 * Note the backref might has changed, and in this case we just return 0.
2396 */
2397static noinline int relink_extent_backref(struct btrfs_path *path,
2398 struct sa_defrag_extent_backref *prev,
2399 struct sa_defrag_extent_backref *backref)
2400{
2401 struct btrfs_file_extent_item *extent;
2402 struct btrfs_file_extent_item *item;
2403 struct btrfs_ordered_extent *ordered;
2404 struct btrfs_trans_handle *trans;
2405 struct btrfs_fs_info *fs_info;
2406 struct btrfs_root *root;
2407 struct btrfs_key key;
2408 struct extent_buffer *leaf;
2409 struct old_sa_defrag_extent *old = backref->old;
2410 struct new_sa_defrag_extent *new = old->new;
2411 struct inode *src_inode = new->inode;
2412 struct inode *inode;
2413 struct extent_state *cached = NULL;
2414 int ret = 0;
2415 u64 start;
2416 u64 len;
2417 u64 lock_start;
2418 u64 lock_end;
2419 bool merge = false;
2420 int index;
2421
2422 if (prev && prev->root_id == backref->root_id &&
2423 prev->inum == backref->inum &&
2424 prev->file_pos + prev->num_bytes == backref->file_pos)
2425 merge = true;
2426
2427 /* step 1: get root */
2428 key.objectid = backref->root_id;
2429 key.type = BTRFS_ROOT_ITEM_KEY;
2430 key.offset = (u64)-1;
2431
2432 fs_info = BTRFS_I(src_inode)->root->fs_info;
2433 index = srcu_read_lock(&fs_info->subvol_srcu);
2434
2435 root = btrfs_read_fs_root_no_name(fs_info, &key);
2436 if (IS_ERR(root)) {
2437 srcu_read_unlock(&fs_info->subvol_srcu, index);
2438 if (PTR_ERR(root) == -ENOENT)
2439 return 0;
2440 return PTR_ERR(root);
2441 }
Liu Bo38c227d2013-01-29 03:18:40 +00002442
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002443 if (btrfs_root_readonly(root)) {
2444 srcu_read_unlock(&fs_info->subvol_srcu, index);
2445 return 0;
2446 }
2447
Liu Bo38c227d2013-01-29 03:18:40 +00002448 /* step 2: get inode */
2449 key.objectid = backref->inum;
2450 key.type = BTRFS_INODE_ITEM_KEY;
2451 key.offset = 0;
2452
2453 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2454 if (IS_ERR(inode)) {
2455 srcu_read_unlock(&fs_info->subvol_srcu, index);
2456 return 0;
2457 }
2458
2459 srcu_read_unlock(&fs_info->subvol_srcu, index);
2460
2461 /* step 3: relink backref */
2462 lock_start = backref->file_pos;
2463 lock_end = backref->file_pos + backref->num_bytes - 1;
2464 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2465 0, &cached);
2466
2467 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2468 if (ordered) {
2469 btrfs_put_ordered_extent(ordered);
2470 goto out_unlock;
2471 }
2472
2473 trans = btrfs_join_transaction(root);
2474 if (IS_ERR(trans)) {
2475 ret = PTR_ERR(trans);
2476 goto out_unlock;
2477 }
2478
2479 key.objectid = backref->inum;
2480 key.type = BTRFS_EXTENT_DATA_KEY;
2481 key.offset = backref->file_pos;
2482
2483 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2484 if (ret < 0) {
2485 goto out_free_path;
2486 } else if (ret > 0) {
2487 ret = 0;
2488 goto out_free_path;
2489 }
2490
2491 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2492 struct btrfs_file_extent_item);
2493
2494 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2495 backref->generation)
2496 goto out_free_path;
2497
2498 btrfs_release_path(path);
2499
2500 start = backref->file_pos;
2501 if (backref->extent_offset < old->extent_offset + old->offset)
2502 start += old->extent_offset + old->offset -
2503 backref->extent_offset;
2504
2505 len = min(backref->extent_offset + backref->num_bytes,
2506 old->extent_offset + old->offset + old->len);
2507 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2508
2509 ret = btrfs_drop_extents(trans, root, inode, start,
2510 start + len, 1);
2511 if (ret)
2512 goto out_free_path;
2513again:
2514 key.objectid = btrfs_ino(inode);
2515 key.type = BTRFS_EXTENT_DATA_KEY;
2516 key.offset = start;
2517
Liu Boa09a0a72013-03-11 09:20:58 +00002518 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002519 if (merge) {
2520 struct btrfs_file_extent_item *fi;
2521 u64 extent_len;
2522 struct btrfs_key found_key;
2523
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002524 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002525 if (ret < 0)
2526 goto out_free_path;
2527
2528 path->slots[0]--;
2529 leaf = path->nodes[0];
2530 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2531
2532 fi = btrfs_item_ptr(leaf, path->slots[0],
2533 struct btrfs_file_extent_item);
2534 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2535
Liu Bo116e0022013-08-02 16:30:40 +08002536 if (extent_len + found_key.offset == start &&
2537 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002538 btrfs_set_file_extent_num_bytes(leaf, fi,
2539 extent_len + len);
2540 btrfs_mark_buffer_dirty(leaf);
2541 inode_add_bytes(inode, len);
2542
2543 ret = 1;
2544 goto out_free_path;
2545 } else {
2546 merge = false;
2547 btrfs_release_path(path);
2548 goto again;
2549 }
2550 }
2551
2552 ret = btrfs_insert_empty_item(trans, root, path, &key,
2553 sizeof(*extent));
2554 if (ret) {
2555 btrfs_abort_transaction(trans, root, ret);
2556 goto out_free_path;
2557 }
2558
2559 leaf = path->nodes[0];
2560 item = btrfs_item_ptr(leaf, path->slots[0],
2561 struct btrfs_file_extent_item);
2562 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2563 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2564 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2565 btrfs_set_file_extent_num_bytes(leaf, item, len);
2566 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2567 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2568 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2569 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2570 btrfs_set_file_extent_encryption(leaf, item, 0);
2571 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2572
2573 btrfs_mark_buffer_dirty(leaf);
2574 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002575 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002576
2577 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2578 new->disk_len, 0,
2579 backref->root_id, backref->inum,
2580 new->file_pos, 0); /* start - extent_offset */
2581 if (ret) {
2582 btrfs_abort_transaction(trans, root, ret);
2583 goto out_free_path;
2584 }
2585
2586 ret = 1;
2587out_free_path:
2588 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002589 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002590 btrfs_end_transaction(trans, root);
2591out_unlock:
2592 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2593 &cached, GFP_NOFS);
2594 iput(inode);
2595 return ret;
2596}
2597
Liu Bo6f519562013-10-29 10:45:05 +08002598static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2599{
2600 struct old_sa_defrag_extent *old, *tmp;
2601
2602 if (!new)
2603 return;
2604
2605 list_for_each_entry_safe(old, tmp, &new->head, list) {
2606 list_del(&old->list);
2607 kfree(old);
2608 }
2609 kfree(new);
2610}
2611
Liu Bo38c227d2013-01-29 03:18:40 +00002612static void relink_file_extents(struct new_sa_defrag_extent *new)
2613{
2614 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002615 struct sa_defrag_extent_backref *backref;
2616 struct sa_defrag_extent_backref *prev = NULL;
2617 struct inode *inode;
2618 struct btrfs_root *root;
2619 struct rb_node *node;
2620 int ret;
2621
2622 inode = new->inode;
2623 root = BTRFS_I(inode)->root;
2624
2625 path = btrfs_alloc_path();
2626 if (!path)
2627 return;
2628
2629 if (!record_extent_backrefs(path, new)) {
2630 btrfs_free_path(path);
2631 goto out;
2632 }
2633 btrfs_release_path(path);
2634
2635 while (1) {
2636 node = rb_first(&new->root);
2637 if (!node)
2638 break;
2639 rb_erase(node, &new->root);
2640
2641 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2642
2643 ret = relink_extent_backref(path, prev, backref);
2644 WARN_ON(ret < 0);
2645
2646 kfree(prev);
2647
2648 if (ret == 1)
2649 prev = backref;
2650 else
2651 prev = NULL;
2652 cond_resched();
2653 }
2654 kfree(prev);
2655
2656 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002657out:
Liu Bo6f519562013-10-29 10:45:05 +08002658 free_sa_defrag_extent(new);
2659
Liu Bo38c227d2013-01-29 03:18:40 +00002660 atomic_dec(&root->fs_info->defrag_running);
2661 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002662}
2663
2664static struct new_sa_defrag_extent *
2665record_old_file_extents(struct inode *inode,
2666 struct btrfs_ordered_extent *ordered)
2667{
2668 struct btrfs_root *root = BTRFS_I(inode)->root;
2669 struct btrfs_path *path;
2670 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002671 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002672 struct new_sa_defrag_extent *new;
2673 int ret;
2674
2675 new = kmalloc(sizeof(*new), GFP_NOFS);
2676 if (!new)
2677 return NULL;
2678
2679 new->inode = inode;
2680 new->file_pos = ordered->file_offset;
2681 new->len = ordered->len;
2682 new->bytenr = ordered->start;
2683 new->disk_len = ordered->disk_len;
2684 new->compress_type = ordered->compress_type;
2685 new->root = RB_ROOT;
2686 INIT_LIST_HEAD(&new->head);
2687
2688 path = btrfs_alloc_path();
2689 if (!path)
2690 goto out_kfree;
2691
2692 key.objectid = btrfs_ino(inode);
2693 key.type = BTRFS_EXTENT_DATA_KEY;
2694 key.offset = new->file_pos;
2695
2696 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2697 if (ret < 0)
2698 goto out_free_path;
2699 if (ret > 0 && path->slots[0] > 0)
2700 path->slots[0]--;
2701
2702 /* find out all the old extents for the file range */
2703 while (1) {
2704 struct btrfs_file_extent_item *extent;
2705 struct extent_buffer *l;
2706 int slot;
2707 u64 num_bytes;
2708 u64 offset;
2709 u64 end;
2710 u64 disk_bytenr;
2711 u64 extent_offset;
2712
2713 l = path->nodes[0];
2714 slot = path->slots[0];
2715
2716 if (slot >= btrfs_header_nritems(l)) {
2717 ret = btrfs_next_leaf(root, path);
2718 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002719 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002720 else if (ret > 0)
2721 break;
2722 continue;
2723 }
2724
2725 btrfs_item_key_to_cpu(l, &key, slot);
2726
2727 if (key.objectid != btrfs_ino(inode))
2728 break;
2729 if (key.type != BTRFS_EXTENT_DATA_KEY)
2730 break;
2731 if (key.offset >= new->file_pos + new->len)
2732 break;
2733
2734 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2735
2736 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2737 if (key.offset + num_bytes < new->file_pos)
2738 goto next;
2739
2740 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2741 if (!disk_bytenr)
2742 goto next;
2743
2744 extent_offset = btrfs_file_extent_offset(l, extent);
2745
2746 old = kmalloc(sizeof(*old), GFP_NOFS);
2747 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002748 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002749
2750 offset = max(new->file_pos, key.offset);
2751 end = min(new->file_pos + new->len, key.offset + num_bytes);
2752
2753 old->bytenr = disk_bytenr;
2754 old->extent_offset = extent_offset;
2755 old->offset = offset - key.offset;
2756 old->len = end - offset;
2757 old->new = new;
2758 old->count = 0;
2759 list_add_tail(&old->list, &new->head);
2760next:
2761 path->slots[0]++;
2762 cond_resched();
2763 }
2764
2765 btrfs_free_path(path);
2766 atomic_inc(&root->fs_info->defrag_running);
2767
2768 return new;
2769
Liu Bo38c227d2013-01-29 03:18:40 +00002770out_free_path:
2771 btrfs_free_path(path);
2772out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002773 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002774 return NULL;
2775}
2776
Miao Xiee570fd22014-06-19 10:42:50 +08002777static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2778 u64 start, u64 len)
2779{
2780 struct btrfs_block_group_cache *cache;
2781
2782 cache = btrfs_lookup_block_group(root->fs_info, start);
2783 ASSERT(cache);
2784
2785 spin_lock(&cache->lock);
2786 cache->delalloc_bytes -= len;
2787 spin_unlock(&cache->lock);
2788
2789 btrfs_put_block_group(cache);
2790}
2791
Chris Masond352ac62008-09-29 15:18:18 -04002792/* as ordered data IO finishes, this gets called so we can finish
2793 * an ordered extent if the range of bytes in the file it covers are
2794 * fully written.
2795 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002796static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002797{
Josef Bacik5fd02042012-05-02 14:00:54 -04002798 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002799 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002800 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002801 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002802 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002803 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002804 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002805 int ret = 0;
2806 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002807 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002808 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002809
Liu Bo83eea1f2012-07-10 05:28:39 -06002810 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002811
Josef Bacik5fd02042012-05-02 14:00:54 -04002812 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2813 ret = -EIO;
2814 goto out;
2815 }
2816
Miao Xief6124962014-09-12 18:44:04 +08002817 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2818 ordered_extent->file_offset +
2819 ordered_extent->len - 1);
2820
Josef Bacik77cef2e2013-08-29 13:57:21 -04002821 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2822 truncated = true;
2823 logical_len = ordered_extent->truncated_len;
2824 /* Truncated the entire extent, don't bother adding */
2825 if (!logical_len)
2826 goto out;
2827 }
2828
Yan, Zhengc2167752009-11-12 09:34:21 +00002829 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002830 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002831 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2832 if (nolock)
2833 trans = btrfs_join_transaction_nolock(root);
2834 else
2835 trans = btrfs_join_transaction(root);
2836 if (IS_ERR(trans)) {
2837 ret = PTR_ERR(trans);
2838 trans = NULL;
2839 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002840 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002841 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2842 ret = btrfs_update_inode_fallback(trans, root, inode);
2843 if (ret) /* -ENOMEM or corruption */
2844 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002845 goto out;
2846 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002847
Josef Bacik2ac55d42010-02-03 19:33:23 +00002848 lock_extent_bits(io_tree, ordered_extent->file_offset,
2849 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002850 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002851
Liu Bo38c227d2013-01-29 03:18:40 +00002852 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2853 ordered_extent->file_offset + ordered_extent->len - 1,
2854 EXTENT_DEFRAG, 1, cached_state);
2855 if (ret) {
2856 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002857 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002858 /* the inode is shared */
2859 new = record_old_file_extents(inode, ordered_extent);
2860
2861 clear_extent_bit(io_tree, ordered_extent->file_offset,
2862 ordered_extent->file_offset + ordered_extent->len - 1,
2863 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2864 }
2865
Josef Bacik0cb59c92010-07-02 12:14:14 -04002866 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002867 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002868 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002869 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002870 if (IS_ERR(trans)) {
2871 ret = PTR_ERR(trans);
2872 trans = NULL;
2873 goto out_unlock;
2874 }
Chris Masona79b7d42014-05-22 16:18:52 -07002875
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002876 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002877
Chris Masonc8b97812008-10-29 14:49:59 -04002878 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002879 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002880 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002881 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002882 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002883 ordered_extent->file_offset,
2884 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002885 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002886 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002887 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002888 ret = insert_reserved_file_extent(trans, inode,
2889 ordered_extent->file_offset,
2890 ordered_extent->start,
2891 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002892 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002893 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002894 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002895 if (!ret)
2896 btrfs_release_delalloc_bytes(root,
2897 ordered_extent->start,
2898 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002899 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002900 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2901 ordered_extent->file_offset, ordered_extent->len,
2902 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002903 if (ret < 0) {
2904 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002905 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002906 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002907
Chris Masone6dcd2d2008-07-17 12:53:50 -04002908 add_pending_csums(trans, inode, ordered_extent->file_offset,
2909 &ordered_extent->list);
2910
Josef Bacik6c760c02012-11-09 10:53:21 -05002911 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2912 ret = btrfs_update_inode_fallback(trans, root, inode);
2913 if (ret) { /* -ENOMEM or corruption */
2914 btrfs_abort_transaction(trans, root, ret);
2915 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002916 }
2917 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002918out_unlock:
2919 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2920 ordered_extent->file_offset +
2921 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002922out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002923 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002924 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002925 if (trans)
2926 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002927
Josef Bacik77cef2e2013-08-29 13:57:21 -04002928 if (ret || truncated) {
2929 u64 start, end;
2930
2931 if (truncated)
2932 start = ordered_extent->file_offset + logical_len;
2933 else
2934 start = ordered_extent->file_offset;
2935 end = ordered_extent->file_offset + ordered_extent->len - 1;
2936 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2937
2938 /* Drop the cache for the part of the extent we didn't write. */
2939 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002940
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002941 /*
2942 * If the ordered extent had an IOERR or something else went
2943 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002944 * back to the allocator. We only free the extent in the
2945 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002946 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002947 if ((ret || !logical_len) &&
2948 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002949 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2950 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002951 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002952 }
2953
2954
Josef Bacik5fd02042012-05-02 14:00:54 -04002955 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002956 * This needs to be done to make sure anybody waiting knows we are done
2957 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002958 */
2959 btrfs_remove_ordered_extent(inode, ordered_extent);
2960
Liu Bo38c227d2013-01-29 03:18:40 +00002961 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002962 if (new) {
2963 if (ret) {
2964 free_sa_defrag_extent(new);
2965 atomic_dec(&root->fs_info->defrag_running);
2966 } else {
2967 relink_file_extents(new);
2968 }
2969 }
Liu Bo38c227d2013-01-29 03:18:40 +00002970
Chris Masone6dcd2d2008-07-17 12:53:50 -04002971 /* once for us */
2972 btrfs_put_ordered_extent(ordered_extent);
2973 /* once for the tree */
2974 btrfs_put_ordered_extent(ordered_extent);
2975
Josef Bacik5fd02042012-05-02 14:00:54 -04002976 return ret;
2977}
2978
2979static void finish_ordered_fn(struct btrfs_work *work)
2980{
2981 struct btrfs_ordered_extent *ordered_extent;
2982 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2983 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002984}
2985
Christoph Hellwigb2950862008-12-02 09:54:17 -05002986static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002987 struct extent_state *state, int uptodate)
2988{
Josef Bacik5fd02042012-05-02 14:00:54 -04002989 struct inode *inode = page->mapping->host;
2990 struct btrfs_root *root = BTRFS_I(inode)->root;
2991 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002992 struct btrfs_workqueue *wq;
2993 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002994
liubo1abe9b82011-03-24 11:18:59 +00002995 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2996
Chris Mason8b62b722009-09-02 16:53:46 -04002997 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002998 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2999 end - start + 1, uptodate))
3000 return 0;
3001
Liu Bo9e0af232014-08-15 23:36:53 +08003002 if (btrfs_is_free_space_inode(inode)) {
3003 wq = root->fs_info->endio_freespace_worker;
3004 func = btrfs_freespace_write_helper;
3005 } else {
3006 wq = root->fs_info->endio_write_workers;
3007 func = btrfs_endio_write_helper;
3008 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003009
Liu Bo9e0af232014-08-15 23:36:53 +08003010 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3011 NULL);
3012 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003013
3014 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003015}
3016
Miao Xiedc380ae2014-09-12 18:43:55 +08003017static int __readpage_endio_check(struct inode *inode,
3018 struct btrfs_io_bio *io_bio,
3019 int icsum, struct page *page,
3020 int pgoff, u64 start, size_t len)
3021{
3022 char *kaddr;
3023 u32 csum_expected;
3024 u32 csum = ~(u32)0;
3025 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
3026 DEFAULT_RATELIMIT_BURST);
3027
3028 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3029
3030 kaddr = kmap_atomic(page);
3031 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3032 btrfs_csum_final(csum, (char *)&csum);
3033 if (csum != csum_expected)
3034 goto zeroit;
3035
3036 kunmap_atomic(kaddr);
3037 return 0;
3038zeroit:
3039 if (__ratelimit(&_rs))
David Sterbaf0954c62014-12-19 18:38:44 +01003040 btrfs_warn(BTRFS_I(inode)->root->fs_info,
Miao Xiedc380ae2014-09-12 18:43:55 +08003041 "csum failed ino %llu off %llu csum %u expected csum %u",
3042 btrfs_ino(inode), start, csum, csum_expected);
3043 memset(kaddr + pgoff, 1, len);
3044 flush_dcache_page(page);
3045 kunmap_atomic(kaddr);
3046 if (csum_expected == 0)
3047 return 0;
3048 return -EIO;
3049}
3050
Chris Masond352ac62008-09-29 15:18:18 -04003051/*
Chris Masond352ac62008-09-29 15:18:18 -04003052 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003053 * if there's a match, we allow the bio to finish. If not, the code in
3054 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003055 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003056static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3057 u64 phy_offset, struct page *page,
3058 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003059{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003060 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003061 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003062 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003063 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003064
Chris Masond20f7042008-12-08 16:58:54 -05003065 if (PageChecked(page)) {
3066 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003067 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003068 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003069
3070 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003071 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003072
Yan Zheng17d217f2008-12-12 10:03:38 -05003073 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003074 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05003075 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
3076 GFP_NOFS);
3077 return 0;
3078 }
3079
Miao Xiefacc8a222013-07-25 19:22:34 +08003080 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003081 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3082 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003083}
Chris Masonb888db2b2007-08-27 16:49:44 -04003084
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003085struct delayed_iput {
3086 struct list_head list;
3087 struct inode *inode;
3088};
3089
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003090/* JDM: If this is fs-wide, why can't we add a pointer to
3091 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003092void btrfs_add_delayed_iput(struct inode *inode)
3093{
3094 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3095 struct delayed_iput *delayed;
3096
3097 if (atomic_add_unless(&inode->i_count, -1, 1))
3098 return;
3099
3100 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3101 delayed->inode = inode;
3102
3103 spin_lock(&fs_info->delayed_iput_lock);
3104 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3105 spin_unlock(&fs_info->delayed_iput_lock);
3106}
3107
3108void btrfs_run_delayed_iputs(struct btrfs_root *root)
3109{
3110 LIST_HEAD(list);
3111 struct btrfs_fs_info *fs_info = root->fs_info;
3112 struct delayed_iput *delayed;
3113 int empty;
3114
3115 spin_lock(&fs_info->delayed_iput_lock);
3116 empty = list_empty(&fs_info->delayed_iputs);
3117 spin_unlock(&fs_info->delayed_iput_lock);
3118 if (empty)
3119 return;
3120
Zhao Leid7c15172015-02-26 10:49:20 +08003121 down_read(&fs_info->delayed_iput_sem);
3122
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003123 spin_lock(&fs_info->delayed_iput_lock);
3124 list_splice_init(&fs_info->delayed_iputs, &list);
3125 spin_unlock(&fs_info->delayed_iput_lock);
3126
3127 while (!list_empty(&list)) {
3128 delayed = list_entry(list.next, struct delayed_iput, list);
3129 list_del(&delayed->list);
3130 iput(delayed->inode);
3131 kfree(delayed);
3132 }
Zhao Leid7c15172015-02-26 10:49:20 +08003133
3134 up_read(&root->fs_info->delayed_iput_sem);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003135}
3136
Yan, Zhengd68fc572010-05-16 10:49:58 -04003137/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003138 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003139 * files in the subvolume, it removes orphan item and frees block_rsv
3140 * structure.
3141 */
3142void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3143 struct btrfs_root *root)
3144{
Josef Bacik90290e12011-12-02 15:44:12 -05003145 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003146 int ret;
3147
Josef Bacik8a35d952012-05-23 14:26:42 -04003148 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003149 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3150 return;
3151
Josef Bacik90290e12011-12-02 15:44:12 -05003152 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003153 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003154 spin_unlock(&root->orphan_lock);
3155 return;
3156 }
3157
3158 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3159 spin_unlock(&root->orphan_lock);
3160 return;
3161 }
3162
3163 block_rsv = root->orphan_block_rsv;
3164 root->orphan_block_rsv = NULL;
3165 spin_unlock(&root->orphan_lock);
3166
Miao Xie27cdeb72014-04-02 19:51:05 +08003167 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003168 btrfs_root_refs(&root->root_item) > 0) {
3169 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3170 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003171 if (ret)
3172 btrfs_abort_transaction(trans, root, ret);
3173 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003174 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3175 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003176 }
3177
Josef Bacik90290e12011-12-02 15:44:12 -05003178 if (block_rsv) {
3179 WARN_ON(block_rsv->size > 0);
3180 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003181 }
3182}
3183
3184/*
Josef Bacik7b128762008-07-24 12:17:14 -04003185 * This creates an orphan entry for the given inode in case something goes
3186 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003187 *
3188 * NOTE: caller of this function should reserve 5 units of metadata for
3189 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003190 */
3191int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3192{
3193 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003194 struct btrfs_block_rsv *block_rsv = NULL;
3195 int reserve = 0;
3196 int insert = 0;
3197 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003198
Yan, Zhengd68fc572010-05-16 10:49:58 -04003199 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003200 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003201 if (!block_rsv)
3202 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003203 }
3204
Yan, Zhengd68fc572010-05-16 10:49:58 -04003205 spin_lock(&root->orphan_lock);
3206 if (!root->orphan_block_rsv) {
3207 root->orphan_block_rsv = block_rsv;
3208 } else if (block_rsv) {
3209 btrfs_free_block_rsv(root, block_rsv);
3210 block_rsv = NULL;
3211 }
Josef Bacik7b128762008-07-24 12:17:14 -04003212
Josef Bacik8a35d952012-05-23 14:26:42 -04003213 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3214 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003215#if 0
3216 /*
3217 * For proper ENOSPC handling, we should do orphan
3218 * cleanup when mounting. But this introduces backward
3219 * compatibility issue.
3220 */
3221 if (!xchg(&root->orphan_item_inserted, 1))
3222 insert = 2;
3223 else
3224 insert = 1;
3225#endif
3226 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003227 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003228 }
Josef Bacik7b128762008-07-24 12:17:14 -04003229
Josef Bacik72ac3c02012-05-23 14:13:11 -04003230 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3231 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003232 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003233 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003234
Yan, Zhengd68fc572010-05-16 10:49:58 -04003235 /* grab metadata reservation from transaction handle */
3236 if (reserve) {
3237 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003238 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003239 }
3240
3241 /* insert an orphan item to track this unlinked/truncated file */
3242 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003243 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003244 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003245 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003246 if (reserve) {
3247 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3248 &BTRFS_I(inode)->runtime_flags);
3249 btrfs_orphan_release_metadata(inode);
3250 }
3251 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003252 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3253 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003254 btrfs_abort_transaction(trans, root, ret);
3255 return ret;
3256 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003257 }
3258 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003259 }
3260
3261 /* insert an orphan item to track subvolume contains orphan files */
3262 if (insert >= 2) {
3263 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3264 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003265 if (ret && ret != -EEXIST) {
3266 btrfs_abort_transaction(trans, root, ret);
3267 return ret;
3268 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003269 }
3270 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003271}
3272
3273/*
3274 * We have done the truncate/delete so we can go ahead and remove the orphan
3275 * item for this particular inode.
3276 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003277static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3278 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003279{
3280 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003281 int delete_item = 0;
3282 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003283 int ret = 0;
3284
Yan, Zhengd68fc572010-05-16 10:49:58 -04003285 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003286 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3287 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003288 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003289
Josef Bacik72ac3c02012-05-23 14:13:11 -04003290 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3291 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003292 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003293 spin_unlock(&root->orphan_lock);
3294
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003295 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003296 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003297 if (trans)
3298 ret = btrfs_del_orphan_item(trans, root,
3299 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003300 }
Josef Bacik7b128762008-07-24 12:17:14 -04003301
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003302 if (release_rsv)
3303 btrfs_orphan_release_metadata(inode);
3304
Josef Bacik4ef31a42013-08-13 14:10:08 -04003305 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003306}
3307
3308/*
3309 * this cleans up any orphans that may be left on the list from the last use
3310 * of this root.
3311 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003312int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003313{
3314 struct btrfs_path *path;
3315 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003316 struct btrfs_key key, found_key;
3317 struct btrfs_trans_handle *trans;
3318 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003319 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003320 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3321
Yan, Zhengd68fc572010-05-16 10:49:58 -04003322 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003323 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003324
3325 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003326 if (!path) {
3327 ret = -ENOMEM;
3328 goto out;
3329 }
Josef Bacik7b128762008-07-24 12:17:14 -04003330 path->reada = -1;
3331
3332 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003333 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003334 key.offset = (u64)-1;
3335
Josef Bacik7b128762008-07-24 12:17:14 -04003336 while (1) {
3337 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003338 if (ret < 0)
3339 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003340
3341 /*
3342 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003343 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003344 * find the key and see if we have stuff that matches
3345 */
3346 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003347 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003348 if (path->slots[0] == 0)
3349 break;
3350 path->slots[0]--;
3351 }
3352
3353 /* pull out the item */
3354 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003355 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3356
3357 /* make sure the item matches what we want */
3358 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3359 break;
David Sterba962a2982014-06-04 18:41:45 +02003360 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003361 break;
3362
3363 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003364 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003365
3366 /*
3367 * this is where we are basically btrfs_lookup, without the
3368 * crossing root thing. we store the inode number in the
3369 * offset of the orphan item.
3370 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003371
3372 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003373 btrfs_err(root->fs_info,
3374 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003375 ret = -EINVAL;
3376 goto out;
3377 }
3378
3379 last_objectid = found_key.offset;
3380
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003381 found_key.objectid = found_key.offset;
3382 found_key.type = BTRFS_INODE_ITEM_KEY;
3383 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003384 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303385 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003386 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003387 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003388
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003389 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3390 struct btrfs_root *dead_root;
3391 struct btrfs_fs_info *fs_info = root->fs_info;
3392 int is_dead_root = 0;
3393
3394 /*
3395 * this is an orphan in the tree root. Currently these
3396 * could come from 2 sources:
3397 * a) a snapshot deletion in progress
3398 * b) a free space cache inode
3399 * We need to distinguish those two, as the snapshot
3400 * orphan must not get deleted.
3401 * find_dead_roots already ran before us, so if this
3402 * is a snapshot deletion, we should find the root
3403 * in the dead_roots list
3404 */
3405 spin_lock(&fs_info->trans_lock);
3406 list_for_each_entry(dead_root, &fs_info->dead_roots,
3407 root_list) {
3408 if (dead_root->root_key.objectid ==
3409 found_key.objectid) {
3410 is_dead_root = 1;
3411 break;
3412 }
3413 }
3414 spin_unlock(&fs_info->trans_lock);
3415 if (is_dead_root) {
3416 /* prevent this orphan from being found again */
3417 key.offset = found_key.objectid - 1;
3418 continue;
3419 }
3420 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003421 /*
3422 * Inode is already gone but the orphan item is still there,
3423 * kill the orphan item.
3424 */
3425 if (ret == -ESTALE) {
3426 trans = btrfs_start_transaction(root, 1);
3427 if (IS_ERR(trans)) {
3428 ret = PTR_ERR(trans);
3429 goto out;
3430 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003431 btrfs_debug(root->fs_info, "auto deleting %Lu",
3432 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003433 ret = btrfs_del_orphan_item(trans, root,
3434 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003435 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003436 if (ret)
3437 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003438 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003439 }
Josef Bacik7b128762008-07-24 12:17:14 -04003440
Josef Bacik7b128762008-07-24 12:17:14 -04003441 /*
3442 * add this inode to the orphan list so btrfs_orphan_del does
3443 * the proper thing when we hit it
3444 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003445 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3446 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003447 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003448
Josef Bacik7b128762008-07-24 12:17:14 -04003449 /* if we have links, this was a truncate, lets do that */
3450 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303451 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003452 iput(inode);
3453 continue;
3454 }
Josef Bacik7b128762008-07-24 12:17:14 -04003455 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003456
3457 /* 1 for the orphan item deletion. */
3458 trans = btrfs_start_transaction(root, 1);
3459 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003460 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003461 ret = PTR_ERR(trans);
3462 goto out;
3463 }
3464 ret = btrfs_orphan_add(trans, inode);
3465 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003466 if (ret) {
3467 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003468 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003469 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003470
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003471 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003472 if (ret)
3473 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003474 } else {
3475 nr_unlink++;
3476 }
3477
3478 /* this will do delete_inode and everything for us */
3479 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003480 if (ret)
3481 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003482 }
Miao Xie3254c872011-11-10 20:45:05 -05003483 /* release the path since we're done with it */
3484 btrfs_release_path(path);
3485
Yan, Zhengd68fc572010-05-16 10:49:58 -04003486 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3487
3488 if (root->orphan_block_rsv)
3489 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3490 (u64)-1);
3491
Miao Xie27cdeb72014-04-02 19:51:05 +08003492 if (root->orphan_block_rsv ||
3493 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003494 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003495 if (!IS_ERR(trans))
3496 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003497 }
Josef Bacik7b128762008-07-24 12:17:14 -04003498
3499 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003500 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003501 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003502 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003503
3504out:
3505 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003506 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003507 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003508 btrfs_free_path(path);
3509 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003510}
3511
Chris Masond352ac62008-09-29 15:18:18 -04003512/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003513 * very simple check to peek ahead in the leaf looking for xattrs. If we
3514 * don't find any xattrs, we know there can't be any acls.
3515 *
3516 * slot is the slot the inode is in, objectid is the objectid of the inode
3517 */
3518static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003519 int slot, u64 objectid,
3520 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003521{
3522 u32 nritems = btrfs_header_nritems(leaf);
3523 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003524 static u64 xattr_access = 0;
3525 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003526 int scanned = 0;
3527
Josef Bacikf23b5a52013-06-19 10:16:26 -04003528 if (!xattr_access) {
3529 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3530 strlen(POSIX_ACL_XATTR_ACCESS));
3531 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3532 strlen(POSIX_ACL_XATTR_DEFAULT));
3533 }
3534
Chris Mason46a53cc2009-04-27 11:47:50 -04003535 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003536 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003537 while (slot < nritems) {
3538 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3539
3540 /* we found a different objectid, there must not be acls */
3541 if (found_key.objectid != objectid)
3542 return 0;
3543
3544 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003545 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003546 if (*first_xattr_slot == -1)
3547 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003548 if (found_key.offset == xattr_access ||
3549 found_key.offset == xattr_default)
3550 return 1;
3551 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003552
3553 /*
3554 * we found a key greater than an xattr key, there can't
3555 * be any acls later on
3556 */
3557 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3558 return 0;
3559
3560 slot++;
3561 scanned++;
3562
3563 /*
3564 * it goes inode, inode backrefs, xattrs, extents,
3565 * so if there are a ton of hard links to an inode there can
3566 * be a lot of backrefs. Don't waste time searching too hard,
3567 * this is just an optimization
3568 */
3569 if (scanned >= 8)
3570 break;
3571 }
3572 /* we hit the end of the leaf before we found an xattr or
3573 * something larger than an xattr. We have to assume the inode
3574 * has acls
3575 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003576 if (*first_xattr_slot == -1)
3577 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003578 return 1;
3579}
3580
3581/*
Chris Masond352ac62008-09-29 15:18:18 -04003582 * read an inode from the btree into the in-memory inode
3583 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003584static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003585{
3586 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003587 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003588 struct btrfs_inode_item *inode_item;
3589 struct btrfs_root *root = BTRFS_I(inode)->root;
3590 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003591 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003592 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003593 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003594 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003595 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003596 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003597
3598 ret = btrfs_fill_inode(inode, &rdev);
3599 if (!ret)
3600 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003601
3602 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003603 if (!path)
3604 goto make_bad;
3605
Chris Mason39279cc2007-06-12 06:35:45 -04003606 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003607
Chris Mason39279cc2007-06-12 06:35:45 -04003608 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003609 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003610 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003611
Chris Mason5f39d392007-10-15 16:14:19 -04003612 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003613
3614 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003615 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003616
Chris Mason5f39d392007-10-15 16:14:19 -04003617 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3618 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003619 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003620 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003621 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3622 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003623 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003624
David Sterbaa937b972014-12-12 17:39:12 +01003625 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3626 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003627
David Sterbaa937b972014-12-12 17:39:12 +01003628 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3629 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003630
David Sterbaa937b972014-12-12 17:39:12 +01003631 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3632 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003633
chandan r9cc97d62012-07-04 12:48:07 +05303634 BTRFS_I(inode)->i_otime.tv_sec =
3635 btrfs_timespec_sec(leaf, &inode_item->otime);
3636 BTRFS_I(inode)->i_otime.tv_nsec =
3637 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003638
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003639 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003640 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003641 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3642
3643 /*
3644 * If we were modified in the current generation and evicted from memory
3645 * and then re-read we need to do a full sync since we don't have any
3646 * idea about which extents were modified before we were evicted from
3647 * cache.
3648 */
3649 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3650 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3651 &BTRFS_I(inode)->runtime_flags);
3652
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003653 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003654 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003655 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003656 rdev = btrfs_inode_rdev(leaf, inode_item);
3657
Josef Bacikaec74772008-07-24 12:12:38 -04003658 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003659 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003660
3661cache_index:
3662 path->slots[0]++;
3663 if (inode->i_nlink != 1 ||
3664 path->slots[0] >= btrfs_header_nritems(leaf))
3665 goto cache_acl;
3666
3667 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3668 if (location.objectid != btrfs_ino(inode))
3669 goto cache_acl;
3670
3671 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3672 if (location.type == BTRFS_INODE_REF_KEY) {
3673 struct btrfs_inode_ref *ref;
3674
3675 ref = (struct btrfs_inode_ref *)ptr;
3676 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3677 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3678 struct btrfs_inode_extref *extref;
3679
3680 extref = (struct btrfs_inode_extref *)ptr;
3681 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3682 extref);
3683 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003684cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003685 /*
3686 * try to precache a NULL acl entry for files that don't have
3687 * any xattrs or acls
3688 */
Li Zefan33345d012011-04-20 10:31:50 +08003689 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003690 btrfs_ino(inode), &first_xattr_slot);
3691 if (first_xattr_slot != -1) {
3692 path->slots[0] = first_xattr_slot;
3693 ret = btrfs_load_inode_props(inode, path);
3694 if (ret)
3695 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003696 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003697 btrfs_ino(inode),
3698 root->root_key.objectid, ret);
3699 }
3700 btrfs_free_path(path);
3701
Al Viro72c04902009-06-24 16:58:48 -04003702 if (!maybe_acls)
3703 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003704
Chris Mason39279cc2007-06-12 06:35:45 -04003705 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003706 case S_IFREG:
3707 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003708 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003709 inode->i_fop = &btrfs_file_operations;
3710 inode->i_op = &btrfs_file_inode_operations;
3711 break;
3712 case S_IFDIR:
3713 inode->i_fop = &btrfs_dir_file_operations;
3714 if (root == root->fs_info->tree_root)
3715 inode->i_op = &btrfs_dir_ro_inode_operations;
3716 else
3717 inode->i_op = &btrfs_dir_inode_operations;
3718 break;
3719 case S_IFLNK:
3720 inode->i_op = &btrfs_symlink_inode_operations;
3721 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3722 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003723 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003724 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003725 init_special_inode(inode, inode->i_mode, rdev);
3726 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003727 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003728
3729 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003730 return;
3731
3732make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003733 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003734 make_bad_inode(inode);
3735}
3736
Chris Masond352ac62008-09-29 15:18:18 -04003737/*
3738 * given a leaf and an inode, copy the inode fields into the leaf
3739 */
Chris Masone02119d2008-09-05 16:13:11 -04003740static void fill_inode_item(struct btrfs_trans_handle *trans,
3741 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003742 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003743 struct inode *inode)
3744{
Liu Bo51fab692012-12-27 09:01:21 +00003745 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003746
Liu Bo51fab692012-12-27 09:01:21 +00003747 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003748
Liu Bo51fab692012-12-27 09:01:21 +00003749 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3750 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3751 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3752 &token);
3753 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3754 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003755
David Sterbaa937b972014-12-12 17:39:12 +01003756 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003757 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003758 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003759 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003760
David Sterbaa937b972014-12-12 17:39:12 +01003761 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003762 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003763 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003764 inode->i_mtime.tv_nsec, &token);
3765
David Sterbaa937b972014-12-12 17:39:12 +01003766 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003767 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003768 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003769 inode->i_ctime.tv_nsec, &token);
3770
chandan r9cc97d62012-07-04 12:48:07 +05303771 btrfs_set_token_timespec_sec(leaf, &item->otime,
3772 BTRFS_I(inode)->i_otime.tv_sec, &token);
3773 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3774 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3775
Liu Bo51fab692012-12-27 09:01:21 +00003776 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3777 &token);
3778 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3779 &token);
3780 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3781 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3782 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3783 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3784 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003785}
3786
Chris Masond352ac62008-09-29 15:18:18 -04003787/*
3788 * copy everything in the in-memory inode into the btree.
3789 */
Chris Mason21151332011-11-10 20:39:08 -05003790static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003791 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003792{
3793 struct btrfs_inode_item *inode_item;
3794 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003795 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003796 int ret;
3797
3798 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003799 if (!path)
3800 return -ENOMEM;
3801
Chris Masonb9473432009-03-13 11:00:37 -04003802 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003803 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3804 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003805 if (ret) {
3806 if (ret > 0)
3807 ret = -ENOENT;
3808 goto failed;
3809 }
3810
Chris Mason5f39d392007-10-15 16:14:19 -04003811 leaf = path->nodes[0];
3812 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003813 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003814
Chris Masone02119d2008-09-05 16:13:11 -04003815 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003816 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003817 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003818 ret = 0;
3819failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003820 btrfs_free_path(path);
3821 return ret;
3822}
3823
Chris Masond352ac62008-09-29 15:18:18 -04003824/*
Chris Mason21151332011-11-10 20:39:08 -05003825 * copy everything in the in-memory inode into the btree.
3826 */
3827noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3828 struct btrfs_root *root, struct inode *inode)
3829{
3830 int ret;
3831
3832 /*
3833 * If the inode is a free space inode, we can deadlock during commit
3834 * if we put it into the delayed code.
3835 *
3836 * The data relocation inode should also be directly updated
3837 * without delay
3838 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003839 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003840 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3841 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003842 btrfs_update_root_times(trans, root);
3843
Chris Mason21151332011-11-10 20:39:08 -05003844 ret = btrfs_delayed_update_inode(trans, root, inode);
3845 if (!ret)
3846 btrfs_set_inode_last_trans(trans, inode);
3847 return ret;
3848 }
3849
3850 return btrfs_update_inode_item(trans, root, inode);
3851}
3852
Josef Bacikbe6aef62012-10-22 15:43:12 -04003853noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3854 struct btrfs_root *root,
3855 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003856{
3857 int ret;
3858
3859 ret = btrfs_update_inode(trans, root, inode);
3860 if (ret == -ENOSPC)
3861 return btrfs_update_inode_item(trans, root, inode);
3862 return ret;
3863}
3864
3865/*
Chris Masond352ac62008-09-29 15:18:18 -04003866 * unlink helper that gets used here in inode.c and in the tree logging
3867 * recovery code. It remove a link in a directory with a given name, and
3868 * also drops the back refs in the inode to the directory
3869 */
Al Viro92986792011-03-04 17:14:37 +00003870static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3871 struct btrfs_root *root,
3872 struct inode *dir, struct inode *inode,
3873 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003874{
3875 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003876 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003877 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003878 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003879 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003880 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003881 u64 ino = btrfs_ino(inode);
3882 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003883
3884 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003885 if (!path) {
3886 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003887 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003888 }
3889
Chris Masonb9473432009-03-13 11:00:37 -04003890 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003891 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003892 name, name_len, -1);
3893 if (IS_ERR(di)) {
3894 ret = PTR_ERR(di);
3895 goto err;
3896 }
3897 if (!di) {
3898 ret = -ENOENT;
3899 goto err;
3900 }
Chris Mason5f39d392007-10-15 16:14:19 -04003901 leaf = path->nodes[0];
3902 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003903 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003904 if (ret)
3905 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003906 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003907
Miao Xie67de1172013-12-26 13:07:06 +08003908 /*
3909 * If we don't have dir index, we have to get it by looking up
3910 * the inode ref, since we get the inode ref, remove it directly,
3911 * it is unnecessary to do delayed deletion.
3912 *
3913 * But if we have dir index, needn't search inode ref to get it.
3914 * Since the inode ref is close to the inode item, it is better
3915 * that we delay to delete it, and just do this deletion when
3916 * we update the inode item.
3917 */
3918 if (BTRFS_I(inode)->dir_index) {
3919 ret = btrfs_delayed_delete_inode_ref(inode);
3920 if (!ret) {
3921 index = BTRFS_I(inode)->dir_index;
3922 goto skip_backref;
3923 }
3924 }
3925
Li Zefan33345d012011-04-20 10:31:50 +08003926 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3927 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003928 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003929 btrfs_info(root->fs_info,
3930 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003931 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003932 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003933 goto err;
3934 }
Miao Xie67de1172013-12-26 13:07:06 +08003935skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003936 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003937 if (ret) {
3938 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003939 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003940 }
Chris Mason39279cc2007-06-12 06:35:45 -04003941
Chris Masone02119d2008-09-05 16:13:11 -04003942 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003943 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003944 if (ret != 0 && ret != -ENOENT) {
3945 btrfs_abort_transaction(trans, root, ret);
3946 goto err;
3947 }
Chris Masone02119d2008-09-05 16:13:11 -04003948
3949 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3950 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003951 if (ret == -ENOENT)
3952 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003953 else if (ret)
3954 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003955err:
3956 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003957 if (ret)
3958 goto out;
3959
3960 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003961 inode_inc_iversion(inode);
3962 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003963 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003964 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003965out:
Chris Mason39279cc2007-06-12 06:35:45 -04003966 return ret;
3967}
3968
Al Viro92986792011-03-04 17:14:37 +00003969int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3970 struct btrfs_root *root,
3971 struct inode *dir, struct inode *inode,
3972 const char *name, int name_len)
3973{
3974 int ret;
3975 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3976 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003977 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003978 ret = btrfs_update_inode(trans, root, inode);
3979 }
3980 return ret;
3981}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003982
3983/*
3984 * helper to start transaction for unlink and rmdir.
3985 *
Josef Bacikd52be812013-05-29 14:54:47 -04003986 * unlink and rmdir are special in btrfs, they do not always free space, so
3987 * if we cannot make our reservations the normal way try and see if there is
3988 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3989 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003990 */
Josef Bacikd52be812013-05-29 14:54:47 -04003991static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003992{
3993 struct btrfs_trans_handle *trans;
3994 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003995 int ret;
3996
Josef Bacike70bea52011-10-11 14:18:24 -04003997 /*
3998 * 1 for the possible orphan item
3999 * 1 for the dir item
4000 * 1 for the dir index
4001 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004002 * 1 for the inode
4003 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04004004 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004005 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
4006 return trans;
4007
Josef Bacikd52be812013-05-29 14:54:47 -04004008 if (PTR_ERR(trans) == -ENOSPC) {
4009 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004010
Josef Bacikd52be812013-05-29 14:54:47 -04004011 trans = btrfs_start_transaction(root, 0);
4012 if (IS_ERR(trans))
4013 return trans;
4014 ret = btrfs_cond_migrate_bytes(root->fs_info,
4015 &root->fs_info->trans_block_rsv,
4016 num_bytes, 5);
4017 if (ret) {
4018 btrfs_end_transaction(trans, root);
4019 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004020 }
Josef Bacik5a77d762011-11-01 14:32:23 -04004021 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04004022 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004023 }
Josef Bacikd52be812013-05-29 14:54:47 -04004024 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004025}
4026
Chris Mason39279cc2007-06-12 06:35:45 -04004027static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4028{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004029 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004030 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04004031 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004032 int ret;
4033
Josef Bacikd52be812013-05-29 14:54:47 -04004034 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004035 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004036 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004037
Chris Mason12fcfd22009-03-24 10:24:20 -04004038 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
4039
Chris Masone02119d2008-09-05 16:13:11 -04004040 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4041 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004042 if (ret)
4043 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004044
Yan, Zhenga22285a2010-05-16 10:48:46 -04004045 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004046 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004047 if (ret)
4048 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004049 }
Josef Bacik7b128762008-07-24 12:17:14 -04004050
Tsutomu Itohb5324022011-07-19 07:27:20 +00004051out:
Josef Bacikd52be812013-05-29 14:54:47 -04004052 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004053 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004054 return ret;
4055}
4056
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004057int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4058 struct btrfs_root *root,
4059 struct inode *dir, u64 objectid,
4060 const char *name, int name_len)
4061{
4062 struct btrfs_path *path;
4063 struct extent_buffer *leaf;
4064 struct btrfs_dir_item *di;
4065 struct btrfs_key key;
4066 u64 index;
4067 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004068 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004069
4070 path = btrfs_alloc_path();
4071 if (!path)
4072 return -ENOMEM;
4073
Li Zefan33345d012011-04-20 10:31:50 +08004074 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004075 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004076 if (IS_ERR_OR_NULL(di)) {
4077 if (!di)
4078 ret = -ENOENT;
4079 else
4080 ret = PTR_ERR(di);
4081 goto out;
4082 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004083
4084 leaf = path->nodes[0];
4085 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4086 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4087 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004088 if (ret) {
4089 btrfs_abort_transaction(trans, root, ret);
4090 goto out;
4091 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004092 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004093
4094 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4095 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004096 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004097 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004098 if (ret != -ENOENT) {
4099 btrfs_abort_transaction(trans, root, ret);
4100 goto out;
4101 }
Li Zefan33345d012011-04-20 10:31:50 +08004102 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004103 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004104 if (IS_ERR_OR_NULL(di)) {
4105 if (!di)
4106 ret = -ENOENT;
4107 else
4108 ret = PTR_ERR(di);
4109 btrfs_abort_transaction(trans, root, ret);
4110 goto out;
4111 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004112
4113 leaf = path->nodes[0];
4114 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004115 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004116 index = key.offset;
4117 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004118 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004119
Miao Xie16cdcec2011-04-22 18:12:22 +08004120 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004121 if (ret) {
4122 btrfs_abort_transaction(trans, root, ret);
4123 goto out;
4124 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004125
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004126 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004127 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004128 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004129 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004130 if (ret)
4131 btrfs_abort_transaction(trans, root, ret);
4132out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004133 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004134 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004135}
4136
Chris Mason39279cc2007-06-12 06:35:45 -04004137static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4138{
4139 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004140 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004141 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004142 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004143
David Sterbab3ae2442012-09-13 16:04:34 -06004144 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004145 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004146 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4147 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004148
Josef Bacikd52be812013-05-29 14:54:47 -04004149 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004150 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004151 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004152
Li Zefan33345d012011-04-20 10:31:50 +08004153 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004154 err = btrfs_unlink_subvol(trans, root, dir,
4155 BTRFS_I(inode)->location.objectid,
4156 dentry->d_name.name,
4157 dentry->d_name.len);
4158 goto out;
4159 }
4160
Josef Bacik7b128762008-07-24 12:17:14 -04004161 err = btrfs_orphan_add(trans, inode);
4162 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004163 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004164
Chris Mason39279cc2007-06-12 06:35:45 -04004165 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004166 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4167 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004168 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004169 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004170out:
Josef Bacikd52be812013-05-29 14:54:47 -04004171 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004172 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004173
Chris Mason39279cc2007-06-12 06:35:45 -04004174 return err;
4175}
4176
Chris Mason28f75a02015-02-04 06:59:29 -08004177static int truncate_space_check(struct btrfs_trans_handle *trans,
4178 struct btrfs_root *root,
4179 u64 bytes_deleted)
4180{
4181 int ret;
4182
4183 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
4184 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4185 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4186 if (!ret)
4187 trans->bytes_reserved += bytes_deleted;
4188 return ret;
4189
4190}
4191
Chris Mason323ac952008-10-01 19:05:46 -04004192/*
Chris Mason39279cc2007-06-12 06:35:45 -04004193 * this can truncate away extent items, csum items and directory items.
4194 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004195 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004196 *
4197 * csum items that cross the new i_size are truncated to the new size
4198 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004199 *
4200 * min_type is the minimum key type to truncate down to. If set to 0, this
4201 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004202 */
Yan, Zheng80825102009-11-12 09:35:36 +00004203int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4204 struct btrfs_root *root,
4205 struct inode *inode,
4206 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004207{
Chris Mason39279cc2007-06-12 06:35:45 -04004208 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004209 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004210 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004211 struct btrfs_key key;
4212 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004213 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004214 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004215 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004216 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004217 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004218 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004219 int found_extent;
4220 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004221 int pending_del_nr = 0;
4222 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004223 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004224 int ret;
4225 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004226 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004227 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004228 bool be_nice = 0;
4229 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004230 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004231
4232 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004233
Chris Mason28ed1342014-12-17 09:41:04 -08004234 /*
4235 * for non-free space inodes and ref cows, we want to back off from
4236 * time to time
4237 */
4238 if (!btrfs_is_free_space_inode(inode) &&
4239 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4240 be_nice = 1;
4241
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004242 path = btrfs_alloc_path();
4243 if (!path)
4244 return -ENOMEM;
4245 path->reada = -1;
4246
Josef Bacik5dc562c2012-08-17 13:14:17 -04004247 /*
4248 * We want to drop from the next block forward in case this new size is
4249 * not block aligned since we will be keeping the last block of the
4250 * extent just the way it is.
4251 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004252 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4253 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004254 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4255 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004256
Miao Xie16cdcec2011-04-22 18:12:22 +08004257 /*
4258 * This function is also used to drop the items in the log tree before
4259 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4260 * it is used to drop the loged items. So we shouldn't kill the delayed
4261 * items.
4262 */
4263 if (min_type == 0 && root == BTRFS_I(inode)->root)
4264 btrfs_kill_delayed_inode_items(inode);
4265
Li Zefan33345d012011-04-20 10:31:50 +08004266 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004267 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004268 key.type = (u8)-1;
4269
Chris Mason85e21ba2008-01-29 15:11:36 -05004270search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004271 /*
4272 * with a 16K leaf size and 128MB extents, you can actually queue
4273 * up a huge file in a single leaf. Most of the time that
4274 * bytes_deleted is > 0, it will be huge by the time we get here
4275 */
4276 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
4277 if (btrfs_should_end_transaction(trans, root)) {
4278 err = -EAGAIN;
4279 goto error;
4280 }
4281 }
4282
4283
Chris Masonb9473432009-03-13 11:00:37 -04004284 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004285 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004286 if (ret < 0) {
4287 err = ret;
4288 goto out;
4289 }
Chris Masond3977122009-01-05 21:25:51 -05004290
Chris Mason85e21ba2008-01-29 15:11:36 -05004291 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004292 /* there are no items in the tree for us to truncate, we're
4293 * done
4294 */
Yan, Zheng80825102009-11-12 09:35:36 +00004295 if (path->slots[0] == 0)
4296 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004297 path->slots[0]--;
4298 }
4299
Chris Masond3977122009-01-05 21:25:51 -05004300 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004301 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004302 leaf = path->nodes[0];
4303 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004304 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004305
Li Zefan33345d012011-04-20 10:31:50 +08004306 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004307 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004308
Chris Mason85e21ba2008-01-29 15:11:36 -05004309 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004310 break;
4311
Chris Mason5f39d392007-10-15 16:14:19 -04004312 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004313 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004314 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004315 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004316 extent_type = btrfs_file_extent_type(leaf, fi);
4317 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004318 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004319 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004320 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004321 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004322 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004323 }
Yan008630c2007-11-07 13:31:09 -05004324 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004325 }
Yan, Zheng80825102009-11-12 09:35:36 +00004326 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004327 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004328 } else {
4329 if (item_end < new_size)
4330 break;
4331 if (found_key.offset >= new_size)
4332 del_item = 1;
4333 else
4334 del_item = 0;
4335 }
Chris Mason39279cc2007-06-12 06:35:45 -04004336 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004337 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004338 if (found_type != BTRFS_EXTENT_DATA_KEY)
4339 goto delete;
4340
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004341 if (del_item)
4342 last_size = found_key.offset;
4343 else
4344 last_size = new_size;
4345
Chris Mason179e29e2007-11-01 11:28:41 -04004346 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004347 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004348 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004349 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004350 u64 orig_num_bytes =
4351 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004352 extent_num_bytes = ALIGN(new_size -
4353 found_key.offset,
4354 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004355 btrfs_set_file_extent_num_bytes(leaf, fi,
4356 extent_num_bytes);
4357 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004358 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004359 if (test_bit(BTRFS_ROOT_REF_COWS,
4360 &root->state) &&
4361 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004362 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004363 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004364 } else {
Chris Masondb945352007-10-15 16:15:53 -04004365 extent_num_bytes =
4366 btrfs_file_extent_disk_num_bytes(leaf,
4367 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004368 extent_offset = found_key.offset -
4369 btrfs_file_extent_offset(leaf, fi);
4370
Chris Mason39279cc2007-06-12 06:35:45 -04004371 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004372 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004373 if (extent_start != 0) {
4374 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004375 if (test_bit(BTRFS_ROOT_REF_COWS,
4376 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004377 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004378 }
4379 }
Chris Mason90692182008-02-08 13:49:28 -05004380 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004381 /*
4382 * we can't truncate inline items that have had
4383 * special encodings
4384 */
4385 if (!del_item &&
4386 btrfs_file_extent_compression(leaf, fi) == 0 &&
4387 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4388 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004389 u32 size = new_size - found_key.offset;
4390
Miao Xie27cdeb72014-04-02 19:51:05 +08004391 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004392 inode_sub_bytes(inode, item_end + 1 -
4393 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004394
4395 /*
4396 * update the ram bytes to properly reflect
4397 * the new size of our item
4398 */
4399 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004400 size =
4401 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004402 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004403 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4404 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004405 inode_sub_bytes(inode, item_end + 1 -
4406 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004407 }
Chris Mason39279cc2007-06-12 06:35:45 -04004408 }
Chris Mason179e29e2007-11-01 11:28:41 -04004409delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004410 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004411 if (!pending_del_nr) {
4412 /* no pending yet, add ourselves */
4413 pending_del_slot = path->slots[0];
4414 pending_del_nr = 1;
4415 } else if (pending_del_nr &&
4416 path->slots[0] + 1 == pending_del_slot) {
4417 /* hop on the pending chunk */
4418 pending_del_nr++;
4419 pending_del_slot = path->slots[0];
4420 } else {
Chris Masond3977122009-01-05 21:25:51 -05004421 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004422 }
Chris Mason39279cc2007-06-12 06:35:45 -04004423 } else {
4424 break;
4425 }
Chris Mason28f75a02015-02-04 06:59:29 -08004426 should_throttle = 0;
4427
Miao Xie27cdeb72014-04-02 19:51:05 +08004428 if (found_extent &&
4429 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4430 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004431 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004432 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004433 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004434 extent_num_bytes, 0,
4435 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004436 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004437 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004438 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004439 btrfs_async_run_delayed_refs(root,
4440 trans->delayed_ref_updates * 2, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004441 if (be_nice) {
4442 if (truncate_space_check(trans, root,
4443 extent_num_bytes)) {
4444 should_end = 1;
4445 }
4446 if (btrfs_should_throttle_delayed_refs(trans,
4447 root)) {
4448 should_throttle = 1;
4449 }
4450 }
Chris Mason39279cc2007-06-12 06:35:45 -04004451 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004452
Yan, Zheng80825102009-11-12 09:35:36 +00004453 if (found_type == BTRFS_INODE_ITEM_KEY)
4454 break;
4455
4456 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004457 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004458 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004459 if (pending_del_nr) {
4460 ret = btrfs_del_items(trans, root, path,
4461 pending_del_slot,
4462 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004463 if (ret) {
4464 btrfs_abort_transaction(trans,
4465 root, ret);
4466 goto error;
4467 }
Yan, Zheng80825102009-11-12 09:35:36 +00004468 pending_del_nr = 0;
4469 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004470 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004471 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004472 unsigned long updates = trans->delayed_ref_updates;
4473 if (updates) {
4474 trans->delayed_ref_updates = 0;
4475 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4476 if (ret && !err)
4477 err = ret;
4478 }
4479 }
Chris Mason28f75a02015-02-04 06:59:29 -08004480 /*
4481 * if we failed to refill our space rsv, bail out
4482 * and let the transaction restart
4483 */
4484 if (should_end) {
4485 err = -EAGAIN;
4486 goto error;
4487 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004488 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004489 } else {
4490 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004491 }
Chris Mason39279cc2007-06-12 06:35:45 -04004492 }
Yan, Zheng80825102009-11-12 09:35:36 +00004493out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004494 if (pending_del_nr) {
4495 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4496 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004497 if (ret)
4498 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004499 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004500error:
Filipe Mananadac57052014-08-29 20:54:26 +01004501 if (last_size != (u64)-1 &&
4502 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004503 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004504
Chris Mason39279cc2007-06-12 06:35:45 -04004505 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004506
Chris Mason28f75a02015-02-04 06:59:29 -08004507 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
Chris Mason28ed1342014-12-17 09:41:04 -08004508 unsigned long updates = trans->delayed_ref_updates;
4509 if (updates) {
4510 trans->delayed_ref_updates = 0;
4511 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4512 if (ret && !err)
4513 err = ret;
4514 }
4515 }
Yan, Zheng80825102009-11-12 09:35:36 +00004516 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004517}
4518
Chris Masona52d9a82007-08-27 16:49:44 -04004519/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004520 * btrfs_truncate_page - read, zero a chunk and write a page
4521 * @inode - inode that we're zeroing
4522 * @from - the offset to start zeroing
4523 * @len - the length to zero, 0 to zero the entire range respective to the
4524 * offset
4525 * @front - zero up to the offset instead of from the offset on
4526 *
4527 * This will find the page for the "from" offset and cow the page and zero the
4528 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004529 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004530int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4531 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004532{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004533 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004534 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004535 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4536 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004537 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004538 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004539 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004540 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4541 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4542 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004543 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004544 int ret = 0;
4545 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004546 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004547
Josef Bacik2aaa6652012-08-29 14:27:18 -04004548 if ((offset & (blocksize - 1)) == 0 &&
4549 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004550 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004551 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004552 if (ret)
4553 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004554
Chris Mason211c17f2008-05-15 09:13:45 -04004555again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004556 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004557 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004558 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004559 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004560 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004561 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004562
4563 page_start = page_offset(page);
4564 page_end = page_start + PAGE_CACHE_SIZE - 1;
4565
Chris Masona52d9a82007-08-27 16:49:44 -04004566 if (!PageUptodate(page)) {
4567 ret = btrfs_readpage(NULL, page);
4568 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004569 if (page->mapping != mapping) {
4570 unlock_page(page);
4571 page_cache_release(page);
4572 goto again;
4573 }
Chris Masona52d9a82007-08-27 16:49:44 -04004574 if (!PageUptodate(page)) {
4575 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004576 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004577 }
4578 }
Chris Mason211c17f2008-05-15 09:13:45 -04004579 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004580
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004581 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004582 set_page_extent_mapped(page);
4583
4584 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4585 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004586 unlock_extent_cached(io_tree, page_start, page_end,
4587 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004588 unlock_page(page);
4589 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004590 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004591 btrfs_put_ordered_extent(ordered);
4592 goto again;
4593 }
4594
Josef Bacik2ac55d42010-02-03 19:33:23 +00004595 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004596 EXTENT_DIRTY | EXTENT_DELALLOC |
4597 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004598 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004599
Josef Bacik2ac55d42010-02-03 19:33:23 +00004600 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4601 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004602 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004603 unlock_extent_cached(io_tree, page_start, page_end,
4604 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004605 goto out_unlock;
4606 }
4607
Chris Masone6dcd2d2008-07-17 12:53:50 -04004608 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004609 if (!len)
4610 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004611 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004612 if (front)
4613 memset(kaddr, 0, offset);
4614 else
4615 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004616 flush_dcache_page(page);
4617 kunmap(page);
4618 }
Chris Mason247e7432008-07-17 12:53:51 -04004619 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004620 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004621 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4622 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004623
Chris Mason89642222008-07-24 09:41:53 -04004624out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004625 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004626 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004627 unlock_page(page);
4628 page_cache_release(page);
4629out:
4630 return ret;
4631}
4632
Josef Bacik16e75492013-10-22 12:18:51 -04004633static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4634 u64 offset, u64 len)
4635{
4636 struct btrfs_trans_handle *trans;
4637 int ret;
4638
4639 /*
4640 * Still need to make sure the inode looks like it's been updated so
4641 * that any holes get logged if we fsync.
4642 */
4643 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4644 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4645 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4646 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4647 return 0;
4648 }
4649
4650 /*
4651 * 1 - for the one we're dropping
4652 * 1 - for the one we're adding
4653 * 1 - for updating the inode.
4654 */
4655 trans = btrfs_start_transaction(root, 3);
4656 if (IS_ERR(trans))
4657 return PTR_ERR(trans);
4658
4659 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4660 if (ret) {
4661 btrfs_abort_transaction(trans, root, ret);
4662 btrfs_end_transaction(trans, root);
4663 return ret;
4664 }
4665
4666 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4667 0, 0, len, 0, len, 0, 0, 0);
4668 if (ret)
4669 btrfs_abort_transaction(trans, root, ret);
4670 else
4671 btrfs_update_inode(trans, root, inode);
4672 btrfs_end_transaction(trans, root);
4673 return ret;
4674}
4675
Josef Bacik695a0d02011-03-04 15:46:53 -05004676/*
4677 * This function puts in dummy file extents for the area we're creating a hole
4678 * for. So if we are truncating this file to a larger size we need to insert
4679 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4680 * the range between oldsize and size
4681 */
Josef Bacika41ad392011-01-31 15:30:16 -05004682int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004683{
Yan Zheng9036c102008-10-30 14:19:41 -04004684 struct btrfs_root *root = BTRFS_I(inode)->root;
4685 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004686 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004687 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004688 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004689 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4690 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004691 u64 last_byte;
4692 u64 cur_offset;
4693 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004694 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004695
Josef Bacika71754f2013-06-17 17:14:39 -04004696 /*
4697 * If our size started in the middle of a page we need to zero out the
4698 * rest of the page before we expand the i_size, otherwise we could
4699 * expose stale data.
4700 */
4701 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4702 if (err)
4703 return err;
4704
Yan Zheng9036c102008-10-30 14:19:41 -04004705 if (size <= hole_start)
4706 return 0;
4707
Yan Zheng9036c102008-10-30 14:19:41 -04004708 while (1) {
4709 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004710
Josef Bacik2ac55d42010-02-03 19:33:23 +00004711 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004712 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004713 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4714 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004715 if (!ordered)
4716 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004717 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4718 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004719 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004720 btrfs_put_ordered_extent(ordered);
4721 }
4722
Yan Zheng9036c102008-10-30 14:19:41 -04004723 cur_offset = hole_start;
4724 while (1) {
4725 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4726 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004727 if (IS_ERR(em)) {
4728 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004729 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004730 break;
4731 }
Yan Zheng9036c102008-10-30 14:19:41 -04004732 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004733 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004734 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004735 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004736 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004737
Josef Bacik16e75492013-10-22 12:18:51 -04004738 err = maybe_insert_hole(root, inode, cur_offset,
4739 hole_size);
4740 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004741 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004742 btrfs_drop_extent_cache(inode, cur_offset,
4743 cur_offset + hole_size - 1, 0);
4744 hole_em = alloc_extent_map();
4745 if (!hole_em) {
4746 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4747 &BTRFS_I(inode)->runtime_flags);
4748 goto next;
4749 }
4750 hole_em->start = cur_offset;
4751 hole_em->len = hole_size;
4752 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004753
Josef Bacik5dc562c2012-08-17 13:14:17 -04004754 hole_em->block_start = EXTENT_MAP_HOLE;
4755 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004756 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004757 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004758 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4759 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004760 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004761
4762 while (1) {
4763 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004764 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004765 write_unlock(&em_tree->lock);
4766 if (err != -EEXIST)
4767 break;
4768 btrfs_drop_extent_cache(inode, cur_offset,
4769 cur_offset +
4770 hole_size - 1, 0);
4771 }
4772 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004773 }
Josef Bacik16e75492013-10-22 12:18:51 -04004774next:
Yan Zheng9036c102008-10-30 14:19:41 -04004775 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004776 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004777 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004778 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004779 break;
4780 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004781 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004782 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4783 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004784 return err;
4785}
4786
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004787static int wait_snapshoting_atomic_t(atomic_t *a)
4788{
4789 schedule();
4790 return 0;
4791}
4792
4793static void wait_for_snapshot_creation(struct btrfs_root *root)
4794{
4795 while (true) {
4796 int ret;
4797
4798 ret = btrfs_start_write_no_snapshoting(root);
4799 if (ret)
4800 break;
4801 wait_on_atomic_t(&root->will_be_snapshoted,
4802 wait_snapshoting_atomic_t,
4803 TASK_UNINTERRUPTIBLE);
4804 }
4805}
4806
Eric Sandeen3972f262013-01-12 02:57:22 +00004807static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004808{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004809 struct btrfs_root *root = BTRFS_I(inode)->root;
4810 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004811 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004812 loff_t newsize = attr->ia_size;
4813 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004814 int ret;
4815
Eric Sandeen3972f262013-01-12 02:57:22 +00004816 /*
4817 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4818 * special case where we need to update the times despite not having
4819 * these flags set. For all other operations the VFS set these flags
4820 * explicitly if it wants a timestamp update.
4821 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004822 if (newsize != oldsize) {
4823 inode_inc_iversion(inode);
4824 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4825 inode->i_ctime = inode->i_mtime =
4826 current_fs_time(inode->i_sb);
4827 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004828
Josef Bacika41ad392011-01-31 15:30:16 -05004829 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004830 truncate_pagecache(inode, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004831 /*
4832 * Don't do an expanding truncate while snapshoting is ongoing.
4833 * This is to ensure the snapshot captures a fully consistent
4834 * state of this file - if the snapshot captures this expanding
4835 * truncation, it must capture all writes that happened before
4836 * this truncation.
4837 */
4838 wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004839 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004840 if (ret) {
4841 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004842 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004843 }
Yan, Zheng80825102009-11-12 09:35:36 +00004844
Miao Xief4a2f4c2011-12-14 20:12:01 -05004845 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004846 if (IS_ERR(trans)) {
4847 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004848 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004849 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004850
4851 i_size_write(inode, newsize);
4852 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4853 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004854 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004855 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004856 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004857
Josef Bacika41ad392011-01-31 15:30:16 -05004858 /*
4859 * We're truncating a file that used to have good data down to
4860 * zero. Make sure it gets into the ordered flush list so that
4861 * any new writes get down to disk quickly.
4862 */
4863 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004864 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4865 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004866
Josef Bacikf3fe8202013-01-07 17:03:21 -05004867 /*
4868 * 1 for the orphan item we're going to add
4869 * 1 for the orphan item deletion.
4870 */
4871 trans = btrfs_start_transaction(root, 2);
4872 if (IS_ERR(trans))
4873 return PTR_ERR(trans);
4874
4875 /*
4876 * We need to do this in case we fail at _any_ point during the
4877 * actual truncate. Once we do the truncate_setsize we could
4878 * invalidate pages which forces any outstanding ordered io to
4879 * be instantly completed which will give us extents that need
4880 * to be truncated. If we fail to get an orphan inode down we
4881 * could have left over extents that were never meant to live,
4882 * so we need to garuntee from this point on that everything
4883 * will be consistent.
4884 */
4885 ret = btrfs_orphan_add(trans, inode);
4886 btrfs_end_transaction(trans, root);
4887 if (ret)
4888 return ret;
4889
Josef Bacika41ad392011-01-31 15:30:16 -05004890 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4891 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004892
4893 /* Disable nonlocked read DIO to avoid the end less truncate */
4894 btrfs_inode_block_unlocked_dio(inode);
4895 inode_dio_wait(inode);
4896 btrfs_inode_resume_unlocked_dio(inode);
4897
Josef Bacika41ad392011-01-31 15:30:16 -05004898 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004899 if (ret && inode->i_nlink) {
4900 int err;
4901
4902 /*
4903 * failed to truncate, disk_i_size is only adjusted down
4904 * as we remove extents, so it should represent the true
4905 * size of the inode, so reset the in memory size and
4906 * delete our orphan entry.
4907 */
4908 trans = btrfs_join_transaction(root);
4909 if (IS_ERR(trans)) {
4910 btrfs_orphan_del(NULL, inode);
4911 return ret;
4912 }
4913 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4914 err = btrfs_orphan_del(trans, inode);
4915 if (err)
4916 btrfs_abort_transaction(trans, root, err);
4917 btrfs_end_transaction(trans, root);
4918 }
Yan, Zheng80825102009-11-12 09:35:36 +00004919 }
4920
Josef Bacika41ad392011-01-31 15:30:16 -05004921 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004922}
4923
Chris Mason39279cc2007-06-12 06:35:45 -04004924static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4925{
4926 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004927 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004928 int err;
4929
Li Zefanb83cc962010-12-20 16:04:08 +08004930 if (btrfs_root_readonly(root))
4931 return -EROFS;
4932
Chris Mason39279cc2007-06-12 06:35:45 -04004933 err = inode_change_ok(inode, attr);
4934 if (err)
4935 return err;
4936
Chris Mason5a3f23d2009-03-31 13:27:11 -04004937 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004938 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004939 if (err)
4940 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004941 }
Yan Zheng9036c102008-10-30 14:19:41 -04004942
Christoph Hellwig10257742010-06-04 11:30:02 +02004943 if (attr->ia_valid) {
4944 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004945 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004946 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004947
Josef Bacik22c44fe2011-11-30 10:45:38 -05004948 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004949 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004950 }
4951
Chris Mason39279cc2007-06-12 06:35:45 -04004952 return err;
4953}
Chris Mason61295eb2008-01-14 16:24:38 -05004954
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004955/*
4956 * While truncating the inode pages during eviction, we get the VFS calling
4957 * btrfs_invalidatepage() against each page of the inode. This is slow because
4958 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4959 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4960 * extent_state structures over and over, wasting lots of time.
4961 *
4962 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4963 * those expensive operations on a per page basis and do only the ordered io
4964 * finishing, while we release here the extent_map and extent_state structures,
4965 * without the excessive merging and splitting.
4966 */
4967static void evict_inode_truncate_pages(struct inode *inode)
4968{
4969 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4970 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4971 struct rb_node *node;
4972
4973 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004974 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004975
4976 write_lock(&map_tree->lock);
4977 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4978 struct extent_map *em;
4979
4980 node = rb_first(&map_tree->map);
4981 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004982 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4983 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004984 remove_extent_mapping(map_tree, em);
4985 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004986 if (need_resched()) {
4987 write_unlock(&map_tree->lock);
4988 cond_resched();
4989 write_lock(&map_tree->lock);
4990 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004991 }
4992 write_unlock(&map_tree->lock);
4993
4994 spin_lock(&io_tree->lock);
4995 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4996 struct extent_state *state;
4997 struct extent_state *cached_state = NULL;
4998
4999 node = rb_first(&io_tree->state);
5000 state = rb_entry(node, struct extent_state, rb_node);
5001 atomic_inc(&state->refs);
5002 spin_unlock(&io_tree->lock);
5003
5004 lock_extent_bits(io_tree, state->start, state->end,
5005 0, &cached_state);
5006 clear_extent_bit(io_tree, state->start, state->end,
5007 EXTENT_LOCKED | EXTENT_DIRTY |
5008 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5009 EXTENT_DEFRAG, 1, 1,
5010 &cached_state, GFP_NOFS);
5011 free_extent_state(state);
5012
Filipe Manana7064dd52014-08-08 02:47:05 +01005013 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005014 spin_lock(&io_tree->lock);
5015 }
5016 spin_unlock(&io_tree->lock);
5017}
5018
Al Virobd555972010-06-07 11:35:40 -04005019void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005020{
5021 struct btrfs_trans_handle *trans;
5022 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005023 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005024 int steal_from_global = 0;
Josef Bacik07127182011-08-19 10:29:59 -04005025 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04005026 int ret;
5027
liubo1abe9b82011-03-24 11:18:59 +00005028 trace_btrfs_inode_evict(inode);
5029
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005030 evict_inode_truncate_pages(inode);
5031
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005032 if (inode->i_nlink &&
5033 ((btrfs_root_refs(&root->root_item) != 0 &&
5034 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5035 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005036 goto no_delete;
5037
Chris Mason39279cc2007-06-12 06:35:45 -04005038 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005039 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005040 goto no_delete;
5041 }
Al Virobd555972010-06-07 11:35:40 -04005042 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04005043 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005044
Miao Xief6124962014-09-12 18:44:04 +08005045 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5046
Yan, Zhengc71bf092009-11-12 09:34:40 +00005047 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06005048 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005049 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005050 goto no_delete;
5051 }
5052
Yan, Zheng76dda932009-09-21 16:00:26 -04005053 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005054 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5055 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005056 goto no_delete;
5057 }
5058
Miao Xie0e8c36a2012-12-19 06:59:51 +00005059 ret = btrfs_commit_inode_delayed_inode(inode);
5060 if (ret) {
5061 btrfs_orphan_del(NULL, inode);
5062 goto no_delete;
5063 }
5064
Miao Xie66d8f3d2012-09-06 04:02:28 -06005065 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005066 if (!rsv) {
5067 btrfs_orphan_del(NULL, inode);
5068 goto no_delete;
5069 }
Josef Bacik4a338542011-08-29 11:01:31 -04005070 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005071 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005072 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005073
Chris Masondbe674a2008-07-17 12:54:05 -04005074 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005075
Josef Bacik4289a662011-08-05 13:22:24 -04005076 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005077 * This is a bit simpler than btrfs_truncate since we've already
5078 * reserved our space for our orphan item in the unlink, so we just
5079 * need to reserve some slack space in case we add bytes and update
5080 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005081 */
Yan, Zheng80825102009-11-12 09:35:36 +00005082 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005083 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5084 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005085
Josef Bacik726c35f2011-09-26 15:46:06 -04005086 /*
5087 * Try and steal from the global reserve since we will
5088 * likely not use this space anyway, we want to try as
5089 * hard as possible to get this to work.
5090 */
5091 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005092 steal_from_global++;
5093 else
5094 steal_from_global = 0;
5095 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005096
Josef Bacik3bce8762015-02-24 12:35:51 -08005097 /*
5098 * steal_from_global == 0: we reserved stuff, hooray!
5099 * steal_from_global == 1: we didn't reserve stuff, boo!
5100 * steal_from_global == 2: we've committed, still not a lot of
5101 * room but maybe we'll have room in the global reserve this
5102 * time.
5103 * steal_from_global == 3: abandon all hope!
5104 */
5105 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005106 btrfs_warn(root->fs_info,
5107 "Could not get space for a delete, will truncate on mount %d",
5108 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005109 btrfs_orphan_del(NULL, inode);
5110 btrfs_free_block_rsv(root, rsv);
5111 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005112 }
5113
Miao Xie0e8c36a2012-12-19 06:59:51 +00005114 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005115 if (IS_ERR(trans)) {
5116 btrfs_orphan_del(NULL, inode);
5117 btrfs_free_block_rsv(root, rsv);
5118 goto no_delete;
5119 }
5120
Josef Bacik3bce8762015-02-24 12:35:51 -08005121 /*
5122 * We can't just steal from the global reserve, we need tomake
5123 * sure there is room to do it, if not we need to commit and try
5124 * again.
5125 */
5126 if (steal_from_global) {
5127 if (!btrfs_check_space_for_delayed_refs(trans, root))
5128 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5129 min_size);
5130 else
5131 ret = -ENOSPC;
5132 }
5133
5134 /*
5135 * Couldn't steal from the global reserve, we have too much
5136 * pending stuff built up, commit the transaction and try it
5137 * again.
5138 */
5139 if (ret) {
5140 ret = btrfs_commit_transaction(trans, root);
5141 if (ret) {
5142 btrfs_orphan_del(NULL, inode);
5143 btrfs_free_block_rsv(root, rsv);
5144 goto no_delete;
5145 }
5146 continue;
5147 } else {
5148 steal_from_global = 0;
5149 }
5150
Josef Bacik4289a662011-08-05 13:22:24 -04005151 trans->block_rsv = rsv;
5152
Yan, Zhengd68fc572010-05-16 10:49:58 -04005153 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005154 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005155 break;
5156
Miao Xie8407aa42012-09-07 01:43:32 -06005157 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005158 btrfs_end_transaction(trans, root);
5159 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005160 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005161 }
5162
Josef Bacik4289a662011-08-05 13:22:24 -04005163 btrfs_free_block_rsv(root, rsv);
5164
Josef Bacik4ef31a42013-08-13 14:10:08 -04005165 /*
5166 * Errors here aren't a big deal, it just means we leave orphan items
5167 * in the tree. They will be cleaned up on the next mount.
5168 */
Yan, Zheng80825102009-11-12 09:35:36 +00005169 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005170 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005171 btrfs_orphan_del(trans, inode);
5172 } else {
5173 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005174 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005175
Josef Bacik4289a662011-08-05 13:22:24 -04005176 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005177 if (!(root == root->fs_info->tree_root ||
5178 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005179 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005180
Chris Mason54aa1f42007-06-22 14:16:25 -04005181 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005182 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005183no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005184 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005185 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005186 return;
Chris Mason39279cc2007-06-12 06:35:45 -04005187}
5188
5189/*
5190 * this returns the key found in the dir entry in the location pointer.
5191 * If no dir entries were found, location->objectid is 0.
5192 */
5193static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5194 struct btrfs_key *location)
5195{
5196 const char *name = dentry->d_name.name;
5197 int namelen = dentry->d_name.len;
5198 struct btrfs_dir_item *di;
5199 struct btrfs_path *path;
5200 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005201 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005202
5203 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005204 if (!path)
5205 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005206
Li Zefan33345d012011-04-20 10:31:50 +08005207 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005208 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005209 if (IS_ERR(di))
5210 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005211
David Sterbac7040052011-04-19 18:00:01 +02005212 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005213 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005214
Chris Mason5f39d392007-10-15 16:14:19 -04005215 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005216out:
Chris Mason39279cc2007-06-12 06:35:45 -04005217 btrfs_free_path(path);
5218 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005219out_err:
5220 location->objectid = 0;
5221 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005222}
5223
5224/*
5225 * when we hit a tree root in a directory, the btrfs part of the inode
5226 * needs to be changed to reflect the root directory of the tree root. This
5227 * is kind of like crossing a mount point.
5228 */
5229static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005230 struct inode *dir,
5231 struct dentry *dentry,
5232 struct btrfs_key *location,
5233 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005234{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005235 struct btrfs_path *path;
5236 struct btrfs_root *new_root;
5237 struct btrfs_root_ref *ref;
5238 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005239 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005240 int ret;
5241 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005242
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005243 path = btrfs_alloc_path();
5244 if (!path) {
5245 err = -ENOMEM;
5246 goto out;
5247 }
Chris Mason39279cc2007-06-12 06:35:45 -04005248
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005249 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005250 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5251 key.type = BTRFS_ROOT_REF_KEY;
5252 key.offset = location->objectid;
5253
5254 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5255 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005256 if (ret) {
5257 if (ret < 0)
5258 err = ret;
5259 goto out;
5260 }
Chris Mason39279cc2007-06-12 06:35:45 -04005261
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005262 leaf = path->nodes[0];
5263 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005264 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005265 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5266 goto out;
5267
5268 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5269 (unsigned long)(ref + 1),
5270 dentry->d_name.len);
5271 if (ret)
5272 goto out;
5273
David Sterbab3b4aa72011-04-21 01:20:15 +02005274 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005275
5276 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5277 if (IS_ERR(new_root)) {
5278 err = PTR_ERR(new_root);
5279 goto out;
5280 }
5281
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005282 *sub_root = new_root;
5283 location->objectid = btrfs_root_dirid(&new_root->root_item);
5284 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005285 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005286 err = 0;
5287out:
5288 btrfs_free_path(path);
5289 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005290}
5291
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005292static void inode_tree_add(struct inode *inode)
5293{
5294 struct btrfs_root *root = BTRFS_I(inode)->root;
5295 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005296 struct rb_node **p;
5297 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005298 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005299 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005300
Al Viro1d3382cb2010-10-23 15:19:20 -04005301 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005302 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005303 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005304 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005305 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005306 while (*p) {
5307 parent = *p;
5308 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5309
Li Zefan33345d012011-04-20 10:31:50 +08005310 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005311 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005312 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005313 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005314 else {
5315 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005316 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005317 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005318 RB_CLEAR_NODE(parent);
5319 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005320 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005321 }
5322 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005323 rb_link_node(new, parent, p);
5324 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005325 spin_unlock(&root->inode_lock);
5326}
5327
5328static void inode_tree_del(struct inode *inode)
5329{
5330 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005331 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005332
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005333 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005334 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005335 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005336 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005337 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005338 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005339 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005340
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005341 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005342 synchronize_srcu(&root->fs_info->subvol_srcu);
5343 spin_lock(&root->inode_lock);
5344 empty = RB_EMPTY_ROOT(&root->inode_tree);
5345 spin_unlock(&root->inode_lock);
5346 if (empty)
5347 btrfs_add_dead_root(root);
5348 }
5349}
5350
Jeff Mahoney143bede2012-03-01 14:56:26 +01005351void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005352{
5353 struct rb_node *node;
5354 struct rb_node *prev;
5355 struct btrfs_inode *entry;
5356 struct inode *inode;
5357 u64 objectid = 0;
5358
Liu Bo7813b3d2014-02-10 17:37:25 +08005359 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5360 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005361
5362 spin_lock(&root->inode_lock);
5363again:
5364 node = root->inode_tree.rb_node;
5365 prev = NULL;
5366 while (node) {
5367 prev = node;
5368 entry = rb_entry(node, struct btrfs_inode, rb_node);
5369
Li Zefan33345d012011-04-20 10:31:50 +08005370 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005371 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005372 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005373 node = node->rb_right;
5374 else
5375 break;
5376 }
5377 if (!node) {
5378 while (prev) {
5379 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005380 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005381 node = prev;
5382 break;
5383 }
5384 prev = rb_next(prev);
5385 }
5386 }
5387 while (node) {
5388 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005389 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005390 inode = igrab(&entry->vfs_inode);
5391 if (inode) {
5392 spin_unlock(&root->inode_lock);
5393 if (atomic_read(&inode->i_count) > 1)
5394 d_prune_aliases(inode);
5395 /*
Al Viro45321ac2010-06-07 13:43:19 -04005396 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005397 * the inode cache when its usage count
5398 * hits zero.
5399 */
5400 iput(inode);
5401 cond_resched();
5402 spin_lock(&root->inode_lock);
5403 goto again;
5404 }
5405
5406 if (cond_resched_lock(&root->inode_lock))
5407 goto again;
5408
5409 node = rb_next(node);
5410 }
5411 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005412}
5413
Chris Masone02119d2008-09-05 16:13:11 -04005414static int btrfs_init_locked_inode(struct inode *inode, void *p)
5415{
5416 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005417 inode->i_ino = args->location->objectid;
5418 memcpy(&BTRFS_I(inode)->location, args->location,
5419 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005420 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005421 return 0;
5422}
5423
5424static int btrfs_find_actor(struct inode *inode, void *opaque)
5425{
5426 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005427 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005428 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005429}
5430
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005431static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005432 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005433 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005434{
5435 struct inode *inode;
5436 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005437 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005438
Chris Mason90d3e592014-01-09 17:28:00 -08005439 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005440 args.root = root;
5441
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005442 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005443 btrfs_init_locked_inode,
5444 (void *)&args);
5445 return inode;
5446}
5447
Balaji Rao1a54ef82008-07-21 02:01:04 +05305448/* Get an inode object given its location and corresponding root.
5449 * Returns in *is_new if the inode was read from disk
5450 */
5451struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005452 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305453{
5454 struct inode *inode;
5455
Chris Mason90d3e592014-01-09 17:28:00 -08005456 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305457 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005458 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305459
5460 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305461 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005462 if (!is_bad_inode(inode)) {
5463 inode_tree_add(inode);
5464 unlock_new_inode(inode);
5465 if (new)
5466 *new = 1;
5467 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005468 unlock_new_inode(inode);
5469 iput(inode);
5470 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005471 }
5472 }
5473
Balaji Rao1a54ef82008-07-21 02:01:04 +05305474 return inode;
5475}
5476
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005477static struct inode *new_simple_dir(struct super_block *s,
5478 struct btrfs_key *key,
5479 struct btrfs_root *root)
5480{
5481 struct inode *inode = new_inode(s);
5482
5483 if (!inode)
5484 return ERR_PTR(-ENOMEM);
5485
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005486 BTRFS_I(inode)->root = root;
5487 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005488 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005489
5490 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005491 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005492 inode->i_fop = &simple_dir_operations;
5493 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
chandan r9cc97d62012-07-04 12:48:07 +05305494 inode->i_mtime = CURRENT_TIME;
5495 inode->i_atime = inode->i_mtime;
5496 inode->i_ctime = inode->i_mtime;
5497 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005498
5499 return inode;
5500}
5501
Chris Mason3de45862008-11-17 21:02:50 -05005502struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005503{
Chris Masond3977122009-01-05 21:25:51 -05005504 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005505 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005506 struct btrfs_root *sub_root = root;
5507 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005508 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005509 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005510
5511 if (dentry->d_name.len > BTRFS_NAME_LEN)
5512 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005513
Jeff Layton39e3c952012-11-28 11:30:53 -05005514 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005515 if (ret < 0)
5516 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005517
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005518 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005519 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005520
5521 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005522 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005523 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005524 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005525
5526 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5527
Yan, Zheng76dda932009-09-21 16:00:26 -04005528 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005529 ret = fixup_tree_root_location(root, dir, dentry,
5530 &location, &sub_root);
5531 if (ret < 0) {
5532 if (ret != -ENOENT)
5533 inode = ERR_PTR(ret);
5534 else
5535 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5536 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005537 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005538 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005539 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5540
Julia Lawall34d19ba2011-01-24 19:55:19 +00005541 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005542 down_read(&root->fs_info->cleanup_work_sem);
5543 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005544 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005545 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005546 if (ret) {
5547 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005548 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005549 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005550 }
5551
Chris Mason3de45862008-11-17 21:02:50 -05005552 return inode;
5553}
5554
Nick Pigginfe15ce42011-01-07 17:49:23 +11005555static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005556{
5557 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005558 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005559
Li Zefan848cce02012-02-21 17:04:28 +08005560 if (!inode && !IS_ROOT(dentry))
5561 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005562
Li Zefan848cce02012-02-21 17:04:28 +08005563 if (inode) {
5564 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005565 if (btrfs_root_refs(&root->root_item) == 0)
5566 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005567
5568 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5569 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005570 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005571 return 0;
5572}
5573
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005574static void btrfs_dentry_release(struct dentry *dentry)
5575{
Daeseok Youn944a4512014-04-14 15:37:02 +09005576 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005577}
5578
Chris Mason3de45862008-11-17 21:02:50 -05005579static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005580 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005581{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005582 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005583
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005584 inode = btrfs_lookup_dentry(dir, dentry);
5585 if (IS_ERR(inode)) {
5586 if (PTR_ERR(inode) == -ENOENT)
5587 inode = NULL;
5588 else
5589 return ERR_CAST(inode);
5590 }
5591
Al Viro41d28bc2014-10-12 22:24:21 -04005592 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005593}
5594
Miao Xie16cdcec2011-04-22 18:12:22 +08005595unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005596 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5597};
5598
Al Viro9cdda8d2013-05-22 16:48:09 -04005599static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005600{
Al Viro9cdda8d2013-05-22 16:48:09 -04005601 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005602 struct btrfs_root *root = BTRFS_I(inode)->root;
5603 struct btrfs_item *item;
5604 struct btrfs_dir_item *di;
5605 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005606 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005607 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005608 struct list_head ins_list;
5609 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005610 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005611 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005612 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005613 unsigned char d_type;
5614 int over = 0;
5615 u32 di_cur;
5616 u32 di_total;
5617 u32 di_len;
5618 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005619 char tmp_name[32];
5620 char *name_ptr;
5621 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005622 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005623
5624 /* FIXME, use a real flag for deciding about the key type */
5625 if (root->fs_info->tree_root == root)
5626 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005627
Al Viro9cdda8d2013-05-22 16:48:09 -04005628 if (!dir_emit_dots(file, ctx))
5629 return 0;
5630
David Woodhouse49593bf2008-08-17 17:08:36 +01005631 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005632 if (!path)
5633 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005634
Josef Bacik026fd312011-05-13 10:32:11 -04005635 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005636
Miao Xie16cdcec2011-04-22 18:12:22 +08005637 if (key_type == BTRFS_DIR_INDEX_KEY) {
5638 INIT_LIST_HEAD(&ins_list);
5639 INIT_LIST_HEAD(&del_list);
5640 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5641 }
5642
David Sterba962a2982014-06-04 18:41:45 +02005643 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005644 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005645 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005646
Chris Mason39279cc2007-06-12 06:35:45 -04005647 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5648 if (ret < 0)
5649 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005650
5651 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005652 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005653 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005654 if (slot >= btrfs_header_nritems(leaf)) {
5655 ret = btrfs_next_leaf(root, path);
5656 if (ret < 0)
5657 goto err;
5658 else if (ret > 0)
5659 break;
5660 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005661 }
Chris Mason3de45862008-11-17 21:02:50 -05005662
Ross Kirkdd3cc162013-09-16 15:58:09 +01005663 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005664 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5665
5666 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005667 break;
David Sterba962a2982014-06-04 18:41:45 +02005668 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005669 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005670 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005671 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005672 if (key_type == BTRFS_DIR_INDEX_KEY &&
5673 btrfs_should_delete_dir_index(&del_list,
5674 found_key.offset))
5675 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005676
Al Viro9cdda8d2013-05-22 16:48:09 -04005677 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005678 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005679
Chris Mason39279cc2007-06-12 06:35:45 -04005680 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5681 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005682 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005683
5684 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005685 struct btrfs_key location;
5686
Josef Bacik22a94d42011-03-16 16:47:17 -04005687 if (verify_dir_item(root, leaf, di))
5688 break;
5689
Chris Mason5f39d392007-10-15 16:14:19 -04005690 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005691 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005692 name_ptr = tmp_name;
5693 } else {
5694 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005695 if (!name_ptr) {
5696 ret = -ENOMEM;
5697 goto err;
5698 }
Chris Mason5f39d392007-10-15 16:14:19 -04005699 }
5700 read_extent_buffer(leaf, name_ptr,
5701 (unsigned long)(di + 1), name_len);
5702
5703 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5704 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005705
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005706
Chris Mason3de45862008-11-17 21:02:50 -05005707 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005708 * skip it.
5709 *
5710 * In contrast to old kernels, we insert the snapshot's
5711 * dir item and dir index after it has been created, so
5712 * we won't find a reference to our own snapshot. We
5713 * still keep the following code for backward
5714 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005715 */
5716 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5717 location.objectid == root->root_key.objectid) {
5718 over = 0;
5719 goto skip;
5720 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005721 over = !dir_emit(ctx, name_ptr, name_len,
5722 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005723
Chris Mason3de45862008-11-17 21:02:50 -05005724skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005725 if (name_ptr != tmp_name)
5726 kfree(name_ptr);
5727
Chris Mason39279cc2007-06-12 06:35:45 -04005728 if (over)
5729 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005730 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005731 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005732 di_cur += di_len;
5733 di = (struct btrfs_dir_item *)((char *)di + di_len);
5734 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005735next:
5736 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005737 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005738
Miao Xie16cdcec2011-04-22 18:12:22 +08005739 if (key_type == BTRFS_DIR_INDEX_KEY) {
5740 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005741 ctx->pos++;
5742 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005743 if (ret)
5744 goto nopos;
5745 }
5746
David Woodhouse49593bf2008-08-17 17:08:36 +01005747 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005748 ctx->pos++;
5749
5750 /*
5751 * Stop new entries from being returned after we return the last
5752 * entry.
5753 *
5754 * New directory entries are assigned a strictly increasing
5755 * offset. This means that new entries created during readdir
5756 * are *guaranteed* to be seen in the future by that readdir.
5757 * This has broken buggy programs which operate on names as
5758 * they're returned by readdir. Until we re-use freed offsets
5759 * we have this hack to stop new entries from being returned
5760 * under the assumption that they'll never reach this huge
5761 * offset.
5762 *
5763 * This is being careful not to overflow 32bit loff_t unless the
5764 * last entry requires it because doing so has broken 32bit apps
5765 * in the past.
5766 */
5767 if (key_type == BTRFS_DIR_INDEX_KEY) {
5768 if (ctx->pos >= INT_MAX)
5769 ctx->pos = LLONG_MAX;
5770 else
5771 ctx->pos = INT_MAX;
5772 }
Chris Mason39279cc2007-06-12 06:35:45 -04005773nopos:
5774 ret = 0;
5775err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005776 if (key_type == BTRFS_DIR_INDEX_KEY)
5777 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005778 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005779 return ret;
5780}
5781
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005782int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005783{
5784 struct btrfs_root *root = BTRFS_I(inode)->root;
5785 struct btrfs_trans_handle *trans;
5786 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005787 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005788
Josef Bacik72ac3c02012-05-23 14:13:11 -04005789 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005790 return 0;
5791
Liu Bo83eea1f2012-07-10 05:28:39 -06005792 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005793 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005794
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005795 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005796 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005797 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005798 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005799 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005800 if (IS_ERR(trans))
5801 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005802 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005803 }
5804 return ret;
5805}
5806
5807/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005808 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005809 * inode changes. But, it is most likely to find the inode in cache.
5810 * FIXME, needs more benchmarking...there are no reasons other than performance
5811 * to keep or drop this code.
5812 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005813static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005814{
5815 struct btrfs_root *root = BTRFS_I(inode)->root;
5816 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005817 int ret;
5818
Josef Bacik72ac3c02012-05-23 14:13:11 -04005819 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005820 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005821
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005822 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005823 if (IS_ERR(trans))
5824 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005825
5826 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005827 if (ret && ret == -ENOSPC) {
5828 /* whoops, lets try again with the full transaction */
5829 btrfs_end_transaction(trans, root);
5830 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005831 if (IS_ERR(trans))
5832 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005833
Chris Mason94b60442010-05-26 11:02:00 -04005834 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005835 }
Chris Mason39279cc2007-06-12 06:35:45 -04005836 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005837 if (BTRFS_I(inode)->delayed_node)
5838 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005839
5840 return ret;
5841}
5842
5843/*
5844 * This is a copy of file_update_time. We need this so we can return error on
5845 * ENOSPC for updating the inode in the case of file write and mmap writes.
5846 */
Josef Bacike41f9412012-03-26 09:46:47 -04005847static int btrfs_update_time(struct inode *inode, struct timespec *now,
5848 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005849{
Alexander Block2bc5565282012-06-15 09:49:33 +02005850 struct btrfs_root *root = BTRFS_I(inode)->root;
5851
5852 if (btrfs_root_readonly(root))
5853 return -EROFS;
5854
Josef Bacike41f9412012-03-26 09:46:47 -04005855 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005856 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005857 if (flags & S_CTIME)
5858 inode->i_ctime = *now;
5859 if (flags & S_MTIME)
5860 inode->i_mtime = *now;
5861 if (flags & S_ATIME)
5862 inode->i_atime = *now;
5863 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005864}
5865
Chris Masond352ac62008-09-29 15:18:18 -04005866/*
5867 * find the highest existing sequence number in a directory
5868 * and then set the in-memory index_cnt variable to reflect
5869 * free sequence numbers
5870 */
Josef Bacikaec74772008-07-24 12:12:38 -04005871static int btrfs_set_inode_index_count(struct inode *inode)
5872{
5873 struct btrfs_root *root = BTRFS_I(inode)->root;
5874 struct btrfs_key key, found_key;
5875 struct btrfs_path *path;
5876 struct extent_buffer *leaf;
5877 int ret;
5878
Li Zefan33345d012011-04-20 10:31:50 +08005879 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005880 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005881 key.offset = (u64)-1;
5882
5883 path = btrfs_alloc_path();
5884 if (!path)
5885 return -ENOMEM;
5886
5887 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5888 if (ret < 0)
5889 goto out;
5890 /* FIXME: we should be able to handle this */
5891 if (ret == 0)
5892 goto out;
5893 ret = 0;
5894
5895 /*
5896 * MAGIC NUMBER EXPLANATION:
5897 * since we search a directory based on f_pos we have to start at 2
5898 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5899 * else has to start at 2
5900 */
5901 if (path->slots[0] == 0) {
5902 BTRFS_I(inode)->index_cnt = 2;
5903 goto out;
5904 }
5905
5906 path->slots[0]--;
5907
5908 leaf = path->nodes[0];
5909 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5910
Li Zefan33345d012011-04-20 10:31:50 +08005911 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005912 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005913 BTRFS_I(inode)->index_cnt = 2;
5914 goto out;
5915 }
5916
5917 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5918out:
5919 btrfs_free_path(path);
5920 return ret;
5921}
5922
Chris Masond352ac62008-09-29 15:18:18 -04005923/*
5924 * helper to find a free sequence number in a given directory. This current
5925 * code is very simple, later versions will do smarter things in the btree
5926 */
Chris Mason3de45862008-11-17 21:02:50 -05005927int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005928{
5929 int ret = 0;
5930
5931 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005932 ret = btrfs_inode_delayed_dir_index_count(dir);
5933 if (ret) {
5934 ret = btrfs_set_inode_index_count(dir);
5935 if (ret)
5936 return ret;
5937 }
Josef Bacikaec74772008-07-24 12:12:38 -04005938 }
5939
Chris Mason00e4e6b2008-08-05 11:18:09 -04005940 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005941 BTRFS_I(dir)->index_cnt++;
5942
5943 return ret;
5944}
5945
Chris Masonb0d5d102014-09-08 13:08:51 -07005946static int btrfs_insert_inode_locked(struct inode *inode)
5947{
5948 struct btrfs_iget_args args;
5949 args.location = &BTRFS_I(inode)->location;
5950 args.root = BTRFS_I(inode)->root;
5951
5952 return insert_inode_locked4(inode,
5953 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5954 btrfs_find_actor, &args);
5955}
5956
Chris Mason39279cc2007-06-12 06:35:45 -04005957static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5958 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005959 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005960 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005961 u64 ref_objectid, u64 objectid,
5962 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005963{
5964 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005965 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005966 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005967 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005968 struct btrfs_inode_ref *ref;
5969 struct btrfs_key key[2];
5970 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005971 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005972 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005973 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005974
Chris Mason5f39d392007-10-15 16:14:19 -04005975 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005976 if (!path)
5977 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005978
Chris Mason39279cc2007-06-12 06:35:45 -04005979 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005980 if (!inode) {
5981 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005982 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005983 }
Chris Mason39279cc2007-06-12 06:35:45 -04005984
Li Zefan581bb052011-04-20 10:06:11 +08005985 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005986 * O_TMPFILE, set link count to 0, so that after this point,
5987 * we fill in an inode item with the correct link count.
5988 */
5989 if (!name)
5990 set_nlink(inode, 0);
5991
5992 /*
Li Zefan581bb052011-04-20 10:06:11 +08005993 * we have to initialize this early, so we can reclaim the inode
5994 * number if we fail afterwards in this function.
5995 */
5996 inode->i_ino = objectid;
5997
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005998 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005999 trace_btrfs_inode_request(dir);
6000
Chris Mason3de45862008-11-17 21:02:50 -05006001 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006002 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006003 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006004 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006005 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006006 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006007 } else if (dir) {
6008 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006009 }
6010 /*
6011 * index_cnt is ignored for everything but a dir,
6012 * btrfs_get_inode_index_count has an explanation for the magic
6013 * number
6014 */
6015 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006016 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006017 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006018 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006019 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006020
Josef Bacik5dc562c2012-08-17 13:14:17 -04006021 /*
6022 * We could have gotten an inode number from somebody who was fsynced
6023 * and then removed in this same transaction, so let's just set full
6024 * sync since it will be a full sync anyway and this will blow away the
6025 * old info in the log.
6026 */
6027 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6028
Chris Mason9c583092008-01-29 15:15:18 -05006029 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006030 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006031 key[0].offset = 0;
6032
Chris Mason9c583092008-01-29 15:15:18 -05006033 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006034
6035 if (name) {
6036 /*
6037 * Start new inodes with an inode_ref. This is slightly more
6038 * efficient for small numbers of hard links since they will
6039 * be packed into one item. Extended refs will kick in if we
6040 * add more hard links than can fit in the ref item.
6041 */
6042 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006043 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006044 key[1].offset = ref_objectid;
6045
6046 sizes[1] = name_len + sizeof(*ref);
6047 }
Chris Mason9c583092008-01-29 15:15:18 -05006048
Chris Masonb0d5d102014-09-08 13:08:51 -07006049 location = &BTRFS_I(inode)->location;
6050 location->objectid = objectid;
6051 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006052 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006053
6054 ret = btrfs_insert_inode_locked(inode);
6055 if (ret < 0)
6056 goto fail;
6057
Chris Masonb9473432009-03-13 11:00:37 -04006058 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006059 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006060 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006061 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006062
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006063 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006064 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306065
6066 inode->i_mtime = CURRENT_TIME;
6067 inode->i_atime = inode->i_mtime;
6068 inode->i_ctime = inode->i_mtime;
6069 BTRFS_I(inode)->i_otime = inode->i_mtime;
6070
Chris Mason5f39d392007-10-15 16:14:19 -04006071 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6072 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006073 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6074 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006075 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006076
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006077 if (name) {
6078 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6079 struct btrfs_inode_ref);
6080 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6081 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6082 ptr = (unsigned long)(ref + 1);
6083 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6084 }
Chris Mason9c583092008-01-29 15:15:18 -05006085
Chris Mason5f39d392007-10-15 16:14:19 -04006086 btrfs_mark_buffer_dirty(path->nodes[0]);
6087 btrfs_free_path(path);
6088
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006089 btrfs_inherit_iflags(inode, dir);
6090
Al Viro569254b2011-07-24 17:08:40 -04006091 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04006092 if (btrfs_test_opt(root, NODATASUM))
6093 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06006094 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006095 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6096 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006097 }
6098
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006099 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006100
6101 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006102 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006103
Alexander Block8ea05e32012-07-25 17:35:53 +02006104 btrfs_update_root_times(trans, root);
6105
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006106 ret = btrfs_inode_inherit_props(trans, inode, dir);
6107 if (ret)
6108 btrfs_err(root->fs_info,
6109 "error inheriting props for ino %llu (root %llu): %d",
6110 btrfs_ino(inode), root->root_key.objectid, ret);
6111
Chris Mason39279cc2007-06-12 06:35:45 -04006112 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006113
6114fail_unlock:
6115 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006116fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006117 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006118 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006119 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006120 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006121 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006122}
6123
6124static inline u8 btrfs_inode_type(struct inode *inode)
6125{
6126 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6127}
6128
Chris Masond352ac62008-09-29 15:18:18 -04006129/*
6130 * utility function to add 'inode' into 'parent_inode' with
6131 * a give name and a given sequence number.
6132 * if 'add_backref' is true, also insert a backref from the
6133 * inode to the parent directory.
6134 */
Chris Masone02119d2008-09-05 16:13:11 -04006135int btrfs_add_link(struct btrfs_trans_handle *trans,
6136 struct inode *parent_inode, struct inode *inode,
6137 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006138{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006139 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006140 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006141 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006142 u64 ino = btrfs_ino(inode);
6143 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006144
Li Zefan33345d012011-04-20 10:31:50 +08006145 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006146 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6147 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006148 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006149 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006150 key.offset = 0;
6151 }
Chris Mason39279cc2007-06-12 06:35:45 -04006152
Li Zefan33345d012011-04-20 10:31:50 +08006153 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006154 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6155 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006156 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006157 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006158 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6159 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006160 }
6161
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006162 /* Nothing to clean up yet */
6163 if (ret)
6164 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006165
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006166 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6167 parent_inode, &key,
6168 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006169 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006170 goto fail_dir_item;
6171 else if (ret) {
6172 btrfs_abort_transaction(trans, root, ret);
6173 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006174 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006175
6176 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6177 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006178 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006179 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
6180 ret = btrfs_update_inode(trans, root, parent_inode);
6181 if (ret)
6182 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006183 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006184
6185fail_dir_item:
6186 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6187 u64 local_index;
6188 int err;
6189 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6190 key.objectid, root->root_key.objectid,
6191 parent_ino, &local_index, name, name_len);
6192
6193 } else if (add_backref) {
6194 u64 local_index;
6195 int err;
6196
6197 err = btrfs_del_inode_ref(trans, root, name, name_len,
6198 ino, parent_ino, &local_index);
6199 }
6200 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006201}
6202
6203static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006204 struct inode *dir, struct dentry *dentry,
6205 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006206{
Josef Bacika1b075d2010-11-19 20:36:11 +00006207 int err = btrfs_add_link(trans, dir, inode,
6208 dentry->d_name.name, dentry->d_name.len,
6209 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006210 if (err > 0)
6211 err = -EEXIST;
6212 return err;
6213}
6214
Josef Bacik618e21d2007-07-11 10:18:17 -04006215static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006216 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006217{
6218 struct btrfs_trans_handle *trans;
6219 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006220 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006221 int err;
6222 int drop_inode = 0;
6223 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006224 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006225
6226 if (!new_valid_dev(rdev))
6227 return -EINVAL;
6228
Josef Bacik9ed74f22009-09-11 16:12:44 -04006229 /*
6230 * 2 for inode item and ref
6231 * 2 for dir items
6232 * 1 for xattr if selinux is on
6233 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006234 trans = btrfs_start_transaction(root, 5);
6235 if (IS_ERR(trans))
6236 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006237
Li Zefan581bb052011-04-20 10:06:11 +08006238 err = btrfs_find_free_ino(root, &objectid);
6239 if (err)
6240 goto out_unlock;
6241
Josef Bacikaec74772008-07-24 12:12:38 -04006242 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006243 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006244 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006245 if (IS_ERR(inode)) {
6246 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006247 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006248 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006249
Casey Schauflerad19db72011-12-15 10:09:07 -05006250 /*
6251 * If the active LSM wants to access the inode during
6252 * d_instantiate it needs these. Smack checks to see
6253 * if the filesystem supports xattrs by looking at the
6254 * ops vector.
6255 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006256 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006257 init_special_inode(inode, inode->i_mode, rdev);
6258
6259 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006260 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006261 goto out_unlock_inode;
6262
6263 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6264 if (err) {
6265 goto out_unlock_inode;
6266 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006267 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006268 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006269 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006270 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006271
Josef Bacik618e21d2007-07-11 10:18:17 -04006272out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006273 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006274 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006275 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006276 if (drop_inode) {
6277 inode_dec_link_count(inode);
6278 iput(inode);
6279 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006280 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006281
6282out_unlock_inode:
6283 drop_inode = 1;
6284 unlock_new_inode(inode);
6285 goto out_unlock;
6286
Josef Bacik618e21d2007-07-11 10:18:17 -04006287}
6288
Chris Mason39279cc2007-06-12 06:35:45 -04006289static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006290 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006291{
6292 struct btrfs_trans_handle *trans;
6293 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006294 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006295 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006296 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006297 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006298 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006299
Josef Bacik9ed74f22009-09-11 16:12:44 -04006300 /*
6301 * 2 for inode item and ref
6302 * 2 for dir items
6303 * 1 for xattr if selinux is on
6304 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006305 trans = btrfs_start_transaction(root, 5);
6306 if (IS_ERR(trans))
6307 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006308
Li Zefan581bb052011-04-20 10:06:11 +08006309 err = btrfs_find_free_ino(root, &objectid);
6310 if (err)
6311 goto out_unlock;
6312
Josef Bacikaec74772008-07-24 12:12:38 -04006313 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006314 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006315 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006316 if (IS_ERR(inode)) {
6317 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006318 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006319 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006320 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006321 /*
6322 * If the active LSM wants to access the inode during
6323 * d_instantiate it needs these. Smack checks to see
6324 * if the filesystem supports xattrs by looking at the
6325 * ops vector.
6326 */
6327 inode->i_fop = &btrfs_file_operations;
6328 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006329 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006330
6331 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6332 if (err)
6333 goto out_unlock_inode;
6334
6335 err = btrfs_update_inode(trans, root, inode);
6336 if (err)
6337 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006338
Josef Bacika1b075d2010-11-19 20:36:11 +00006339 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006340 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006341 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006342
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006343 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006344 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006345 d_instantiate(dentry, inode);
6346
Chris Mason39279cc2007-06-12 06:35:45 -04006347out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006348 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006349 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006350 inode_dec_link_count(inode);
6351 iput(inode);
6352 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006353 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006354 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006355 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006356
6357out_unlock_inode:
6358 unlock_new_inode(inode);
6359 goto out_unlock;
6360
Chris Mason39279cc2007-06-12 06:35:45 -04006361}
6362
6363static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6364 struct dentry *dentry)
6365{
6366 struct btrfs_trans_handle *trans;
6367 struct btrfs_root *root = BTRFS_I(dir)->root;
6368 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006369 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006370 int err;
6371 int drop_inode = 0;
6372
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006373 /* do not allow sys_link's with other subvols of the same device */
6374 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006375 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006376
Mark Fashehf1863732012-08-08 11:32:27 -07006377 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006378 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006379
Chris Mason3de45862008-11-17 21:02:50 -05006380 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006381 if (err)
6382 goto fail;
6383
Yan, Zhenga22285a2010-05-16 10:48:46 -04006384 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006385 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006386 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006387 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006388 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006389 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006390 if (IS_ERR(trans)) {
6391 err = PTR_ERR(trans);
6392 goto fail;
6393 }
Chris Mason5f39d392007-10-15 16:14:19 -04006394
Miao Xie67de1172013-12-26 13:07:06 +08006395 /* There are several dir indexes for this inode, clear the cache. */
6396 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006397 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006398 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006399 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006400 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006401 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006402
Josef Bacika1b075d2010-11-19 20:36:11 +00006403 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006404
Yan, Zhenga5719522009-09-24 09:17:31 -04006405 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006406 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006407 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006408 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006409 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006410 if (err)
6411 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006412 if (inode->i_nlink == 1) {
6413 /*
6414 * If new hard link count is 1, it's a file created
6415 * with open(2) O_TMPFILE flag.
6416 */
6417 err = btrfs_orphan_del(trans, inode);
6418 if (err)
6419 goto fail;
6420 }
Al Viro08c422c2011-12-23 07:58:13 -05006421 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006422 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006423 }
Chris Mason39279cc2007-06-12 06:35:45 -04006424
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006425 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006426 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006427fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006428 if (drop_inode) {
6429 inode_dec_link_count(inode);
6430 iput(inode);
6431 }
Liu Bob53d3f52012-11-14 14:34:34 +00006432 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006433 return err;
6434}
6435
Al Viro18bb1db2011-07-26 01:41:39 -04006436static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006437{
Chris Masonb9d86662008-05-02 16:13:49 -04006438 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006439 struct btrfs_trans_handle *trans;
6440 struct btrfs_root *root = BTRFS_I(dir)->root;
6441 int err = 0;
6442 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006443 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006444 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006445
Josef Bacik9ed74f22009-09-11 16:12:44 -04006446 /*
6447 * 2 items for inode and ref
6448 * 2 items for dir items
6449 * 1 for xattr if selinux is on
6450 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006451 trans = btrfs_start_transaction(root, 5);
6452 if (IS_ERR(trans))
6453 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006454
Li Zefan581bb052011-04-20 10:06:11 +08006455 err = btrfs_find_free_ino(root, &objectid);
6456 if (err)
6457 goto out_fail;
6458
Josef Bacikaec74772008-07-24 12:12:38 -04006459 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006460 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006461 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006462 if (IS_ERR(inode)) {
6463 err = PTR_ERR(inode);
6464 goto out_fail;
6465 }
Chris Mason5f39d392007-10-15 16:14:19 -04006466
Chris Mason39279cc2007-06-12 06:35:45 -04006467 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006468 /* these must be set before we unlock the inode */
6469 inode->i_op = &btrfs_dir_inode_operations;
6470 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006471
Eric Paris2a7dba32011-02-01 11:05:39 -05006472 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006473 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006474 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006475
Chris Masondbe674a2008-07-17 12:54:05 -04006476 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006477 err = btrfs_update_inode(trans, root, inode);
6478 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006479 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006480
Josef Bacika1b075d2010-11-19 20:36:11 +00006481 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6482 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006483 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006484 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006485
Chris Mason39279cc2007-06-12 06:35:45 -04006486 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006487 /*
6488 * mkdir is special. We're unlocking after we call d_instantiate
6489 * to avoid a race with nfsd calling d_instantiate.
6490 */
6491 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006492 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006493
6494out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006495 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006496 if (drop_on_err) {
6497 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006498 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006499 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006500 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006501 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006502 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006503
6504out_fail_inode:
6505 unlock_new_inode(inode);
6506 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006507}
6508
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006509/* Find next extent map of a given extent map, caller needs to ensure locks */
6510static struct extent_map *next_extent_map(struct extent_map *em)
6511{
6512 struct rb_node *next;
6513
6514 next = rb_next(&em->rb_node);
6515 if (!next)
6516 return NULL;
6517 return container_of(next, struct extent_map, rb_node);
6518}
6519
6520static struct extent_map *prev_extent_map(struct extent_map *em)
6521{
6522 struct rb_node *prev;
6523
6524 prev = rb_prev(&em->rb_node);
6525 if (!prev)
6526 return NULL;
6527 return container_of(prev, struct extent_map, rb_node);
6528}
6529
Chris Masond352ac62008-09-29 15:18:18 -04006530/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006531 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006532 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006533 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006534 */
Chris Mason3b951512008-04-17 11:29:12 -04006535static int merge_extent_mapping(struct extent_map_tree *em_tree,
6536 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006537 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006538 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006539{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006540 struct extent_map *prev;
6541 struct extent_map *next;
6542 u64 start;
6543 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006544 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006545
Chris Masone6dcd2d2008-07-17 12:53:50 -04006546 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006547
6548 if (existing->start > map_start) {
6549 next = existing;
6550 prev = prev_extent_map(next);
6551 } else {
6552 prev = existing;
6553 next = next_extent_map(prev);
6554 }
6555
6556 start = prev ? extent_map_end(prev) : em->start;
6557 start = max_t(u64, start, em->start);
6558 end = next ? next->start : extent_map_end(em);
6559 end = min_t(u64, end, extent_map_end(em));
6560 start_diff = start - em->start;
6561 em->start = start;
6562 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006563 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6564 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006565 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006566 em->block_len -= start_diff;
6567 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006568 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006569}
6570
Chris Masonc8b97812008-10-29 14:49:59 -04006571static noinline int uncompress_inline(struct btrfs_path *path,
6572 struct inode *inode, struct page *page,
6573 size_t pg_offset, u64 extent_offset,
6574 struct btrfs_file_extent_item *item)
6575{
6576 int ret;
6577 struct extent_buffer *leaf = path->nodes[0];
6578 char *tmp;
6579 size_t max_size;
6580 unsigned long inline_size;
6581 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006582 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006583
6584 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006585 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006586 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6587 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006588 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006589 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006590 if (!tmp)
6591 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006592 ptr = btrfs_file_extent_inline_start(item);
6593
6594 read_extent_buffer(leaf, tmp, ptr, inline_size);
6595
Chris Mason5b050f02008-11-11 09:34:41 -05006596 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006597 ret = btrfs_decompress(compress_type, tmp, page,
6598 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006599 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006600 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006601}
6602
Chris Masond352ac62008-09-29 15:18:18 -04006603/*
6604 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006605 * the ugly parts come from merging extents from the disk with the in-ram
6606 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006607 * where the in-ram extents might be locked pending data=ordered completion.
6608 *
6609 * This also copies inline extents directly into the page.
6610 */
Chris Masond3977122009-01-05 21:25:51 -05006611
Chris Masona52d9a82007-08-27 16:49:44 -04006612struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006613 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006614 int create)
6615{
6616 int ret;
6617 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006618 u64 extent_start = 0;
6619 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006620 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006621 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006622 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006623 struct btrfs_root *root = BTRFS_I(inode)->root;
6624 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006625 struct extent_buffer *leaf;
6626 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006627 struct extent_map *em = NULL;
6628 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006629 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006630 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006631 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006632
Chris Masona52d9a82007-08-27 16:49:44 -04006633again:
Chris Mason890871b2009-09-02 16:24:52 -04006634 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006635 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006636 if (em)
6637 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006638 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006639
Chris Masona52d9a82007-08-27 16:49:44 -04006640 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006641 if (em->start > start || em->start + em->len <= start)
6642 free_extent_map(em);
6643 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006644 free_extent_map(em);
6645 else
6646 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006647 }
David Sterba172ddd62011-04-21 00:48:27 +02006648 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006649 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006650 err = -ENOMEM;
6651 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006652 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006653 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006654 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006655 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006656 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006657 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006658
6659 if (!path) {
6660 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006661 if (!path) {
6662 err = -ENOMEM;
6663 goto out;
6664 }
6665 /*
6666 * Chances are we'll be called again, so go ahead and do
6667 * readahead
6668 */
6669 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006670 }
6671
Chris Mason179e29e2007-11-01 11:28:41 -04006672 ret = btrfs_lookup_file_extent(trans, root, path,
6673 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006674 if (ret < 0) {
6675 err = ret;
6676 goto out;
6677 }
6678
6679 if (ret != 0) {
6680 if (path->slots[0] == 0)
6681 goto not_found;
6682 path->slots[0]--;
6683 }
6684
Chris Mason5f39d392007-10-15 16:14:19 -04006685 leaf = path->nodes[0];
6686 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006687 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006688 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006689 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006690 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006691 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006692 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006693 /*
6694 * If we backup past the first extent we want to move forward
6695 * and see if there is an extent in front of us, otherwise we'll
6696 * say there is a hole for our whole search range which can
6697 * cause problems.
6698 */
6699 extent_end = start;
6700 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006701 }
6702
Chris Mason5f39d392007-10-15 16:14:19 -04006703 found_type = btrfs_file_extent_type(leaf, item);
6704 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006705 if (found_type == BTRFS_FILE_EXTENT_REG ||
6706 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006707 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006708 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006709 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6710 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006711 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006712 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006713 }
Josef Bacik25a50342013-10-14 12:08:38 -04006714next:
Yan Zheng9036c102008-10-30 14:19:41 -04006715 if (start >= extent_end) {
6716 path->slots[0]++;
6717 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6718 ret = btrfs_next_leaf(root, path);
6719 if (ret < 0) {
6720 err = ret;
6721 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006722 }
Yan Zheng9036c102008-10-30 14:19:41 -04006723 if (ret > 0)
6724 goto not_found;
6725 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006726 }
Yan Zheng9036c102008-10-30 14:19:41 -04006727 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6728 if (found_key.objectid != objectid ||
6729 found_key.type != BTRFS_EXTENT_DATA_KEY)
6730 goto not_found;
6731 if (start + len <= found_key.offset)
6732 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006733 if (start > found_key.offset)
6734 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006735 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006736 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006737 em->len = found_key.offset - start;
6738 goto not_found_em;
6739 }
6740
Filipe Manana7ffbb592014-06-09 03:48:05 +01006741 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6742
Yan Zhengd899e052008-10-30 14:25:28 -04006743 if (found_type == BTRFS_FILE_EXTENT_REG ||
6744 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006745 goto insert;
6746 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006747 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006748 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006749 size_t size;
6750 size_t extent_offset;
6751 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006752
Filipe Manana7ffbb592014-06-09 03:48:05 +01006753 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006754 goto out;
Yan689f9342007-10-29 11:41:07 -04006755
Chris Mason514ac8a2014-01-03 21:07:00 -08006756 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006757 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006758 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006759 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006760 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006761 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006762 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006763 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006764 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006765 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006766 if (btrfs_file_extent_compression(leaf, item) !=
6767 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006768 ret = uncompress_inline(path, inode, page,
6769 pg_offset,
6770 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006771 if (ret) {
6772 err = ret;
6773 goto out;
6774 }
Chris Masonc8b97812008-10-29 14:49:59 -04006775 } else {
6776 map = kmap(page);
6777 read_extent_buffer(leaf, map + pg_offset, ptr,
6778 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006779 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6780 memset(map + pg_offset + copy_size, 0,
6781 PAGE_CACHE_SIZE - pg_offset -
6782 copy_size);
6783 }
Chris Masonc8b97812008-10-29 14:49:59 -04006784 kunmap(page);
6785 }
Chris Mason179e29e2007-11-01 11:28:41 -04006786 flush_dcache_page(page);
6787 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006788 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006789 if (!trans) {
6790 kunmap(page);
6791 free_extent_map(em);
6792 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006793
David Sterbab3b4aa72011-04-21 01:20:15 +02006794 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006795 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006796
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006797 if (IS_ERR(trans))
6798 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006799 goto again;
6800 }
Chris Masonc8b97812008-10-29 14:49:59 -04006801 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006802 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006803 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006804 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006805 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006806 }
Chris Masond1310b22008-01-24 16:13:08 -05006807 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006808 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006809 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006810 }
6811not_found:
6812 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006813 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006814 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006815not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006816 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006817 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006818insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006819 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006820 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006821 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006822 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006823 err = -EIO;
6824 goto out;
6825 }
Chris Masond1310b22008-01-24 16:13:08 -05006826
6827 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006828 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006829 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006830 /* it is possible that someone inserted the extent into the tree
6831 * while we had the lock dropped. It is also possible that
6832 * an overlapping map exists in the tree
6833 */
Chris Masona52d9a82007-08-27 16:49:44 -04006834 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006835 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006836
6837 ret = 0;
6838
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006839 existing = search_extent_mapping(em_tree, start, len);
6840 /*
6841 * existing will always be non-NULL, since there must be
6842 * extent causing the -EEXIST.
6843 */
6844 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006845 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006846 /*
6847 * The existing extent map is the one nearest to
6848 * the [start, start + len) range which overlaps
6849 */
6850 err = merge_extent_mapping(em_tree, existing,
6851 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006852 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006853 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006854 free_extent_map(em);
6855 em = NULL;
6856 }
6857 } else {
6858 free_extent_map(em);
6859 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006860 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006861 }
Chris Masona52d9a82007-08-27 16:49:44 -04006862 }
Chris Mason890871b2009-09-02 16:24:52 -04006863 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006864out:
liubo1abe9b82011-03-24 11:18:59 +00006865
Steven Rostedt4cd85872013-11-14 22:57:29 -05006866 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006867
Chris Masonf4219502008-07-22 11:18:09 -04006868 if (path)
6869 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006870 if (trans) {
6871 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006872 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006873 err = ret;
6874 }
Chris Masona52d9a82007-08-27 16:49:44 -04006875 if (err) {
6876 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006877 return ERR_PTR(err);
6878 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006879 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006880 return em;
6881}
6882
Chris Masonec29ed52011-02-23 16:23:20 -05006883struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6884 size_t pg_offset, u64 start, u64 len,
6885 int create)
6886{
6887 struct extent_map *em;
6888 struct extent_map *hole_em = NULL;
6889 u64 range_start = start;
6890 u64 end;
6891 u64 found;
6892 u64 found_end;
6893 int err = 0;
6894
6895 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6896 if (IS_ERR(em))
6897 return em;
6898 if (em) {
6899 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006900 * if our em maps to
6901 * - a hole or
6902 * - a pre-alloc extent,
6903 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006904 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006905 if (em->block_start != EXTENT_MAP_HOLE &&
6906 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006907 return em;
6908 else
6909 hole_em = em;
6910 }
6911
6912 /* check to see if we've wrapped (len == -1 or similar) */
6913 end = start + len;
6914 if (end < start)
6915 end = (u64)-1;
6916 else
6917 end -= 1;
6918
6919 em = NULL;
6920
6921 /* ok, we didn't find anything, lets look for delalloc */
6922 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6923 end, len, EXTENT_DELALLOC, 1);
6924 found_end = range_start + found;
6925 if (found_end < range_start)
6926 found_end = (u64)-1;
6927
6928 /*
6929 * we didn't find anything useful, return
6930 * the original results from get_extent()
6931 */
6932 if (range_start > end || found_end <= start) {
6933 em = hole_em;
6934 hole_em = NULL;
6935 goto out;
6936 }
6937
6938 /* adjust the range_start to make sure it doesn't
6939 * go backwards from the start they passed in
6940 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306941 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006942 found = found_end - range_start;
6943
6944 if (found > 0) {
6945 u64 hole_start = start;
6946 u64 hole_len = len;
6947
David Sterba172ddd62011-04-21 00:48:27 +02006948 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006949 if (!em) {
6950 err = -ENOMEM;
6951 goto out;
6952 }
6953 /*
6954 * when btrfs_get_extent can't find anything it
6955 * returns one huge hole
6956 *
6957 * make sure what it found really fits our range, and
6958 * adjust to make sure it is based on the start from
6959 * the caller
6960 */
6961 if (hole_em) {
6962 u64 calc_end = extent_map_end(hole_em);
6963
6964 if (calc_end <= start || (hole_em->start > end)) {
6965 free_extent_map(hole_em);
6966 hole_em = NULL;
6967 } else {
6968 hole_start = max(hole_em->start, start);
6969 hole_len = calc_end - hole_start;
6970 }
6971 }
6972 em->bdev = NULL;
6973 if (hole_em && range_start > hole_start) {
6974 /* our hole starts before our delalloc, so we
6975 * have to return just the parts of the hole
6976 * that go until the delalloc starts
6977 */
6978 em->len = min(hole_len,
6979 range_start - hole_start);
6980 em->start = hole_start;
6981 em->orig_start = hole_start;
6982 /*
6983 * don't adjust block start at all,
6984 * it is fixed at EXTENT_MAP_HOLE
6985 */
6986 em->block_start = hole_em->block_start;
6987 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006988 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6989 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006990 } else {
6991 em->start = range_start;
6992 em->len = found;
6993 em->orig_start = range_start;
6994 em->block_start = EXTENT_MAP_DELALLOC;
6995 em->block_len = found;
6996 }
6997 } else if (hole_em) {
6998 return hole_em;
6999 }
7000out:
7001
7002 free_extent_map(hole_em);
7003 if (err) {
7004 free_extent_map(em);
7005 return ERR_PTR(err);
7006 }
7007 return em;
7008}
7009
Josef Bacik4b46fce2010-05-23 11:00:55 -04007010static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7011 u64 start, u64 len)
7012{
7013 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007014 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007015 struct btrfs_key ins;
7016 u64 alloc_hint;
7017 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007018
Josef Bacik4b46fce2010-05-23 11:00:55 -04007019 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04007020 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007021 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007022 if (ret)
7023 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007024
Josef Bacik70c8a912012-10-11 16:54:30 -04007025 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007026 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04007027 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08007028 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007029 return em;
7030 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007031
7032 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
7033 ins.offset, ins.offset, 0);
7034 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08007035 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007036 free_extent_map(em);
7037 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007038 }
Josef Bacik00361582013-08-14 14:02:47 -04007039
Dongsheng Yang31193212014-12-12 16:44:35 +08007040 btrfs_qgroup_update_reserved_bytes(root->fs_info,
7041 root->root_key.objectid,
7042 ins.offset, 1);
7043
Josef Bacik4b46fce2010-05-23 11:00:55 -04007044 return em;
7045}
7046
Chris Mason46bfbb52010-05-26 11:04:10 -04007047/*
7048 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7049 * block must be cow'd
7050 */
Josef Bacik00361582013-08-14 14:02:47 -04007051noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007052 u64 *orig_start, u64 *orig_block_len,
7053 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007054{
Josef Bacik00361582013-08-14 14:02:47 -04007055 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007056 struct btrfs_path *path;
7057 int ret;
7058 struct extent_buffer *leaf;
7059 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007060 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007061 struct btrfs_file_extent_item *fi;
7062 struct btrfs_key key;
7063 u64 disk_bytenr;
7064 u64 backref_offset;
7065 u64 extent_end;
7066 u64 num_bytes;
7067 int slot;
7068 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007069 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007070
Chris Mason46bfbb52010-05-26 11:04:10 -04007071 path = btrfs_alloc_path();
7072 if (!path)
7073 return -ENOMEM;
7074
Josef Bacik00361582013-08-14 14:02:47 -04007075 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007076 offset, 0);
7077 if (ret < 0)
7078 goto out;
7079
7080 slot = path->slots[0];
7081 if (ret == 1) {
7082 if (slot == 0) {
7083 /* can't find the item, must cow */
7084 ret = 0;
7085 goto out;
7086 }
7087 slot--;
7088 }
7089 ret = 0;
7090 leaf = path->nodes[0];
7091 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007092 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007093 key.type != BTRFS_EXTENT_DATA_KEY) {
7094 /* not our file or wrong item type, must cow */
7095 goto out;
7096 }
7097
7098 if (key.offset > offset) {
7099 /* Wrong offset, must cow */
7100 goto out;
7101 }
7102
7103 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7104 found_type = btrfs_file_extent_type(leaf, fi);
7105 if (found_type != BTRFS_FILE_EXTENT_REG &&
7106 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7107 /* not a regular extent, must cow */
7108 goto out;
7109 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007110
7111 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7112 goto out;
7113
Miao Xiee77751a2013-12-27 21:11:50 +08007114 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7115 if (extent_end <= offset)
7116 goto out;
7117
Chris Mason46bfbb52010-05-26 11:04:10 -04007118 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007119 if (disk_bytenr == 0)
7120 goto out;
7121
7122 if (btrfs_file_extent_compression(leaf, fi) ||
7123 btrfs_file_extent_encryption(leaf, fi) ||
7124 btrfs_file_extent_other_encoding(leaf, fi))
7125 goto out;
7126
Chris Mason46bfbb52010-05-26 11:04:10 -04007127 backref_offset = btrfs_file_extent_offset(leaf, fi);
7128
Josef Bacik7ee9e442013-06-21 16:37:03 -04007129 if (orig_start) {
7130 *orig_start = key.offset - backref_offset;
7131 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7132 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7133 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007134
Chris Mason46bfbb52010-05-26 11:04:10 -04007135 if (btrfs_extent_readonly(root, disk_bytenr))
7136 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007137
7138 num_bytes = min(offset + *len, extent_end) - offset;
7139 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7140 u64 range_end;
7141
7142 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7143 ret = test_range_bit(io_tree, offset, range_end,
7144 EXTENT_DELALLOC, 0, NULL);
7145 if (ret) {
7146 ret = -EAGAIN;
7147 goto out;
7148 }
7149 }
7150
Josef Bacik1bda19e2013-10-18 12:10:36 -04007151 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007152
7153 /*
7154 * look for other files referencing this extent, if we
7155 * find any we must cow
7156 */
Josef Bacik00361582013-08-14 14:02:47 -04007157 trans = btrfs_join_transaction(root);
7158 if (IS_ERR(trans)) {
7159 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007160 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007161 }
7162
7163 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7164 key.offset - backref_offset, disk_bytenr);
7165 btrfs_end_transaction(trans, root);
7166 if (ret) {
7167 ret = 0;
7168 goto out;
7169 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007170
7171 /*
7172 * adjust disk_bytenr and num_bytes to cover just the bytes
7173 * in this extent we are about to write. If there
7174 * are any csums in that range we have to cow in order
7175 * to keep the csums correct
7176 */
7177 disk_bytenr += backref_offset;
7178 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007179 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7180 goto out;
7181 /*
7182 * all of the above have passed, it is safe to overwrite this extent
7183 * without cow
7184 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007185 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007186 ret = 1;
7187out:
7188 btrfs_free_path(path);
7189 return ret;
7190}
7191
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007192bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7193{
7194 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7195 int found = false;
7196 void **pagep = NULL;
7197 struct page *page = NULL;
7198 int start_idx;
7199 int end_idx;
7200
7201 start_idx = start >> PAGE_CACHE_SHIFT;
7202
7203 /*
7204 * end is the last byte in the last page. end == start is legal
7205 */
7206 end_idx = end >> PAGE_CACHE_SHIFT;
7207
7208 rcu_read_lock();
7209
7210 /* Most of the code in this while loop is lifted from
7211 * find_get_page. It's been modified to begin searching from a
7212 * page and return just the first page found in that range. If the
7213 * found idx is less than or equal to the end idx then we know that
7214 * a page exists. If no pages are found or if those pages are
7215 * outside of the range then we're fine (yay!) */
7216 while (page == NULL &&
7217 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7218 page = radix_tree_deref_slot(pagep);
7219 if (unlikely(!page))
7220 break;
7221
7222 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007223 if (radix_tree_deref_retry(page)) {
7224 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007225 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007226 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007227 /*
7228 * Otherwise, shmem/tmpfs must be storing a swap entry
7229 * here as an exceptional entry: so return it without
7230 * attempting to raise page count.
7231 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007232 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007233 break; /* TODO: Is this relevant for this use case? */
7234 }
7235
Filipe Manana91405152014-06-05 13:22:24 +01007236 if (!page_cache_get_speculative(page)) {
7237 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007238 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007239 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007240
7241 /*
7242 * Has the page moved?
7243 * This is part of the lockless pagecache protocol. See
7244 * include/linux/pagemap.h for details.
7245 */
7246 if (unlikely(page != *pagep)) {
7247 page_cache_release(page);
7248 page = NULL;
7249 }
7250 }
7251
7252 if (page) {
7253 if (page->index <= end_idx)
7254 found = true;
7255 page_cache_release(page);
7256 }
7257
7258 rcu_read_unlock();
7259 return found;
7260}
7261
Josef Bacikeb838e72012-07-31 16:28:48 -04007262static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7263 struct extent_state **cached_state, int writing)
7264{
7265 struct btrfs_ordered_extent *ordered;
7266 int ret = 0;
7267
7268 while (1) {
7269 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7270 0, cached_state);
7271 /*
7272 * We're concerned with the entire range that we're going to be
7273 * doing DIO to, so we need to make sure theres no ordered
7274 * extents in this range.
7275 */
7276 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7277 lockend - lockstart + 1);
7278
7279 /*
7280 * We need to make sure there are no buffered pages in this
7281 * range either, we could have raced between the invalidate in
7282 * generic_file_direct_write and locking the extent. The
7283 * invalidate needs to happen so that reads after a write do not
7284 * get stale data.
7285 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007286 if (!ordered &&
7287 (!writing ||
7288 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007289 break;
7290
7291 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7292 cached_state, GFP_NOFS);
7293
7294 if (ordered) {
7295 btrfs_start_ordered_extent(inode, ordered, 1);
7296 btrfs_put_ordered_extent(ordered);
7297 } else {
7298 /* Screw you mmap */
Filipe Manana728404d2014-10-10 09:43:11 +01007299 ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
Filipe Manana075bdbd2014-10-09 21:18:55 +01007300 if (ret)
7301 break;
7302 ret = filemap_fdatawait_range(inode->i_mapping,
7303 lockstart,
7304 lockend);
Josef Bacikeb838e72012-07-31 16:28:48 -04007305 if (ret)
7306 break;
7307
7308 /*
7309 * If we found a page that couldn't be invalidated just
7310 * fall back to buffered.
7311 */
7312 ret = invalidate_inode_pages2_range(inode->i_mapping,
7313 lockstart >> PAGE_CACHE_SHIFT,
7314 lockend >> PAGE_CACHE_SHIFT);
7315 if (ret)
7316 break;
7317 }
7318
7319 cond_resched();
7320 }
7321
7322 return ret;
7323}
7324
Josef Bacik69ffb542012-09-11 15:40:07 -04007325static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7326 u64 len, u64 orig_start,
7327 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007328 u64 orig_block_len, u64 ram_bytes,
7329 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007330{
7331 struct extent_map_tree *em_tree;
7332 struct extent_map *em;
7333 struct btrfs_root *root = BTRFS_I(inode)->root;
7334 int ret;
7335
7336 em_tree = &BTRFS_I(inode)->extent_tree;
7337 em = alloc_extent_map();
7338 if (!em)
7339 return ERR_PTR(-ENOMEM);
7340
7341 em->start = start;
7342 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007343 em->mod_start = start;
7344 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007345 em->len = len;
7346 em->block_len = block_len;
7347 em->block_start = block_start;
7348 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007349 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007350 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007351 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007352 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7353 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007354 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007355
7356 do {
7357 btrfs_drop_extent_cache(inode, em->start,
7358 em->start + em->len - 1, 0);
7359 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007360 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007361 write_unlock(&em_tree->lock);
7362 } while (ret == -EEXIST);
7363
7364 if (ret) {
7365 free_extent_map(em);
7366 return ERR_PTR(ret);
7367 }
7368
7369 return em;
7370}
7371
7372
Josef Bacik4b46fce2010-05-23 11:00:55 -04007373static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7374 struct buffer_head *bh_result, int create)
7375{
7376 struct extent_map *em;
7377 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007378 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007379 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007380 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007381 u64 len = bh_result->b_size;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007382 u64 *outstanding_extents = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007383 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007384 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007385
Miao Xie172a5042013-02-21 02:48:22 -07007386 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007387 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007388 else
Josef Bacikc3298612012-08-03 16:49:19 -04007389 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007390
Josef Bacikc3298612012-08-03 16:49:19 -04007391 lockstart = start;
7392 lockend = start + len - 1;
7393
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007394 if (current->journal_info) {
7395 /*
7396 * Need to pull our outstanding extents and set journal_info to NULL so
7397 * that anything that needs to check if there's a transction doesn't get
7398 * confused.
7399 */
7400 outstanding_extents = current->journal_info;
7401 current->journal_info = NULL;
7402 }
7403
Josef Bacikeb838e72012-07-31 16:28:48 -04007404 /*
7405 * If this errors out it's because we couldn't invalidate pagecache for
7406 * this range and we need to fallback to buffered.
7407 */
7408 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7409 return -ENOTBLK;
7410
Josef Bacik4b46fce2010-05-23 11:00:55 -04007411 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007412 if (IS_ERR(em)) {
7413 ret = PTR_ERR(em);
7414 goto unlock_err;
7415 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007416
7417 /*
7418 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7419 * io. INLINE is special, and we could probably kludge it in here, but
7420 * it's still buffered so for safety lets just fall back to the generic
7421 * buffered path.
7422 *
7423 * For COMPRESSED we _have_ to read the entire extent in so we can
7424 * decompress it, so there will be buffering required no matter what we
7425 * do, so go ahead and fallback to buffered.
7426 *
7427 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7428 * to buffered IO. Don't blame me, this is the price we pay for using
7429 * the generic code.
7430 */
7431 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7432 em->block_start == EXTENT_MAP_INLINE) {
7433 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007434 ret = -ENOTBLK;
7435 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007436 }
7437
7438 /* Just a good old fashioned hole, return */
7439 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7440 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7441 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007442 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007443 }
7444
7445 /*
7446 * We don't allocate a new extent in the following cases
7447 *
7448 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7449 * existing extent.
7450 * 2) The extent is marked as PREALLOC. We're good to go here and can
7451 * just use the extent.
7452 *
7453 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007454 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007455 len = min(len, em->len - (start - em->start));
7456 lockstart = start + len;
7457 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007458 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007459
7460 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7461 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7462 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007463 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007464 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007465
7466 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7467 type = BTRFS_ORDERED_PREALLOC;
7468 else
7469 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007470 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007471 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007472
Josef Bacik00361582013-08-14 14:02:47 -04007473 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007474 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007475 if (type == BTRFS_ORDERED_PREALLOC) {
7476 free_extent_map(em);
7477 em = create_pinned_em(inode, start, len,
7478 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007479 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007480 orig_block_len,
7481 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007482 if (IS_ERR(em)) {
7483 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007484 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007485 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007486 }
7487
Chris Mason46bfbb52010-05-26 11:04:10 -04007488 ret = btrfs_add_ordered_extent_dio(inode, start,
7489 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007490 if (ret) {
7491 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007492 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007493 }
7494 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007495 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007496 }
Josef Bacik00361582013-08-14 14:02:47 -04007497
Chris Mason46bfbb52010-05-26 11:04:10 -04007498 /*
7499 * this will cow the extent, reset the len in case we changed
7500 * it above
7501 */
7502 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007503 free_extent_map(em);
7504 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007505 if (IS_ERR(em)) {
7506 ret = PTR_ERR(em);
7507 goto unlock_err;
7508 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007509 len = min(len, em->len - (start - em->start));
7510unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007511 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7512 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007513 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007514 bh_result->b_bdev = em->bdev;
7515 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007516 if (create) {
7517 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7518 set_buffer_new(bh_result);
7519
7520 /*
7521 * Need to update the i_size under the extent lock so buffered
7522 * readers will get the updated i_size when we unlock.
7523 */
7524 if (start + len > i_size_read(inode))
7525 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007526
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007527 /*
7528 * If we have an outstanding_extents count still set then we're
7529 * within our reservation, otherwise we need to adjust our inode
7530 * counter appropriately.
7531 */
7532 if (*outstanding_extents) {
7533 (*outstanding_extents)--;
7534 } else {
Josef Bacik3e05bde2015-02-11 15:08:57 -05007535 spin_lock(&BTRFS_I(inode)->lock);
7536 BTRFS_I(inode)->outstanding_extents++;
7537 spin_unlock(&BTRFS_I(inode)->lock);
7538 }
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007539
7540 current->journal_info = outstanding_extents;
Josef Bacik32667892015-02-11 15:08:58 -05007541 btrfs_free_reserved_data_space(inode, len);
Josef Bacikc3473e82012-06-19 10:59:00 -04007542 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007543
Josef Bacikeb838e72012-07-31 16:28:48 -04007544 /*
7545 * In the case of write we need to clear and unlock the entire range,
7546 * in the case of read we need to unlock only the end area that we
7547 * aren't using if there is any left over space.
7548 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007549 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007550 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7551 lockend, unlock_bits, 1, 0,
7552 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007553 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007554 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007555 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007556
Josef Bacik4b46fce2010-05-23 11:00:55 -04007557 free_extent_map(em);
7558
7559 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007560
7561unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007562 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7563 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007564 if (outstanding_extents)
7565 current->journal_info = outstanding_extents;
Josef Bacikeb838e72012-07-31 16:28:48 -04007566 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007567}
7568
Miao Xie8b110e32014-09-12 18:44:03 +08007569static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7570 int rw, int mirror_num)
7571{
7572 struct btrfs_root *root = BTRFS_I(inode)->root;
7573 int ret;
7574
7575 BUG_ON(rw & REQ_WRITE);
7576
7577 bio_get(bio);
7578
7579 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7580 BTRFS_WQ_ENDIO_DIO_REPAIR);
7581 if (ret)
7582 goto err;
7583
7584 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7585err:
7586 bio_put(bio);
7587 return ret;
7588}
7589
7590static int btrfs_check_dio_repairable(struct inode *inode,
7591 struct bio *failed_bio,
7592 struct io_failure_record *failrec,
7593 int failed_mirror)
7594{
7595 int num_copies;
7596
7597 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7598 failrec->logical, failrec->len);
7599 if (num_copies == 1) {
7600 /*
7601 * we only have a single copy of the data, so don't bother with
7602 * all the retry and error correction code that follows. no
7603 * matter what the error is, it is very likely to persist.
7604 */
7605 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7606 num_copies, failrec->this_mirror, failed_mirror);
7607 return 0;
7608 }
7609
7610 failrec->failed_mirror = failed_mirror;
7611 failrec->this_mirror++;
7612 if (failrec->this_mirror == failed_mirror)
7613 failrec->this_mirror++;
7614
7615 if (failrec->this_mirror > num_copies) {
7616 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7617 num_copies, failrec->this_mirror, failed_mirror);
7618 return 0;
7619 }
7620
7621 return 1;
7622}
7623
7624static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7625 struct page *page, u64 start, u64 end,
7626 int failed_mirror, bio_end_io_t *repair_endio,
7627 void *repair_arg)
7628{
7629 struct io_failure_record *failrec;
7630 struct bio *bio;
7631 int isector;
7632 int read_mode;
7633 int ret;
7634
7635 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7636
7637 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7638 if (ret)
7639 return ret;
7640
7641 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7642 failed_mirror);
7643 if (!ret) {
7644 free_io_failure(inode, failrec);
7645 return -EIO;
7646 }
7647
7648 if (failed_bio->bi_vcnt > 1)
7649 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7650 else
7651 read_mode = READ_SYNC;
7652
7653 isector = start - btrfs_io_bio(failed_bio)->logical;
7654 isector >>= inode->i_sb->s_blocksize_bits;
7655 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7656 0, isector, repair_endio, repair_arg);
7657 if (!bio) {
7658 free_io_failure(inode, failrec);
7659 return -EIO;
7660 }
7661
7662 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7663 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7664 read_mode, failrec->this_mirror, failrec->in_validation);
7665
7666 ret = submit_dio_repair_bio(inode, bio, read_mode,
7667 failrec->this_mirror);
7668 if (ret) {
7669 free_io_failure(inode, failrec);
7670 bio_put(bio);
7671 }
7672
7673 return ret;
7674}
7675
7676struct btrfs_retry_complete {
7677 struct completion done;
7678 struct inode *inode;
7679 u64 start;
7680 int uptodate;
7681};
7682
7683static void btrfs_retry_endio_nocsum(struct bio *bio, int err)
7684{
7685 struct btrfs_retry_complete *done = bio->bi_private;
7686 struct bio_vec *bvec;
7687 int i;
7688
7689 if (err)
7690 goto end;
7691
7692 done->uptodate = 1;
7693 bio_for_each_segment_all(bvec, bio, i)
7694 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7695end:
7696 complete(&done->done);
7697 bio_put(bio);
7698}
7699
7700static int __btrfs_correct_data_nocsum(struct inode *inode,
7701 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007702{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007703 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007704 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007705 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007706 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007707 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007708
Miao Xiec1dc0892014-09-12 18:43:56 +08007709 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007710 done.inode = inode;
7711
7712 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7713try_again:
7714 done.uptodate = 0;
7715 done.start = start;
7716 init_completion(&done.done);
7717
7718 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7719 start + bvec->bv_len - 1,
7720 io_bio->mirror_num,
7721 btrfs_retry_endio_nocsum, &done);
7722 if (ret)
7723 return ret;
7724
7725 wait_for_completion(&done.done);
7726
7727 if (!done.uptodate) {
7728 /* We might have another mirror, so try again */
7729 goto try_again;
7730 }
7731
7732 start += bvec->bv_len;
7733 }
7734
7735 return 0;
7736}
7737
7738static void btrfs_retry_endio(struct bio *bio, int err)
7739{
7740 struct btrfs_retry_complete *done = bio->bi_private;
7741 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7742 struct bio_vec *bvec;
7743 int uptodate;
7744 int ret;
7745 int i;
7746
7747 if (err)
7748 goto end;
7749
7750 uptodate = 1;
7751 bio_for_each_segment_all(bvec, bio, i) {
7752 ret = __readpage_endio_check(done->inode, io_bio, i,
7753 bvec->bv_page, 0,
7754 done->start, bvec->bv_len);
7755 if (!ret)
7756 clean_io_failure(done->inode, done->start,
7757 bvec->bv_page, 0);
7758 else
7759 uptodate = 0;
7760 }
7761
7762 done->uptodate = uptodate;
7763end:
7764 complete(&done->done);
7765 bio_put(bio);
7766}
7767
7768static int __btrfs_subio_endio_read(struct inode *inode,
7769 struct btrfs_io_bio *io_bio, int err)
7770{
7771 struct bio_vec *bvec;
7772 struct btrfs_retry_complete done;
7773 u64 start;
7774 u64 offset = 0;
7775 int i;
7776 int ret;
7777
7778 err = 0;
7779 start = io_bio->logical;
7780 done.inode = inode;
7781
Miao Xiec1dc0892014-09-12 18:43:56 +08007782 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007783 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7784 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007785 if (likely(!ret))
7786 goto next;
7787try_again:
7788 done.uptodate = 0;
7789 done.start = start;
7790 init_completion(&done.done);
7791
7792 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7793 start + bvec->bv_len - 1,
7794 io_bio->mirror_num,
7795 btrfs_retry_endio, &done);
7796 if (ret) {
7797 err = ret;
7798 goto next;
7799 }
7800
7801 wait_for_completion(&done.done);
7802
7803 if (!done.uptodate) {
7804 /* We might have another mirror, so try again */
7805 goto try_again;
7806 }
7807next:
7808 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007809 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007810 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007811
7812 return err;
7813}
7814
Miao Xie8b110e32014-09-12 18:44:03 +08007815static int btrfs_subio_endio_read(struct inode *inode,
7816 struct btrfs_io_bio *io_bio, int err)
7817{
7818 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7819
7820 if (skip_csum) {
7821 if (unlikely(err))
7822 return __btrfs_correct_data_nocsum(inode, io_bio);
7823 else
7824 return 0;
7825 } else {
7826 return __btrfs_subio_endio_read(inode, io_bio, err);
7827 }
7828}
7829
Miao Xiec1dc0892014-09-12 18:43:56 +08007830static void btrfs_endio_direct_read(struct bio *bio, int err)
7831{
7832 struct btrfs_dio_private *dip = bio->bi_private;
7833 struct inode *inode = dip->inode;
7834 struct bio *dio_bio;
7835 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7836
Miao Xie8b110e32014-09-12 18:44:03 +08007837 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7838 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007839
Josef Bacik4b46fce2010-05-23 11:00:55 -04007840 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007841 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007842 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007843
Josef Bacik4b46fce2010-05-23 11:00:55 -04007844 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007845
7846 /* If we had a csum failure make sure to clear the uptodate flag */
7847 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007848 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7849 dio_end_io(dio_bio, err);
Miao Xie23ea8e52014-09-12 18:43:54 +08007850
7851 if (io_bio->end_io)
7852 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007853 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007854}
7855
7856static void btrfs_endio_direct_write(struct bio *bio, int err)
7857{
7858 struct btrfs_dio_private *dip = bio->bi_private;
7859 struct inode *inode = dip->inode;
7860 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007861 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007862 u64 ordered_offset = dip->logical_offset;
7863 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007864 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007865 int ret;
7866
7867 if (err)
7868 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007869again:
7870 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7871 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007872 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007873 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007874 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007875
Liu Bo9e0af232014-08-15 23:36:53 +08007876 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7877 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007878 btrfs_queue_work(root->fs_info->endio_write_workers,
7879 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007880out_test:
7881 /*
7882 * our bio might span multiple ordered extents. If we haven't
7883 * completed the accounting for the whole dio, go back and try again
7884 */
7885 if (ordered_offset < dip->logical_offset + dip->bytes) {
7886 ordered_bytes = dip->logical_offset + dip->bytes -
7887 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007888 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007889 goto again;
7890 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007891out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007892 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007893
Josef Bacik4b46fce2010-05-23 11:00:55 -04007894 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007895
7896 /* If we had an error make sure to clear the uptodate flag */
7897 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007898 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7899 dio_end_io(dio_bio, err);
7900 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007901}
7902
Chris Masoneaf25d92010-05-25 09:48:28 -04007903static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7904 struct bio *bio, int mirror_num,
7905 unsigned long bio_flags, u64 offset)
7906{
7907 int ret;
7908 struct btrfs_root *root = BTRFS_I(inode)->root;
7909 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007910 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007911 return 0;
7912}
7913
Miao Xiee65e1532010-11-22 03:04:43 +00007914static void btrfs_end_dio_bio(struct bio *bio, int err)
7915{
7916 struct btrfs_dio_private *dip = bio->bi_private;
7917
Miao Xie8b110e32014-09-12 18:44:03 +08007918 if (err)
7919 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
7920 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7921 btrfs_ino(dip->inode), bio->bi_rw,
7922 (unsigned long long)bio->bi_iter.bi_sector,
7923 bio->bi_iter.bi_size, err);
7924
7925 if (dip->subio_endio)
7926 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007927
7928 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007929 dip->errors = 1;
7930
7931 /*
7932 * before atomic variable goto zero, we must make sure
7933 * dip->errors is perceived to be set.
7934 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007935 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007936 }
7937
7938 /* if there are more bios still pending for this dio, just exit */
7939 if (!atomic_dec_and_test(&dip->pending_bios))
7940 goto out;
7941
Chris Mason9be33952013-05-17 18:30:14 -04007942 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007943 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007944 } else {
7945 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007946 bio_endio(dip->orig_bio, 0);
7947 }
7948out:
7949 bio_put(bio);
7950}
7951
7952static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7953 u64 first_sector, gfp_t gfp_flags)
7954{
7955 int nr_vecs = bio_get_nr_vecs(bdev);
7956 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7957}
7958
Miao Xiec1dc0892014-09-12 18:43:56 +08007959static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
7960 struct inode *inode,
7961 struct btrfs_dio_private *dip,
7962 struct bio *bio,
7963 u64 file_offset)
7964{
7965 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7966 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
7967 int ret;
7968
7969 /*
7970 * We load all the csum data we need when we submit
7971 * the first bio to reduce the csum tree search and
7972 * contention.
7973 */
7974 if (dip->logical_offset == file_offset) {
7975 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
7976 file_offset);
7977 if (ret)
7978 return ret;
7979 }
7980
7981 if (bio == dip->orig_bio)
7982 return 0;
7983
7984 file_offset -= dip->logical_offset;
7985 file_offset >>= inode->i_sb->s_blocksize_bits;
7986 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
7987
7988 return 0;
7989}
7990
Miao Xiee65e1532010-11-22 03:04:43 +00007991static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7992 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007993 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007994{
Miao Xiefacc8a222013-07-25 19:22:34 +08007995 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007996 int write = rw & REQ_WRITE;
7997 struct btrfs_root *root = BTRFS_I(inode)->root;
7998 int ret;
7999
Josef Bacikb812ce22012-11-16 13:56:32 -05008000 if (async_submit)
8001 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8002
Miao Xiee65e1532010-11-22 03:04:43 +00008003 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008004
8005 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02008006 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8007 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008008 if (ret)
8009 goto err;
8010 }
Miao Xiee65e1532010-11-22 03:04:43 +00008011
Josef Bacik1ae39932011-04-06 14:41:34 -04008012 if (skip_sum)
8013 goto map;
8014
8015 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008016 ret = btrfs_wq_submit_bio(root->fs_info,
8017 inode, rw, bio, 0, 0,
8018 file_offset,
8019 __btrfs_submit_bio_start_direct_io,
8020 __btrfs_submit_bio_done);
8021 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008022 } else if (write) {
8023 /*
8024 * If we aren't doing async submit, calculate the csum of the
8025 * bio now.
8026 */
8027 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8028 if (ret)
8029 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008030 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008031 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8032 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008033 if (ret)
8034 goto err;
8035 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008036map:
8037 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008038err:
8039 bio_put(bio);
8040 return ret;
8041}
8042
8043static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8044 int skip_sum)
8045{
8046 struct inode *inode = dip->inode;
8047 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008048 struct bio *bio;
8049 struct bio *orig_bio = dip->orig_bio;
8050 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008051 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008052 u64 file_offset = dip->logical_offset;
8053 u64 submit_len = 0;
8054 u64 map_length;
8055 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08008056 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04008057 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008058
Kent Overstreet4f024f32013-10-11 15:44:27 -07008059 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008060 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008061 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008062 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008063 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008064
Kent Overstreet4f024f32013-10-11 15:44:27 -07008065 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008066 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008067 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008068 goto submit;
8069 }
8070
David Woodhouse53b381b2013-01-29 18:40:14 -05008071 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008072 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008073 async_submit = 0;
8074 else
8075 async_submit = 1;
8076
Josef Bacik02f57c72011-04-06 14:25:44 -04008077 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8078 if (!bio)
8079 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008080
Josef Bacik02f57c72011-04-06 14:25:44 -04008081 bio->bi_private = dip;
8082 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008083 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008084 atomic_inc(&dip->pending_bios);
8085
Miao Xiee65e1532010-11-22 03:04:43 +00008086 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02008087 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00008088 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02008089 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00008090 /*
8091 * inc the count before we submit the bio so
8092 * we know the end IO handler won't happen before
8093 * we inc the count. Otherwise, the dip might get freed
8094 * before we're done setting it up
8095 */
8096 atomic_inc(&dip->pending_bios);
8097 ret = __btrfs_submit_dio_bio(bio, inode, rw,
8098 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008099 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008100 if (ret) {
8101 bio_put(bio);
8102 atomic_dec(&dip->pending_bios);
8103 goto out_err;
8104 }
8105
Miao Xiee65e1532010-11-22 03:04:43 +00008106 start_sector += submit_len >> 9;
8107 file_offset += submit_len;
8108
8109 submit_len = 0;
8110 nr_pages = 0;
8111
8112 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8113 start_sector, GFP_NOFS);
8114 if (!bio)
8115 goto out_err;
8116 bio->bi_private = dip;
8117 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008118 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008119
Kent Overstreet4f024f32013-10-11 15:44:27 -07008120 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008121 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008122 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008123 &map_length, NULL, 0);
8124 if (ret) {
8125 bio_put(bio);
8126 goto out_err;
8127 }
8128 } else {
8129 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308130 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00008131 bvec++;
8132 }
8133 }
8134
Josef Bacik02f57c72011-04-06 14:25:44 -04008135submit:
Miao Xiee65e1532010-11-22 03:04:43 +00008136 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008137 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008138 if (!ret)
8139 return 0;
8140
8141 bio_put(bio);
8142out_err:
8143 dip->errors = 1;
8144 /*
8145 * before atomic variable goto zero, we must
8146 * make sure dip->errors is perceived to be set.
8147 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008148 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008149 if (atomic_dec_and_test(&dip->pending_bios))
8150 bio_io_error(dip->orig_bio);
8151
8152 /* bio_end_io() will handle error, so we needn't return it */
8153 return 0;
8154}
8155
Chris Mason9be33952013-05-17 18:30:14 -04008156static void btrfs_submit_direct(int rw, struct bio *dio_bio,
8157 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008158{
8159 struct btrfs_root *root = BTRFS_I(inode)->root;
8160 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04008161 struct bio *io_bio;
Miao Xie23ea8e52014-09-12 18:43:54 +08008162 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008163 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02008164 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008165 int ret = 0;
8166
8167 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8168
Chris Mason9be33952013-05-17 18:30:14 -04008169 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008170 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008171 ret = -ENOMEM;
8172 goto free_ordered;
8173 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008174
Miao Xiec1dc0892014-09-12 18:43:56 +08008175 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008176 if (!dip) {
8177 ret = -ENOMEM;
8178 goto free_io_bio;
8179 }
8180
8181 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008182 dip->inode = inode;
8183 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008184 dip->bytes = dio_bio->bi_iter.bi_size;
8185 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008186 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008187 dip->orig_bio = io_bio;
8188 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008189 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008190 btrfs_bio = btrfs_io_bio(io_bio);
8191 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008192
Miao Xiec1dc0892014-09-12 18:43:56 +08008193 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008194 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008195 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008196 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008197 dip->subio_endio = btrfs_subio_endio_read;
8198 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008199
Miao Xiee65e1532010-11-22 03:04:43 +00008200 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8201 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008202 return;
Chris Mason9be33952013-05-17 18:30:14 -04008203
Miao Xie23ea8e52014-09-12 18:43:54 +08008204 if (btrfs_bio->end_io)
8205 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008206free_io_bio:
8207 bio_put(io_bio);
8208
Josef Bacik4b46fce2010-05-23 11:00:55 -04008209free_ordered:
8210 /*
8211 * If this is a write, we need to clean up the reserved space and kill
8212 * the ordered extent.
8213 */
8214 if (write) {
8215 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05008216 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008217 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
8218 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
8219 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08008220 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008221 btrfs_put_ordered_extent(ordered);
8222 btrfs_put_ordered_extent(ordered);
8223 }
Chris Mason9be33952013-05-17 18:30:14 -04008224 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008225}
8226
Chris Mason5a5f79b2010-05-26 21:33:37 -04008227static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008228 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008229{
8230 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008231 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008232 unsigned blocksize_mask = root->sectorsize - 1;
8233 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008234
8235 if (offset & blocksize_mask)
8236 goto out;
8237
Al Viro28060d52014-03-22 05:15:17 -04008238 if (iov_iter_alignment(iter) & blocksize_mask)
8239 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008240
Al Viro28060d52014-03-22 05:15:17 -04008241 /* If this is a write we don't need to check anymore */
8242 if (rw & WRITE)
8243 return 0;
8244 /*
8245 * Check to make sure we don't have duplicate iov_base's in this
8246 * iovec, if so return EINVAL, otherwise we'll get csum errors
8247 * when reading back.
8248 */
8249 for (seg = 0; seg < iter->nr_segs; seg++) {
8250 for (i = seg + 1; i < iter->nr_segs; i++) {
8251 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008252 goto out;
8253 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008254 }
8255 retval = 0;
8256out:
8257 return retval;
8258}
Josef Bacikeb838e72012-07-31 16:28:48 -04008259
Chris Mason16432982008-04-10 10:23:21 -04008260static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05008261 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04008262{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008263 struct file *file = iocb->ki_filp;
8264 struct inode *inode = file->f_mapping->host;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008265 u64 outstanding_extents = 0;
Miao Xie09348562013-02-07 10:12:07 +00008266 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008267 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008268 bool wakeup = true;
8269 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008270 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008271
Al Viro28060d52014-03-22 05:15:17 -04008272 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008273 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008274
Miao Xie38851cc2013-02-08 07:04:11 +00008275 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008276 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008277
Josef Bacik0e267c42013-07-02 10:38:02 -04008278 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008279 * The generic stuff only does filemap_write_and_wait_range, which
8280 * isn't enough if we've written compressed pages to this area, so
8281 * we need to flush the dirty pages again to make absolutely sure
8282 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008283 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008284 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008285 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8286 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008287 filemap_fdatawrite_range(inode->i_mapping, offset,
8288 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008289
Miao Xie09348562013-02-07 10:12:07 +00008290 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008291 /*
8292 * If the write DIO is beyond the EOF, we need update
8293 * the isize, but it is protected by i_mutex. So we can
8294 * not unlock the i_mutex at this case.
8295 */
8296 if (offset + count <= inode->i_size) {
8297 mutex_unlock(&inode->i_mutex);
8298 relock = true;
8299 }
Miao Xie09348562013-02-07 10:12:07 +00008300 ret = btrfs_delalloc_reserve_space(inode, count);
8301 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008302 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008303 outstanding_extents = div64_u64(count +
8304 BTRFS_MAX_EXTENT_SIZE - 1,
8305 BTRFS_MAX_EXTENT_SIZE);
8306
8307 /*
8308 * We need to know how many extents we reserved so that we can
8309 * do the accounting properly if we go over the number we
8310 * originally calculated. Abuse current->journal_info for this.
8311 */
8312 current->journal_info = &outstanding_extents;
David Sterbaee39b432014-09-30 01:33:33 +02008313 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8314 &BTRFS_I(inode)->runtime_flags)) {
Miao Xie38851cc2013-02-08 07:04:11 +00008315 inode_dio_done(inode);
8316 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8317 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008318 }
8319
8320 ret = __blockdev_direct_IO(rw, iocb, inode,
8321 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05008322 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00008323 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00008324 if (rw & WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008325 current->journal_info = NULL;
Miao Xie09348562013-02-07 10:12:07 +00008326 if (ret < 0 && ret != -EIOCBQUEUED)
8327 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07008328 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00008329 btrfs_delalloc_release_space(inode,
8330 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008331 }
Miao Xie38851cc2013-02-08 07:04:11 +00008332out:
Miao Xie2e60a512013-02-08 07:01:08 +00008333 if (wakeup)
8334 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008335 if (relock)
8336 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008337
8338 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008339}
8340
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008341#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8342
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008343static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8344 __u64 start, __u64 len)
8345{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008346 int ret;
8347
8348 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8349 if (ret)
8350 return ret;
8351
Chris Masonec29ed52011-02-23 16:23:20 -05008352 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008353}
8354
Chris Mason9ebefb182007-06-15 13:50:00 -04008355int btrfs_readpage(struct file *file, struct page *page)
8356{
Chris Masond1310b22008-01-24 16:13:08 -05008357 struct extent_io_tree *tree;
8358 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008359 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008360}
Chris Mason1832a6d2007-12-21 16:27:21 -05008361
Chris Mason39279cc2007-06-12 06:35:45 -04008362static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8363{
Chris Masond1310b22008-01-24 16:13:08 -05008364 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04008365
8366
8367 if (current->flags & PF_MEMALLOC) {
8368 redirty_page_for_writepage(wbc, page);
8369 unlock_page(page);
8370 return 0;
8371 }
Chris Masond1310b22008-01-24 16:13:08 -05008372 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008373 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8374}
Chris Mason39279cc2007-06-12 06:35:45 -04008375
Eric Sandeen48a3b632013-04-25 20:41:01 +00008376static int btrfs_writepages(struct address_space *mapping,
8377 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008378{
Chris Masond1310b22008-01-24 16:13:08 -05008379 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008380
Chris Masond1310b22008-01-24 16:13:08 -05008381 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008382 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8383}
8384
Chris Mason3ab2fb52007-11-08 10:59:22 -05008385static int
8386btrfs_readpages(struct file *file, struct address_space *mapping,
8387 struct list_head *pages, unsigned nr_pages)
8388{
Chris Masond1310b22008-01-24 16:13:08 -05008389 struct extent_io_tree *tree;
8390 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008391 return extent_readpages(tree, mapping, pages, nr_pages,
8392 btrfs_get_extent);
8393}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008394static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008395{
Chris Masond1310b22008-01-24 16:13:08 -05008396 struct extent_io_tree *tree;
8397 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008398 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008399
Chris Masond1310b22008-01-24 16:13:08 -05008400 tree = &BTRFS_I(page->mapping->host)->io_tree;
8401 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008402 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008403 if (ret == 1) {
8404 ClearPagePrivate(page);
8405 set_page_private(page, 0);
8406 page_cache_release(page);
8407 }
8408 return ret;
8409}
Chris Mason39279cc2007-06-12 06:35:45 -04008410
Chris Masone6dcd2d2008-07-17 12:53:50 -04008411static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8412{
Chris Mason98509cf2008-09-11 15:51:43 -04008413 if (PageWriteback(page) || PageDirty(page))
8414 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008415 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008416}
8417
Lukas Czernerd47992f2013-05-21 23:17:23 -04008418static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8419 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008420{
Josef Bacik5fd02042012-05-02 14:00:54 -04008421 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008422 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008423 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008424 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008425 u64 page_start = page_offset(page);
8426 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008427 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008428
Chris Mason8b62b722009-09-02 16:53:46 -04008429 /*
8430 * we have the page locked, so new writeback can't start,
8431 * and the dirty bit won't be cleared while we are here.
8432 *
8433 * Wait for IO on this page so that we can safely clear
8434 * the PagePrivate2 bit and do ordered accounting
8435 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008436 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008437
Josef Bacik5fd02042012-05-02 14:00:54 -04008438 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008439 if (offset) {
8440 btrfs_releasepage(page, GFP_NOFS);
8441 return;
8442 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008443
8444 if (!inode_evicting)
8445 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8446 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008447 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008448 /*
8449 * IO on this page will never be started, so we need
8450 * to account for any ordered extents now
8451 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008452 if (!inode_evicting)
8453 clear_extent_bit(tree, page_start, page_end,
8454 EXTENT_DIRTY | EXTENT_DELALLOC |
8455 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8456 EXTENT_DEFRAG, 1, 0, &cached_state,
8457 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008458 /*
8459 * whoever cleared the private bit is responsible
8460 * for the finish_ordered_io
8461 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008462 if (TestClearPagePrivate2(page)) {
8463 struct btrfs_ordered_inode_tree *tree;
8464 u64 new_len;
8465
8466 tree = &BTRFS_I(inode)->ordered_tree;
8467
8468 spin_lock_irq(&tree->lock);
8469 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8470 new_len = page_start - ordered->file_offset;
8471 if (new_len < ordered->truncated_len)
8472 ordered->truncated_len = new_len;
8473 spin_unlock_irq(&tree->lock);
8474
8475 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8476 page_start,
8477 PAGE_CACHE_SIZE, 1))
8478 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008479 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008480 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008481 if (!inode_evicting) {
8482 cached_state = NULL;
8483 lock_extent_bits(tree, page_start, page_end, 0,
8484 &cached_state);
8485 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008486 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008487
8488 if (!inode_evicting) {
8489 clear_extent_bit(tree, page_start, page_end,
8490 EXTENT_LOCKED | EXTENT_DIRTY |
8491 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8492 EXTENT_DEFRAG, 1, 1,
8493 &cached_state, GFP_NOFS);
8494
8495 __btrfs_releasepage(page, GFP_NOFS);
8496 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008497
Chris Mason4a096752008-07-21 10:29:44 -04008498 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008499 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008500 ClearPagePrivate(page);
8501 set_page_private(page, 0);
8502 page_cache_release(page);
8503 }
Chris Mason39279cc2007-06-12 06:35:45 -04008504}
8505
Chris Mason9ebefb182007-06-15 13:50:00 -04008506/*
8507 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8508 * called from a page fault handler when a page is first dirtied. Hence we must
8509 * be careful to check for EOF conditions here. We set the page up correctly
8510 * for a written page which means we get ENOSPC checking when writing into
8511 * holes and correct delalloc and unwritten extent mapping on filesystems that
8512 * support these features.
8513 *
8514 * We are not allowed to take the i_mutex here so we have to play games to
8515 * protect against truncate races as the page could now be beyond EOF. Because
8516 * vmtruncate() writes the inode size before removing pages, once we have the
8517 * page lock we can determine safely if the page is beyond EOF. If it is not
8518 * beyond EOF, then the page is guaranteed safe against truncation until we
8519 * unlock the page.
8520 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008521int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008522{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008523 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008524 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008525 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008526 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8527 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008528 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008529 char *kaddr;
8530 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008531 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008532 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008533 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008534 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008535 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008536
Jan Karab2b5ef52012-06-12 16:20:45 +02008537 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04008538 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008539 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008540 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008541 reserved = 1;
8542 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008543 if (ret) {
8544 if (ret == -ENOMEM)
8545 ret = VM_FAULT_OOM;
8546 else /* -ENOSPC, -EIO, etc */
8547 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008548 if (reserved)
8549 goto out;
8550 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008551 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008552
Nick Piggin56a76f82009-03-31 15:23:23 -07008553 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008554again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008555 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008556 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008557 page_start = page_offset(page);
8558 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04008559
Chris Mason9ebefb182007-06-15 13:50:00 -04008560 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008561 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008562 /* page got truncated out from underneath us */
8563 goto out_unlock;
8564 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008565 wait_on_page_writeback(page);
8566
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008567 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008568 set_page_extent_mapped(page);
8569
Chris Masoneb84ae02008-07-17 13:53:27 -04008570 /*
8571 * we can't set the delalloc bits if there are pending ordered
8572 * extents. Drop our locks and wait for them to finish
8573 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008574 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8575 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008576 unlock_extent_cached(io_tree, page_start, page_end,
8577 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008578 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008579 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008580 btrfs_put_ordered_extent(ordered);
8581 goto again;
8582 }
8583
Josef Bacikfbf19082009-10-01 17:10:23 -04008584 /*
8585 * XXX - page_mkwrite gets called every time the page is dirtied, even
8586 * if it was already dirty, so for space accounting reasons we need to
8587 * clear any delalloc bits for the range we are fixing to save. There
8588 * is probably a better way to do this, but for now keep consistent with
8589 * prepare_pages in the normal write path.
8590 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008591 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008592 EXTENT_DIRTY | EXTENT_DELALLOC |
8593 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008594 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008595
Josef Bacik2ac55d42010-02-03 19:33:23 +00008596 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8597 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008598 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008599 unlock_extent_cached(io_tree, page_start, page_end,
8600 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008601 ret = VM_FAULT_SIGBUS;
8602 goto out_unlock;
8603 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008604 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008605
8606 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008607 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008608 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008609 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008610 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008611
Chris Masone6dcd2d2008-07-17 12:53:50 -04008612 if (zero_start != PAGE_CACHE_SIZE) {
8613 kaddr = kmap(page);
8614 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8615 flush_dcache_page(page);
8616 kunmap(page);
8617 }
Chris Mason247e7432008-07-17 12:53:51 -04008618 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008619 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008620 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008621
Chris Mason257c62e2009-10-13 13:21:08 -04008622 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8623 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008624 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008625
Josef Bacik2ac55d42010-02-03 19:33:23 +00008626 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008627
8628out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008629 if (!ret) {
8630 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008631 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008632 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008633 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008634out:
Josef Bacikec39e182012-01-12 19:10:12 -05008635 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008636out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008637 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008638 return ret;
8639}
8640
Josef Bacika41ad392011-01-31 15:30:16 -05008641static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008642{
8643 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008644 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008645 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008646 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008647 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008648 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008649 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008650
Josef Bacik0ef8b722013-10-25 16:13:35 -04008651 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8652 (u64)-1);
8653 if (ret)
8654 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008655
Josef Bacikfcb80c22011-05-03 10:40:22 -04008656 /*
8657 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8658 * 3 things going on here
8659 *
8660 * 1) We need to reserve space for our orphan item and the space to
8661 * delete our orphan item. Lord knows we don't want to have a dangling
8662 * orphan item because we didn't reserve space to remove it.
8663 *
8664 * 2) We need to reserve space to update our inode.
8665 *
8666 * 3) We need to have something to cache all the space that is going to
8667 * be free'd up by the truncate operation, but also have some slack
8668 * space reserved in case it uses space during the truncate (thank you
8669 * very much snapshotting).
8670 *
8671 * And we need these to all be seperate. The fact is we can use alot of
8672 * space doing the truncate, and we have no earthly idea how much space
8673 * we will use, so we need the truncate reservation to be seperate so it
8674 * doesn't end up using space reserved for updating the inode or
8675 * removing the orphan item. We also need to be able to stop the
8676 * transaction and start a new one, which means we need to be able to
8677 * update the inode several times, and we have no idea of knowing how
8678 * many times that will be, so we can't just reserve 1 item for the
8679 * entirety of the opration, so that has to be done seperately as well.
8680 * Then there is the orphan item, which does indeed need to be held on
8681 * to for the whole operation, and we need nobody to touch this reserved
8682 * space except the orphan code.
8683 *
8684 * So that leaves us with
8685 *
8686 * 1) root->orphan_block_rsv - for the orphan deletion.
8687 * 2) rsv - for the truncate reservation, which we will steal from the
8688 * transaction reservation.
8689 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8690 * updating the inode.
8691 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008692 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008693 if (!rsv)
8694 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008695 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008696 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008697
Josef Bacik907cbce2011-08-08 13:46:15 -04008698 /*
Josef Bacik07127182011-08-19 10:29:59 -04008699 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008700 * 1 for updating the inode.
8701 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008702 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008703 if (IS_ERR(trans)) {
8704 err = PTR_ERR(trans);
8705 goto out;
8706 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008707
Josef Bacik907cbce2011-08-08 13:46:15 -04008708 /* Migrate the slack space for the truncate to our reserve */
8709 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8710 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008711 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008712
Chris Mason5a3f23d2009-03-31 13:27:11 -04008713 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008714 * So if we truncate and then write and fsync we normally would just
8715 * write the extents that changed, which is a problem if we need to
8716 * first truncate that entire inode. So set this flag so we write out
8717 * all of the extents in the inode to the sync log so we're completely
8718 * safe.
8719 */
8720 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008721 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008722
Yan, Zheng80825102009-11-12 09:35:36 +00008723 while (1) {
8724 ret = btrfs_truncate_inode_items(trans, root, inode,
8725 inode->i_size,
8726 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08008727 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05008728 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008729 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008730 }
Chris Mason39279cc2007-06-12 06:35:45 -04008731
Josef Bacikfcb80c22011-05-03 10:40:22 -04008732 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008733 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008734 if (ret) {
8735 err = ret;
8736 break;
8737 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008738
Yan, Zheng80825102009-11-12 09:35:36 +00008739 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008740 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008741
8742 trans = btrfs_start_transaction(root, 2);
8743 if (IS_ERR(trans)) {
8744 ret = err = PTR_ERR(trans);
8745 trans = NULL;
8746 break;
8747 }
8748
8749 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8750 rsv, min_size);
8751 BUG_ON(ret); /* shouldn't happen */
8752 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008753 }
8754
8755 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008756 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008757 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008758 if (ret)
8759 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008760 }
8761
Chris Mason917c16b2011-11-08 14:49:59 -05008762 if (trans) {
8763 trans->block_rsv = &root->fs_info->trans_block_rsv;
8764 ret = btrfs_update_inode(trans, root, inode);
8765 if (ret && !err)
8766 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008767
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008768 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008769 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008770 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008771
8772out:
8773 btrfs_free_block_rsv(root, rsv);
8774
Josef Bacik3893e332011-01-31 16:03:11 -05008775 if (ret && !err)
8776 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008777
Josef Bacik3893e332011-01-31 16:03:11 -05008778 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008779}
8780
Sven Wegener3b963622008-06-09 21:57:42 -04008781/*
Chris Masond352ac62008-09-29 15:18:18 -04008782 * create a new subvolume directory/inode (helper for the ioctl).
8783 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008784int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008785 struct btrfs_root *new_root,
8786 struct btrfs_root *parent_root,
8787 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008788{
Chris Mason39279cc2007-06-12 06:35:45 -04008789 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008790 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008791 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008792
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008793 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8794 new_dirid, new_dirid,
8795 S_IFDIR | (~current_umask() & S_IRWXUGO),
8796 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008797 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008798 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008799 inode->i_op = &btrfs_dir_inode_operations;
8800 inode->i_fop = &btrfs_dir_file_operations;
8801
Miklos Szeredibfe86842011-10-28 14:13:29 +02008802 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008803 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008804 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008805
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008806 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8807 if (err)
8808 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008809 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008810 new_root->root_key.objectid, err);
8811
Yan, Zheng76dda932009-09-21 16:00:26 -04008812 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008813
Yan, Zheng76dda932009-09-21 16:00:26 -04008814 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008815 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008816}
8817
Chris Mason39279cc2007-06-12 06:35:45 -04008818struct inode *btrfs_alloc_inode(struct super_block *sb)
8819{
8820 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008821 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008822
8823 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8824 if (!ei)
8825 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008826
8827 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008828 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008829 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008830 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008831 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008832 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008833 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008834 ei->disk_i_size = 0;
8835 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008836 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008837 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008838 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008839 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008840 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008841
Josef Bacik9e0baf62011-07-15 15:16:44 +00008842 spin_lock_init(&ei->lock);
8843 ei->outstanding_extents = 0;
8844 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008845
Josef Bacik72ac3c02012-05-23 14:13:11 -04008846 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008847 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008848
Miao Xie16cdcec2011-04-22 18:12:22 +08008849 ei->delayed_node = NULL;
8850
chandan r9cc97d62012-07-04 12:48:07 +05308851 ei->i_otime.tv_sec = 0;
8852 ei->i_otime.tv_nsec = 0;
8853
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008854 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008855 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008856 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8857 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008858 ei->io_tree.track_uptodate = 1;
8859 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008860 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008861 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008862 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008863 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008864 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008865 RB_CLEAR_NODE(&ei->rb_node);
8866
8867 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008868}
8869
Josef Bacikaaedb552013-10-11 14:44:09 -04008870#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8871void btrfs_test_destroy_inode(struct inode *inode)
8872{
8873 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8874 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8875}
8876#endif
8877
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008878static void btrfs_i_callback(struct rcu_head *head)
8879{
8880 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008881 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8882}
8883
Chris Mason39279cc2007-06-12 06:35:45 -04008884void btrfs_destroy_inode(struct inode *inode)
8885{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008886 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008887 struct btrfs_root *root = BTRFS_I(inode)->root;
8888
Al Virob3d9b7a2012-06-09 13:51:19 -04008889 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008890 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008891 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8892 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008893 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8894 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008895 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008896
Chris Mason5a3f23d2009-03-31 13:27:11 -04008897 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008898 * This can happen where we create an inode, but somebody else also
8899 * created the same inode and we need to destroy the one we already
8900 * created.
8901 */
8902 if (!root)
8903 goto free;
8904
Josef Bacik8a35d952012-05-23 14:26:42 -04008905 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8906 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008907 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008908 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008909 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008910 }
Josef Bacik7b128762008-07-24 12:17:14 -04008911
Chris Masond3977122009-01-05 21:25:51 -05008912 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008913 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8914 if (!ordered)
8915 break;
8916 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008917 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008918 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008919 btrfs_remove_ordered_extent(inode, ordered);
8920 btrfs_put_ordered_extent(ordered);
8921 btrfs_put_ordered_extent(ordered);
8922 }
8923 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008924 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008925 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008926free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008927 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008928}
8929
Al Viro45321ac2010-06-07 13:43:19 -04008930int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008931{
8932 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008933
Naohiro Aota6379ef92013-06-06 09:56:34 +00008934 if (root == NULL)
8935 return 1;
8936
Liu Bofa6ac872013-02-20 14:10:23 +00008937 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008938 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008939 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008940 else
Al Viro45321ac2010-06-07 13:43:19 -04008941 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008942}
8943
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008944static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008945{
8946 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8947
8948 inode_init_once(&ei->vfs_inode);
8949}
8950
8951void btrfs_destroy_cachep(void)
8952{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008953 /*
8954 * Make sure all delayed rcu free inodes are flushed before we
8955 * destroy cache.
8956 */
8957 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008958 if (btrfs_inode_cachep)
8959 kmem_cache_destroy(btrfs_inode_cachep);
8960 if (btrfs_trans_handle_cachep)
8961 kmem_cache_destroy(btrfs_trans_handle_cachep);
8962 if (btrfs_transaction_cachep)
8963 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008964 if (btrfs_path_cachep)
8965 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008966 if (btrfs_free_space_cachep)
8967 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008968 if (btrfs_delalloc_work_cachep)
8969 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008970}
8971
8972int btrfs_init_cachep(void)
8973{
David Sterba837e1972012-09-07 03:00:48 -06008974 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008975 sizeof(struct btrfs_inode), 0,
8976 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008977 if (!btrfs_inode_cachep)
8978 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008979
David Sterba837e1972012-09-07 03:00:48 -06008980 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008981 sizeof(struct btrfs_trans_handle), 0,
8982 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008983 if (!btrfs_trans_handle_cachep)
8984 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008985
David Sterba837e1972012-09-07 03:00:48 -06008986 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008987 sizeof(struct btrfs_transaction), 0,
8988 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008989 if (!btrfs_transaction_cachep)
8990 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008991
David Sterba837e1972012-09-07 03:00:48 -06008992 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008993 sizeof(struct btrfs_path), 0,
8994 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008995 if (!btrfs_path_cachep)
8996 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008997
David Sterba837e1972012-09-07 03:00:48 -06008998 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008999 sizeof(struct btrfs_free_space), 0,
9000 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
9001 if (!btrfs_free_space_cachep)
9002 goto fail;
9003
Miao Xie8ccf6f192012-10-25 09:28:04 +00009004 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
9005 sizeof(struct btrfs_delalloc_work), 0,
9006 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
9007 NULL);
9008 if (!btrfs_delalloc_work_cachep)
9009 goto fail;
9010
Chris Mason39279cc2007-06-12 06:35:45 -04009011 return 0;
9012fail:
9013 btrfs_destroy_cachep();
9014 return -ENOMEM;
9015}
9016
9017static int btrfs_getattr(struct vfsmount *mnt,
9018 struct dentry *dentry, struct kstat *stat)
9019{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009020 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04009021 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05009022 u32 blocksize = inode->i_sb->s_blocksize;
9023
Chris Mason39279cc2007-06-12 06:35:45 -04009024 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009025 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05009026 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009027
9028 spin_lock(&BTRFS_I(inode)->lock);
9029 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9030 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009031 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009032 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009033 return 0;
9034}
9035
Chris Masond3977122009-01-05 21:25:51 -05009036static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9037 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009038{
9039 struct btrfs_trans_handle *trans;
9040 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009041 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04009042 struct inode *new_inode = new_dentry->d_inode;
9043 struct inode *old_inode = old_dentry->d_inode;
9044 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009045 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009046 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009047 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009048 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009049
Li Zefan33345d012011-04-20 10:31:50 +08009050 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009051 return -EPERM;
9052
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009053 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009054 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009055 return -EXDEV;
9056
Li Zefan33345d012011-04-20 10:31:50 +08009057 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9058 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009059 return -ENOTEMPTY;
9060
Chris Mason39279cc2007-06-12 06:35:45 -04009061 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009062 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009063 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009064
9065
9066 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009067 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009068 new_dentry->d_name.name,
9069 new_dentry->d_name.len);
9070
9071 if (ret) {
9072 if (ret == -EEXIST) {
9073 /* we shouldn't get
9074 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309075 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009076 return ret;
9077 }
9078 } else {
9079 /* maybe -EOVERFLOW */
9080 return ret;
9081 }
9082 }
9083 ret = 0;
9084
Chris Mason5a3f23d2009-03-31 13:27:11 -04009085 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009086 * we're using rename to replace one file with another. Start IO on it
9087 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009088 */
Chris Mason8d875f92014-08-12 10:47:42 -07009089 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009090 filemap_flush(old_inode->i_mapping);
9091
Yan, Zheng76dda932009-09-21 16:00:26 -04009092 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009093 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009094 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009095 /*
9096 * We want to reserve the absolute worst case amount of items. So if
9097 * both inodes are subvols and we need to unlink them then that would
9098 * require 4 item modifications, but if they are both normal inodes it
9099 * would require 5 item modifications, so we'll assume their normal
9100 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9101 * should cover the worst case number of items we'll modify.
9102 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04009103 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009104 if (IS_ERR(trans)) {
9105 ret = PTR_ERR(trans);
9106 goto out_notrans;
9107 }
Chris Mason5f39d392007-10-15 16:14:19 -04009108
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009109 if (dest != root)
9110 btrfs_record_root_in_trans(trans, dest);
9111
Yan, Zhenga5719522009-09-24 09:17:31 -04009112 ret = btrfs_set_inode_index(new_dir, &index);
9113 if (ret)
9114 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009115
Miao Xie67de1172013-12-26 13:07:06 +08009116 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009117 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009118 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009119 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009120 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009121 ret = btrfs_insert_inode_ref(trans, dest,
9122 new_dentry->d_name.name,
9123 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009124 old_ino,
9125 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009126 if (ret)
9127 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009128 /*
9129 * this is an ugly little race, but the rename is required
9130 * to make sure that if we crash, the inode is either at the
9131 * old name or the new one. pinning the log transaction lets
9132 * us make sure we don't allow a log commit to come in after
9133 * we unlink the name but before we add the new name back in.
9134 */
9135 btrfs_pin_log_trans(root);
9136 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009137
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009138 inode_inc_iversion(old_dir);
9139 inode_inc_iversion(new_dir);
9140 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009141 old_dir->i_ctime = old_dir->i_mtime = ctime;
9142 new_dir->i_ctime = new_dir->i_mtime = ctime;
9143 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04009144
Chris Mason12fcfd22009-03-24 10:24:20 -04009145 if (old_dentry->d_parent != new_dentry->d_parent)
9146 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9147
Li Zefan33345d012011-04-20 10:31:50 +08009148 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009149 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9150 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9151 old_dentry->d_name.name,
9152 old_dentry->d_name.len);
9153 } else {
Al Viro92986792011-03-04 17:14:37 +00009154 ret = __btrfs_unlink_inode(trans, root, old_dir,
9155 old_dentry->d_inode,
9156 old_dentry->d_name.name,
9157 old_dentry->d_name.len);
9158 if (!ret)
9159 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009160 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009161 if (ret) {
9162 btrfs_abort_transaction(trans, root, ret);
9163 goto out_fail;
9164 }
Chris Mason39279cc2007-06-12 06:35:45 -04009165
9166 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009167 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009168 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08009169 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009170 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9171 root_objectid = BTRFS_I(new_inode)->location.objectid;
9172 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9173 root_objectid,
9174 new_dentry->d_name.name,
9175 new_dentry->d_name.len);
9176 BUG_ON(new_inode->i_nlink == 0);
9177 } else {
9178 ret = btrfs_unlink_inode(trans, dest, new_dir,
9179 new_dentry->d_inode,
9180 new_dentry->d_name.name,
9181 new_dentry->d_name.len);
9182 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009183 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04009184 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009185 if (ret) {
9186 btrfs_abort_transaction(trans, root, ret);
9187 goto out_fail;
9188 }
Chris Mason39279cc2007-06-12 06:35:45 -04009189 }
Josef Bacikaec74772008-07-24 12:12:38 -04009190
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009191 ret = btrfs_add_link(trans, new_dir, old_inode,
9192 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009193 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009194 if (ret) {
9195 btrfs_abort_transaction(trans, root, ret);
9196 goto out_fail;
9197 }
Chris Mason39279cc2007-06-12 06:35:45 -04009198
Miao Xie67de1172013-12-26 13:07:06 +08009199 if (old_inode->i_nlink == 1)
9200 BTRFS_I(old_inode)->dir_index = index;
9201
Li Zefan33345d012011-04-20 10:31:50 +08009202 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04009203 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00009204 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009205 btrfs_end_log_trans(root);
9206 }
Chris Mason39279cc2007-06-12 06:35:45 -04009207out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009208 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009209out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009210 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009211 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009212
Chris Mason39279cc2007-06-12 06:35:45 -04009213 return ret;
9214}
9215
Miklos Szeredi80ace852014-07-23 15:15:32 +02009216static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9217 struct inode *new_dir, struct dentry *new_dentry,
9218 unsigned int flags)
9219{
9220 if (flags & ~RENAME_NOREPLACE)
9221 return -EINVAL;
9222
9223 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
9224}
9225
Miao Xie8ccf6f192012-10-25 09:28:04 +00009226static void btrfs_run_delalloc_work(struct btrfs_work *work)
9227{
9228 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009229 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009230
9231 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9232 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009233 inode = delalloc_work->inode;
9234 if (delalloc_work->wait) {
9235 btrfs_wait_ordered_range(inode, 0, (u64)-1);
9236 } else {
9237 filemap_flush(inode->i_mapping);
9238 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9239 &BTRFS_I(inode)->runtime_flags))
9240 filemap_flush(inode->i_mapping);
9241 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00009242
9243 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009244 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009245 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009246 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009247 complete(&delalloc_work->completion);
9248}
9249
9250struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
9251 int wait, int delay_iput)
9252{
9253 struct btrfs_delalloc_work *work;
9254
9255 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
9256 if (!work)
9257 return NULL;
9258
9259 init_completion(&work->completion);
9260 INIT_LIST_HEAD(&work->list);
9261 work->inode = inode;
9262 work->wait = wait;
9263 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009264 WARN_ON_ONCE(!inode);
9265 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9266 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009267
9268 return work;
9269}
9270
9271void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9272{
9273 wait_for_completion(&work->completion);
9274 kmem_cache_free(btrfs_delalloc_work_cachep, work);
9275}
9276
Chris Masond352ac62008-09-29 15:18:18 -04009277/*
9278 * some fairly slow code that needs optimization. This walks the list
9279 * of all the inodes with pending delalloc and forces them to disk.
9280 */
Miao Xie6c255e62014-03-06 13:55:01 +08009281static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9282 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009283{
Chris Masonea8c2812008-08-04 23:17:27 -04009284 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009285 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009286 struct btrfs_delalloc_work *work, *next;
9287 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009288 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009289 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009290
Miao Xie8ccf6f192012-10-25 09:28:04 +00009291 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009292 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009293
Miao Xie573bfb72014-03-06 13:55:03 +08009294 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009295 spin_lock(&root->delalloc_lock);
9296 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009297 while (!list_empty(&splice)) {
9298 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009299 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009300
Miao Xieeb73c1b2013-05-15 07:48:22 +00009301 list_move_tail(&binode->delalloc_inodes,
9302 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009303 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009304 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009305 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009306 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009307 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009308 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009309
9310 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009311 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009312 if (delay_iput)
9313 btrfs_add_delayed_iput(inode);
9314 else
9315 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009316 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009317 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009318 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009319 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009320 btrfs_queue_work(root->fs_info->flush_workers,
9321 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009322 ret++;
9323 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009324 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009325 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009326 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009327 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009328 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009329
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009330out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009331 list_for_each_entry_safe(work, next, &works, list) {
9332 list_del_init(&work->list);
9333 btrfs_wait_and_free_delalloc_work(work);
9334 }
9335
Miao Xieeb73c1b2013-05-15 07:48:22 +00009336 if (!list_empty_careful(&splice)) {
9337 spin_lock(&root->delalloc_lock);
9338 list_splice_tail(&splice, &root->delalloc_inodes);
9339 spin_unlock(&root->delalloc_lock);
9340 }
Miao Xie573bfb72014-03-06 13:55:03 +08009341 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009342 return ret;
9343}
9344
9345int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9346{
9347 int ret;
9348
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009349 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009350 return -EROFS;
9351
Miao Xie6c255e62014-03-06 13:55:01 +08009352 ret = __start_delalloc_inodes(root, delay_iput, -1);
9353 if (ret > 0)
9354 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009355 /*
9356 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009357 * we have to make sure the IO is actually started and that
9358 * ordered extents get created before we return
9359 */
9360 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009361 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009362 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009363 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009364 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9365 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009366 }
9367 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009368 return ret;
9369}
9370
Miao Xie6c255e62014-03-06 13:55:01 +08009371int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9372 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009373{
9374 struct btrfs_root *root;
9375 struct list_head splice;
9376 int ret;
9377
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009378 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009379 return -EROFS;
9380
9381 INIT_LIST_HEAD(&splice);
9382
Miao Xie573bfb72014-03-06 13:55:03 +08009383 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009384 spin_lock(&fs_info->delalloc_root_lock);
9385 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009386 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009387 root = list_first_entry(&splice, struct btrfs_root,
9388 delalloc_root);
9389 root = btrfs_grab_fs_root(root);
9390 BUG_ON(!root);
9391 list_move_tail(&root->delalloc_root,
9392 &fs_info->delalloc_roots);
9393 spin_unlock(&fs_info->delalloc_root_lock);
9394
Miao Xie6c255e62014-03-06 13:55:01 +08009395 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009396 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009397 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009398 goto out;
9399
Miao Xie6c255e62014-03-06 13:55:01 +08009400 if (nr != -1) {
9401 nr -= ret;
9402 WARN_ON(nr < 0);
9403 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009404 spin_lock(&fs_info->delalloc_root_lock);
9405 }
9406 spin_unlock(&fs_info->delalloc_root_lock);
9407
Miao Xie6c255e62014-03-06 13:55:01 +08009408 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009409 atomic_inc(&fs_info->async_submit_draining);
9410 while (atomic_read(&fs_info->nr_async_submits) ||
9411 atomic_read(&fs_info->async_delalloc_pages)) {
9412 wait_event(fs_info->async_submit_wait,
9413 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9414 atomic_read(&fs_info->async_delalloc_pages) == 0));
9415 }
9416 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009417out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009418 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009419 spin_lock(&fs_info->delalloc_root_lock);
9420 list_splice_tail(&splice, &fs_info->delalloc_roots);
9421 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009422 }
Miao Xie573bfb72014-03-06 13:55:03 +08009423 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009424 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009425}
9426
Chris Mason39279cc2007-06-12 06:35:45 -04009427static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9428 const char *symname)
9429{
9430 struct btrfs_trans_handle *trans;
9431 struct btrfs_root *root = BTRFS_I(dir)->root;
9432 struct btrfs_path *path;
9433 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009434 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009435 int err;
9436 int drop_inode = 0;
9437 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309438 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009439 int name_len;
9440 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009441 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009442 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009443 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009444
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009445 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009446 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9447 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009448
Josef Bacik9ed74f22009-09-11 16:12:44 -04009449 /*
9450 * 2 items for inode item and ref
9451 * 2 items for dir items
9452 * 1 item for xattr if selinux is on
9453 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009454 trans = btrfs_start_transaction(root, 5);
9455 if (IS_ERR(trans))
9456 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009457
Li Zefan581bb052011-04-20 10:06:11 +08009458 err = btrfs_find_free_ino(root, &objectid);
9459 if (err)
9460 goto out_unlock;
9461
Josef Bacikaec74772008-07-24 12:12:38 -04009462 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009463 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04009464 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009465 if (IS_ERR(inode)) {
9466 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009467 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009468 }
Chris Mason39279cc2007-06-12 06:35:45 -04009469
Casey Schauflerad19db72011-12-15 10:09:07 -05009470 /*
9471 * If the active LSM wants to access the inode during
9472 * d_instantiate it needs these. Smack checks to see
9473 * if the filesystem supports xattrs by looking at the
9474 * ops vector.
9475 */
9476 inode->i_fop = &btrfs_file_operations;
9477 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009478 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009479 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9480
9481 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9482 if (err)
9483 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009484
Josef Bacika1b075d2010-11-19 20:36:11 +00009485 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009486 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009487 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009488
9489 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009490 if (!path) {
9491 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009492 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009493 }
Li Zefan33345d012011-04-20 10:31:50 +08009494 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009495 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009496 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009497 datasize = btrfs_file_extent_calc_inline_size(name_len);
9498 err = btrfs_insert_empty_item(trans, root, path, &key,
9499 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009500 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009501 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009502 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009503 }
Chris Mason5f39d392007-10-15 16:14:19 -04009504 leaf = path->nodes[0];
9505 ei = btrfs_item_ptr(leaf, path->slots[0],
9506 struct btrfs_file_extent_item);
9507 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9508 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009509 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009510 btrfs_set_file_extent_encryption(leaf, ei, 0);
9511 btrfs_set_file_extent_compression(leaf, ei, 0);
9512 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9513 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9514
Chris Mason39279cc2007-06-12 06:35:45 -04009515 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009516 write_extent_buffer(leaf, symname, ptr, name_len);
9517 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009518 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009519
Chris Mason39279cc2007-06-12 06:35:45 -04009520 inode->i_op = &btrfs_symlink_inode_operations;
9521 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -04009522 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009523 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009524 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009525 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009526 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009527 goto out_unlock_inode;
9528 }
9529
9530 unlock_new_inode(inode);
9531 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009532
9533out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009534 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009535 if (drop_inode) {
9536 inode_dec_link_count(inode);
9537 iput(inode);
9538 }
Liu Bob53d3f52012-11-14 14:34:34 +00009539 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009540 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009541
9542out_unlock_inode:
9543 drop_inode = 1;
9544 unlock_new_inode(inode);
9545 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009546}
Chris Mason16432982008-04-10 10:23:21 -04009547
Josef Bacik0af3d002010-06-21 14:48:16 -04009548static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9549 u64 start, u64 num_bytes, u64 min_size,
9550 loff_t actual_len, u64 *alloc_hint,
9551 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009552{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009553 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9554 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009555 struct btrfs_root *root = BTRFS_I(inode)->root;
9556 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009557 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009558 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009559 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009560 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009561 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009562
Josef Bacik0af3d002010-06-21 14:48:16 -04009563 if (trans)
9564 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009565 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009566 if (own_trans) {
9567 trans = btrfs_start_transaction(root, 3);
9568 if (IS_ERR(trans)) {
9569 ret = PTR_ERR(trans);
9570 break;
9571 }
Yan Zhengd899e052008-10-30 14:25:28 -04009572 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009573
Chris Mason154ea282013-03-05 11:11:26 -05009574 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9575 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009576 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009577 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009578 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009579 if (own_trans)
9580 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009581 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009582 }
9583
Yan Zhengd899e052008-10-30 14:25:28 -04009584 ret = insert_reserved_file_extent(trans, inode,
9585 cur_offset, ins.objectid,
9586 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009587 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009588 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009589 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009590 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009591 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009592 btrfs_abort_transaction(trans, root, ret);
9593 if (own_trans)
9594 btrfs_end_transaction(trans, root);
9595 break;
9596 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009597
9598 btrfs_qgroup_update_reserved_bytes(root->fs_info,
9599 root->root_key.objectid,
9600 ins.offset, 1);
9601
Chris Masona1ed8352009-09-11 12:27:37 -04009602 btrfs_drop_extent_cache(inode, cur_offset,
9603 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009604
Josef Bacik5dc562c2012-08-17 13:14:17 -04009605 em = alloc_extent_map();
9606 if (!em) {
9607 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9608 &BTRFS_I(inode)->runtime_flags);
9609 goto next;
9610 }
9611
9612 em->start = cur_offset;
9613 em->orig_start = cur_offset;
9614 em->len = ins.offset;
9615 em->block_start = ins.objectid;
9616 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009617 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009618 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009619 em->bdev = root->fs_info->fs_devices->latest_bdev;
9620 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9621 em->generation = trans->transid;
9622
9623 while (1) {
9624 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009625 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009626 write_unlock(&em_tree->lock);
9627 if (ret != -EEXIST)
9628 break;
9629 btrfs_drop_extent_cache(inode, cur_offset,
9630 cur_offset + ins.offset - 1,
9631 0);
9632 }
9633 free_extent_map(em);
9634next:
Yan Zhengd899e052008-10-30 14:25:28 -04009635 num_bytes -= ins.offset;
9636 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009637 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009638
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009639 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009640 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009641 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009642 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009643 (actual_len > inode->i_size) &&
9644 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009645 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009646 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009647 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009648 i_size = cur_offset;
9649 i_size_write(inode, i_size);
9650 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009651 }
9652
Yan Zhengd899e052008-10-30 14:25:28 -04009653 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009654
9655 if (ret) {
9656 btrfs_abort_transaction(trans, root, ret);
9657 if (own_trans)
9658 btrfs_end_transaction(trans, root);
9659 break;
9660 }
Yan Zhengd899e052008-10-30 14:25:28 -04009661
Josef Bacik0af3d002010-06-21 14:48:16 -04009662 if (own_trans)
9663 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009664 }
Yan Zhengd899e052008-10-30 14:25:28 -04009665 return ret;
9666}
9667
Josef Bacik0af3d002010-06-21 14:48:16 -04009668int btrfs_prealloc_file_range(struct inode *inode, int mode,
9669 u64 start, u64 num_bytes, u64 min_size,
9670 loff_t actual_len, u64 *alloc_hint)
9671{
9672 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9673 min_size, actual_len, alloc_hint,
9674 NULL);
9675}
9676
9677int btrfs_prealloc_file_range_trans(struct inode *inode,
9678 struct btrfs_trans_handle *trans, int mode,
9679 u64 start, u64 num_bytes, u64 min_size,
9680 loff_t actual_len, u64 *alloc_hint)
9681{
9682 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9683 min_size, actual_len, alloc_hint, trans);
9684}
9685
Chris Masone6dcd2d2008-07-17 12:53:50 -04009686static int btrfs_set_page_dirty(struct page *page)
9687{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009688 return __set_page_dirty_nobuffers(page);
9689}
9690
Al Viro10556cb2011-06-20 19:28:19 -04009691static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009692{
Li Zefanb83cc962010-12-20 16:04:08 +08009693 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009694 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009695
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009696 if (mask & MAY_WRITE &&
9697 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9698 if (btrfs_root_readonly(root))
9699 return -EROFS;
9700 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9701 return -EACCES;
9702 }
Al Viro2830ba72011-06-20 19:16:29 -04009703 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009704}
Chris Mason39279cc2007-06-12 06:35:45 -04009705
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009706static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9707{
9708 struct btrfs_trans_handle *trans;
9709 struct btrfs_root *root = BTRFS_I(dir)->root;
9710 struct inode *inode = NULL;
9711 u64 objectid;
9712 u64 index;
9713 int ret = 0;
9714
9715 /*
9716 * 5 units required for adding orphan entry
9717 */
9718 trans = btrfs_start_transaction(root, 5);
9719 if (IS_ERR(trans))
9720 return PTR_ERR(trans);
9721
9722 ret = btrfs_find_free_ino(root, &objectid);
9723 if (ret)
9724 goto out;
9725
9726 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9727 btrfs_ino(dir), objectid, mode, &index);
9728 if (IS_ERR(inode)) {
9729 ret = PTR_ERR(inode);
9730 inode = NULL;
9731 goto out;
9732 }
9733
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009734 inode->i_fop = &btrfs_file_operations;
9735 inode->i_op = &btrfs_file_inode_operations;
9736
9737 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009738 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9739
Chris Masonb0d5d102014-09-08 13:08:51 -07009740 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9741 if (ret)
9742 goto out_inode;
9743
9744 ret = btrfs_update_inode(trans, root, inode);
9745 if (ret)
9746 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009747 ret = btrfs_orphan_add(trans, inode);
9748 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009749 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009750
Filipe Manana5762b5c2014-08-01 00:10:32 +01009751 /*
9752 * We set number of links to 0 in btrfs_new_inode(), and here we set
9753 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9754 * through:
9755 *
9756 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9757 */
9758 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009759 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009760 d_tmpfile(dentry, inode);
9761 mark_inode_dirty(inode);
9762
9763out:
9764 btrfs_end_transaction(trans, root);
9765 if (ret)
9766 iput(inode);
9767 btrfs_balance_delayed_items(root);
9768 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009769 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009770
9771out_inode:
9772 unlock_new_inode(inode);
9773 goto out;
9774
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009775}
9776
Filipe Mananab38ef712014-11-13 17:01:45 +00009777/* Inspired by filemap_check_errors() */
9778int btrfs_inode_check_errors(struct inode *inode)
9779{
9780 int ret = 0;
9781
9782 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
9783 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
9784 ret = -ENOSPC;
9785 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
9786 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
9787 ret = -EIO;
9788
9789 return ret;
9790}
9791
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009792static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009793 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009794 .lookup = btrfs_lookup,
9795 .create = btrfs_create,
9796 .unlink = btrfs_unlink,
9797 .link = btrfs_link,
9798 .mkdir = btrfs_mkdir,
9799 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009800 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009801 .symlink = btrfs_symlink,
9802 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009803 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009804 .setxattr = btrfs_setxattr,
9805 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009806 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009807 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009808 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009809 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009810 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009811 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009812 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009813};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009814static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009815 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009816 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009817 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009818 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009819 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009820};
Yan, Zheng76dda932009-09-21 16:00:26 -04009821
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009822static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009823 .llseek = generic_file_llseek,
9824 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009825 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009826 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009827#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009828 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009829#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009830 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009831 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009832};
9833
Chris Masond1310b22008-01-24 16:13:08 -05009834static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009835 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009836 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009837 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009838 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009839 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009840 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009841 .set_bit_hook = btrfs_set_bit_hook,
9842 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009843 .merge_extent_hook = btrfs_merge_extent_hook,
9844 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009845};
9846
Chris Mason35054392009-01-21 13:11:13 -05009847/*
9848 * btrfs doesn't support the bmap operation because swapfiles
9849 * use bmap to make a mapping of extents in the file. They assume
9850 * these extents won't change over the life of the file and they
9851 * use the bmap result to do IO directly to the drive.
9852 *
9853 * the btrfs bmap call would return logical addresses that aren't
9854 * suitable for IO and they also will change frequently as COW
9855 * operations happen. So, swapfile + btrfs == corruption.
9856 *
9857 * For now we're avoiding this by dropping bmap.
9858 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009859static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009860 .readpage = btrfs_readpage,
9861 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04009862 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009863 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009864 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009865 .invalidatepage = btrfs_invalidatepage,
9866 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009867 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009868 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009869};
9870
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009871static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009872 .readpage = btrfs_readpage,
9873 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009874 .invalidatepage = btrfs_invalidatepage,
9875 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009876};
9877
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009878static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009879 .getattr = btrfs_getattr,
9880 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009881 .setxattr = btrfs_setxattr,
9882 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009883 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009884 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009885 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009886 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009887 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009888 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009889 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009890};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009891static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009892 .getattr = btrfs_getattr,
9893 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009894 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009895 .setxattr = btrfs_setxattr,
9896 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009897 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009898 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009899 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009900 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009901 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009902};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009903static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009904 .readlink = generic_readlink,
9905 .follow_link = page_follow_link_light,
9906 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009907 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009908 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009909 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009910 .setxattr = btrfs_setxattr,
9911 .getxattr = btrfs_getxattr,
9912 .listxattr = btrfs_listxattr,
9913 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009914 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009915};
Yan, Zheng76dda932009-09-21 16:00:26 -04009916
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009917const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009918 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009919 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009920};